main.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 = min(79 , max(0, pos.x + delta_x));
  61. pos.y = min(49, max(0, pos.y + delta_y));
  62. }
  63. }
  64. }
  65. fn player_input(gs: &mut State, ctx: &mut Rltk) {
  66. // Player movement
  67. match ctx.key {
  68. None => {} // Nothing happened
  69. Some(key) => match key {
  70. VirtualKeyCode::Left => try_move_player(-1, 0, &mut gs.ecs),
  71. VirtualKeyCode::Right => try_move_player(1, 0, &mut gs.ecs),
  72. VirtualKeyCode::Up => try_move_player(0, -1, &mut gs.ecs),
  73. VirtualKeyCode::Down => try_move_player(0, 1, &mut gs.ecs),
  74. _ => {}
  75. },
  76. }
  77. }
  78. fn draw_map(map: &[TileType], ctx : &mut Rltk) {
  79. let mut y = 0;
  80. let mut x = 0;
  81. for tile in map.iter() {
  82. // Render a tile depending upon the tile type
  83. match tile {
  84. TileType::Floor => {
  85. ctx.set(x, y, RGB::from_f32(0.5, 0.5, 0.5), RGB::from_f32(0., 0., 0.), rltk::to_cp437('.'));
  86. }
  87. TileType::Wall => {
  88. ctx.set(x, y, RGB::from_f32(0.0, 1.0, 0.0), RGB::from_f32(0., 0., 0.), rltk::to_cp437('#'));
  89. }
  90. }
  91. // Move the coordinates
  92. x += 1;
  93. if x > 79 {
  94. x = 0;
  95. y += 1;
  96. }
  97. }
  98. }
  99. impl GameState for State {
  100. fn tick(&mut self, ctx : &mut Rltk) {
  101. ctx.cls();
  102. player_input(self, ctx);
  103. self.run_systems();
  104. let map = self.ecs.fetch::<Vec<TileType>>();
  105. draw_map(&map, ctx);
  106. let positions = self.ecs.read_storage::<Position>();
  107. let renderables = self.ecs.read_storage::<Renderable>();
  108. for (pos, render) in (&positions, &renderables).join() {
  109. ctx.set(pos.x, pos.y, render.fg, render.bg, render.glyph);
  110. }
  111. }
  112. }
  113. impl State {
  114. fn run_systems(&mut self) {
  115. self.ecs.maintain();
  116. }
  117. }
  118. fn main() {
  119. let context = Rltk::init_simple8x8(80, 50, "Hello Rust World", "resources");
  120. let mut gs = State {
  121. ecs: World::new()
  122. };
  123. gs.ecs.register::<Position>();
  124. gs.ecs.register::<Renderable>();
  125. gs.ecs.register::<Player>();
  126. gs.ecs.insert(new_map());
  127. gs.ecs
  128. .create_entity()
  129. .with(Position { x: 40, y: 25 })
  130. .with(Renderable {
  131. glyph: rltk::to_cp437('@'),
  132. fg: RGB::named(rltk::YELLOW),
  133. bg: RGB::named(rltk::BLACK),
  134. })
  135. .with(Player{})
  136. .build();
  137. rltk::main_loop(context, gs);
  138. }