From 8a3b485ecead87b7c2c1233c30b3b14f9e3e3f83 Mon Sep 17 00:00:00 2001 From: JP Stringham Date: Fri, 28 Nov 2025 16:42:22 -0500 Subject: [PATCH] Wind! --- src/main.rs | 62 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index 387ab6c..9437047 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,10 +24,20 @@ struct SailboatState { rudder_dir: f32, } +#[derive(Default, Resource)] +struct WindConditions { + direction: f32, + strength: f32, +} + fn main() { let mut app = App::new(); app.add_plugins((DefaultPlugins, Wireframe2dPlugin::default())) + .insert_resource(WindConditions { + strength: 1.0, + ..default() + }) .add_systems(Startup, (hello_world, camera_setup, sprite_setup).chain()) .add_systems(Update, (handle_keys, player_physics).chain()); @@ -107,11 +117,47 @@ fn player_physics( &mut EntityPhysics, &mut Transform, )>, + wind_conditions: Res, ) { let Ok((input, mut boat_state, mut physics, mut transform)) = q_player.single_mut() else { return; }; + if input.y > 0. { + boat_state.sail_extension = (1.0 + boat_state.sail_extension) * 0.5; + } else if input.y < 0. { + boat_state.sail_extension *= 0.5; + } + + boat_state.sail_extension = boat_state.sail_extension.clamp(0., 1.); + + physics.speed *= 0.99; + + let sail_speed_x = physics.heading.sin(); + let sail_speed_y = physics.heading.cos(); + + let wind_speed_x = wind_conditions.direction.sin(); + let wind_speed_y = wind_conditions.direction.cos(); + + let sail_speed_vec = Vec2 { + x: sail_speed_x, + y: sail_speed_y, + }; + let wind_speed_vec = Vec2 { + x: wind_speed_x, + y: wind_speed_y, + }; + + // technically you could put your sail out and try to go backwards but.. + // let's not tackle that (Yet?) + let sail_strength = sail_speed_vec.dot(wind_speed_vec).clamp(0., 1.); + + if boat_state.sail_extension > 0. { + physics.speed += 0.01 * boat_state.sail_extension * sail_strength; + } + + physics.speed = physics.speed.clamp(-MAX_SAILING_SPEED, MAX_SAILING_SPEED); + if input.x < 0. { boat_state.rudder_dir = (-1.0 + boat_state.rudder_dir) * 0.5; } else if input.x > 0. { @@ -127,22 +173,6 @@ fn player_physics( physics.turn_speed = physics.turn_speed.clamp(-MAX_TURN_SPEED, MAX_TURN_SPEED); physics.heading += physics.turn_speed * 0.1; - if input.y > 0. { - boat_state.sail_extension = (1.0 + boat_state.sail_extension) * 0.5; - } else if input.y < 0. { - boat_state.sail_extension *= 0.5; - } - - boat_state.sail_extension = boat_state.sail_extension.clamp(0., 1.); - - physics.speed *= 0.99; - - if boat_state.sail_extension > 0. { - physics.speed += 0.01 * boat_state.sail_extension; - } - - physics.speed = physics.speed.clamp(-MAX_SAILING_SPEED, MAX_SAILING_SPEED); - let x_speed = physics.heading.sin() * physics.speed; let y_speed = physics.heading.cos() * physics.speed;