From 1ac4d2866bb179376a915a7ddd025f437b3bd98c Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 28 Apr 2019 16:28:31 +0200 Subject: [PATCH] Commit a lot of stuff --- Assets/Prefabs/Player.prefab | 14 +++++ Assets/Prefabs/Slime.prefab | 4 +- Assets/Prefabs/TEST Item.prefab | 4 +- Assets/Scripts/Entity.cs | 8 +++ Assets/Scripts/Item.cs | 9 ++- Assets/Scripts/Map.cs | 2 +- Assets/Scripts/MessageBar.cs | 3 +- Assets/Scripts/Player.cs | 67 ++++++++++++++++++++--- Assets/Scripts/PlayerAttack.cs | 24 ++++++++ Assets/Scripts/PlayerAttack.cs.meta | 11 ++++ Assets/Scripts/PlayerAttack_Melee.cs | 18 ++++++ Assets/Scripts/PlayerAttack_Melee.cs.meta | 11 ++++ Assets/Scripts/TurnHandler.cs | 27 ++++----- 13 files changed, 172 insertions(+), 30 deletions(-) create mode 100644 Assets/Scripts/PlayerAttack.cs create mode 100644 Assets/Scripts/PlayerAttack.cs.meta create mode 100644 Assets/Scripts/PlayerAttack_Melee.cs create mode 100644 Assets/Scripts/PlayerAttack_Melee.cs.meta diff --git a/Assets/Prefabs/Player.prefab b/Assets/Prefabs/Player.prefab index d9785e0..b04f372 100644 --- a/Assets/Prefabs/Player.prefab +++ b/Assets/Prefabs/Player.prefab @@ -11,6 +11,7 @@ GameObject: - component: {fileID: 2935319493830293414} - component: {fileID: 2935319493830293413} - component: {fileID: -5893075763862095451} + - component: {fileID: 8856581523727923621} m_Layer: 0 m_Name: Player m_TagString: Player @@ -99,6 +100,19 @@ SpriteRenderer: m_WasSpriteAssigned: 1 m_MaskInteraction: 0 m_SpriteSortPoint: 0 +--- !u!114 &8856581523727923621 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2935319493830293412} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 05a8e698d12930547a9e83f369e506d2, type: 3} + m_Name: + m_EditorClassIdentifier: + damage: 1 --- !u!1001 &5734536248370648602 PrefabInstance: m_ObjectHideFlags: 0 diff --git a/Assets/Prefabs/Slime.prefab b/Assets/Prefabs/Slime.prefab index 30a9177..e687bba 100644 --- a/Assets/Prefabs/Slime.prefab +++ b/Assets/Prefabs/Slime.prefab @@ -44,9 +44,11 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 2fcfe4b94620b234fb7a294bf4453a69, type: 3} m_Name: m_EditorClassIdentifier: - hpMax: 0 + hpMax: 2 + overlappable: 0 hp: 0 sprite: {fileID: 21300000, guid: ee7ef72c830dc204c9df0595e161f049, type: 3} + moveChance: 0.5 visionRange: 4 --- !u!212 &1351652798109185028 SpriteRenderer: diff --git a/Assets/Prefabs/TEST Item.prefab b/Assets/Prefabs/TEST Item.prefab index d91ed64..00c5c0f 100644 --- a/Assets/Prefabs/TEST Item.prefab +++ b/Assets/Prefabs/TEST Item.prefab @@ -12,7 +12,7 @@ GameObject: - component: {fileID: 470211819356819162} - component: {fileID: 7501912107144583665} m_Layer: 0 - m_Name: TEST_ Item + m_Name: TEST Item m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -44,7 +44,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 1f0883433553dbb4fa23a815949f596b, type: 3} m_Name: m_EditorClassIdentifier: - hpMax: 0 + hpMax: 1 overlappable: 0 hp: 0 sprite: {fileID: 21300000, guid: 5e4d29e1adc2ea0429b199b82253416c, type: 3} diff --git a/Assets/Scripts/Entity.cs b/Assets/Scripts/Entity.cs index bae1035..fc30c76 100644 --- a/Assets/Scripts/Entity.cs +++ b/Assets/Scripts/Entity.cs @@ -23,6 +23,7 @@ public class Entity : MonoBehaviour protected SpriteRenderer spriteRenderer; protected TurnHandler turnHandler; protected Map map; + protected MessageBar messageBar; protected virtual void Start() { @@ -31,6 +32,13 @@ public class Entity : MonoBehaviour gameController = GameObject.FindGameObjectWithTag("GameController"); turnHandler = gameController.GetComponentInChildren(); map = gameController.GetComponentInChildren(); + GameObject canvas = GameObject.FindGameObjectWithTag("Canvas"); + messageBar = canvas.GetComponentInChildren(); hp = hpMax; } + + public virtual void Die() { + Debug.LogWarning("Die not overridden"); + Destroy(gameObject); + } } diff --git a/Assets/Scripts/Item.cs b/Assets/Scripts/Item.cs index 5d99cf6..d115ecc 100644 --- a/Assets/Scripts/Item.cs +++ b/Assets/Scripts/Item.cs @@ -4,6 +4,8 @@ using UnityEngine; public class Item : Entity { + public static string itemName = "White Triangle"; + protected override void Start() { base.Start(); overlappable = true; @@ -11,7 +13,12 @@ public class Item : Entity public virtual void OnPickup(Player player) { Debug.LogWarning("OnPickup not overridden"); - turnHandler.WriteToMessageBar("Picked up [NULL]."); + messageBar.Write("Picked up: " + itemName, Color.yellow); + Destroy(gameObject); + } + + public override void Die() { + messageBar.Write("Destroyed: " + itemName, Color.red); Destroy(gameObject); } } \ No newline at end of file diff --git a/Assets/Scripts/Map.cs b/Assets/Scripts/Map.cs index 696e369..8afa7d2 100644 --- a/Assets/Scripts/Map.cs +++ b/Assets/Scripts/Map.cs @@ -138,7 +138,7 @@ public class Map : MonoBehaviour { try { bool walkable = GetTile(position).walkable; - List entities = turnHandler.GetEntityAtPosition(position); + List entities = turnHandler.GetEntitiesAtPosition(position); bool free = true; foreach(Entity entity in entities) { free &= entity.overlappable; diff --git a/Assets/Scripts/MessageBar.cs b/Assets/Scripts/MessageBar.cs index e70ffe8..1c2fc3d 100644 --- a/Assets/Scripts/MessageBar.cs +++ b/Assets/Scripts/MessageBar.cs @@ -17,7 +17,8 @@ public class MessageBar : MonoBehaviour opacity = 0f; } - public void Write(string message) { + public void Write(string message, Color color) { + text.color = color; text.text = message; opacity = 1f; } diff --git a/Assets/Scripts/Player.cs b/Assets/Scripts/Player.cs index dd6ccc9..6f57ee4 100644 --- a/Assets/Scripts/Player.cs +++ b/Assets/Scripts/Player.cs @@ -2,20 +2,69 @@ using System.Collections.Generic; using UnityEngine; +public enum ControlMode { + Move, + Attack +} + public class Player : Entity { public int exp; public int level; - void Update() - { - CheckForMovementInput(); + protected ControlMode controlMode; + + protected override void Start() { + base.Start(); + controlMode = ControlMode.Move; } - void CheckForMovementInput() + protected void Update() + { + CheckForControlModeChange(); + if(controlMode == ControlMode.Move) CheckForMovementInput(); + if(controlMode == ControlMode.Attack) CheckForAttackInput(); + } + + protected void CheckForControlModeChange() { + if(Input.GetKeyDown(KeyCode.Escape)) { + controlMode = ControlMode.Move; + messageBar.Write("Control mode: Move", Color.cyan); + } + if(Input.GetKeyDown(KeyCode.A)) { + controlMode = ControlMode.Attack; + messageBar.Write("Control mode: Attack", Color.cyan); + } + } + + protected void CheckForAttackInput() { + bool hasAttacked = false; + if (Input.GetKeyDown(KeyCode.LeftArrow)) + { + hasAttacked = GetComponent().Attack(MapPosition + Vector2Int.left); + } + else if (Input.GetKeyDown(KeyCode.RightArrow)) + { + hasAttacked = GetComponent().Attack(MapPosition + Vector2Int.right); + } + else if (Input.GetKeyDown(KeyCode.UpArrow)) + { + hasAttacked = GetComponent().Attack(MapPosition + Vector2Int.up); + } + else if (Input.GetKeyDown(KeyCode.DownArrow)) + { + hasAttacked = GetComponent().Attack(MapPosition + Vector2Int.down); + } + if(hasAttacked) { + //Turn happens! + turnHandler.OnTurn(); + } + } + + protected void CheckForMovementInput() { bool hasMoved = false; - if (Input.GetKeyDown(KeyCode.A)) + if (Input.GetKeyDown(KeyCode.LeftArrow)) { if (map.CanMoveTo(MapPosition + Vector2Int.left)) { transform.Translate(Vector3.left); @@ -23,7 +72,7 @@ public class Player : Entity spriteRenderer.flipX = false; } } - else if (Input.GetKeyDown(KeyCode.D)) + else if (Input.GetKeyDown(KeyCode.RightArrow)) { if (map.CanMoveTo(MapPosition + Vector2Int.right)) { transform.Translate(Vector3.right); @@ -31,14 +80,14 @@ public class Player : Entity spriteRenderer.flipX = true; } } - else if (Input.GetKeyDown(KeyCode.W)) + else if (Input.GetKeyDown(KeyCode.UpArrow)) { if (map.CanMoveTo(MapPosition + Vector2Int.up)) { transform.Translate(Vector3.up); hasMoved = true; } } - else if (Input.GetKeyDown(KeyCode.S)) + else if (Input.GetKeyDown(KeyCode.DownArrow)) { if (map.CanMoveTo(MapPosition + Vector2Int.down)) { transform.Translate(Vector3.down); @@ -47,7 +96,7 @@ public class Player : Entity } if(hasMoved) { //Check for pickuppable items - List entities = turnHandler.GetEntityAtPosition(MapPosition); + List entities = turnHandler.GetEntitiesAtPosition(MapPosition); foreach(Entity entity in entities) { if(entity is Item) { Item item = entity as Item; diff --git a/Assets/Scripts/PlayerAttack.cs b/Assets/Scripts/PlayerAttack.cs new file mode 100644 index 0000000..2f33699 --- /dev/null +++ b/Assets/Scripts/PlayerAttack.cs @@ -0,0 +1,24 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class PlayerAttack : MonoBehaviour +{ + protected Player player; + protected GameObject gameController; + protected TurnHandler turnHandler; + protected Map map; + + protected void Start() { + player = GetComponent(); + gameController = GameObject.FindGameObjectWithTag("GameController"); + turnHandler = gameController.GetComponentInChildren(); + map = gameController.GetComponentInChildren(); + } + + public virtual bool Attack(Vector2Int target) { + //Returns if the attack was successful. + Debug.LogWarning("Attack not overridden"); + return false; + } +} \ No newline at end of file diff --git a/Assets/Scripts/PlayerAttack.cs.meta b/Assets/Scripts/PlayerAttack.cs.meta new file mode 100644 index 0000000..86ea067 --- /dev/null +++ b/Assets/Scripts/PlayerAttack.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1bb56d02d177e9e4e9146940057ba1ad +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/PlayerAttack_Melee.cs b/Assets/Scripts/PlayerAttack_Melee.cs new file mode 100644 index 0000000..81f67d6 --- /dev/null +++ b/Assets/Scripts/PlayerAttack_Melee.cs @@ -0,0 +1,18 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class PlayerAttack_Melee : PlayerAttack +{ + public float damage = 1f; + + public override bool Attack(Vector2Int target) { + List targetEntities = turnHandler.GetEntitiesAtPosition(target); + if(targetEntities.Count == 0) { + return false; + } + Entity targetEntity = targetEntities[0]; + targetEntity.hp -= damage; + return true; + } +} \ No newline at end of file diff --git a/Assets/Scripts/PlayerAttack_Melee.cs.meta b/Assets/Scripts/PlayerAttack_Melee.cs.meta new file mode 100644 index 0000000..ad531d2 --- /dev/null +++ b/Assets/Scripts/PlayerAttack_Melee.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 05a8e698d12930547a9e83f369e506d2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/TurnHandler.cs b/Assets/Scripts/TurnHandler.cs index 4493018..bd41358 100644 --- a/Assets/Scripts/TurnHandler.cs +++ b/Assets/Scripts/TurnHandler.cs @@ -4,25 +4,22 @@ using UnityEngine; public class TurnHandler : MonoBehaviour { - private MessageBar messageBar; - - private void Start() { - GameObject canvas = GameObject.FindGameObjectWithTag("Canvas"); - messageBar = canvas.GetComponentInChildren(); - } - - public void WriteToMessageBar(string message) { - messageBar.Write(message); - } - public void OnTurn() { - AI[] ais = gameObject.GetComponentsInChildren(); - foreach(AI ai in ais) { - ai.OnTurn(); + Entity[] entities = gameObject.GetComponentsInChildren(); + foreach(Entity entity in entities) { + //Check for deaths + if(entity.hp <= 0) { + entity.Die(); + } + //Move AIs + if(entity is AI) { + AI ai = entity as AI; + ai.OnTurn(); + } } } - public List GetEntityAtPosition(Vector2Int position) { + public List GetEntitiesAtPosition(Vector2Int position) { Entity[] entities = GetComponentsInChildren(); List found = new List(); foreach(Entity entity in entities) {