main.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #[macro_use]
  2. extern crate specs_derive;
  3. #[macro_use]
  4. extern crate specs_system_macro;
  5. use ggez::GameError;
  6. use specs::prelude::*;
  7. use rand::Rng;
  8. #[derive(PartialEq, Clone, Copy, Debug)]
  9. pub enum TileType {
  10. Wall,
  11. Floor,
  12. }
  13. impl TileType {
  14. fn glyph(&self) -> carpet::CP437 {
  15. match self {
  16. TileType::Wall => carpet::CP437::from_char('#'),
  17. TileType::Floor => carpet::CP437::from_u8(0),
  18. }
  19. }
  20. }
  21. pub struct Map {
  22. tiles: carpet::Board<TileType>,
  23. }
  24. impl Map {
  25. fn new() -> Map {
  26. let mut rng = rand::thread_rng();
  27. let tiles = carpet::Board::new_from(80, 50, |x, y| {
  28. if x == 0 || x == 79 || y == 0 || y == 49 {
  29. TileType::Wall
  30. } else if rng.gen::<u8>() % 8 == 0 {
  31. TileType::Wall
  32. } else {
  33. TileType::Floor
  34. }
  35. });
  36. Map { tiles }
  37. }
  38. fn passable(&self, (x, y): (usize, usize)) -> bool {
  39. Some(&TileType::Floor) == self.tiles.get(x, y)
  40. }
  41. }
  42. #[derive(Component)]
  43. pub struct Pos {
  44. x: usize,
  45. y: usize,
  46. }
  47. #[derive(Component)]
  48. pub struct Renderable {
  49. glyph: carpet::CP437,
  50. color: carpet::Color,
  51. }
  52. #[derive(Component)]
  53. pub struct Player;
  54. impl Player {
  55. fn get_entity(world: &mut specs::World) -> Entity {
  56. let storage = (&world.read_component::<Player>(), &world.entities());
  57. storage.join().next().expect("No entities tagged as Player").1
  58. }
  59. }
  60. #[derive(Component)]
  61. pub struct Motion {
  62. down: i8,
  63. right: i8,
  64. }
  65. impl Motion {
  66. fn move_player(world: &mut specs::World, down: i8, right: i8) {
  67. let player = Player::get_entity(world);
  68. world.write_component::<Motion>().insert(player, Motion { down, right }).unwrap();
  69. }
  70. }
  71. system_impl! {
  72. Draw(
  73. resource mut game_board: carpet::GameBoard<carpet::CP437>,
  74. resource map: Map,
  75. renderable: Renderable,
  76. pos: Pos,
  77. ) {
  78. game_board.clear();
  79. for (x, y, t) in map.tiles.iter() {
  80. game_board.set([x, y], t.glyph());
  81. }
  82. for (p, r) in (&pos, &renderable).join() {
  83. game_board.set_with_color([p.x, p.y], r.glyph, r.color);
  84. }
  85. }
  86. }
  87. system! {
  88. Move (
  89. resource map: Map,
  90. mut motion: Motion,
  91. mut pos: Pos,
  92. ) {
  93. let tgt_x = (pos.x as i8 + motion.right) as usize;
  94. let tgt_y = (pos.y as i8 + motion.down) as usize;
  95. if map.passable((tgt_x, tgt_y)) {
  96. pos.x = tgt_x;
  97. pos.y = tgt_y;
  98. }
  99. } finally {
  100. motion.clear();
  101. }
  102. }
  103. fn main() -> Result<(), GameError> {
  104. let mut game: carpet::Game<carpet::CP437> =
  105. carpet::GameBuilder::new()
  106. .name("game")
  107. .author("me")
  108. .resource_path({
  109. let base = std::env::var("CARGO_MANIFEST_DIR").unwrap();
  110. let mut path = std::path::PathBuf::from(base);
  111. path.pop();
  112. path.push("resources");
  113. path
  114. })
  115. .tileset("/haberdash.gif", [12, 12])
  116. .map_size(80, 50)
  117. .build()?;
  118. game.register::<Pos>();
  119. game.register::<Renderable>();
  120. game.register::<Motion>();
  121. game.register::<Player>();
  122. game.insert(Map::new());
  123. game.create_entity()
  124. .with(Pos { x: 40, y: 25 })
  125. .with(Player)
  126. .with(Renderable {
  127. glyph: carpet::CP437::from_char('A'),
  128. color: carpet::Color::Blue,
  129. })
  130. .build();
  131. game.on_key((carpet::VirtualKeyCode::W, carpet::KeyMods::NONE), |world| {
  132. Motion::move_player(world, -1, 0);
  133. });
  134. game.on_key((carpet::VirtualKeyCode::A, carpet::KeyMods::NONE), |world| {
  135. Motion::move_player(world, 0, -1);
  136. });
  137. game.on_key((carpet::VirtualKeyCode::S, carpet::KeyMods::NONE), |world| {
  138. Motion::move_player(world, 1, 0);
  139. });
  140. game.on_key((carpet::VirtualKeyCode::D, carpet::KeyMods::NONE), |world| {
  141. Motion::move_player(world, 0, 1);
  142. });
  143. game.run_with_systems(|world| {
  144. Draw.run_now(&world);
  145. Move.run_now(&world);
  146. })
  147. }