main.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. }
  39. #[derive(Component)]
  40. pub struct Pos {
  41. x: usize,
  42. y: usize,
  43. }
  44. #[derive(Component)]
  45. pub struct Renderable {
  46. glyph: carpet::CP437,
  47. color: carpet::Color,
  48. }
  49. #[derive(Component)]
  50. pub struct Player;
  51. impl Player {
  52. fn get_entity(world: &mut specs::World) -> Entity {
  53. let storage = (&world.read_component::<Player>(), &world.entities());
  54. storage.join().next().expect("No entities tagged as Player").1
  55. }
  56. }
  57. #[derive(Component)]
  58. pub struct Motion {
  59. down: i8,
  60. right: i8,
  61. }
  62. impl Motion {
  63. fn move_player(world: &mut specs::World, down: i8, right: i8) {
  64. let player = Player::get_entity(world);
  65. world.write_component::<Motion>().insert(player, Motion { down, right }).unwrap();
  66. }
  67. }
  68. system_impl! {
  69. Draw(
  70. resource mut game_board: carpet::GameBoard<carpet::CP437>,
  71. resource map: Map,
  72. renderable: Renderable,
  73. pos: Pos,
  74. ) {
  75. game_board.clear();
  76. for (x, y, t) in map.tiles.iter() {
  77. game_board.set([x, y], t.glyph());
  78. }
  79. for (p, r) in (&pos, &renderable).join() {
  80. game_board.set_with_color([p.x, p.y], r.glyph, r.color);
  81. }
  82. }
  83. }
  84. system! {
  85. Move (
  86. mut motion: Motion,
  87. mut pos: Pos,
  88. ) {
  89. pos.x = (pos.x as i8 + motion.right) as usize;
  90. pos.y = (pos.y as i8 + motion.down) as usize;
  91. } finally {
  92. motion.clear();
  93. }
  94. }
  95. fn main() -> Result<(), GameError> {
  96. let mut game: carpet::Game<carpet::CP437> =
  97. carpet::GameBuilder::new()
  98. .name("game")
  99. .author("me")
  100. .resource_path({
  101. let base = std::env::var("CARGO_MANIFEST_DIR").unwrap();
  102. let mut path = std::path::PathBuf::from(base);
  103. path.push("resources");
  104. path
  105. })
  106. .tileset("/terminal8x8.jpg", [8, 8])
  107. .map_size(80, 50)
  108. .build()?;
  109. game.register::<Pos>();
  110. game.register::<Renderable>();
  111. game.register::<Motion>();
  112. game.register::<Player>();
  113. game.insert(Map::new());
  114. game.create_entity()
  115. .with(Pos { x: 40, y: 25 })
  116. .with(Player)
  117. .with(Renderable {
  118. glyph: carpet::CP437::from_char('A'),
  119. color: carpet::Color::Blue,
  120. })
  121. .build();
  122. game.on_key((carpet::VirtualKeyCode::W, carpet::KeyMods::NONE), |world| {
  123. Motion::move_player(world, -1, 0);
  124. });
  125. game.on_key((carpet::VirtualKeyCode::A, carpet::KeyMods::NONE), |world| {
  126. Motion::move_player(world, 0, -1);
  127. });
  128. game.on_key((carpet::VirtualKeyCode::S, carpet::KeyMods::NONE), |world| {
  129. Motion::move_player(world, 1, 0);
  130. });
  131. game.on_key((carpet::VirtualKeyCode::D, carpet::KeyMods::NONE), |world| {
  132. Motion::move_player(world, 0, 1);
  133. });
  134. game.run_with_systems(|world| {
  135. Draw.run_now(&world);
  136. Move.run_now(&world);
  137. })
  138. }