Felix Boucher 4fbce56853 travail sur le save et le load
problème : pas de moyen de sauvegarder et de charger les niveaux entamés

solution : fonctions save et load dans le LevelManager
2023-05-27 20:38:43 -04:00

58 lines
1.6 KiB
C#

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
{
[LevelSerialize]
public Vector3 Position { get => transform.position; protected set => transform.position = value; }
[LevelSerialize]
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.Instance.Prefabs) }
};
}
public virtual void LoadDictionary(Dictionary<string, object> 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]);
}
public void RemoveFromLevel()
{
Destroy(gameObject);
}
}