Skip to content

Commit b2e603a

Browse files
committed
Refactored projectiles
1 parent 61aeef1 commit b2e603a

File tree

15 files changed

+156
-139
lines changed

15 files changed

+156
-139
lines changed

src/command/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,5 @@ mod actor_player_set;
22
mod actor_release;
33
mod actor_set;
44
mod cursor_grab;
5-
mod projectile_spawn;
65

7-
pub use self::{
8-
actor_player_set::*, actor_release::*, actor_set::*, cursor_grab::*, projectile_spawn::*,
9-
};
6+
pub use self::{actor_player_set::*, actor_release::*, actor_set::*, cursor_grab::*};

src/component/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ mod actor;
22
mod collision;
33
mod inertia;
44
mod player;
5-
mod projectile;
65

7-
pub use self::{actor::*, collision::*, inertia::*, player::*, projectile::*};
6+
pub use self::{actor::*, collision::*, inertia::*, player::*};

src/component/projectile.rs

Lines changed: 0 additions & 111 deletions
This file was deleted.

src/main.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ mod system;
1010
mod util;
1111

1212
use crate::{
13-
component::ProjectileMaterial,
1413
data::APP_TITLE,
1514
event::ActorDeathEvent,
1615
model::AppState,
@@ -29,10 +28,9 @@ use bevy::{
2928
log::LogPlugin,
3029
prelude::{App, DefaultPlugins, IntoSystem, IntoSystemConfigs, PluginGroup},
3130
render::texture::ImagePlugin,
32-
sprite::Material2dPlugin,
3331
window::{Window, WindowPlugin, WindowResolution},
3432
};
35-
use plugin::WeaponPlugin;
33+
use plugin::{ProjectilePlugin, WeaponPlugin};
3634

3735
fn main() {
3836
// TODO: init logger earlier
@@ -93,12 +91,12 @@ fn main() {
9391
.add_plugins(HeartbeatPlugin)
9492
.add_plugins(LaserSightPlugin)
9593
.add_plugins(ParticlePlugin)
94+
.add_plugins(ProjectilePlugin)
9695
.add_plugins(StatusBarPlugin)
9796
.add_plugins(TerrainPlugin)
9897
.add_plugins(TileMapPlugin)
9998
.add_plugins(UiNotificationPlugin)
10099
.add_plugins(WeaponPlugin)
101-
.add_plugins(Material2dPlugin::<ProjectileMaterial>::default())
102100
.add_state::<AppState>()
103101
.add_event::<ActorDeathEvent>()
104102
.insert_resource(AssetStorage::default())
@@ -119,8 +117,6 @@ fn main() {
119117
s.add(inertia.after(actor));
120118
s.add(collision_find.pipe(collision_resolve).after(inertia));
121119
s.add(melee.after(collision_resolve));
122-
s.add(projectile.after(collision_resolve));
123-
s.add(projectile_whiz);
124120
s.add(ambience_fx());
125121
s.add(scenario);
126122
})

src/plugin/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod health;
1111
mod heartbeat;
1212
mod laser;
1313
mod particle;
14+
mod projectile;
1415
mod status_bar;
1516
mod terrain;
1617
mod tile_map;
@@ -19,6 +20,6 @@ mod weapon;
1920

2021
pub use self::{
2122
audio::*, blood::*, bonus::*, breath::*, camera_target::*, crosshair::*, debug::*,
22-
footsteps::*, health::*, heartbeat::*, laser::*, particle::*, status_bar::*, terrain::*,
23-
tile_map::*, ui_notification::*, weapon::*,
23+
footsteps::*, health::*, heartbeat::*, laser::*, particle::*, projectile::*, status_bar::*,
24+
terrain::*, tile_map::*, ui_notification::*, weapon::*,
2425
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
2-
component::{Projectile, ProjectileConfig, ProjectileMaterial},
32
data::LAYER_PROJECTILE,
43
model::TransformLite,
4+
plugin::{projectile::material::ProjectileMaterial, Projectile, ProjectileConfig},
55
resource::AssetStorage,
66
util::ext::Vec2Ext,
77
};

src/plugin/projectile/component.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use crate::plugin::ProjectileConfig;
2+
use bevy::{
3+
ecs::{component::Component, entity::Entity},
4+
math::Vec2,
5+
};
6+
use std::time::Duration;
7+
8+
#[derive(Component)]
9+
pub struct Projectile {
10+
pub config: &'static ProjectileConfig,
11+
pub initial_time: Duration,
12+
pub initial_position: Vec2,
13+
pub initial_velocity: Vec2,
14+
pub shooter: Option<Entity>,
15+
pub stopped: bool,
16+
}
17+
18+
impl Projectile {
19+
pub const VELOCITY_MIN: f32 = 5.0;
20+
pub const VELOCITY_VISUAL_FACTOR: f32 = 1.0 / 5.0;
21+
22+
pub const fn new(
23+
config: &'static ProjectileConfig,
24+
time: Duration,
25+
position: Vec2,
26+
velocity: Vec2,
27+
shooter: Option<Entity>,
28+
) -> Self {
29+
return Self {
30+
config,
31+
initial_time: time,
32+
initial_position: position,
33+
initial_velocity: velocity,
34+
shooter,
35+
stopped: false,
36+
};
37+
}
38+
39+
pub fn calc_data(&self, time: Duration) -> (Vec2, Vec2) {
40+
let t = time.saturating_sub(self.initial_time).as_secs_f32();
41+
let a = self.config.acceleration();
42+
let p = self.initial_position;
43+
let v0 = self.initial_velocity;
44+
let v1 = v0 * (t * a).exp();
45+
return (p + (v1 - v0) / a * Projectile::VELOCITY_VISUAL_FACTOR, v1);
46+
}
47+
}

src/plugin/projectile/config.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#[derive(Clone)]
2+
pub struct ProjectileConfig {
3+
pub fragments: u8,
4+
pub mass: f32,
5+
pub size: f32,
6+
}
7+
8+
impl ProjectileConfig {
9+
pub const _9X18: Self = Self {
10+
fragments: 1,
11+
mass: 0.0061,
12+
size: 0.7,
13+
};
14+
15+
pub const _7_62X25: Self = Self {
16+
fragments: 1,
17+
mass: 0.0055,
18+
size: 0.7,
19+
};
20+
21+
pub const _12X76: Self = Self {
22+
fragments: 12,
23+
mass: 0.048,
24+
size: 0.1,
25+
};
26+
27+
pub const _5_45X39: Self = Self {
28+
fragments: 1,
29+
mass: 0.0034,
30+
size: 1.0,
31+
};
32+
33+
pub const _7_62X54: Self = Self {
34+
fragments: 1,
35+
mass: 0.0096,
36+
size: 1.2,
37+
};
38+
39+
pub fn acceleration(&self) -> f32 {
40+
return -1.0 / self.fragment_mass() * 0.006 - 4.2;
41+
}
42+
43+
pub fn fragment_mass(&self) -> f32 {
44+
return self.mass / f32::from(self.fragments);
45+
}
46+
}

src/plugin/projectile/material.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use bevy::{
2+
asset::Asset,
3+
prelude::{Handle, Image},
4+
reflect::TypePath,
5+
render::render_resource::{AsBindGroup, ShaderRef},
6+
sprite::Material2d,
7+
};
8+
9+
#[derive(Debug, Clone, Asset, TypePath, AsBindGroup)]
10+
pub struct ProjectileMaterial {
11+
#[texture(0)]
12+
#[sampler(1)]
13+
pub image: Handle<Image>,
14+
}
15+
16+
impl Material2d for ProjectileMaterial {
17+
fn fragment_shader() -> ShaderRef {
18+
return "shader/projectile.wgsl".into();
19+
}
20+
}

src/plugin/projectile/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
mod command;
2+
mod component;
3+
mod config;
4+
mod material;
5+
mod sys_update;
6+
mod sys_whiz;
7+
8+
use self::material::ProjectileMaterial;
9+
pub use self::{command::*, component::*, config::*};
10+
use crate::{model::AppState, system::game::collision_resolve, util::ext::AppExt};
11+
use bevy::{
12+
prelude::{App, IntoSystemConfigs, Plugin},
13+
sprite::Material2dPlugin,
14+
};
15+
16+
pub struct ProjectilePlugin;
17+
18+
impl Plugin for ProjectilePlugin {
19+
fn build(&self, app: &mut App) {
20+
app.add_plugins(Material2dPlugin::<ProjectileMaterial>::default());
21+
app.add_state_system(
22+
AppState::Game,
23+
sys_update::on_update.after(collision_resolve),
24+
);
25+
app.add_state_system(AppState::Game, sys_whiz::on_update);
26+
}
27+
}

0 commit comments

Comments
 (0)