-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplosion.rs
117 lines (105 loc) · 3.46 KB
/
explosion.rs
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
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;
use crate::{util::{get_time_millis, random_vec3, random_u64, random_f32}, player::Player, definitions::*};
#[allow(dead_code)]
pub enum ExplosionType {
SMALL,
MEDIUM,
LARGE,
HUGE,
}
#[derive(Component)]
pub struct ExplosionEffect {
pub start_time: u64,
pub life_time: u64,
}
pub fn spawn_explosion(
commands: &mut Commands,
meshes: &mut ResMut<Assets<Mesh>>,
materials: &mut ResMut<Assets<StandardMaterial>>,
explosion_type: ExplosionType,
position: &Vec3,
) {
match explosion_type {
ExplosionType::SMALL => {
for _ in 0..20 {
spawn_explosion_giblet(commands, meshes, materials, position, random_f32(0.05, 0.1), random_u64(2000, 5000));
}
},
ExplosionType::MEDIUM => {
},
ExplosionType::LARGE => {
},
ExplosionType::HUGE => {
},
};
}
fn spawn_explosion_giblet(
commands: &mut Commands,
meshes: &mut ResMut<Assets<Mesh>>,
materials: &mut ResMut<Assets<StandardMaterial>>,
position: &Vec3,
size: f32,
life_time: u64,
) {
let explosion_handle = meshes.add(Cuboid::new(size, size, size));
commands
.spawn(PbrBundle {
mesh: explosion_handle,
material: materials.add(StandardMaterial {
base_color: Color::srgb(1.0, 1.0, 0.2),
emissive: Color::srgb(1.0, 1.0, 0.2).into(),
..default()
}),
..Default::default()
})
.insert(ExplosionEffect {
start_time: get_time_millis(),
life_time: life_time,
})
.insert(Collider::cuboid(size, size, size))
.insert(CollisionGroups::new(Group::from_bits_truncate(COLLISION_MASK_EFFECT),
Group::from_bits_truncate(
COLLISION_MASK_TERRAIN
)))
.insert(RigidBody::Dynamic)
.insert(Velocity{linvel: random_vec3(10.0), angvel: random_vec3(10.0)})
.insert(ColliderMassProperties::Density(100.0))
.insert(PointLightBundle {
point_light: PointLight {
color: Color::srgb(1.0, 1.0, 0.3),
intensity: 100.,
range: 10.,
shadows_enabled: false,
..default()
},
..default()
})
.insert(Transform::from_xyz(position.x, position.y, position.z));
}
pub fn handle_explosion_test(
mut commands: Commands,
player: Query<(Entity, &Transform), With<Player>>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
input: Res<ButtonInput<KeyCode>>,
) {
if input.just_pressed(KeyCode::KeyO) {
for p in player.iter() {
let position = p.1.translation;
spawn_explosion(&mut commands, &mut meshes, &mut materials, ExplosionType::SMALL, &position);
}
}
}
pub fn update_explosion_effects(
mut commands: Commands,
mut explosion_effects: Query<(Entity, &ExplosionEffect, &mut PointLight)>,
) {
let time = get_time_millis();
for (entity, explosion_effect, mut point_light) in explosion_effects.iter_mut() {
point_light.intensity = 100.0 * (1.0 - (time - explosion_effect.start_time) as f32 / explosion_effect.life_time as f32);
if time - explosion_effect.start_time > explosion_effect.life_time {
commands.entity(entity).despawn_recursive();
}
}
}