diff --git a/Assets/New Database.asset b/Assets/New Database.asset deleted file mode 100644 index 451bbe7..0000000 --- a/Assets/New Database.asset +++ /dev/null @@ -1,31 +0,0 @@ -%YAML 1.1 -%TAG !u! tag:unity3d.com,2011: ---- !u!114 &11400000 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 0} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: edb1aa3d6230b1e4c9a50056da756015, type: 3} - m_Name: New Database - m_EditorClassIdentifier: - _prefabs: - - {fileID: 6962989255644195630, guid: 6cd87b398e7a0e94580f4fcbe2fd310a, type: 3} - - {fileID: 6962989255644195630, guid: 377c7275c0001cc47a6b8926ac57d573, type: 3} - - {fileID: 6962989255644195630, guid: 869a03bba705e8d4485aa73daad773dc, type: 3} - - {fileID: 6962989255644195630, guid: 9b40c232eddfd1b469bea688e3c970c0, type: 3} - - {fileID: 4052934186652138539, guid: 8560e1f66d452b543a705c8a0f3e22fa, type: 3} - - {fileID: 3814095509541806390, guid: 9527f3a1482b90a48bb6c62acc70f986, type: 3} - _scriptableObjects: - - {fileID: 11400000, guid: e715669e1ed4b294c82d07ac011e89bb, type: 2} - - {fileID: 11400000, guid: a6e34739c9325da4cac4fbaea30d052c, type: 2} - - {fileID: 11400000, guid: d37561e153d6a6448a03839488fdec5e, type: 2} - - {fileID: 11400000, guid: 4002377ed7e87b34699f126f2b10c703, type: 2} - - {fileID: 11400000, guid: 4aaf448680c7f8a438a9a5861c622a55, type: 2} - _folders: - - {fileID: 102900000, guid: 0ca30b5ca281be24bb62d7e48cc2bec8, type: 3} - - {fileID: 102900000, guid: f3dee7994db941e47b9445cb464c69a9, type: 3} - - {fileID: 102900000, guid: 4f8284570682f8f4c954e446b35ea0ae, type: 3} diff --git a/Assets/New Database.asset.meta b/Assets/New Database.asset.meta deleted file mode 100644 index c5c7e57..0000000 --- a/Assets/New Database.asset.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 8fac5b907c0463e4590e43f7fedee816 -NativeFormatImporter: - externalObjects: {} - mainObjectFileID: 11400000 - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Entity.cs b/Assets/Scripts/Entity.cs index 8de99da..eb49380 100644 --- a/Assets/Scripts/Entity.cs +++ b/Assets/Scripts/Entity.cs @@ -6,13 +6,13 @@ using static Extensions; public class Entity : LevelObject { //Attribut - [SerializeField, LevelSerialize] + [SerializeField] private int _hp; - [SerializeField, LevelSerialize] + [SerializeField] private float _speed; - [SerializeField, LevelSerialize] + [SerializeField] private int _attack_damage; - [SerializeField, LevelSerialize] + [SerializeField] private float _attack_speed; private float _attack_speed_wait = 0f; @@ -83,8 +83,19 @@ public class Entity : LevelObject dict[nameof(_speed)] = _speed; dict[nameof(_attack_speed)] = _attack_speed; dict[nameof(_attack_damage)] = _attack_damage; + dict[nameof(_attack_speed_wait)] = _attack_speed_wait; return dict; } + public override void LoadDictionary(Dictionary dict) + { + base.LoadDictionary(dict); + + _hp = dict[nameof(_hp)].ToInt(); + _speed = dict[nameof(_speed)].ToFloat(); + _attack_speed = dict[nameof(_attack_speed)].ToFloat(); + _attack_damage = dict[nameof(_attack_damage)].ToInt(); + _attack_speed_wait = dict[nameof(_attack_speed_wait)].ToFloat(); + } #endregion } diff --git a/Assets/Scripts/General/Database.cs b/Assets/Scripts/General/Database.cs index 52b29b8..958b2b9 100644 --- a/Assets/Scripts/General/Database.cs +++ b/Assets/Scripts/General/Database.cs @@ -27,15 +27,15 @@ public class DatabaseEditor : Editor foreach (var file in GetAllPaths(path)) { var scriptableObject = AssetDatabase.LoadAssetAtPath(file); - if (scriptableObject && !Database.Instance.ScriptableObjects.Contains(scriptableObject)) + if (scriptableObject && !targ.ScriptableObjects.Contains(scriptableObject)) { - Database.Instance.ScriptableObjects.Add(scriptableObject); + targ.ScriptableObjects.Add(scriptableObject); } var prefab = AssetDatabase.LoadAssetAtPath(file); - if (prefab && !Database.Instance.Prefabs.Contains(prefab)) + if (prefab && !targ.Prefabs.Contains(prefab)) { - Database.Instance.Prefabs.Add(prefab); + targ.Prefabs.Add(prefab); } } } @@ -53,15 +53,8 @@ public class DatabaseEditor : Editor } } #endif - -[CreateAssetMenu(menuName = "Gather And Defend/Database")] -public class Database : ScriptableObject +public class Database : SingletonBehaviour { - public static Database Instance - { - get; - private set; - } public const string TYPE = nameof(TYPE); [Serializable] @@ -99,7 +92,6 @@ public class Database : ScriptableObject public Database() { - if (!Instance) Instance = this; _prefabs = new List(); _scriptableObjects = new List(); } @@ -108,4 +100,4 @@ public class Database : ScriptableObject private List _folders; public List Folders => _folders; -} +} \ No newline at end of file diff --git a/Assets/Scripts/General/Database.cs.meta b/Assets/Scripts/General/Database.cs.meta index cd94a48..3b3e688 100644 --- a/Assets/Scripts/General/Database.cs.meta +++ b/Assets/Scripts/General/Database.cs.meta @@ -4,7 +4,7 @@ MonoImporter: externalObjects: {} serializedVersion: 2 defaultReferences: [] - executionOrder: 0 + executionOrder: -1 icon: {instanceID: 0} userData: assetBundleName: diff --git a/Assets/Scripts/General/Extensions.cs b/Assets/Scripts/General/Extensions.cs index e55c9b3..922d1e7 100644 --- a/Assets/Scripts/General/Extensions.cs +++ b/Assets/Scripts/General/Extensions.cs @@ -2,9 +2,46 @@ using System.Collections.Generic; using BindingFlags = System.Reflection.BindingFlags; using UnityEngine; +using System.Collections; public static class Extensions { + public static int ToInt(this object jobj) + { + return int.Parse(jobj.ToString()); + } + public static float ToFloat(this object jobj) + { + return float.Parse(jobj.ToString()); + } + public static Vector3 ToVector3(this object jobj) + { + var p_enum = (jobj as IEnumerable).GetEnumerator(); + float[] p_array = new float[3]; + + p_enum.MoveNext(); + p_array[0] = float.Parse(p_enum.Current.ToString()); + p_enum.MoveNext(); + p_array[1] = float.Parse(p_enum.Current.ToString()); + p_enum.MoveNext(); + p_array[2] = float.Parse(p_enum.Current.ToString()); + + return new Vector3(p_array[0], p_array[1], p_array[2]); + } + public static bool ToBool(this object jobj) => bool.Parse(jobj.ToString()); + public static GameObject Create(this GameObject prefab, Vector3 position, Quaternion rotation = default, Transform parent = null) + { + if (!prefab) + { + Debug.Log(""); + } + if (rotation == default) rotation = Quaternion.identity; + var instance = GameObject.Instantiate(prefab, position, rotation); + instance.transform.SetParent(parent); + instance.name = prefab.name; + return instance; + + } public static T GetComponentInChildren(this Component obj, string name) where T : Component { foreach (var comp in obj.GetComponentsInChildren()) @@ -18,36 +55,4 @@ public static class Extensions { return Mathf.Approximately(Vector3.Distance(vect, other), 0); } - - [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] - public class LevelSerializeAttribute : Attribute { } - - /// - /// turns an object into a serializable dictionary - /// - /// - /// - /// - public static Dictionary ToDictionary(this T obj) where T : ILevelObject - { - var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; - var toReturn = new Dictionary(); - var type = obj.GetType(); - - foreach (var field in type.GetFields(flags)) - { - if (!Attribute.IsDefined(field, typeof(LevelSerializeAttribute))) continue; - toReturn[field.Name] = field.GetValue(obj); - } - - foreach (var property in type.GetProperties(flags)) - { - if (!Attribute.IsDefined(property, typeof(LevelSerializeAttribute))) continue; - toReturn[property.Name] = property.GetValue(obj); - } - - if (obj is LevelObject) toReturn[nameof(LevelObjectType)] = LevelObjectType.GameObject; - else toReturn[nameof(LevelObjectType)] = LevelObjectType.Tile; - return toReturn; - } } \ No newline at end of file diff --git a/Assets/Scripts/LevelManager/LevelManagerScript.cs b/Assets/Scripts/LevelManager/LevelManagerScript.cs index 947164c..3756f14 100644 --- a/Assets/Scripts/LevelManager/LevelManagerScript.cs +++ b/Assets/Scripts/LevelManager/LevelManagerScript.cs @@ -4,6 +4,7 @@ using Unity.Plastic.Newtonsoft.Json; using UnityEngine; using UnityEngine.Tilemaps; using GatherAndDefend.LevelEditor; +using Unity.VisualScripting.YamlDotNet.Core.Tokens; #region [custom inspector] #if UNITY_EDITOR @@ -17,11 +18,11 @@ public class LevelManagerEditor : Editor DrawDefaultInspector(); if (GUILayout.Button("Save")) { - LevelManager.Instance.SaveLevel(); + LevelManager.Instance.SaveFile(); } if (GUILayout.Button("Load")) { - LevelManager.Instance.LoadSave(); + LevelManager.Instance.LoadFile(); } } } @@ -130,7 +131,7 @@ public class LevelManager : Singleton } Clear(); } - public void SaveLevel() + public void SaveFile() { var list = _levelObjects.Select(obj => obj.ToDictionary()).ToList(); list.Add(new Dictionary() @@ -143,16 +144,14 @@ public class LevelManager : Singleton Debug.Log("game saved successfully"); } - public void LoadLevel(string levelName, bool clear = false) + public void LoadLevel(Level level, bool clear = false) { if (clear) { ClearLevel(); } - //fetch level from database - _currentLevel = Database.Instance.ScriptableObjects[levelName] as Level; - + _currentLevel = level; Grid grid = Object.FindObjectOfType(); //create new grid if there is none if (!grid) @@ -178,35 +177,49 @@ public class LevelManager : Singleton } Debug.Log("level loaded successfully"); } - public void LoadSave() + public void LoadLevel(string levelName, bool clear = false) + { + if (clear) + { + ClearLevel(); +} + +//fetch level from database + _currentLevel = Database.Instance.ScriptableObjects[levelName] as Level; + + LoadLevel(_currentLevel, clear); + } + public void LoadFile() { ClearLevel(); var dicts = JsonConvert.DeserializeObject>>(saved); - //instantiate all prefabs - foreach (var dict in JsonConvert.DeserializeObject>>(saved)) - { - switch (dict[Database.TYPE].ToString()) - { - case nameof(Database.Prefabs): - CreatePrefab(dict); - break; - case nameof(Level): - LoadLevel(dict["Name"].ToString()); - break; - } - } + var prefabDicts = dicts.FindAll(x => x[Database.TYPE].ToString() == nameof(Database.Prefabs)); + foreach (var prefabDict in prefabDicts) CreatePrefab(prefabDict); + + var tileDicts = dicts.FindAll(x => x[Database.TYPE].ToString() == nameof(Database.ScriptableObjects)); + foreach (var tileDict in tileDicts) CreateTile(tileDict); + Debug.Log("game loaded successfully"); } private void CreatePrefab(Dictionary dict) { var name = dict["Name"].ToString(); var prefab = Database.Instance.Prefabs[name]; - var instance = Object.Instantiate(prefab, LevelTransform); + var instance = prefab.Create(Vector3.zero, parent: LevelTransform); var comp = instance.GetComponent(); comp.LoadDictionary(dict); } + private void CreateTile(Dictionary dict) + { + var name = dict["Name"].ToString(); + + var tile = Object.Instantiate(Database.Instance.ScriptableObjects[name]) as LevelTile; + tile.Instantiated = true; + tile.LoadDictionary(dict); + tile.AddToLevel(); + } #endregion } #endregion @@ -216,6 +229,7 @@ public class LevelManagerScript : MonoBehaviour { public Level firstLevel; private static LevelManagerScript _instance; + void Awake() { if (!_instance) _instance = this; diff --git a/Assets/Scripts/LevelManager/LevelObject.cs b/Assets/Scripts/LevelManager/LevelObject.cs index c0a035f..2006473 100644 --- a/Assets/Scripts/LevelManager/LevelObject.cs +++ b/Assets/Scripts/LevelManager/LevelObject.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System.Collections; +using System.Collections.Generic; using UnityEngine; using static Extensions; @@ -7,9 +8,7 @@ using static Extensions; /// public abstract class LevelObject : MonoBehaviour, ILevelObject { - [LevelSerialize] public Vector3 Position { get => transform.position; protected set => transform.position = value; } - [LevelSerialize] public string Name { get => name; protected set => name = value; } void Awake() @@ -41,14 +40,13 @@ public abstract class LevelObject : MonoBehaviour, ILevelObject { {nameof(Name), Name }, {nameof(Position), new float[]{Position.x, Position.y, Position.z } }, - {Database.TYPE, nameof(Database.Instance.Prefabs) } + {Database.TYPE, nameof(Database.Prefabs) } }; } public virtual void LoadDictionary(Dictionary dict) { Name = dict[nameof(Name)].ToString(); - var p_array = (float[])dict[nameof(Position)]; - Position = new Vector3(p_array[0], p_array[1], p_array[2]); + Position = dict[nameof(Position)].ToVector3(); } public void RemoveFromLevel() diff --git a/Assets/Scripts/LevelManager/LevelTile.cs b/Assets/Scripts/LevelManager/LevelTile.cs index 9500fb5..c6f02d4 100644 --- a/Assets/Scripts/LevelManager/LevelTile.cs +++ b/Assets/Scripts/LevelManager/LevelTile.cs @@ -11,16 +11,16 @@ public abstract class LevelTile : TileBase, ILevelObject { [SerializeField] private Sprite _sprite; - [LevelSerialize] + + public bool Instantiated { get; set; } + public Vector3 Position { get; protected set; } private Tilemap _tilemap; - [LevelSerialize] public string Tilemap { get => _tilemap.name; } - [LevelSerialize] public string Name { get => name; protected set => name = value; } public virtual void LevelStart() { } @@ -41,6 +41,12 @@ public abstract class LevelTile : TileBase, ILevelObject return base.StartUp(position, tilemap, go); } + //if it was created from the level manager, then it is already instantiated, and we shouldn't do it again + if (Instantiated) + { + return base.StartUp(position, tilemap, go); + } + var comp = tilemap.GetComponent(); //need to create an instance of the tile, otherwise the position will change for all tiles instead of only this one. @@ -72,19 +78,24 @@ public abstract class LevelTile : TileBase, ILevelObject {nameof(Name), Name }, {nameof(Position), new float[]{Position.x, Position.y, Position.z } }, {nameof(Tilemap), Tilemap }, - {Database.TYPE, nameof(Database.Instance.Prefabs) } + {Database.TYPE, nameof(Database.ScriptableObjects) } }; } public virtual void LoadDictionary(Dictionary dict) { Name = dict[nameof(Name)].ToString(); - float[] pArray = (float[])dict[nameof(Position)]; - Position = new Vector3(pArray[0], pArray[1], pArray[2]); + Position = dict[nameof(Position)].ToVector3(); - var tilemap = FindObjectOfType().GetComponentInChildren(Tilemap); + var tilemapName = dict[nameof(Tilemap)].ToString(); + _tilemap = FindObjectOfType().GetComponentInChildren(tilemapName); } + public void AddToLevel() + { + _tilemap.SetTile(Vector3Int.RoundToInt(Position), this); + } + public void RemoveFromLevel() { _tilemap.SetTile(Vector3Int.RoundToInt(Position), null); diff --git a/Assets/Scripts/Tiles/ResourceTile.cs b/Assets/Scripts/Tiles/ResourceTile.cs index 6015696..b63bb6c 100644 --- a/Assets/Scripts/Tiles/ResourceTile.cs +++ b/Assets/Scripts/Tiles/ResourceTile.cs @@ -9,13 +9,11 @@ public class ResourceTile : LevelTile [Tooltip("the prefab of the currency that will be spawned when mining this resource")] private GameObject _yieldPrefab; - [LevelSerialize] private string YieldPrefabName => _yieldPrefab.name; - [SerializeField, LevelSerialize] + [SerializeField] private float _yieldSpeed = 1; //resource per second private float _yieldCounter = 0; - [LevelSerialize] public bool Occupied { get; set; } public override void LevelUpdate() @@ -52,7 +50,8 @@ public class ResourceTile : LevelTile base.LoadDictionary(dict); var prefabName = dict[nameof(YieldPrefabName)].ToString(); _yieldPrefab = Database.Instance.Prefabs[prefabName]; - _yieldSpeed = (float)dict[nameof(_yieldSpeed)]; - Occupied = (bool)dict[nameof(Occupied)]; + _yieldSpeed = dict[nameof(_yieldSpeed)].ToFloat(); + _yieldCounter = dict[nameof(_yieldCounter)].ToFloat(); + Occupied = dict[nameof(Occupied)].ToBool(); } } \ No newline at end of file diff --git a/Assets/Scripts/Tiles/SpawnerTile.cs b/Assets/Scripts/Tiles/SpawnerTile.cs index 7cf91f8..d281933 100644 --- a/Assets/Scripts/Tiles/SpawnerTile.cs +++ b/Assets/Scripts/Tiles/SpawnerTile.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using Unity.Plastic.Newtonsoft.Json.Linq; using UnityEngine; [CreateAssetMenu(menuName = "Gather And Defend/Spawner Tile")] @@ -8,33 +7,36 @@ public class SpawnerTile : LevelTile [SerializeField] private GameObject _prefab; [SerializeField] - private bool _spawnOnStart = true; + private bool _spawnOnStart; + private float _lifetime; [SerializeField] private float _spawnSpeed = 0; + [SerializeField, Range(0, 1.001f)] private float _spawnCounter = 0; + public override void LevelStart() { - if (!_spawnOnStart) return; - var instance = Instantiate(_prefab, Position, Quaternion.identity); - instance.transform.SetParent(LevelManager.Instance.LevelTransform); + if (_spawnOnStart && _lifetime <= 0) + { + _prefab.Create(Position, parent: LevelManager.Instance.LevelTransform); + } } public override void LevelUpdate() { + _lifetime += Time.deltaTime; _spawnCounter += Time.deltaTime * _spawnSpeed; if (_spawnCounter < 1) return; _spawnCounter = 0; - var instance = Instantiate(_prefab, Position, Quaternion.identity); - instance.transform.SetParent(LevelManager.Instance.LevelTransform); + _prefab.Create(Position, parent: LevelManager.Instance.LevelTransform); } public override bool Equals(ILevelObject other) { return other is SpawnerTile spawner && base.Equals(spawner) && spawner._prefab == _prefab - && spawner._spawnOnStart == _spawnOnStart && spawner._spawnSpeed == _spawnSpeed; } public override Dictionary ToDictionary() @@ -42,9 +44,10 @@ public class SpawnerTile : LevelTile var dict = base.ToDictionary(); dict[nameof(_prefab)] = _prefab.name; - dict[nameof(_spawnOnStart)] = _spawnOnStart; dict[nameof(_spawnSpeed)] = _spawnSpeed; - + dict[nameof(_spawnCounter)] = _spawnCounter; + dict[nameof(_lifetime)] = _lifetime; + dict[nameof(_spawnOnStart)] = _spawnOnStart; return dict; } public override void LoadDictionary(Dictionary dict) @@ -53,7 +56,9 @@ public class SpawnerTile : LevelTile var prefabName = dict[nameof(_prefab)].ToString(); _prefab = Database.Instance.Prefabs[prefabName]; - _spawnOnStart = (bool)dict[nameof(_spawnOnStart)]; - _spawnSpeed = (float)dict[nameof(_spawnSpeed)]; + _spawnSpeed = dict[nameof(_spawnSpeed)].ToFloat(); + _spawnCounter = dict[nameof(_spawnCounter)].ToFloat(); + _lifetime = dict[nameof(_lifetime)].ToFloat(); + _spawnOnStart = dict[nameof(_spawnOnStart)].ToBool(); } } \ No newline at end of file diff --git a/Assets/Tiles/EnemySpawner.asset b/Assets/Tiles/EnemySpawner.asset index 4bc142e..0aa5b3c 100644 --- a/Assets/Tiles/EnemySpawner.asset +++ b/Assets/Tiles/EnemySpawner.asset @@ -16,3 +16,4 @@ MonoBehaviour: _prefab: {fileID: 6962989255644195630, guid: 377c7275c0001cc47a6b8926ac57d573, type: 3} _spawnOnStart: 1 _spawnSpeed: 0 + _spawnCounter: 0 diff --git a/Assets/Tiles/StickSpawner.asset b/Assets/Tiles/StickSpawner.asset index 723e2e3..e2b5480 100644 --- a/Assets/Tiles/StickSpawner.asset +++ b/Assets/Tiles/StickSpawner.asset @@ -16,3 +16,4 @@ MonoBehaviour: _prefab: {fileID: 6962989255644195630, guid: 6cd87b398e7a0e94580f4fcbe2fd310a, type: 3} _spawnOnStart: 1 _spawnSpeed: 0 + _spawnCounter: 0