main.rs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. system_impl! {
  20. Draw(
  21. resource mut board: carpet::Board<carpet::CP437>,
  22. renderable: Renderable,
  23. pos: Pos,
  24. ) {
  25. board.clear();
  26. for (p, r) in (&pos, &renderable).join() {
  27. board.set_with_color([p.x, p.y], r.glyph, r.color);
  28. }
  29. }
  30. }
  31. system! {
  32. Leftward (
  33. _left: MoveLeft,
  34. mut pos: Pos,
  35. ) {
  36. if pos.x == 0 {
  37. pos.x = 79;
  38. } else {
  39. pos.x -= 1;
  40. }
  41. }
  42. }
  43. fn main() -> Result<(), GameError> {
  44. let mut game = carpet::GameBuilder::new()
  45. .name("game")
  46. .author("me")
  47. .resource_path({
  48. let base = std::env::var("CARGO_MANIFEST_DIR").unwrap();
  49. let mut path = std::path::PathBuf::from(base);
  50. path.push("resources");
  51. path
  52. })
  53. .tileset("/terminal8x8.jpg", [8, 8])
  54. .map_size(80, 50)
  55. .build()?;
  56. game.register::<Pos>();
  57. game.register::<Renderable>();
  58. game.register::<MoveLeft>();
  59. game.world.print([1, 1], "Hello, world!");
  60. game.create_entity()
  61. .with(Pos { x: 40, y: 25 })
  62. .with(Renderable {
  63. glyph: carpet::CP437::from_char('A'),
  64. color: carpet::Color::Blue,
  65. })
  66. .build();
  67. for i in 0..10 {
  68. game.create_entity()
  69. .with(Pos { x: i * 7, y: 20 })
  70. .with(Renderable {
  71. glyph: carpet::CP437::from_char('X'),
  72. color: carpet::Color::Red,
  73. })
  74. .with(MoveLeft)
  75. .build();
  76. }
  77. game.run_with_systems(|world| {
  78. Draw.run_now(&world);
  79. Leftward.run_now(&world);
  80. })
  81. }