Advertisement
Guest User

Untitled

a guest
Feb 28th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.26 KB | None | 0 0
  1. fn process_effects(entity: &mut Entity, mut damage: Damage) {
  2.     // we pass in the damage, which is a struct with damage
  3.     // split into type and amount, so it might be 50 magical + 25 physical and 0 everything else
  4.     // this damage isn't applied until the *end* of this function
  5.  
  6.     // presumably entity has a list of active effects, so we go through them
  7.     for mut effect in entity.effects {
  8.         // entity might be on fire, which could tick up the "fire" part of the Damage
  9.         effect.tick(&mut damage, &mut entity);        
  10.     }
  11.     for mut effect in entity.effects {
  12.         // now that all the tickers are applied, we go through each effect and see if anything
  13.         // wants to modify the damage, either to reduce it, or to amplify it
  14.         effect.modify_damage(&mut damage);
  15.     }
  16.  
  17.     // lastly, we go through and remove any expired buffs
  18.     // see: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.retain
  19.     entity.effects.retain(|&effect| !effect.expired());
  20.  
  21.     // finally done, so we can now *actually* apply the damage we've been building up/modifying:
  22.     // could conceivably modify the damage further in there, affected by the entity stats/equipment
  23.     // alternatively, you could rename this function and do it here
  24.     entity.deal_damage(damage);
  25. }
  26.  
  27. #[derive(serde::Serialize, serde::Deserialize, Debug)]
  28. struct MagicShield {
  29.     max_health: i32,
  30.     health_left: i32,
  31.     max_duration: i32,
  32.     duration_left: i32,
  33.    
  34. }
  35.  
  36. // this says, if you implement effect you also need to implement Debug
  37. // which we "do" by deriving it
  38. trait Effect : std::fmt::Debug {
  39.    
  40.     fn begin(&mut self, entity: &mut Entity);
  41.     fn tick(&mut self, damage: &mut Damage, entity: &mut Entity);
  42.     fn modify_damage(&mut self, damage: &mut Damage); // possibly take entity here?
  43.     fn expired(&self) -> bool;
  44.    
  45. }
  46.  
  47. impl Effect for MagicShield {
  48.     fn begin(&mut self, entity: &mut Entity) {
  49.         // if your int is below 20, your shield is weaker; as it goes higher, it becomes more effective
  50.         self.max_health = (self.max_health as f64 * (entity.intelligence() as f64 / 20.0)) as i32;
  51.         self.health_left = self.max_health;
  52.         self.duration_left = self.max_duration; // could amplify this too
  53.         // things might be tricky if you wanted to have buffs that "amplify" effect durations tho
  54.         // could pass in entity, and store some kind of status_amp value
  55.  
  56.     }
  57.     fn tick(&mut self, damage: &mut Damage, entity: &mut Entity) {
  58.         // tick down duration, otherwise do nothing
  59.         self.duration_left -= 1;
  60.     }
  61.     fn modify_damage(&mut self, damage: &mut Damage) {
  62.         if damage.physical > 0 {
  63.             // try to absorb the damage
  64.             self.health_left -= damage.physical;
  65.             if self.health_left > 0 {
  66.                 // we absorbed it all!
  67.                 damage.physical = 0;
  68.             } else {
  69.                 // oh no, it broke our shield!
  70.                 damage.physical = self.health_left.abs();
  71.                 // set the physical damage to whatever we couldn't absorb
  72.             }
  73.         }
  74.         // otherwise, we don't do anything at all . . .
  75.     }
  76.     fn expired(&self) -> bool {
  77.         self.health_left > 0 || self.duration_left <= 0
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement