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.
64 lines
2.0 KiB
C#
64 lines
2.0 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
[CreateAssetMenu(menuName = "Gather And Defend/Spawner Tile")]
|
|
public class SpawnerTile : LevelTile
|
|
{
|
|
[SerializeField]
|
|
private GameObject _prefab;
|
|
[SerializeField]
|
|
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 && _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;
|
|
_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._spawnSpeed == _spawnSpeed;
|
|
}
|
|
public override Dictionary<string, object> ToDictionary()
|
|
{
|
|
var dict = base.ToDictionary();
|
|
|
|
dict[nameof(_prefab)] = _prefab.name;
|
|
dict[nameof(_spawnSpeed)] = _spawnSpeed;
|
|
dict[nameof(_spawnCounter)] = _spawnCounter;
|
|
dict[nameof(_lifetime)] = _lifetime;
|
|
dict[nameof(_spawnOnStart)] = _spawnOnStart;
|
|
return dict;
|
|
}
|
|
public override void LoadDictionary(Dictionary<string, object> dict)
|
|
{
|
|
base.LoadDictionary(dict);
|
|
|
|
var prefabName = dict[nameof(_prefab)].ToString();
|
|
_prefab = Database.Instance.Prefabs[prefabName];
|
|
_spawnSpeed = dict[nameof(_spawnSpeed)].ToFloat();
|
|
_spawnCounter = dict[nameof(_spawnCounter)].ToFloat();
|
|
_lifetime = dict[nameof(_lifetime)].ToFloat();
|
|
_spawnOnStart = dict[nameof(_spawnOnStart)].ToBool();
|
|
}
|
|
} |