Tower Levels
Lesson 16 of 25 in Coddy's Unity Game: Tower Defense course.
Towers will be upgraded: each level adds damage and range, and each upgrade costs more than the last. The levels follow a rule, so the asset holds the rule rather than a list of numbers, and methods on the ScriptableObject work it out, so every script that asks gets the same answer. A farm game's crop:
public int YieldAt(int level)
{
return baseYield + yieldPerLevel * (level - 1);
}Level 1 is the base value. The tower keeps only its own level, and fires with the numbers for that level.
Challenge
MediumGive towers levels.
- In
TowerData, add the public intsmaxLevel,upgradeCostanddamagePerLeveland the public floatrangePerLevel. - Add
DamageAt(int level):damage + damagePerLevel * (level - 1);RangeAtthe same;UpgradeCost(int level):upgradeCost * level. - In
Tower, addprivate int level = 1;,public int Level => level;andpublic void Upgrade() { level++; }; fire withDamageAt(level)withinRangeAt(level).
Press Play: the towers fight as before. Then Check.
Try it yourself
using UnityEngine;
[CreateAssetMenu(fileName = "NewTower", menuName = "Tower Defense/Tower Data")]
public class TowerData : ScriptableObject
{
public string towerName;
public int cost = 50;
public int damage = 2;
public float range = 4f;
public float fireRate = 1f;
}
All lessons in Unity Game: Tower Defense
Practice on your own: Unity playground