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; && otherEntity._attack_damage == _attack_damage;
} }
public override void LevelDestroy()
{
}
public override void LevelStart()
{
}
public override void LevelUpdate()
{
}
public override Dictionary<string, object> ToDictionary() public override Dictionary<string, object> ToDictionary()
{ {
return Extensions.ToDictionary(this); return Extensions.ToDictionary(this);

View File

@ -19,10 +19,19 @@ public abstract class LevelTile : TileBase, ILevelObject
[LevelSerialize] [LevelSerialize]
public string Name => name; public string Name => name;
public abstract void LevelStart(); public virtual void LevelStart() { }
public abstract void LevelDestroy(); public virtual void LevelDestroy() { }
public abstract void LevelUpdate(); public virtual void LevelUpdate() { }
public abstract bool Equals(ILevelObject other); 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) public override bool StartUp(Vector3Int position, ITilemap tilemap, GameObject go)
{ {
//only execute if application is in play mode //only execute if application is in play mode
@ -39,11 +48,8 @@ public abstract class LevelTile : TileBase, ILevelObject
instance.Position = position; instance.Position = position;
instance.Tilemap = comp.name; instance.Tilemap = comp.name;
//lambda expression to be used to check if the tile already exists //if tile already exists, dont add to level manager
bool isSameTile(LevelTile tile) => tile.Equals(instance); if (LevelManager.Instance.Has<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); return base.StartUp(position, tilemap, go);
} }

View File

@ -19,10 +19,23 @@ public abstract class LevelObject : MonoBehaviour, ILevelObject
LevelManager.Instance.Add(this); LevelManager.Instance.Add(this);
} }
public abstract void LevelStart(); public virtual void LevelStart()
public abstract void LevelDestroy(); {
public abstract void LevelUpdate(); }
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(); public abstract Dictionary<string, object> ToDictionary();
} }