main.rs 4.1 KB

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