main.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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::Board<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.push("resources");
  81. path
  82. })
  83. .tileset("/terminal8x8.jpg", [8, 8])
  84. .map_size(80, 50)
  85. .build()?;
  86. game.register::<Pos>();
  87. game.register::<Renderable>();
  88. game.register::<MoveLeft>();
  89. game.register::<Motion>();
  90. game.register::<Player>();
  91. game.world.print([1, 1], "Hello, world!");
  92. game.create_entity()
  93. .with(Pos { x: 40, y: 25 })
  94. .with(Player)
  95. .with(Renderable {
  96. glyph: carpet::CP437::from_char('A'),
  97. color: carpet::Color::Blue,
  98. })
  99. .build();
  100. for i in 0..10 {
  101. game.create_entity()
  102. .with(Pos { x: i * 7, y: 20 })
  103. .with(Renderable {
  104. glyph: carpet::CP437::from_char('X'),
  105. color: carpet::Color::Red,
  106. })
  107. .with(MoveLeft)
  108. .build();
  109. }
  110. game.on_key((carpet::VirtualKeyCode::W, carpet::KeyMods::NONE), |world| {
  111. Motion::move_player(world, -1, 0);
  112. });
  113. game.on_key((carpet::VirtualKeyCode::A, carpet::KeyMods::NONE), |world| {
  114. Motion::move_player(world, 0, -1);
  115. });
  116. game.on_key((carpet::VirtualKeyCode::S, carpet::KeyMods::NONE), |world| {
  117. Motion::move_player(world, 1, 0);
  118. });
  119. game.on_key((carpet::VirtualKeyCode::D, carpet::KeyMods::NONE), |world| {
  120. Motion::move_player(world, 0, 1);
  121. });
  122. game.run_with_systems(|world| {
  123. Draw.run_now(&world);
  124. Leftward.run_now(&world);
  125. Move.run_now(&world);
  126. })
  127. }