main.rs 4.3 KB

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