summaryrefslogtreecommitdiff
path: root/src/controllable/player.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/controllable/player.rs')
-rw-r--r--src/controllable/player.rs96
1 files changed, 92 insertions, 4 deletions
diff --git a/src/controllable/player.rs b/src/controllable/player.rs
index a87ef9d..fdc110e 100644
--- a/src/controllable/player.rs
+++ b/src/controllable/player.rs
@@ -1,10 +1,98 @@
-use bevy::{ecs::component::Component, text::Text2d};
+use avian2d::prelude::*;
+use bevy::prelude::*;
+
+use crate::lunar::DestPlanet;
#[derive(Component)]
pub struct Player {
- // pub display_name: Text2d,
+ pub velocity: f32,
+ pub display_name: Text2d,
pub mass_kg: f32,
}
-pub struct ControlledObject {
+pub struct ControlledObject {}
+enum Direction {
+ Up,
+ Down,
+ Left,
+ Right,
+ None,
+}
+fn map_key_to_movement(key: &KeyCode) -> Direction {
+ match key {
+ KeyCode::KeyW => Direction::Up,
+ KeyCode::KeyS => Direction::Down,
+ KeyCode::KeyA => Direction::Left,
+ KeyCode::KeyD => Direction::Right,
+ _ => Direction::None,
+ }
+}
+pub fn check_transition_level(
+ mut commands: Commands,
+ player_transform: Query<&Transform, (With<Player>, Without<DestPlanet>)>,
+ moon_transform: Query<(Entity, &DestPlanet, &Transform), (With<DestPlanet>, Without<Player>)>
+) {
+ let (moon_e, moon, moon_t) = moon_transform.single();
+ let player_t = player_transform.single();
+ let shroud_padding = 1. + moon.radius;
+ let bounds = (
+ Vec2 {
+ x: moon_t.translation.x - shroud_padding,
+ y: moon_t.translation.y - shroud_padding,
+ },
+ Vec2 {
+ x: moon_t.translation.x + shroud_padding,
+ y: moon_t.translation.y + shroud_padding,
+ }
+ );
+ if player_t.translation.x > bounds.0.x &&
+ player_t.translation.x < bounds.1.x &&
+ player_t.translation.y > bounds.0.y &&
+ player_t.translation.y < bounds.1.y {
+ info!("CAN TRANSITION TO NEXT LEVEL");
+ commands.entity(moon_e).despawn();
+ }
+}
+pub fn camera_follow_player(
+ mut camera_transform: Query<&mut Transform, (With<Camera2d>, Without<Player>)>,
+ player_transform: Query<&Transform, (With<Player>, Without<Camera>)>,
+) {
+ let mut camera_t = camera_transform.single_mut();
+ let player_t = player_transform.single();
+ camera_t.translation = player_t.translation;
+}
+pub fn player_movements(
+ time: Res<Time>,
+ input: Res<ButtonInput<KeyCode>>,
+ mut ps: Query<(&Player, &Sprite, &mut ExternalForce), (With<Player>)>,
+) {
+ let movements: Vec<Direction> = input.get_pressed().map(map_key_to_movement).collect();
+ for (p, s, mut propulsion) in &mut ps {
+ let mut pvel_vec = Vec2 { x: 0., y: 0. };
+ let pv = p.velocity;
+ movements.iter().for_each(|m| match m {
+ Direction::Up => pvel_vec.y = pv,
+ Direction::Down => pvel_vec.y = -pv,
+ Direction::Left => pvel_vec.x = -pv,
+ Direction::Right => pvel_vec.x = pv,
+ Direction::None => pvel_vec = Vec2 { x: 0., y: 0. },
+ });
+ let pvv = pvel_vec.normalize_or_zero();
+ propulsion.apply_force(pvv * pv);
+ }
+}
+pub fn setup_player_with_controls(mut commands: Commands) {
+ commands.spawn((
+ Player {
+ velocity: 20000.0,
+ display_name: Text2d("Main player".to_string()),
+ mass_kg: 100.,
+ },
+ RigidBody::Dynamic,
+ Transform::from_xyz(0., 70., 0.),
+ ExternalForce::ZERO.with_persistence(false),
+ Sprite::from_color(Color::BLACK, Vec2 { x: 10., y: 10. }),
+ Collider::rectangle(10., 10.),
+
+ ));
+}
-} \ No newline at end of file