made LevelObject and Tile methods virtual

problème : le fait que les méthodes update/start/destroy etc des ILevelObject soit abstraites était restreignant (obligation de override)

solution : j'ai rendu les méthodes virtuelles à la place de abstraites.
This commit is contained in:
Felix Boucher 2023-05-26 00:19:07 -04:00
parent ef8a70aba4
commit 1bf7e04798
3 changed files with 32 additions and 28 deletions

View File

@ -77,21 +77,6 @@ public class Entity : LevelObject
&& otherEntity._attack_damage == _attack_damage;
}
public override void LevelDestroy()
{
}
public override void LevelStart()
{
}
public override void LevelUpdate()
{
}
public override Dictionary<string, object> ToDictionary()
{
return Extensions.ToDictionary(this);

View File

@ -19,10 +19,19 @@ public abstract class LevelTile : TileBase, ILevelObject
[LevelSerialize]
public string Name => name;
public abstract void LevelStart();
public abstract void LevelDestroy();
public abstract void LevelUpdate();
public abstract bool Equals(ILevelObject other);
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
@ -39,11 +48,8 @@ public abstract class LevelTile : TileBase, ILevelObject
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))
//if tile already exists, dont add to level manager
if (LevelManager.Instance.Has<LevelTile>(tile => tile.Equals(instance)))
{
return base.StartUp(position, tilemap, go);
}

View File

@ -19,10 +19,23 @@ public abstract class LevelObject : MonoBehaviour, ILevelObject
LevelManager.Instance.Add(this);
}
public abstract void LevelStart();
public abstract void LevelDestroy();
public abstract void LevelUpdate();
public virtual void LevelStart()
{
}
public virtual void LevelDestroy()
{
}
public virtual void LevelUpdate()
{
}
public abstract bool Equals(ILevelObject other);
public virtual bool Equals(ILevelObject other)
{
if (!other.GetType().Equals(this.GetType())) return false;
var otherObject = other as LevelObject;
return otherObject.Name == Name
&& otherObject.Position == Position;
}
public abstract Dictionary<string, object> ToDictionary();
}