Search Unity

(RPG)How multiple handle duration of multiple buffs(effects)

Discussion in 'Scripting' started by spil778, Jan 31, 2015.

  1. spil778

    spil778

    Joined:
    Mar 31, 2013
    Posts:
    57
    I am running into the problems of how to handle the duration of multiple effects. If the effect exist it should override the current effect however if not add and remove after X amount of time. My current handler looks like this:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. function Parser(dmg : int, effect : String, dura : int, stat : String){
    4.     switch(effect){
    5.         case "Slow":
    6.             Slow(dmg,dura);
    7.             break;
    8.         case "Posion":
    9.             Posion(dmg,dura);
    10.             break;
    11.         case "Stun":
    12.             Stun(dura);
    13.             break;
    14.         case "BoostStat":
    15.             StatBuff(dmg,dura,stat);
    16.             break;
    17.         case "DebuffStat":
    18.             StatDeBuff(dmg,dura,stat);
    19.             break;
    20.         case "Attack":
    21.             Attack(dmg);
    22.             break;
    23.     }
    24. }
    25.  
    26. function Attack(dmg){
    27.     var status = this.GetComponent(Status);
    28.     status.OnDamage(dmg);
    29. }
    30.  
    31. function StatDeBuff(dmg : int, dura : int, stat : String){
    32.     var status = this.GetComponent(Status);
    33.     status.ModStat(stat,dmg*-1);
    34.     yield WaitForSeconds(dura);
    35.     status.ModStat(stat,dmg);
    36.     status.SaveData();
    37. }
    38.  
    39. function StatBuff(dmg : int, dura : int, stat : String){
    40.     var status = this.GetComponent(Status);
    41.     status.ModStat(stat,dmg);
    42.     yield WaitForSeconds(dura);
    43.     status.ModStat(stat,dmg*-1);
    44.     status.SaveData();
    45. }
    46.  
    47. function HealingHp(dmg : int, dura : int){
    48.     var status = this.GetComponent(Status);
    49.     var tempPosion : int;
    50.      do
    51.     {
    52.         status.Heal(dmg,0);
    53.         yield WaitForSeconds(1);
    54.         tempPosion += 1;
    55.        
    56.     }while(dura != tempPosion);
    57. }
    58.  
    59. function HealingMp(dmg : int, dura : int){
    60.     var status = this.GetComponent(Status);
    61.     var tempPosion : int;
    62.      do
    63.     {
    64.         status.Heal(0,dmg);
    65.         yield WaitForSeconds(1);
    66.         tempPosion += 1;
    67.        
    68.     }while(dura != tempPosion);
    69. }
    70.  
    71. function Stun(dura : int){
    72.     var aiSet = this.GetComponent(AIset);
    73.     aiSet.freeze = true;
    74.     yield WaitForSeconds(dura);
    75.     aiSet.freeze = false;
    76. }
    77.  
    78. function Slow(dmg : int, dura){
    79.         var aiSet = this.GetComponent(AIset);
    80.         if(aiSet.speed-dmg <= 0){
    81.             aiSet.speed = 0;
    82.         }
    83.         else{
    84.             aiSet.speed -= dmg;
    85.         }
    86.         yield WaitForSeconds(dura);
    87.         aiSet.speed += dmg;
    88. }
    89. function Posion(dmg : int, dura : int){
    90.     var status = this.GetComponent(Status);
    91.     var tempPosion : int;
    92.      do
    93.     {
    94.         status.OnDamage(dmg);
    95.         yield WaitForSeconds(1);
    96.         tempPosion += 1;
    97.        
    98.     }while(dura != tempPosion);
    99. }
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Keep a list of the effects that are currently on the player. Then remove them when you are done. If the effect is present reset the effect time, if it does not add the effect.
     
  3. spil778

    spil778

    Joined:
    Mar 31, 2013
    Posts:
    57
    Lets say we have 3 things on the list
    Slow - 10 sec
    Posion - 3 sec
    debuff str - 37 secs.

    Then how do I check each timer indivually? and for that sake find out at where this timers effect is in the list? if that made sense.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Your structure is broken and should be thrown out the nearest window.

    Create a base class for you effects. Give it a timer and a virtual method to apply the effect. Each effect should inherit from this base class.

    Then you can hold a list of all of the active effects, iterate through them for their apply method and for their timer values.