1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use std::ops::{Mul, Sub};
use crate::prelude::*;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProjectileKind {
Bullet,
Beam,
}
pub struct ProjectileParams {
pub kind: ProjectileKind,
pub effects: Vec<Effect>,
pub color: Color,
pub size: f32,
pub origin: Vec2,
pub direction: Vec2,
pub speed: f32,
pub range: f32,
pub on_hit_sound_effect: Option<Sound>,
}
pub struct Projectile {
actor_id: String,
actor: Handle<Actor>,
factions: Vec<String>,
kind: ProjectileKind,
effects: Vec<Effect>,
color: Color,
size: f32,
origin: Vec2,
position: Vec2,
direction: Vec2,
speed: f32,
distance_traveled: f32,
range: f32,
on_hit_sound_effect: Option<Sound>,
}
impl Projectile {
pub fn new(
actor_id: &str,
actor: Handle<Actor>,
factions: &[String],
params: ProjectileParams,
) -> Self {
Projectile {
actor_id: actor_id.to_string(),
actor,
factions: factions.to_vec(),
kind: params.kind,
effects: params.effects,
color: params.color,
size: params.size,
origin: params.origin,
position: params.origin,
direction: params.direction,
speed: params.speed,
distance_traveled: 0.0,
range: params.range,
on_hit_sound_effect: params.on_hit_sound_effect,
}
}
}
#[derive(Default)]
pub struct Projectiles {
active: Vec<Projectile>,
}
impl Projectiles {
pub const DEFAULT_PROJECTILE_COLOR: Color = color::YELLOW;
pub const DEFAULT_PROJECTILE_SIZE: f32 = 1.0;
pub const DEFAULT_BEAM_COLOR: Color = color::RED;
pub const DEFAULT_BEAM_SIZE: f32 = 2.0;
const MIN_PROJECTILE_SPEED: f32 = 1.0;
const MAX_PROJECTILE_SPEED: f32 = 200.0;
const SPEED_VARIANCE_MIN: f32 = 0.9;
const SPEED_VARIANCE_MAX: f32 = 1.1;
const SPREAD_CALCULATION_DISTANCE: f32 = 100.0;
const PROJECTILE_LENGTH_FACTOR_MIN: f32 = 3.0;
const PROJECTILE_LENGTH_FACTOR_MAX: f32 = 15.0;
const BEAM_LENGTH_FACTOR_MIN: f32 = 2.0;
const BEAM_LENGTH_FACTOR_MAX: f32 = 6.0;
pub fn new() -> Self {
Projectiles { active: Vec::new() }
}
pub fn add_node() -> Handle<Self> {
scene::add_node(Self::new())
}
pub fn spawn(
&mut self,
actor_id: &str,
actor: Handle<Actor>,
factions: &[String],
spread: f32,
mut params: ProjectileParams,
) {
let spread_target = params.direction * Self::SPREAD_CALCULATION_DISTANCE;
params.direction = vec2(
rand::gen_range(spread_target.x - spread, spread_target.x + spread),
rand::gen_range(spread_target.y - spread, spread_target.y + spread),
)
.normalize_or_zero();
params.speed = rand::gen_range(
params.speed * Self::SPEED_VARIANCE_MIN,
params.speed * Self::SPEED_VARIANCE_MAX,
)
.clamp(Self::MIN_PROJECTILE_SPEED, Self::MAX_PROJECTILE_SPEED);
self.active
.push(Projectile::new(actor_id, actor, factions, params));
}
}
impl Node for Projectiles {
fn fixed_update(mut node: RefMut<Self>) {
for projectile in &mut node.active {
let distance = (projectile.direction * projectile.speed) * 50.0 * get_frame_time();
projectile.position += distance;
projectile.distance_traveled += distance.length();
}
node.active.retain(|projectile| {
if projectile.distance_traveled > projectile.range {
return false;
}
let collider =
Collider::circle(0.0, 0.0, projectile.size / 2.0).with_offset(projectile.position);
'outer: for mut other_actor in scene::find_nodes_by_type::<Actor>() {
if let Some(other_collider) = other_actor.body.get_offset_collider() {
if collider.overlaps(other_collider) {
let mut is_hit = false;
for effect in &projectile.effects {
if other_actor.apply_effect(
&projectile.actor_id,
projectile.actor,
&projectile.factions,
effect.clone(),
) {
is_hit = true;
} else {
continue 'outer;
}
}
if is_hit {
if let Some(sound_effect) = projectile.on_hit_sound_effect {
play_sound(sound_effect, false);
}
return false;
}
}
}
}
let map = storage::get::<Map>();
for (_, kind) in map.get_collisions(collider) {
if kind == CollisionKind::Solid {
if let Some(sound_effect) = projectile.on_hit_sound_effect {
play_sound(sound_effect, false);
}
return false;
}
}
true
});
}
fn draw(mut node: RefMut<Self>) {
let viewport = storage::get::<Viewport>();
let frustum = viewport.get_frustum();
for projectile in &mut node.active {
if frustum.contains(projectile.position) {
match projectile.kind {
ProjectileKind::Bullet => {
let mut begin = projectile.position.sub(projectile.direction.mul(
projectile.size
* rand::gen_range(
Self::PROJECTILE_LENGTH_FACTOR_MIN,
Self::PROJECTILE_LENGTH_FACTOR_MAX,
),
));
if begin.distance(projectile.position) > projectile.distance_traveled {
begin = projectile.origin;
}
draw_line(
begin.x,
begin.y,
projectile.position.x,
projectile.position.y,
projectile.size,
projectile.color,
);
}
ProjectileKind::Beam => {
let mut begin = projectile.position.sub(projectile.direction.mul(
projectile.size
* rand::gen_range(
Self::BEAM_LENGTH_FACTOR_MIN,
Self::BEAM_LENGTH_FACTOR_MAX,
),
));
if begin.distance(projectile.position) > projectile.distance_traveled {
begin = projectile.origin;
}
if projectile.size > 2.0 {
draw_circle(begin.x, begin.y, projectile.size / 2.0, projectile.color);
draw_circle(
projectile.position.x,
projectile.position.y,
projectile.size / 2.0,
projectile.color,
);
}
draw_line(
begin.x,
begin.y,
projectile.position.x,
projectile.position.y,
projectile.size,
projectile.color,
);
}
}
}
}
}
}