I am creating a tower defense game.
Currently I have 2 different tower types 'Shooting tower', amd 'Unit tower'.
I have a Scriptable object to initialize the tower. It contains some tower properties like its attack radius, damage and so on.
I am initializing my towers from a ScriptableObject called TowerSO, it also contains a prefab for a tower.
The problem here is that I have different properties for different tower types stored in TowerSO, for example, the unit tower has properties like spawnDelay
, maximumUnitsToSpawn
, while the archer tower doesn't need this info.
So I guess it is not good approach because anytime I add a new tower type with additional properties it would mean that TowerSO will grow more.
I also want to be able to initialize each tower when it's Instantiated but now I don't know what tower I am instantiating and I don't want to add specialized conditional logic for each type like if (towerType == ArcherTower)
Currently my code looks like this:
GameObject tower = Instantiate(
currentlySelectedTowerSO.prefab,
new Vector3(gridPosition.x + xOffset, gridPosition.y + yOffset, currentlySelectedTowerSO.prefab.transform.position.z),
Quaternion.identity);
// initialize knight tower(if it is a knight tower)
tower.GetComponent<KnightTower>().Initialize(maxUnitNumber, spawnDelay);
// initialize archer tower(if it is an archer tower)
tower.GetComponent<KnightTower>().Initialize(minDamage, maxDamage, arrowSpeed);
How do I separate different tower properties to different Scriptable Objects with completely different fields, and also how do I initialize them, passing relative arguments to each tower?
For ArcherTower (min-max damage, arrowSpeed...)
For UnitTower(MaxUnitNumber, SpawnDelay)