Skip to content

Commit 7fb9adc

Browse files
committed
fix(physics): use proper settings for projectile physics
1 parent c2c3b2a commit 7fb9adc

File tree

5 files changed

+19
-24
lines changed

5 files changed

+19
-24
lines changed

assets/unit/enemy-spear.ron

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
// Size of the healthbar
2323
healthbar_size: (20, 3),
2424
// Where above the unit it floats
25-
healthbar_offset: (-10, -25),
25+
healthbar_offset: (-1, -8),
2626

2727
// physics: (
2828
// // Handle all collisions manually

assets/unit/spear.ron

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,14 @@
1313
// Interval in seconds for when a new projectile is thrown
1414
projectile_spawn_interval: 5,
1515
// Don't let the projectile spawn in the ground
16-
projectile_spawn_offset: (0, -40),
16+
projectile_spawn_offset: (0, -20),
1717
// How fast a projectile is thrown
18-
projectile_velocity: Range( min: 5, max: 10 ),
18+
projectile_velocity: Range(min: 50, max: 100),
1919
// How long the hands are hidden after launching a projectile in seconds
2020
hide_hands_delay: 2,
2121

2222
// Size of the healthbar
2323
healthbar_size: (20, 3),
2424
// Where above the unit it floats
25-
healthbar_offset: (-10, -25),
26-
27-
// physics: (
28-
// // Handle all collisions manually
29-
// is_kinematic: true,
30-
// ),
31-
32-
// // Collider shape for collisions with projectile
33-
// collider: (
34-
// shape: "rectangle",
35-
// width: 10,
36-
// height: 30,
37-
// ),
25+
healthbar_offset: (-3, -8),
3826
)

src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use terrain::Terrain;
1616
use unit::{Unit, UnitType};
1717
use utils::timer::Timer;
1818

19-
const WIDTH: f32 = 320.0;
20-
const HEIGHT: f32 = 240.0;
19+
const WIDTH: f32 = 640.0;
20+
const HEIGHT: f32 = 480.0;
2121

2222
/// Game state.
2323
pub enum GameState {
@@ -96,7 +96,7 @@ impl Game for GameState {
9696
terrain.draw(ctx.clone(), *camera);
9797

9898
// Update all projectiles
99-
projectiles.retain_mut(|projectile| projectile.update(terrain, ctx.clone()));
99+
projectiles.retain_mut(|projectile| !projectile.update(terrain, dt, &settings));
100100

101101
// Update all units
102102
units.iter_mut().for_each(|unit| {
@@ -128,7 +128,7 @@ impl Game for GameState {
128128

129129
fn render(&mut self, ctx: Context) {
130130
// Draw a basic FPS counter
131-
let fps = ctx.delta_time().recip();
131+
let fps = ctx.frames_per_second();
132132
ctx.text("font.torus-sans", &format!("{fps:.1}")).draw();
133133

134134
match self {

src/projectile.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ impl Projectile {
2323
/// Move the projectile.
2424
///
2525
/// Returns whether the projectile should be removed.
26-
pub fn update(&mut self, terrain: &Terrain, ctx: Context) -> bool {
26+
pub fn update(&mut self, terrain: &Terrain, dt: f32, settings: &Settings) -> bool {
2727
puffin::profile_function!();
2828

29-
self.pos += self.vel * ctx.delta_time();
30-
self.vel.y += ctx.asset::<Settings>("settings").projectile_gravity;
29+
self.pos += self.vel * dt;
30+
self.vel.y += settings.projectile_gravity * dt;
3131

3232
terrain.point_collides(self.pos)
3333
}

src/unit.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,20 @@ impl Unit {
140140
pub fn render(&self, camera: Camera, ctx: Context) {
141141
puffin::profile_scope!("Unit render");
142142

143+
let settings = self.settings(ctx.clone());
144+
143145
let draw_pos = self.pos - self.ground_collision_point() + camera.into();
144146

145147
ctx.sprite(self.r#type.asset_path())
146148
.translate(draw_pos)
147149
.draw();
148150

149-
if let Some(hands_asset_path) = &self.settings(ctx.clone()).hands_asset_path {
151+
ctx.text("font.debug", &format!("{}", self.health))
152+
.translate(draw_pos)
153+
.translate(settings.healthbar_offset)
154+
.draw();
155+
156+
if let Some(hands_asset_path) = &settings.hands_asset_path {
150157
if self.hide_hands_delay <= 0.0 {
151158
ctx.sprite(hands_asset_path)
152159
.translate(draw_pos - Vector2::splat(1.0))

0 commit comments

Comments
 (0)