main.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. use rltk::{Console, GameState, Rltk, RGB, VirtualKeyCode};
  2. use specs::prelude::*;
  3. #[macro_use]
  4. extern crate specs_derive;
  5. rltk::add_wasm_support!();
  6. #[derive(Component)]
  7. struct Position {
  8. x: i32,
  9. y: i32,
  10. }
  11. #[derive(Component)]
  12. struct Renderable {
  13. glyph: u8,
  14. fg: RGB,
  15. bg: RGB,
  16. }
  17. #[derive(Component)]
  18. struct LeftMover {}
  19. #[derive(Component, Debug)]
  20. struct Player {}
  21. struct State {
  22. ecs: World
  23. }
  24. fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
  25. let mut positions = ecs.write_storage::<Position>();
  26. let mut players = ecs.write_storage::<Player>();
  27. for (_player, pos) in (&mut players, &mut positions).join() {
  28. pos.x += delta_x;
  29. pos.y += delta_y;
  30. if pos.x < 0 { pos.x = 0; }
  31. if pos.x > 79 { pos.y = 79; }
  32. if pos.y < 0 { pos.y = 0; }
  33. if pos.y > 49 { pos.y = 49; }
  34. }
  35. }
  36. fn player_input(gs: &mut State, ctx: &mut Rltk) {
  37. // Player movement
  38. match ctx.key {
  39. None => {} // Nothing happened
  40. Some(key) => match key {
  41. VirtualKeyCode::Left => try_move_player(-1, 0, &mut gs.ecs),
  42. VirtualKeyCode::Right => try_move_player(1, 0, &mut gs.ecs),
  43. VirtualKeyCode::Up => try_move_player(0, -1, &mut gs.ecs),
  44. VirtualKeyCode::Down => try_move_player(0, 1, &mut gs.ecs),
  45. _ => {}
  46. },
  47. }
  48. }
  49. impl GameState for State {
  50. fn tick(&mut self, ctx : &mut Rltk) {
  51. ctx.cls();
  52. player_input(self, ctx);
  53. self.run_systems();
  54. let positions = self.ecs.read_storage::<Position>();
  55. let renderables = self.ecs.read_storage::<Renderable>();
  56. for (pos, render) in (&positions, &renderables).join() {
  57. ctx.set(pos.x, pos.y, render.fg, render.bg, render.glyph);
  58. }
  59. }
  60. }
  61. struct LeftWalker {}
  62. impl<'a> System<'a> for LeftWalker {
  63. type SystemData = (ReadStorage<'a, LeftMover>,
  64. WriteStorage<'a, Position>);
  65. fn run(&mut self, (lefty, mut pos) : Self::SystemData) {
  66. for (_lefty,pos) in (&lefty, &mut pos).join() {
  67. pos.x -= 1;
  68. if pos.x < 0 { pos.x = 79; }
  69. }
  70. }
  71. }
  72. impl State {
  73. fn run_systems(&mut self) {
  74. let mut lw = LeftWalker{};
  75. lw.run_now(&self.ecs);
  76. self.ecs.maintain();
  77. }
  78. }
  79. fn main() {
  80. let context = Rltk::init_simple8x8(80, 50, "Hello Rust World", "resources");
  81. let mut gs = State {
  82. ecs: World::new()
  83. };
  84. gs.ecs.register::<Position>();
  85. gs.ecs.register::<Renderable>();
  86. gs.ecs.register::<LeftMover>();
  87. gs.ecs.register::<Player>();
  88. gs.ecs
  89. .create_entity()
  90. .with(Position { x: 40, y: 25 })
  91. .with(Renderable {
  92. glyph: rltk::to_cp437('@'),
  93. fg: RGB::named(rltk::YELLOW),
  94. bg: RGB::named(rltk::BLACK),
  95. })
  96. .with(Player{})
  97. .build();
  98. for i in 0..10 {
  99. gs.ecs
  100. .create_entity()
  101. .with(Position { x: i * 7, y: 20 })
  102. .with(Renderable {
  103. glyph: rltk::to_cp437('☺'),
  104. fg: RGB::named(rltk::RED),
  105. bg: RGB::named(rltk::BLACK),
  106. })
  107. .with(LeftMover{})
  108. .build();
  109. }
  110. rltk::main_loop(context, gs);
  111. }