12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- #[macro_use]
- extern crate specs_derive;
- #[macro_use]
- extern crate specs_system_macro;
- use ggez::GameError;
- use specs::prelude::*;
- #[derive(Component)]
- pub struct Pos {
- x: usize,
- y: usize,
- }
- #[derive(Component)]
- pub struct Renderable {
- glyph: carpet::CP437,
- color: carpet::Color,
- }
- #[derive(Component)]
- pub struct MoveLeft;
- system_impl! {
- Draw(
- resource mut board: carpet::Board<carpet::CP437>,
- renderable: Renderable,
- pos: Pos,
- ) {
- board.clear();
- for (p, r) in (&pos, &renderable).join() {
- board.set_with_color([p.x, p.y], r.glyph, r.color);
- }
- }
- }
- system! {
- Leftward (
- _left: MoveLeft,
- mut pos: Pos,
- ) {
- if pos.x == 0 {
- pos.x = 79;
- } else {
- pos.x -= 1;
- }
- }
- }
- fn main() -> Result<(), GameError> {
- let mut game = carpet::GameBuilder::new()
- .name("game")
- .author("me")
- .resource_path({
- let base = std::env::var("CARGO_MANIFEST_DIR").unwrap();
- let mut path = std::path::PathBuf::from(base);
- path.push("resources");
- path
- })
- .tileset("/terminal8x8.jpg", [8, 8])
- .map_size(80, 50)
- .build()?;
- game.register::<Pos>();
- game.register::<Renderable>();
- game.register::<MoveLeft>();
- game.world.print([1, 1], "Hello, world!");
- game.create_entity()
- .with(Pos { x: 40, y: 25 })
- .with(Renderable {
- glyph: carpet::CP437::from_char('A'),
- color: carpet::Color::Blue,
- })
- .build();
- for i in 0..10 {
- game.create_entity()
- .with(Pos { x: i * 7, y: 20 })
- .with(Renderable {
- glyph: carpet::CP437::from_char('X'),
- color: carpet::Color::Red,
- })
- .with(MoveLeft)
- .build();
- }
- game.run_with_systems(|world| {
- Draw.run_now(&world);
- Leftward.run_now(&world);
- })
- }
|