main.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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, Debug)]
  18. struct Player {}
  19. #[derive(PartialEq, Copy, Clone)]
  20. enum TileType {
  21. Wall, Floor
  22. }
  23. struct State {
  24. ecs: World
  25. }
  26. pub fn xy_idx(x: i32, y: i32) -> usize {
  27. (y as usize * 80) + x as usize
  28. }
  29. fn new_map() -> Vec<TileType> {
  30. let mut map = vec![TileType::Floor; 80*50];
  31. // Make the boundaries walls
  32. for x in 0..80 {
  33. map[xy_idx(x, 0)] = TileType::Wall;
  34. map[xy_idx(x, 49)] = TileType::Wall;
  35. }
  36. for y in 0..50 {
  37. map[xy_idx(0, y)] = TileType::Wall;
  38. map[xy_idx(79, y)] = TileType::Wall;
  39. }
  40. // Now we'll randomly splat a bunch of walls. It won't be pretty, but it's a decent illustration.
  41. // First, obtain the thread-local RNG:
  42. let mut rng = rltk::RandomNumberGenerator::new();
  43. for _i in 0..400 {
  44. let x = rng.roll_dice(1, 79);
  45. let y = rng.roll_dice(1, 49);
  46. let idx = xy_idx(x, y);
  47. if idx != xy_idx(40, 25) {
  48. map[idx] = TileType::Wall;
  49. }
  50. }
  51. map
  52. }
  53. fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
  54. let mut positions = ecs.write_storage::<Position>();
  55. let mut players = ecs.write_storage::<Player>();
  56. let map = ecs.fetch::<Vec<TileType>>();
  57. for (_player, pos) in (&mut players, &mut positions).join() {
  58. let destination_idx = xy_idx(pos.x + delta_x, pos.y + delta_y);
  59. if map[destination_idx] != TileType::Wall {
  60. pos.x += delta_x;
  61. pos.y += delta_y;
  62. if pos.x < 0 { pos.x = 0; }
  63. if pos.x > 79 { pos.y = 79; }
  64. if pos.y < 0 { pos.y = 0; }
  65. if pos.y > 49 { pos.y = 49; }
  66. }
  67. }
  68. }
  69. fn player_input(gs: &mut State, ctx: &mut Rltk) {
  70. // Player movement
  71. match ctx.key {
  72. None => {} // Nothing happened
  73. Some(key) => match key {
  74. VirtualKeyCode::Left => try_move_player(-1, 0, &mut gs.ecs),
  75. VirtualKeyCode::Right => try_move_player(1, 0, &mut gs.ecs),
  76. VirtualKeyCode::Up => try_move_player(0, -1, &mut gs.ecs),
  77. VirtualKeyCode::Down => try_move_player(0, 1, &mut gs.ecs),
  78. _ => {}
  79. },
  80. }
  81. }
  82. fn draw_map(map: &[TileType], ctx : &mut Rltk) {
  83. let mut y = 0;
  84. let mut x = 0;
  85. for tile in map.iter() {
  86. // Render a tile depending upon the tile type
  87. match tile {
  88. TileType::Floor => {
  89. ctx.set(x, y, RGB::from_f32(0.5, 0.5, 0.5), RGB::from_f32(0., 0., 0.), rltk::to_cp437('.'));
  90. }
  91. TileType::Wall => {
  92. ctx.set(x, y, RGB::from_f32(0.0, 1.0, 0.0), RGB::from_f32(0., 0., 0.), rltk::to_cp437('#'));
  93. }
  94. }
  95. // Move the coordinates
  96. x += 1;
  97. if x > 79 {
  98. x = 0;
  99. y += 1;
  100. }
  101. }
  102. }
  103. impl GameState for State {
  104. fn tick(&mut self, ctx : &mut Rltk) {
  105. ctx.cls();
  106. player_input(self, ctx);
  107. self.run_systems();
  108. let map = self.ecs.fetch::<Vec<TileType>>();
  109. draw_map(&map, ctx);
  110. let positions = self.ecs.read_storage::<Position>();
  111. let renderables = self.ecs.read_storage::<Renderable>();
  112. for (pos, render) in (&positions, &renderables).join() {
  113. ctx.set(pos.x, pos.y, render.fg, render.bg, render.glyph);
  114. }
  115. }
  116. }
  117. impl State {
  118. fn run_systems(&mut self) {
  119. self.ecs.maintain();
  120. }
  121. }
  122. fn main() {
  123. let context = Rltk::init_simple8x8(80, 50, "Hello Rust World", "resources");
  124. let mut gs = State {
  125. ecs: World::new()
  126. };
  127. gs.ecs.register::<Position>();
  128. gs.ecs.register::<Renderable>();
  129. gs.ecs.register::<Player>();
  130. gs.ecs.insert(new_map());
  131. gs.ecs
  132. .create_entity()
  133. .with(Position { x: 40, y: 25 })
  134. .with(Renderable {
  135. glyph: rltk::to_cp437('@'),
  136. fg: RGB::named(rltk::YELLOW),
  137. bg: RGB::named(rltk::BLACK),
  138. })
  139. .with(Player{})
  140. .build();
  141. rltk::main_loop(context, gs);
  142. }