Felix Boucher d4f32e439f working save / load functions
problème : au moment de load une save, les tiles qui spawnaient des GameObjects au start les spawnaient malgré qu'ils l'avait déjà spawné dans la dernière session de jeu + problèmes de sérialisation divers

solution : spawner le GameObject seulement si le lifetime de la tile est de zéro. correction des différents problèmes de sérialisation.

note : les tiles ne semblent vraiment pas être faites pour avoir une update loop. mais bon, maintenant ça marche.
2023-05-28 01:52:44 -04:00

56 lines
1.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static Extensions;
/// <summary>
/// can be inherited by MonoBehaviours in order to be added to the level manager
/// </summary>
public abstract class LevelObject : MonoBehaviour, ILevelObject
{
public Vector3 Position { get => transform.position; protected set => transform.position = value; }
public string Name { get => name; protected set => name = value; }
void Awake()
{
if (LevelManager.Instance.Has<LevelObject>(obj => obj.Equals(this))) return;
LevelManager.Instance.Add(this);
}
public virtual void LevelStart()
{
}
public virtual void LevelDestroy()
{
}
public virtual void LevelUpdate()
{
}
public virtual bool Equals(ILevelObject other)
{
return other is LevelObject otherObject
&& otherObject.Name == Name
&& otherObject.Position == Position;
}
public virtual Dictionary<string, object> ToDictionary()
{
return new Dictionary<string, object>()
{
{nameof(Name), Name },
{nameof(Position), new float[]{Position.x, Position.y, Position.z } },
{Database.TYPE, nameof(Database.Prefabs) }
};
}
public virtual void LoadDictionary(Dictionary<string, object> dict)
{
Name = dict[nameof(Name)].ToString();
Position = dict[nameof(Position)].ToVector3();
}
public void RemoveFromLevel()
{
Destroy(gameObject);
}
}