Skip to content

Instantly share code, notes, and snippets.

@Kerollmops
Created July 11, 2016 19:20
Show Gist options
  • Select an option

  • Save Kerollmops/2d245032cc11c3b10f48efb099a2af08 to your computer and use it in GitHub Desktop.

Select an option

Save Kerollmops/2d245032cc11c3b10f48efb099a2af08 to your computer and use it in GitHub Desktop.
//! Systems to specifically deal with entities.
use std::collections::HashMap;
use std::ops::{Deref, DerefMut};
use ecs::Aspect;
use ecs::DataHelper;
use ecs::{Entity, IndexedEntity};
use ecs::EntityData;
use ecs::EntityIter;
use ecs::{System, Process};
use piston_window::{G2d, Context};
use graphics::{Graphics, DrawState};
use graphics::rectangle::{Rectangle, square};
use super::super::Components;
use super::super::Services;
pub struct RenderEntity
{
interested: HashMap<Entity, IndexedEntity<Components>>,
aspect: Aspect<Components>,
}
impl RenderEntity
{
pub fn new(aspect: Aspect<Components>) -> RenderEntity {
RenderEntity {
interested: HashMap::new(),
aspect: aspect,
}
}
pub fn render(&mut self, custom_arg: i32, c: Context, g: &mut G2d, data: &mut DataHelper<Components, Services>) {
let entities = EntityIter::Map(self.interested.values());
for (i, _) in entities.enumerate() {
println!(" {:?}", i);
}
let draw_state = DrawState::default();
let rect = Rectangle::new([1.0, 0.0, 0.0, 1.0]);
println!("hello custom_arg: {:?}", custom_arg);
g.clear_color([1.0, 1.0, 1.0, 1.0]);
rect.draw(square(10.0, 10.0, 200.0), &draw_state, c.transform, g);
}
}
impl System for RenderEntity
{
type Components = Components;
type Services = Services;
fn activated(&mut self, entity: &EntityData<Components>, components: &Components, services: &mut Self::Services) {
if self.aspect.check(entity, components) {
self.interested.insert(***entity, (**entity).__clone());
// self.inner.activated(entity, components, services);
}
}
fn reactivated(&mut self, entity: &EntityData<Components>, components: &Components, services: &mut Self::Services)
{
if self.interested.contains_key(entity) {
if self.aspect.check(entity, components) {
// self.inner.reactivated(entity, components, services);
}
else {
self.interested.remove(entity);
// self.inner.deactivated(entity, components, services);
}
}
else if self.aspect.check(entity, components) {
self.interested.insert(***entity, (**entity).__clone());
// self.inner.activated(entity, components, services);
}
}
fn deactivated(&mut self, entity: &EntityData<Components>, components: &Components, services: &mut Self::Services)
{
if self.interested.remove(entity).is_some() {
// self.inner.deactivated(entity, components, services);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment