|
@@ -21,6 +21,15 @@ pub struct Renderable {
|
|
|
#[derive(Component)]
|
|
|
pub struct MoveLeft;
|
|
|
|
|
|
+#[derive(Component)]
|
|
|
+pub struct Player;
|
|
|
+
|
|
|
+#[derive(Component)]
|
|
|
+pub struct Motion {
|
|
|
+ down: i8,
|
|
|
+ right: i8,
|
|
|
+}
|
|
|
+
|
|
|
system_impl! {
|
|
|
Draw(
|
|
|
resource mut board: carpet::Board<carpet::CP437>,
|
|
@@ -47,6 +56,18 @@ system! {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+system! {
|
|
|
+ Move (
|
|
|
+ mut motion: Motion,
|
|
|
+ mut pos: Pos,
|
|
|
+ ) {
|
|
|
+ pos.x = (pos.x as i8 + motion.right) as usize;
|
|
|
+ pos.y = (pos.y as i8 + motion.down) as usize;
|
|
|
+ } finally {
|
|
|
+ motion.clear();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
fn main() -> Result<(), GameError> {
|
|
|
let mut game = carpet::GameBuilder::new()
|
|
|
.name("game")
|
|
@@ -64,10 +85,13 @@ fn main() -> Result<(), GameError> {
|
|
|
game.register::<Pos>();
|
|
|
game.register::<Renderable>();
|
|
|
game.register::<MoveLeft>();
|
|
|
+ game.register::<Motion>();
|
|
|
+ game.register::<Player>();
|
|
|
game.world.print([1, 1], "Hello, world!");
|
|
|
|
|
|
game.create_entity()
|
|
|
.with(Pos { x: 40, y: 25 })
|
|
|
+ .with(Player)
|
|
|
.with(Renderable {
|
|
|
glyph: carpet::CP437::from_char('A'),
|
|
|
color: carpet::Color::Blue,
|
|
@@ -84,8 +108,30 @@ fn main() -> Result<(), GameError> {
|
|
|
.with(MoveLeft)
|
|
|
.build();
|
|
|
}
|
|
|
+
|
|
|
+ game.on_key((carpet::VirtualKeyCode::W, carpet::KeyMods::NONE), |world| {
|
|
|
+ let player = (&world.read_component::<Player>(), &world.entities()).join().next().unwrap().1;
|
|
|
+ world.write_component::<Motion>().insert(player, Motion { down: -1, right: 0 }).unwrap();
|
|
|
+ });
|
|
|
+
|
|
|
+ game.on_key((carpet::VirtualKeyCode::A, carpet::KeyMods::NONE), |world| {
|
|
|
+ let player = (&world.read_component::<Player>(), &world.entities()).join().next().unwrap().1;
|
|
|
+ world.write_component::<Motion>().insert(player, Motion { down: 0, right: -1 }).unwrap();
|
|
|
+ });
|
|
|
+
|
|
|
+ game.on_key((carpet::VirtualKeyCode::S, carpet::KeyMods::NONE), |world| {
|
|
|
+ let player = (&world.read_component::<Player>(), &world.entities()).join().next().unwrap().1;
|
|
|
+ world.write_component::<Motion>().insert(player, Motion { down: 1, right: 0 }).unwrap();
|
|
|
+ });
|
|
|
+
|
|
|
+ game.on_key((carpet::VirtualKeyCode::D, carpet::KeyMods::NONE), |world| {
|
|
|
+ let player = (&world.read_component::<Player>(), &world.entities()).join().next().unwrap().1;
|
|
|
+ world.write_component::<Motion>().insert(player, Motion { down: 0, right: 1 }).unwrap();
|
|
|
+ });
|
|
|
+
|
|
|
game.run_with_systems(|world| {
|
|
|
Draw.run_now(&world);
|
|
|
Leftward.run_now(&world);
|
|
|
+ Move.run_now(&world);
|
|
|
})
|
|
|
}
|