Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn process_effects(entity: &mut Entity, mut damage: Damage) {
- // we pass in the damage, which is a struct with damage
- // split into type and amount, so it might be 50 magical + 25 physical and 0 everything else
- // this damage isn't applied until the *end* of this function
- // presumably entity has a list of active effects, so we go through them
- for mut effect in entity.effects {
- // entity might be on fire, which could tick up the "fire" part of the Damage
- effect.tick(&mut damage, &mut entity);
- }
- for mut effect in entity.effects {
- // now that all the tickers are applied, we go through each effect and see if anything
- // wants to modify the damage, either to reduce it, or to amplify it
- effect.modify_damage(&mut damage);
- }
- // lastly, we go through and remove any expired buffs
- // see: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.retain
- entity.effects.retain(|&effect| !effect.expired());
- // finally done, so we can now *actually* apply the damage we've been building up/modifying:
- // could conceivably modify the damage further in there, affected by the entity stats/equipment
- // alternatively, you could rename this function and do it here
- entity.deal_damage(damage);
- }
- #[derive(serde::Serialize, serde::Deserialize, Debug)]
- struct MagicShield {
- max_health: i32,
- health_left: i32,
- max_duration: i32,
- duration_left: i32,
- }
- // this says, if you implement effect you also need to implement Debug
- // which we "do" by deriving it
- trait Effect : std::fmt::Debug {
- fn begin(&mut self, entity: &mut Entity);
- fn tick(&mut self, damage: &mut Damage, entity: &mut Entity);
- fn modify_damage(&mut self, damage: &mut Damage); // possibly take entity here?
- fn expired(&self) -> bool;
- }
- impl Effect for MagicShield {
- fn begin(&mut self, entity: &mut Entity) {
- // if your int is below 20, your shield is weaker; as it goes higher, it becomes more effective
- self.max_health = (self.max_health as f64 * (entity.intelligence() as f64 / 20.0)) as i32;
- self.health_left = self.max_health;
- self.duration_left = self.max_duration; // could amplify this too
- // things might be tricky if you wanted to have buffs that "amplify" effect durations tho
- // could pass in entity, and store some kind of status_amp value
- }
- fn tick(&mut self, damage: &mut Damage, entity: &mut Entity) {
- // tick down duration, otherwise do nothing
- self.duration_left -= 1;
- }
- fn modify_damage(&mut self, damage: &mut Damage) {
- if damage.physical > 0 {
- // try to absorb the damage
- self.health_left -= damage.physical;
- if self.health_left > 0 {
- // we absorbed it all!
- damage.physical = 0;
- } else {
- // oh no, it broke our shield!
- damage.physical = self.health_left.abs();
- // set the physical damage to whatever we couldn't absorb
- }
- }
- // otherwise, we don't do anything at all . . .
- }
- fn expired(&self) -> bool {
- self.health_left > 0 || self.duration_left <= 0
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement