Wind!
This commit is contained in:
62
src/main.rs
62
src/main.rs
@@ -24,10 +24,20 @@ struct SailboatState {
|
|||||||
rudder_dir: f32,
|
rudder_dir: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Resource)]
|
||||||
|
struct WindConditions {
|
||||||
|
direction: f32,
|
||||||
|
strength: f32,
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut app = App::new();
|
let mut app = App::new();
|
||||||
|
|
||||||
app.add_plugins((DefaultPlugins, Wireframe2dPlugin::default()))
|
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(Startup, (hello_world, camera_setup, sprite_setup).chain())
|
||||||
.add_systems(Update, (handle_keys, player_physics).chain());
|
.add_systems(Update, (handle_keys, player_physics).chain());
|
||||||
|
|
||||||
@@ -107,11 +117,47 @@ fn player_physics(
|
|||||||
&mut EntityPhysics,
|
&mut EntityPhysics,
|
||||||
&mut Transform,
|
&mut Transform,
|
||||||
)>,
|
)>,
|
||||||
|
wind_conditions: Res<WindConditions>,
|
||||||
) {
|
) {
|
||||||
let Ok((input, mut boat_state, mut physics, mut transform)) = q_player.single_mut() else {
|
let Ok((input, mut boat_state, mut physics, mut transform)) = q_player.single_mut() else {
|
||||||
return;
|
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. {
|
if input.x < 0. {
|
||||||
boat_state.rudder_dir = (-1.0 + boat_state.rudder_dir) * 0.5;
|
boat_state.rudder_dir = (-1.0 + boat_state.rudder_dir) * 0.5;
|
||||||
} else if input.x > 0. {
|
} 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.turn_speed = physics.turn_speed.clamp(-MAX_TURN_SPEED, MAX_TURN_SPEED);
|
||||||
physics.heading += physics.turn_speed * 0.1;
|
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 x_speed = physics.heading.sin() * physics.speed;
|
||||||
let y_speed = physics.heading.cos() * physics.speed;
|
let y_speed = physics.heading.cos() * physics.speed;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user