diff --git a/Assets/Editor/BeforeStartAttributeDrawer.cs b/Assets/Editor/CustomAttributeDrawers.cs similarity index 55% rename from Assets/Editor/BeforeStartAttributeDrawer.cs rename to Assets/Editor/CustomAttributeDrawers.cs index 19006b1..babc351 100644 --- a/Assets/Editor/BeforeStartAttributeDrawer.cs +++ b/Assets/Editor/CustomAttributeDrawers.cs @@ -18,4 +18,22 @@ public class BeforeStartAttributeDrawer : PropertyDrawer EditorGUI.PropertyField(position, property, label, true); GUI.enabled = true; } +} + +[CustomPropertyDrawer(typeof(AfterStartAttribute))] +public class AfterStartAttributeDrawer : PropertyDrawer +{ + // Necessary since some properties tend to collapse smaller than their content + public override float GetPropertyHeight(SerializedProperty property, GUIContent label) + { + return EditorGUI.GetPropertyHeight(property, label, true); + } + + // Draw a disabled property field + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) + { + GUI.enabled = Application.isPlaying; + EditorGUI.PropertyField(position, property, label, true); + GUI.enabled = true; + } } \ No newline at end of file diff --git a/Assets/Scripts/Mostro.cs.meta b/Assets/Editor/CustomAttributeDrawers.cs.meta similarity index 83% rename from Assets/Scripts/Mostro.cs.meta rename to Assets/Editor/CustomAttributeDrawers.cs.meta index 31bb5e3..2c1c0f3 100644 --- a/Assets/Scripts/Mostro.cs.meta +++ b/Assets/Editor/CustomAttributeDrawers.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: d484827da1a4f984c837dbfb9f970bdd +guid: cd8509fca2c0ebe47acb277e2aed11f2 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/Scripts/BeforeStartAttribute.cs b/Assets/Scripts/BeforeStartAttribute.cs deleted file mode 100644 index cd1cf30..0000000 --- a/Assets/Scripts/BeforeStartAttribute.cs +++ /dev/null @@ -1,5 +0,0 @@ -using UnityEngine; - -public class BeforeStartAttribute : PropertyAttribute { - -} \ No newline at end of file diff --git a/Assets/Scripts/CustomAttributes.cs b/Assets/Scripts/CustomAttributes.cs new file mode 100644 index 0000000..01377f1 --- /dev/null +++ b/Assets/Scripts/CustomAttributes.cs @@ -0,0 +1,5 @@ +using UnityEngine; + +public class BeforeStartAttribute : PropertyAttribute {} + +public class AfterStartAttribute : PropertyAttribute {} \ No newline at end of file diff --git a/Assets/Editor/BeforeStartAttributeDrawer.cs.meta b/Assets/Scripts/CustomAttributes.cs.meta similarity index 83% rename from Assets/Editor/BeforeStartAttributeDrawer.cs.meta rename to Assets/Scripts/CustomAttributes.cs.meta index 4216c56..6e0a606 100644 --- a/Assets/Editor/BeforeStartAttributeDrawer.cs.meta +++ b/Assets/Scripts/CustomAttributes.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 13bad693cf0c2634ea1e2be679edf00f +guid: 6fdc88abee025004d9040a4026bfcb11 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/Scripts/Map.cs b/Assets/Scripts/Map.cs index 0da467c..182bf0a 100644 --- a/Assets/Scripts/Map.cs +++ b/Assets/Scripts/Map.cs @@ -154,6 +154,11 @@ public class Map : MonoBehaviour return tile; } + public bool CanMoveTo(Vector2Int direction) + { + return GetTile(direction).walkable; + } + private void InitTile(Vector2Int position, bool walkable, Sprite tileSprite, bool roomPart) { Tile tile = GetTile(position); tile.walkable = walkable; diff --git a/Assets/Scripts/Monster.cs b/Assets/Scripts/Monster.cs new file mode 100644 index 0000000..f84b5da --- /dev/null +++ b/Assets/Scripts/Monster.cs @@ -0,0 +1,40 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Monster : MonoBehaviour +{ + public int hpMax; + public int hp; + public bool isUndead; + public int movesPerTurn; + public bool hasSpottedPlayer; + + [BeforeStartAttribute] + public Sprite sprite; + + private Map map; + + // Start is called before the first frame update + void Start() + { + map = GameObject.FindGameObjectWithTag("Map").GetComponent(); + hasSpottedPlayer = false; + } + + void Turn(){ + int direction = Random.Range(0, 4); + if (direction == 0 && map.CanMoveTo(Vector2Int.left)){ + transform.Translate(Vector3.left); + } + else if (direction == 1 && map.CanMoveTo(Vector2Int.right)){ + transform.Translate(Vector3.right); + } + else if (direction == 2 && map.CanMoveTo(Vector2Int.up)){ + transform.Translate(Vector3.up); + } + else if (direction == 3 && map.CanMoveTo(Vector2Int.left)){ + transform.Translate(Vector3.left); + } + } +} diff --git a/Assets/Scripts/BeforeStartAttribute.cs.meta b/Assets/Scripts/Monster.cs.meta similarity index 83% rename from Assets/Scripts/BeforeStartAttribute.cs.meta rename to Assets/Scripts/Monster.cs.meta index 68df79c..f99ee29 100644 --- a/Assets/Scripts/BeforeStartAttribute.cs.meta +++ b/Assets/Scripts/Monster.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: ec73aa347f0168f4f95e1acbd3072646 +guid: a72ad0eb2136f284bab78f19fa561bb8 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/Scripts/Mostro.cs b/Assets/Scripts/Mostro.cs deleted file mode 100644 index cf9d969..0000000 --- a/Assets/Scripts/Mostro.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -/* -public class Mostro : MonoBehaviour -{ - public string nome; - public Sprite sprite; - public int hpmax, hp, exp; - public bool is_undead; - public enum speed {slow, standard, fast}; - private bool player_spotted; - public Map map; - // Start is called before the first frame update - void Start() - { - map = gameObjectMappa.GetComponent(); - player_spotted = false; - } - - // Update is called once per frame - void Update() - { - if (!player_spotted){ - roam(); - } - //Da mettere il pathfinding per quando trova il personaggio - } - void roam(){ - while (true) - { - int direction = Random.Range(1, 5); - if (direction==1 && is_valid_movement("left")){ - transform.Translate(Vector3.left); - break; - } - else if (direction==2 && is_valid_movement("right")){ - transform.Translate(Vector3.right); - break; - } - else if (direction==3 && is_valid_movement("up")){ - transform.Translate(Vector3.up); - break; - } - else if (is_valid_movement("down")){ - transform.Translate(Vector3.up); - break; - } - } - } - bool is_valid_movement(string direction) - { - Tile tile; - int posX = (int) transform.position.x; - int posY = (int) transform.position.y; - if (direction == "left") tile = map.GetTile(posX - 1, posY); - else if (direction == "right") tile = map.GetTile(posX + 1, posY); - else if (direction == "up") tile = map.GetTile(posX, posY + 1); - else tile = map.GetTile(posX, posY - 1); - return tile.walkable; - } -} -*/ diff --git a/Assets/Scripts/Player.cs b/Assets/Scripts/Player.cs index dffaeaf..86df64c 100644 --- a/Assets/Scripts/Player.cs +++ b/Assets/Scripts/Player.cs @@ -4,18 +4,23 @@ using UnityEngine; public class Player : MonoBehaviour { - private int exp, level, hp, maxhp; - public int startingHp; - public Map map; - //TODO: Aggiungi gli oggetti in inventario + public int exp; + public int level; + public int hpMax; + + [AfterStartAttribute] + public int hp; + + private Map map; + private GameObject gameController; - // Start is called before the first frame update void Start() { - hp = startingHp; + map = GameObject.FindGameObjectWithTag("Map").GetComponent(); + gameController = GameObject.FindGameObjectWithTag("GameController"); + hp = hpMax; } - // Update is called once per frame void Update() { CheckForMovementInput(); @@ -26,32 +31,34 @@ public class Player : MonoBehaviour bool hasMoved = false; if (Input.GetKeyDown(KeyCode.A)) { - if (CanMoveTo(Vector2Int.left)) { - transform.Translate(Vector3.left);} + if (map.CanMoveTo(Vector2Int.left)) { + transform.Translate(Vector3.left); + hasMoved = true; + } } else if (Input.GetKeyDown(KeyCode.D)) { - if (CanMoveTo(Vector2Int.right)) { + if (map.CanMoveTo(Vector2Int.right)) { transform.Translate(Vector3.right); + hasMoved = true; } } else if (Input.GetKeyDown(KeyCode.W)) { - if (CanMoveTo(Vector2Int.up)) { + if (map.CanMoveTo(Vector2Int.up)) { transform.Translate(Vector3.up); + hasMoved = true; } } else if (Input.GetKeyDown(KeyCode.S)) { - if (CanMoveTo(Vector2Int.down)) { + if (map.CanMoveTo(Vector2Int.down)) { transform.Translate(Vector3.down); + hasMoved = true; } } - - } - - bool CanMoveTo(Vector2Int direction) - { - return map.GetTile(direction).walkable; + if(hasMoved) { + gameController.BroadcastMessage("Turn"); + } } }