Bumping this topic real quick to share what I’ve done this afternoon, that can be quite useful for people coming on this topic.
I’ve been working on the Traps System today, which is basically traps that the player can set on the map to kill monsters. But bare with me, it is appliable to what we discussed earlier on this topic.
I have a DefenseData class, like the following (removed some variables so it’s more readable here) :
public class DefenseData : ScriptableObject
{
[SerializeField] private string _name;
[SerializeField, TextArea(order = 3)] private string _description;
[SerializeField, Range(0, 10)] private int _health;
[SerializeField] private List<DefensePosCondSO> _positionConditions = new List<DefensePosCondSO>();
public string Name
{
get { return _name; }
}
public string Description
{
get { return _description; }
}
public int Health
{
get { return _health; }
}
public List<DefensePosCondSO> PositionConditions
{
get { return _positionConditions; }
}
When the player sets traps, I want to check if he can actually set them here. For example, the Fence can only be set on Dirt.
So I created an abstract class with Position Conditions, that inherits from Scriptable Object, like the following :
public abstract class DefensePosCondSO : ScriptableObject
{
public abstract bool CheckCondition(Vector2Int position);
}
I then created a child class, that checks for a specific type of tile where the player wants to set the trap, like so :
[CreateAssetMenu(menuName = ("Defenses/On Specific Tile"))]
public class OnSpecificTile : DefensePosCondSO
{
[SerializeField] private TileData checkedTile;
public override bool CheckCondition(Vector2Int position)
{
if (!CollisionManager.Tiles.ContainsKey(position))
return false;
if (CollisionManager.Tiles[position] == checkedTile)
return true;
return false;
}
}
And finally, on the DefenseBuilder script, when the player is setting the trap, we iterate through all conditions to check wether or not he can set the trap here :
private bool CheckConditions(DefenseData data, Vector2Int position)
{
foreach(var condition in data.PositionConditions)
{
if (!condition.CheckCondition(position))
return false;
}
return true;
}
And voilà !
I think this logic is really close from what I have been told earlier on this topic, it works like a charm and seems quite scalable for a small team, so I wanted to share it !