main.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #[macro_use]
  2. extern crate specs_derive;
  3. #[macro_use]
  4. extern crate specs_system_macro;
  5. use ggez::{event::EventHandler, Context, 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: u8,
  15. color: carpet::Color,
  16. }
  17. system_impl! {
  18. Draw(
  19. resource mut board: carpet::Board<u8>,
  20. renderable: Renderable,
  21. pos: Pos,
  22. ) {
  23. board.clear();
  24. for (p, r) in (&pos, &renderable).join() {
  25. board.set_with_color([p.x, p.y], r.glyph, r.color);
  26. }
  27. }
  28. }
  29. #[derive(Component)]
  30. pub struct MoveLeft;
  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. struct State {
  44. world: World,
  45. }
  46. impl EventHandler for State {
  47. fn draw(&mut self, ctx: &mut Context) -> Result<(), GameError> {
  48. ggez::graphics::clear(ctx, ggez::graphics::BLACK);
  49. self.world.fetch::<carpet::Board<u8>>().draw(ctx)?;
  50. ggez::graphics::present(ctx)
  51. }
  52. fn update(&mut self, _ctx: &mut Context) -> Result<(), GameError> {
  53. Draw.run_now(&mut self.world);
  54. Leftward.run_now(&mut self.world);
  55. Ok(())
  56. }
  57. }
  58. fn main() -> Result<(), GameError> {
  59. let (mut ctx, mut evloop) = ggez::ContextBuilder::new("game", "me")
  60. .add_resource_path({
  61. let base = std::env::var("CARGO_MANIFEST_DIR").unwrap();
  62. let mut path = std::path::PathBuf::from(base);
  63. path.push("resources");
  64. path
  65. })
  66. .window_mode(ggez::conf::WindowMode {
  67. width: 80.0 * 8.0,
  68. height: 50.0 * 8.0,
  69. ..ggez::conf::WindowMode::default()
  70. })
  71. .build()?;
  72. let mut world = World::new();
  73. world.register::<Pos>();
  74. world.register::<Renderable>();
  75. world.register::<MoveLeft>();
  76. let tileset = carpet::Tileset::from_file(&mut ctx, [8, 8], "/terminal8x8.jpg")?;
  77. let mut board = carpet::Board::new([80, 50], tileset);
  78. board.print([1, 1], "Hello, world!");
  79. world
  80. .create_entity()
  81. .with(Pos { x: 40, y: 25 })
  82. .with(Renderable {
  83. glyph: 'A' as u8,
  84. color: carpet::Color::Blue,
  85. })
  86. .build();
  87. for i in 0..10 {
  88. world
  89. .create_entity()
  90. .with(Pos { x: i * 7, y: 20 })
  91. .with(Renderable {
  92. glyph: 'X' as u8,
  93. color: carpet::Color::Red,
  94. })
  95. .with(MoveLeft)
  96. .build();
  97. }
  98. world.insert(board);
  99. let mut state = State { world };
  100. ggez::event::run(&mut ctx, &mut evloop, &mut state)
  101. }
  102. // Local Variables:
  103. // cargo-process--command-build: "build --release"
  104. // cargo-process--command-run: "run --release --bin ch2"
  105. // End: