main.rs 3.2 KB

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