Felix Boucher ef8a70aba4 finish to hijack update loop + serialization
problem : tiles dont have an update loop, neither do they have a start / destroy method

solution : finished working on a way to create a custom start/update/destroy pipeline for the projects custom tiles. it's using LevelManagerScript as to Update.

note : also created a database and a start of serialization system so we can save and load stuff.
2023-05-27 20:38:42 -04:00

62 lines
2.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Unity.Plastic.Newtonsoft.Json.Linq;
using UnityEngine;
using UnityEngine.Tilemaps;
using static Extensions;
/// <summary>
/// can be inherited by tiles in order to be added to the level manager
/// </summary>
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 abstract void LevelStart();
public abstract void LevelDestroy();
public abstract void LevelUpdate();
public abstract bool Equals(ILevelObject other);
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<Tilemap>();
//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;
//lambda expression to be used to check if the tile already exists
bool isSameTile(LevelTile tile) => tile.Equals(instance);
//if tile is exactly the same as before, don't add.
if (LevelManager.Instance.Has<LevelTile>(isSameTile))
{
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<string, object> ToDictionary();
}