64 lines
1.5 KiB
Rust
64 lines
1.5 KiB
Rust
#![allow(dead_code)]
|
|
|
|
use component_macro::Component;
|
|
use engine::QuwiQuwi;
|
|
use engine::{Sprite, System};
|
|
use std::any::{Any, TypeId};
|
|
use std::rc::Rc;
|
|
mod engine;
|
|
|
|
use engine::Component;
|
|
|
|
#[derive(Component)]
|
|
struct Player {
|
|
sprite: Sprite,
|
|
}
|
|
|
|
#[derive(Component)]
|
|
struct Position(i32, i32);
|
|
|
|
struct Gravity;
|
|
|
|
impl System for Gravity {
|
|
fn on_update(&self, ctx: &mut engine::Context) -> Result<(), engine::Error> {
|
|
for id in ctx.entities_with_component::<Position>() {
|
|
let Position(_, y) = ctx.entity_component::<Position>(id);
|
|
*y += 1;
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
struct SpriteRenderer;
|
|
|
|
impl System for SpriteRenderer {
|
|
fn on_update(&self, ctx: &mut engine::Context) -> Result<(), engine::Error> {
|
|
for id in engine::Quwi::<(Player, Position)>::new().run(&ctx) {
|
|
let &mut Position(x, y) = ctx.entity_component::<Position>(id);
|
|
let sprite = ctx.entity_component::<Player>(id).sprite;
|
|
|
|
ctx.draw_sprite(sprite, x, y)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
struct PlayerMovement;
|
|
|
|
impl System for PlayerMovement {
|
|
fn on_update(&self, _ctx: &mut engine::Context) -> Result<(), engine::Error> {
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let mut game = engine::Game::new().unwrap();
|
|
|
|
let mut context = game.context();
|
|
context.add_system(Rc::new(SpriteRenderer));
|
|
context.add_system(Rc::new(Gravity));
|
|
let sprite = context.load_sprite("textures/player.png").unwrap();
|
|
context.spawn(vec![Box::new(Player { sprite }), Box::new(Position(16, 0))]);
|
|
game.run();
|
|
}
|