veloren/common/src/comp/projectile.rs

471 lines
16 KiB
Rust
Raw Normal View History

use crate::{
2021-01-29 00:04:44 +00:00
combat::{
2021-06-10 02:04:19 +00:00
Attack, AttackDamage, AttackEffect, CombatBuff, CombatBuffStrength, CombatEffect,
CombatRequirement, Damage, DamageKind, DamageSource, GroupTarget, Knockback, KnockbackDir,
2021-01-29 00:04:44 +00:00
},
2021-06-10 02:04:19 +00:00
comp::{buff::BuffKind, item::Reagent},
uid::Uid,
2021-01-29 00:04:44 +00:00
Explosion, RadiusEffect,
};
use serde::{Deserialize, Serialize};
use specs::Component;
use specs_idvs::IdvStorage;
use std::time::Duration;
2019-09-17 12:43:19 +00:00
2021-05-16 03:09:39 +00:00
#[derive(Clone, Debug, Serialize, Deserialize)]
2019-09-17 12:43:19 +00:00
pub enum Effect {
2021-01-29 00:04:44 +00:00
Attack(Attack),
Explode(Explosion),
2019-09-17 12:43:19 +00:00
Vanish,
2019-09-28 19:35:28 +00:00
Stick,
Possess,
2021-08-30 15:02:13 +00:00
Bonk, // Knock/dislodge/change objects on hit
2019-09-17 12:43:19 +00:00
}
2021-05-16 03:09:39 +00:00
#[derive(Clone, Debug, Serialize, Deserialize)]
2019-09-17 12:43:19 +00:00
pub struct Projectile {
2019-11-04 00:57:36 +00:00
// TODO: use SmallVec for these effects
2020-04-26 15:16:35 +00:00
pub hit_solid: Vec<Effect>,
2019-09-17 12:43:19 +00:00
pub hit_entity: Vec<Effect>,
/// Time left until the projectile will despawn
pub time_left: Duration,
pub owner: Option<Uid>,
/// Whether projectile collides with entities in the same group as its
/// owner
pub ignore_group: bool,
2021-06-01 01:51:47 +00:00
/// Whether the projectile is sticky
pub is_sticky: bool,
/// Whether the projectile should use a point collider
pub is_point: bool,
2019-09-17 12:43:19 +00:00
}
impl Component for Projectile {
type Storage = IdvStorage<Self>;
2019-09-17 12:43:19 +00:00
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub enum ProjectileConstructor {
Arrow {
damage: f32,
knockback: f32,
energy_regen: f32,
},
Fireball {
damage: f32,
radius: f32,
energy_regen: f32,
2021-10-27 19:42:11 +00:00
min_falloff: f32,
},
2021-02-16 05:18:05 +00:00
Frostball {
damage: f32,
radius: f32,
2021-10-27 19:42:11 +00:00
min_falloff: f32,
2021-02-16 05:18:05 +00:00
},
NecroticSphere {
damage: f32,
radius: f32,
2021-10-27 19:42:11 +00:00
min_falloff: f32,
},
Possess,
2021-05-04 23:02:18 +00:00
ClayRocket {
damage: f32,
radius: f32,
knockback: f32,
2021-10-27 19:42:11 +00:00
min_falloff: f32,
2021-05-04 23:02:18 +00:00
},
2021-06-01 01:51:47 +00:00
Snowball {
damage: f32,
radius: f32,
2021-10-27 19:42:11 +00:00
min_falloff: f32,
2021-06-01 01:51:47 +00:00
},
2021-06-10 02:04:19 +00:00
ExplodingPumpkin {
damage: f32,
radius: f32,
knockback: f32,
2021-10-27 19:42:11 +00:00
min_falloff: f32,
2021-06-10 02:04:19 +00:00
},
}
impl ProjectileConstructor {
pub fn create_projectile(
self,
owner: Option<Uid>,
crit_chance: f32,
crit_mult: f32,
2021-07-10 16:04:12 +00:00
buff_strength: f32,
) -> Projectile {
use ProjectileConstructor::*;
match self {
Arrow {
damage,
knockback,
energy_regen,
} => {
2021-02-02 18:02:40 +00:00
let knockback = AttackEffect::new(
Some(GroupTarget::OutOfGroup),
CombatEffect::Knockback(Knockback {
strength: knockback,
direction: KnockbackDir::Away,
}),
)
.with_requirement(CombatRequirement::AnyDamage);
let energy = AttackEffect::new(None, CombatEffect::EnergyReward(energy_regen))
.with_requirement(CombatRequirement::AnyDamage);
2021-07-10 16:04:12 +00:00
let buff = CombatEffect::Buff(CombatBuff {
kind: BuffKind::Bleeding,
dur_secs: 10.0,
strength: CombatBuffStrength::DamageFraction(0.1 * buff_strength),
chance: 0.1,
});
2021-02-02 18:02:40 +00:00
let damage = AttackDamage::new(
Damage {
source: DamageSource::Projectile,
kind: DamageKind::Piercing,
2021-02-02 18:02:40 +00:00
value: damage,
},
Some(GroupTarget::OutOfGroup),
)
.with_effect(buff);
2021-01-29 00:04:44 +00:00
let attack = Attack::default()
.with_damage(damage)
.with_crit(crit_chance, crit_mult)
.with_effect(energy)
.with_effect(knockback)
.with_combo_increment();
2021-01-29 00:04:44 +00:00
Projectile {
2021-08-30 15:02:13 +00:00
hit_solid: vec![Effect::Stick, Effect::Bonk],
2021-01-29 00:04:44 +00:00
hit_entity: vec![Effect::Attack(attack), Effect::Vanish],
time_left: Duration::from_secs(15),
owner,
ignore_group: true,
2021-06-01 01:51:47 +00:00
is_sticky: true,
is_point: true,
}
},
Fireball {
damage,
radius,
energy_regen,
2021-10-27 19:42:11 +00:00
min_falloff,
2021-01-30 22:35:00 +00:00
} => {
2021-02-02 18:02:40 +00:00
let energy = AttackEffect::new(None, CombatEffect::EnergyReward(energy_regen))
.with_requirement(CombatRequirement::AnyDamage);
2021-10-27 21:19:29 +00:00
let buff = CombatEffect::Buff(CombatBuff {
kind: BuffKind::Bleeding,
dur_secs: 5.0,
strength: CombatBuffStrength::DamageFraction(0.1 * buff_strength),
chance: 0.1,
});
2021-02-02 18:02:40 +00:00
let damage = AttackDamage::new(
Damage {
source: DamageSource::Explosion,
kind: DamageKind::Energy,
2021-02-02 18:02:40 +00:00
value: damage,
},
Some(GroupTarget::OutOfGroup),
2021-10-27 21:19:29 +00:00
)
.with_effect(buff);
let attack = Attack::default()
.with_damage(damage)
.with_crit(crit_chance, crit_mult)
.with_effect(energy)
.with_combo_increment();
2021-01-30 22:35:00 +00:00
let explosion = Explosion {
effects: vec![
RadiusEffect::Attack(attack),
RadiusEffect::TerrainDestruction(2.0),
],
radius,
2021-02-16 05:18:05 +00:00
reagent: Some(Reagent::Red),
2021-10-27 19:42:11 +00:00
min_falloff,
2021-02-16 05:18:05 +00:00
};
Projectile {
hit_solid: vec![Effect::Explode(explosion.clone()), Effect::Vanish],
hit_entity: vec![Effect::Explode(explosion), Effect::Vanish],
time_left: Duration::from_secs(10),
owner,
ignore_group: true,
2021-06-01 01:51:47 +00:00
is_sticky: true,
is_point: true,
2021-02-16 05:18:05 +00:00
}
},
2021-10-27 19:42:11 +00:00
Frostball {
damage,
radius,
min_falloff,
} => {
2021-02-16 05:18:05 +00:00
let damage = AttackDamage::new(
Damage {
source: DamageSource::Explosion,
kind: DamageKind::Energy,
2021-02-16 05:18:05 +00:00
value: damage,
},
Some(GroupTarget::OutOfGroup),
);
let attack = Attack::default()
.with_damage(damage)
.with_crit(crit_chance, crit_mult)
.with_combo_increment();
2021-02-16 05:18:05 +00:00
let explosion = Explosion {
2021-02-20 20:38:27 +00:00
effects: vec![RadiusEffect::Attack(attack)],
2021-02-16 05:18:05 +00:00
radius,
2021-06-01 01:51:47 +00:00
reagent: Some(Reagent::White),
2021-10-27 19:42:11 +00:00
min_falloff,
2021-01-30 22:35:00 +00:00
};
Projectile {
hit_solid: vec![Effect::Explode(explosion.clone()), Effect::Vanish],
hit_entity: vec![Effect::Explode(explosion), Effect::Vanish],
time_left: Duration::from_secs(10),
owner,
ignore_group: true,
2021-06-01 01:51:47 +00:00
is_sticky: true,
is_point: true,
2021-01-30 22:35:00 +00:00
}
2020-12-24 17:54:00 +00:00
},
2021-10-27 19:42:11 +00:00
NecroticSphere {
damage,
radius,
min_falloff,
} => {
let damage = AttackDamage::new(
Damage {
source: DamageSource::Explosion,
kind: DamageKind::Energy,
value: damage,
},
Some(GroupTarget::OutOfGroup),
);
let attack = Attack::default()
.with_damage(damage)
.with_crit(crit_chance, crit_mult)
.with_combo_increment();
let explosion = Explosion {
effects: vec![RadiusEffect::Attack(attack)],
radius,
reagent: Some(Reagent::Purple),
2021-10-27 19:42:11 +00:00
min_falloff,
};
Projectile {
hit_solid: vec![Effect::Explode(explosion.clone()), Effect::Vanish],
hit_entity: vec![Effect::Explode(explosion), Effect::Vanish],
time_left: Duration::from_secs(10),
owner,
ignore_group: true,
2021-06-01 01:51:47 +00:00
is_sticky: true,
is_point: true,
}
},
Possess => Projectile {
hit_solid: vec![Effect::Stick],
hit_entity: vec![Effect::Stick, Effect::Possess],
time_left: Duration::from_secs(10),
owner,
ignore_group: false,
2021-06-01 01:51:47 +00:00
is_sticky: true,
is_point: true,
},
2021-05-04 23:02:18 +00:00
ClayRocket {
damage,
radius,
knockback,
2021-10-27 19:42:11 +00:00
min_falloff,
2021-05-04 23:02:18 +00:00
} => {
let knockback = AttackEffect::new(
Some(GroupTarget::OutOfGroup),
CombatEffect::Knockback(Knockback {
strength: knockback,
direction: KnockbackDir::Away,
}),
)
.with_requirement(CombatRequirement::AnyDamage);
let damage = AttackDamage::new(
Damage {
source: DamageSource::Explosion,
2021-05-06 23:45:48 +00:00
kind: DamageKind::Energy,
2021-05-04 23:02:18 +00:00
value: damage,
},
Some(GroupTarget::OutOfGroup),
);
let attack = Attack::default()
.with_damage(damage)
.with_crit(crit_chance, crit_mult)
.with_effect(knockback);
let explosion = Explosion {
effects: vec![
RadiusEffect::Attack(attack),
RadiusEffect::TerrainDestruction(5.0),
],
radius,
reagent: Some(Reagent::Red),
2021-10-27 19:42:11 +00:00
min_falloff,
2021-05-04 23:02:18 +00:00
};
Projectile {
hit_solid: vec![Effect::Explode(explosion.clone()), Effect::Vanish],
hit_entity: vec![Effect::Explode(explosion), Effect::Vanish],
time_left: Duration::from_secs(10),
owner,
ignore_group: true,
2021-06-01 01:51:47 +00:00
is_sticky: true,
is_point: true,
}
},
2021-10-27 19:42:11 +00:00
Snowball {
damage,
radius,
min_falloff,
} => {
2021-06-01 01:51:47 +00:00
let damage = AttackDamage::new(
Damage {
source: DamageSource::Explosion,
kind: DamageKind::Energy,
value: damage,
},
Some(GroupTarget::OutOfGroup),
);
let attack = Attack::default()
.with_damage(damage)
.with_crit(crit_chance, crit_mult);
let explosion = Explosion {
effects: vec![RadiusEffect::Attack(attack)],
radius,
reagent: Some(Reagent::White),
2021-10-27 19:42:11 +00:00
min_falloff,
2021-06-01 01:51:47 +00:00
};
Projectile {
hit_solid: vec![],
hit_entity: vec![Effect::Explode(explosion), Effect::Vanish],
time_left: Duration::from_secs(120),
owner,
ignore_group: true,
is_sticky: false,
is_point: false,
2021-05-04 23:02:18 +00:00
}
},
2021-06-10 02:04:19 +00:00
ExplodingPumpkin {
damage,
radius,
knockback,
2021-10-27 19:42:11 +00:00
min_falloff,
2021-06-10 02:04:19 +00:00
} => {
let knockback = AttackEffect::new(
Some(GroupTarget::OutOfGroup),
CombatEffect::Knockback(Knockback {
strength: knockback,
direction: KnockbackDir::Away,
}),
)
.with_requirement(CombatRequirement::AnyDamage);
let buff = AttackEffect::new(
Some(GroupTarget::OutOfGroup),
CombatEffect::Buff(CombatBuff {
kind: BuffKind::Burning,
dur_secs: 5.0,
2021-07-10 16:04:12 +00:00
strength: CombatBuffStrength::DamageFraction(0.2 * buff_strength),
2021-06-10 02:04:19 +00:00
chance: 1.0,
}),
)
.with_requirement(CombatRequirement::AnyDamage);
let damage = AttackDamage::new(
Damage {
source: DamageSource::Explosion,
kind: DamageKind::Energy,
value: damage,
},
Some(GroupTarget::OutOfGroup),
);
let attack = Attack::default()
.with_damage(damage)
.with_crit(crit_chance, crit_mult)
.with_effect(knockback)
.with_effect(buff);
let explosion = Explosion {
effects: vec![
RadiusEffect::Attack(attack),
RadiusEffect::TerrainDestruction(5.0),
],
radius,
reagent: Some(Reagent::Red),
2021-10-27 19:42:11 +00:00
min_falloff,
2021-06-10 02:04:19 +00:00
};
Projectile {
hit_solid: vec![Effect::Explode(explosion.clone()), Effect::Vanish],
hit_entity: vec![Effect::Explode(explosion), Effect::Vanish],
time_left: Duration::from_secs(10),
owner,
ignore_group: true,
is_sticky: true,
is_point: true,
}
},
}
}
2021-08-16 17:43:57 +00:00
// TODO: split this to three methods per stat
pub fn modified_projectile(mut self, power: f32, regen: f32, range: f32) -> Self {
use ProjectileConstructor::*;
match self {
2020-12-23 02:28:55 +00:00
Arrow {
ref mut damage,
ref mut energy_regen,
..
} => {
*damage *= power;
*energy_regen *= regen;
},
2020-12-24 17:54:00 +00:00
Fireball {
ref mut damage,
ref mut energy_regen,
ref mut radius,
..
} => {
*damage *= power;
*energy_regen *= regen;
2020-12-24 17:54:00 +00:00
*radius *= range;
},
2021-02-16 05:18:05 +00:00
Frostball {
ref mut damage,
ref mut radius,
..
} => {
*damage *= power;
*radius *= range;
},
NecroticSphere {
ref mut damage,
ref mut radius,
..
} => {
*damage *= power;
*radius *= range;
},
Possess => {},
2021-05-04 23:02:18 +00:00
ClayRocket {
ref mut damage,
ref mut radius,
..
} => {
*damage *= power;
*radius *= range;
},
2021-06-01 01:51:47 +00:00
Snowball {
ref mut damage,
ref mut radius,
2021-10-27 19:42:11 +00:00
..
2021-06-01 01:51:47 +00:00
} => {
*damage *= power;
*radius *= range;
},
2021-06-10 02:04:19 +00:00
ExplodingPumpkin {
ref mut damage,
ref mut radius,
..
} => {
*damage *= power;
*radius *= range;
},
}
self
}
}