- Home /
Array to create a skill progression queue based on collected powerups?
hey guys,
Sorry if that thread title was vague. To be honest, this is not a straight Unity question, more like a programming and logic question but since I am developing in Unity and will use some Unity terms I thought to ask it here. Thanks in advance for your patience.
I want to create the following functionality:
Player is moving through environment, fighting and collecting dropped powerups. Some powerups increment an experience value, let's call them experience powerups. Some powerups offer bonuses once a certain amount of experience powerups have been collected, lets call them skill powerups. The dropping and collecting basic powerups functionality I already have working.
Here's my best idea so far:
There will be a central script which manages current bonuses earned through powerups. This will track experience earned and increment variables that create the powerup effect. For now these are things like +x damage, +x hp etc.
I know how to create this assuming there is a static progression. I could hardcode "if (experience = 100) { damageModifier = 10} " with damage modifier being added to base damage for total damage on attack.
The problem is, how do I dynamically create a queue of new modifiers based on skill powerup pickups?
I'd like to achieve something like this: Player has 0 exp and kills a tough enemy who drops a "+10 damage after 100 more exp, +10 health after 50 more exp and a +10 movespeed after 200 exp" The player would collect these and they'd go into a queue, they'd then unlock them in the order picked up meaning at 100 exp he'd get +10 damage, at 150 exp he'd get +10 health and at 350 exp he'd get +10 movespeed.
I've worked with arrays a bit to hold random loot drop prefabs and got that working. I have an intuition that might be part of the solution. I think I need a data structure where onTriggerEnter the skill powerup prefab adds the attributes to an array or similar data management structure. These stored values would then be loaded based on reaching certain values in the experience counter.
What's hard for me is how to store multiple data types in an array like "+10 hp" and how to say "for this item load next value in array after 50 exp, for next one load next after 100"
Or is an array even the cleanest way to do this?
I would love some advice from some more experienced people. I am currently working in Javascript primarily but have ended up needing to incorporate some C# scripts (I know mixing script types is not optimal), this is to say if you want to provide a code example JS is preferred but I am learning C# too.
For some general context I am creating a 3d flying roguelike-ish game where a player flies around and fights enemies while collecting powerups and improving their abilities, with permadeath. The design and mechanics are heavily influenced by Risk of Rain, Binding of Isaac and Dota 2. It's a single player exploratory roguelike with simple procedurally generated levels. I'm trying to make the experience as dynamic and randomized as possible. Perhaps this is all very ambitious, and yes I'm a bit of a newb. I'm going for it anyway.
For more about my overall project you can check out http://www.newbquest.com/ and see my latest gameplay video dev blog.
thanks again, M.
Answer by Jamora · Dec 05, 2013 at 06:30 AM
I would attempt to solve this by having a class for the bonuses, let's call it SkillBonus. Then the matter is just as simple as having the killed mob (or picked up bonus, or whatever adds the bonus) add the correct components on the player at an appropriate time (picking up, on death, etc.).
The code could look like this:
public abstract class SkillBonus : MonoBehaviour{
protected float bonus = 0f;
protected int xpRequired;
public void Init(float bonusAmount, int xp){
bonus = bonusAmount;
xpRequired = xp;
}
public abstract void ApplyBonus();
protected void Start(){
if (bonus == 0f){
Debug.LogWarning("A Bonus has not been set: "+this);
Destroy(this);
}
}
}
public class HpBonus : SkillBonus{
public override void ApplyBonus(){
gameObject.GetComponent<Player>().hp += bonus;
Destroy(this);
}
}
And the system would work as follows: each time the player's xp is increased, it loops over all added SkillBonuses on its gameObject, checks if it has enough xp to apply the bonus. If the bonus is applicable, then it calls ApplyBonus() on the SkillBonus. Alternatively, you could have an Update in SkillBonus that checks if the player has enough xp... I like to have as few Updates as possible, but it's up to you, really. You could also consider caching the SkillBonuses so you don't have to call GetComponent constantly.