main.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #[derive(Component)]
  8. pub struct Pos {
  9. x: usize,
  10. y: usize,
  11. }
  12. #[derive(Component)]
  13. pub struct Renderable {
  14. glyph: carpet::CP437,
  15. color: carpet::Color,
  16. }
  17. #[derive(Component)]
  18. pub struct MoveLeft;
  19. #[derive(Component)]
  20. pub struct Player;
  21. impl Player {
  22. fn get_entity(world: &mut specs::World) -> Entity {
  23. let storage = (&world.read_component::<Player>(), &world.entities());
  24. storage.join().next().expect("No entities tagged as Player").1
  25. }
  26. }
  27. #[derive(Component)]
  28. pub struct Motion {
  29. down: i8,
  30. right: i8,
  31. }
  32. impl Motion {
  33. fn move_player(world: &mut specs::World, down: i8, right: i8) {
  34. let player = Player::get_entity(world);
  35. world.write_component::<Motion>().insert(player, Motion { down, right }).unwrap();
  36. }
  37. }
  38. system_impl! {
  39. Draw(
  40. resource mut board: carpet::GameBoard<carpet::CP437>,
  41. renderable: Renderable,
  42. pos: Pos,
  43. ) {
  44. board.clear();
  45. for (p, r) in (&pos, &renderable).join() {
  46. board.set_with_color([p.x, p.y], r.glyph, r.color);
  47. }
  48. }
  49. }
  50. system! {
  51. Leftward (
  52. _left: MoveLeft,
  53. mut pos: Pos,
  54. ) {
  55. if pos.x == 0 {
  56. pos.x = 79;
  57. } else {
  58. pos.x -= 1;
  59. }
  60. }
  61. }
  62. system! {
  63. Move (
  64. mut motion: Motion,
  65. mut pos: Pos,
  66. ) {
  67. pos.x = (pos.x as i8 + motion.right) as usize;
  68. pos.y = (pos.y as i8 + motion.down) as usize;
  69. } finally {
  70. motion.clear();
  71. }
  72. }
  73. fn main() -> Result<(), GameError> {
  74. let mut game = carpet::GameBuilder::new()
  75. .name("game")
  76. .author("me")
  77. .resource_path({
  78. let base = std::env::var("CARGO_MANIFEST_DIR").unwrap();
  79. let mut path = std::path::PathBuf::from(base);
  80. path.pop();
  81. path.push("resources");
  82. path
  83. })
  84. .tileset("/haberdash.gif", [12, 12])
  85. .map_size(80, 50)
  86. .build()?;
  87. game.register::<Pos>();
  88. game.register::<Renderable>();
  89. game.register::<MoveLeft>();
  90. game.register::<Motion>();
  91. game.register::<Player>();
  92. game.world.print([1, 1], "Hello, world!");
  93. game.create_entity()
  94. .with(Pos { x: 40, y: 25 })
  95. .with(Player)
  96. .with(Renderable {
  97. glyph: carpet::CP437::from_char('A'),
  98. color: carpet::Color::Blue,
  99. })
  100. .build();
  101. for i in 0..10 {
  102. game.create_entity()
  103. .with(Pos { x: i * 7, y: 20 })
  104. .with(Renderable {
  105. glyph: carpet::CP437::from_char('X'),
  106. color: carpet::Color::Red,
  107. })
  108. .with(MoveLeft)
  109. .build();
  110. }
  111. game.on_key((carpet::VirtualKeyCode::W, carpet::KeyMods::NONE), |world| {
  112. Motion::move_player(world, -1, 0);
  113. });
  114. game.on_key((carpet::VirtualKeyCode::A, carpet::KeyMods::NONE), |world| {
  115. Motion::move_player(world, 0, -1);
  116. });
  117. game.on_key((carpet::VirtualKeyCode::S, carpet::KeyMods::NONE), |world| {
  118. Motion::move_player(world, 1, 0);
  119. });
  120. game.on_key((carpet::VirtualKeyCode::D, carpet::KeyMods::NONE), |world| {
  121. Motion::move_player(world, 0, 1);
  122. });
  123. game.run_with_systems(|world| {
  124. Draw.run_now(&world);
  125. Leftward.run_now(&world);
  126. Move.run_now(&world);
  127. })
  128. }