main.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. use rltk::{Console, GameState, Rltk, RGB, VirtualKeyCode};
  2. use specs::prelude::*;
  3. #[macro_use]
  4. extern crate specs_derive;
  5. #[derive(Component)]
  6. struct Position {
  7. x: i32,
  8. y: i32,
  9. }
  10. #[derive(Component)]
  11. struct Renderable {
  12. glyph: u8,
  13. fg: RGB,
  14. bg: RGB,
  15. }
  16. #[derive(Component, Debug)]
  17. struct Player {}
  18. #[derive(PartialEq, Copy, Clone)]
  19. enum TileType {
  20. Wall, Floor
  21. }
  22. struct State {
  23. ecs: World,
  24. systems: Dispatcher<'static, 'static>
  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.systems.dispatch(&self.ecs);
  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. fn main() {
  118. let context = Rltk::init_simple8x8(80, 50, "Hello Rust World", "../resources");
  119. let mut gs = State {
  120. ecs: World::new(),
  121. systems : DispatcherBuilder::new()
  122. .build()
  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. }