Commit 32ce6166 authored by Rahix's avatar Rahix 🦀
Browse files

player: Add controllable player movement

Control the player with WASD like a pro gamer.
parent 12b78a6a
mod edge;
mod movable;
mod node;
mod orbitbody;
mod player;
......@@ -6,6 +7,7 @@ mod position;
mod thesun;
pub use edge::Edge;
pub use movable::Movable;
pub use node::Node;
pub use orbitbody::{Gravity, OrbitBody};
pub use player::Player;
......
pub struct Movable {
pub velocity: nalgebra::Vector2<f32>,
}
impl Movable {
pub fn new() -> Movable {
Movable {
velocity: nalgebra::Vector2::new(0.0, 0.0),
}
}
}
use crate::components;
pub fn create_player(world: &mut legion::World) -> legion::Entity {
world.push((components::Player, components::Position::new(600.0, 600.0)))
world.push((
components::Player,
components::Position::new(600.0, 600.0),
components::Movable::new(),
))
}
......@@ -15,8 +15,8 @@ impl Clock {
Clock { perf, start }
}
pub fn game_time(&self) -> f64 {
(self.perf.now() - self.start) / 1000.0
pub fn frame_delta(&self) -> f32 {
0.1
}
pub fn wall_time(&self) -> f64 {
......
use crate::colliders;
use crate::colors;
use crate::components;
use crate::entities;
use crate::gamestate;
use crate::resources;
......@@ -38,6 +39,8 @@ impl InGameState {
.flush()
.add_system(colliders::update_collision_world_system())
.flush()
.add_thread_local(systems::move_movable_system())
.flush()
.add_thread_local(systems::move_camera_to_player_system())
.flush()
.add_thread_local(systems::camera_system())
......@@ -79,12 +82,21 @@ impl gamestate::State for InGameState {
}
fn event(&mut self, event: gamestate::Event) -> gamestate::Transition {
let mut cam = self.resources.get_mut::<resources::Camera>().unwrap();
use legion::IntoQuery;
let player = self.resources.get::<resources::Player>().unwrap().0.clone();
let player_movable = <&mut components::Movable>::query()
.get_mut(&mut self.world, player)
.unwrap();
match event {
gamestate::Event::KeyDown("w") => cam.position.y -= 50.0,
gamestate::Event::KeyDown("a") => cam.position.x -= 50.0,
gamestate::Event::KeyDown("s") => cam.position.y += 50.0,
gamestate::Event::KeyDown("d") => cam.position.x += 50.0,
gamestate::Event::KeyDown("w") => player_movable.velocity.y = -30.0,
gamestate::Event::KeyUp("w") => player_movable.velocity.y = 0.0,
gamestate::Event::KeyDown("a") => player_movable.velocity.x = -30.0,
gamestate::Event::KeyUp("a") => player_movable.velocity.x = 0.0,
gamestate::Event::KeyDown("s") => player_movable.velocity.y = 30.0,
gamestate::Event::KeyUp("s") => player_movable.velocity.y = 0.0,
gamestate::Event::KeyDown("d") => player_movable.velocity.x = 30.0,
gamestate::Event::KeyUp("d") => player_movable.velocity.x = 0.0,
_ => (),
}
gamestate::Transition::Keep
......
......@@ -5,5 +5,5 @@ mod tmp_player;
pub use camera::camera_system;
pub use level::draw_level_layer_system;
pub use moving::move_camera_to_player_system;
pub use moving::{move_camera_to_player_system, move_movable_system};
pub use tmp_player::draw_tmp_player_system;
......@@ -13,3 +13,12 @@ pub fn move_camera_to_player(
let pos = positions.get(world, player.0).unwrap().0;
camera.position = nalgebra::Point2::new(pos.x as f64, pos.y as f64);
}
#[legion::system(for_each)]
pub fn move_movable(
position: &mut components::Position,
movable: &components::Movable,
#[resource] clock: &resources::Clock,
) {
position.0 += movable.velocity * clock.frame_delta();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment