keys or something idk
This commit is contained in:
parent
3beca27b67
commit
7e2bac57cd
@ -1,11 +1,13 @@
|
|||||||
use std::any::{Any, TypeId};
|
use std::any::{Any, TypeId};
|
||||||
|
use std::collections::HashSet;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
pub use sdl2::keyboard::Keycode;
|
||||||
|
|
||||||
use sdl2::{
|
use sdl2::{
|
||||||
event::Event,
|
event::Event,
|
||||||
image::{self, LoadTexture, Sdl2ImageContext},
|
image::{self, LoadTexture, Sdl2ImageContext},
|
||||||
keyboard::Keycode,
|
|
||||||
pixels::Color,
|
pixels::Color,
|
||||||
rect::Rect,
|
rect::Rect,
|
||||||
render::{Canvas, Texture, TextureCreator},
|
render::{Canvas, Texture, TextureCreator},
|
||||||
@ -64,6 +66,7 @@ where
|
|||||||
entities: &'context mut Vec<Entity>,
|
entities: &'context mut Vec<Entity>,
|
||||||
systems: &'context mut Vec<Rc<dyn System>>,
|
systems: &'context mut Vec<Rc<dyn System>>,
|
||||||
textures: &'context mut Vec<(Id, Texture<'game>)>,
|
textures: &'context mut Vec<(Id, Texture<'game>)>,
|
||||||
|
currently_pressed_keys: &'context HashSet<Keycode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'context, 'game> Context<'context, 'game> {
|
impl<'context, 'game> Context<'context, 'game> {
|
||||||
@ -148,6 +151,10 @@ impl<'context, 'game> Context<'context, 'game> {
|
|||||||
system.on_add(self);
|
system.on_add(self);
|
||||||
self.systems.push(system)
|
self.systems.push(system)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn key_pressed(&self, keycode: Keycode) -> bool {
|
||||||
|
self.currently_pressed_keys.contains(&keycode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait System {
|
pub trait System {
|
||||||
@ -169,6 +176,7 @@ pub struct Game<'a> {
|
|||||||
components: Vec<(u64, Box<dyn Component>)>,
|
components: Vec<(u64, Box<dyn Component>)>,
|
||||||
systems: Vec<Rc<dyn System>>,
|
systems: Vec<Rc<dyn System>>,
|
||||||
textures: Vec<(Id, Texture<'a>)>,
|
textures: Vec<(Id, Texture<'a>)>,
|
||||||
|
currently_pressed_keys: HashSet<Keycode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'game> Game<'game> {
|
impl<'game> Game<'game> {
|
||||||
@ -200,6 +208,7 @@ impl<'game> Game<'game> {
|
|||||||
components: vec![],
|
components: vec![],
|
||||||
systems: vec![],
|
systems: vec![],
|
||||||
textures: vec![],
|
textures: vec![],
|
||||||
|
currently_pressed_keys: HashSet::new(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -212,6 +221,12 @@ impl<'game> Game<'game> {
|
|||||||
keycode: Some(Keycode::Escape),
|
keycode: Some(Keycode::Escape),
|
||||||
..
|
..
|
||||||
} => break 'running,
|
} => break 'running,
|
||||||
|
Event::KeyDown { keycode, .. } => {
|
||||||
|
self.currently_pressed_keys.insert(keycode.unwrap());
|
||||||
|
}
|
||||||
|
Event::KeyUp { keycode, .. } => {
|
||||||
|
self.currently_pressed_keys.remove(&keycode.unwrap());
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
18
src/main.rs
18
src/main.rs
@ -10,7 +10,7 @@ use engine::Component;
|
|||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
struct Player {
|
struct Player {
|
||||||
sprite: Option<Sprite>,
|
sprite: Sprite,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
@ -36,13 +36,18 @@ impl System for PlayerRenderer {
|
|||||||
let &mut Position(x, y) = ctx.entity_component::<Position>(id);
|
let &mut Position(x, y) = ctx.entity_component::<Position>(id);
|
||||||
let player = ctx.entity_component::<Player>(id);
|
let player = ctx.entity_component::<Player>(id);
|
||||||
|
|
||||||
let sprite = player.sprite.unwrap();
|
ctx.draw_sprite(&player.sprite, x, y)?;
|
||||||
ctx.draw_sprite(&sprite, x, y)?;
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct PlayerMovement;
|
||||||
|
|
||||||
|
impl System for PlayerMovement {
|
||||||
|
fn on_update(&self, _ctx: &mut engine::Context) -> Result<(), engine::Error> {}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut game = engine::Game::new().unwrap();
|
let mut game = engine::Game::new().unwrap();
|
||||||
|
|
||||||
@ -50,11 +55,6 @@ fn main() {
|
|||||||
context.add_system(Rc::new(PlayerRenderer));
|
context.add_system(Rc::new(PlayerRenderer));
|
||||||
context.add_system(Rc::new(Gravity));
|
context.add_system(Rc::new(Gravity));
|
||||||
let sprite = context.load_sprite("textures/player.png").unwrap();
|
let sprite = context.load_sprite("textures/player.png").unwrap();
|
||||||
context.spawn(vec![
|
context.spawn(vec![Box::new(Player { sprite }), Box::new(Position(16, 0))]);
|
||||||
Box::new(Player {
|
|
||||||
sprite: Some(sprite),
|
|
||||||
}),
|
|
||||||
Box::new(Position(16, 0)),
|
|
||||||
]);
|
|
||||||
game.run();
|
game.run();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user