using System.Collections; using System.Collections.Generic; using Unity.Plastic.Newtonsoft.Json.Linq; using UnityEngine; using UnityEngine.Tilemaps; using static Extensions; /// /// can be inherited by tiles in order to be added to the level manager /// public abstract class LevelTile : TileBase, ILevelObject { [SerializeField] private Sprite _sprite; [LevelSerialize] public Vector3 Position { get; protected set; } [LevelSerialize] public string Tilemap { get; protected set; } [LevelSerialize] public string Name => name; public virtual void LevelStart() { } public virtual void LevelDestroy() { } public virtual void LevelUpdate() { } public virtual bool Equals(ILevelObject other) { if (!other.GetType().Equals(GetType())) return false; var otherTile = other as LevelTile; return Name == otherTile.Name && Position == otherTile.Position && Tilemap == otherTile.Tilemap; } public override bool StartUp(Vector3Int position, ITilemap tilemap, GameObject go) { //only execute if application is in play mode if (!Application.isPlaying) { 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. var instance = Instantiate(this); instance.name = name; instance.Position = position; instance.Tilemap = comp.name; //if tile already exists, dont add to level manager if (LevelManager.Instance.Has(tile => tile.Equals(instance))) { return base.StartUp(position, tilemap, go); } LevelManager.Instance.Add(instance); return base.StartUp(position, tilemap, go); } public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData) { tileData.sprite = _sprite; tileData.transform.SetTRS(Vector3.zero, Quaternion.identity, Vector3.one); tileData.color = Color.white; } public abstract Dictionary ToDictionary(); }