rust 😭
This commit is contained in:
parent
7e2bac57cd
commit
fd793f4b45
@ -69,6 +69,60 @@ where
|
||||
currently_pressed_keys: &'context HashSet<Keycode>,
|
||||
}
|
||||
|
||||
pub struct Quwi<T>(std::marker::PhantomData<T>);
|
||||
|
||||
impl<T> Quwi<T> {
|
||||
pub fn new() -> Self {
|
||||
Self(std::marker::PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait QuwiQuwi {
|
||||
fn run(&self, context: &Context) -> Vec<u64>;
|
||||
}
|
||||
|
||||
impl<T0> QuwiQuwi for Quwi<T0>
|
||||
where
|
||||
T0: 'static + Component,
|
||||
{
|
||||
fn run(&self, context: &Context) -> Vec<u64> {
|
||||
context.entities_with_component::<T0>()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T0, T1> QuwiQuwi for Quwi<(T0, T1)>
|
||||
where
|
||||
T0: 'static + Component,
|
||||
T1: 'static + Component,
|
||||
{
|
||||
fn run(&self, context: &Context) -> Vec<u64> {
|
||||
let vs0 = context.entities_with_component::<T0>();
|
||||
let vs1 = context.entities_with_component::<T1>();
|
||||
vs0.into_iter()
|
||||
.filter(|v0| vs1.iter().find(|v1| *v0 == **v1).is_some())
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T0, T1, T2> QuwiQuwi for Quwi<(T0, T1, T2)>
|
||||
where
|
||||
T0: 'static + Component,
|
||||
T1: 'static + Component,
|
||||
T2: 'static + Component,
|
||||
{
|
||||
fn run(&self, context: &Context) -> Vec<u64> {
|
||||
let vs0 = context.entities_with_component::<T0>();
|
||||
let vs1 = context.entities_with_component::<T1>();
|
||||
let vs2 = context.entities_with_component::<T2>();
|
||||
vs0.into_iter()
|
||||
.filter(|v0| {
|
||||
vs1.iter().find(|v1| *v0 == **v1).is_some()
|
||||
&& vs2.iter().find(|v2| *v0 == **v2).is_some()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'context, 'game> Context<'context, 'game> {
|
||||
pub fn entities_with_component<T: 'static + Component>(&self) -> Vec<u64> {
|
||||
let entity_type_id = TypeId::of::<T>();
|
||||
@ -117,12 +171,11 @@ impl<'context, 'game> Context<'context, 'game> {
|
||||
let texture: Texture<'game> = unsafe { (*self.texture_creator).load_texture(&path)? };
|
||||
let id = *self.id_counter;
|
||||
*self.id_counter += 1;
|
||||
|
||||
self.textures.push((id, texture));
|
||||
Ok(Sprite(id))
|
||||
}
|
||||
|
||||
pub fn draw_sprite(&mut self, sprite: &Sprite, x: i32, y: i32) -> Result<(), Error> {
|
||||
pub fn draw_sprite(&mut self, sprite: Sprite, x: i32, y: i32) -> Result<(), Error> {
|
||||
let texture = self
|
||||
.textures
|
||||
.iter()
|
||||
@ -254,6 +307,7 @@ impl<'game> Game<'game> {
|
||||
entities: &mut self.entities,
|
||||
systems: &mut self.systems,
|
||||
textures: &mut self.textures,
|
||||
currently_pressed_keys: &mut self.currently_pressed_keys,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
17
src/main.rs
17
src/main.rs
@ -1,6 +1,7 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use component_macro::Component;
|
||||
use engine::QuwiQuwi;
|
||||
use engine::{Sprite, System};
|
||||
use std::any::{Any, TypeId};
|
||||
use std::rc::Rc;
|
||||
@ -28,15 +29,15 @@ impl System for Gravity {
|
||||
}
|
||||
}
|
||||
|
||||
struct PlayerRenderer;
|
||||
struct SpriteRenderer;
|
||||
|
||||
impl System for PlayerRenderer {
|
||||
impl System for SpriteRenderer {
|
||||
fn on_update(&self, ctx: &mut engine::Context) -> Result<(), engine::Error> {
|
||||
for id in ctx.entities_with_component::<Player>() {
|
||||
for id in engine::Quwi::<(Player, Position)>::new().run(&ctx) {
|
||||
let &mut Position(x, y) = ctx.entity_component::<Position>(id);
|
||||
let player = ctx.entity_component::<Player>(id);
|
||||
let sprite = ctx.entity_component::<Player>(id).sprite;
|
||||
|
||||
ctx.draw_sprite(&player.sprite, x, y)?;
|
||||
ctx.draw_sprite(sprite, x, y)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -45,14 +46,16 @@ impl System for PlayerRenderer {
|
||||
struct PlayerMovement;
|
||||
|
||||
impl System for PlayerMovement {
|
||||
fn on_update(&self, _ctx: &mut engine::Context) -> Result<(), engine::Error> {}
|
||||
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(PlayerRenderer));
|
||||
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))]);
|
||||
|
Loading…
Reference in New Issue
Block a user