summaryrefslogtreecommitdiff
path: root/src/room.rs
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-02-08 18:50:58 +0100
committertroido <troido@protonmail.com>2020-02-08 18:50:58 +0100
commit3ebe9e6f792a0457c6f3b37b6e9d92c83f8694e2 (patch)
treeaaa81d23a4816ebecacc64593419d3d811b7283b /src/room.rs
parent7dfc7956a7c2df9a1df3ea0b32e0c3d2036fa3ce (diff)
don't rebuild the ground each step
Diffstat (limited to 'src/room.rs')
-rw-r--r--src/room.rs50
1 files changed, 14 insertions, 36 deletions
diff --git a/src/room.rs b/src/room.rs
index dc7c849..84a1040 100644
--- a/src/room.rs
+++ b/src/room.rs
@@ -4,15 +4,12 @@ use std::collections::HashMap;
use specs::{
World,
WorldExt,
- Builder,
DispatcherBuilder,
- Dispatcher,
- Entity
+ Dispatcher
};
use super::controls::Action;
use super::pos::Pos;
-use super::components::Position;
use super::worldmessages::WorldMessage;
use super::resources::{
Size,
@@ -23,35 +20,38 @@ use super::resources::{
};
use super::systems::{
moving::Move,
- clearcontrols::ClearControllers,
makefloor::MakeFloor,
controlinput::ControlInput,
- view::View
+ view::View,
+ remove::Remove,
+ create::Create
};
-use super::componentwrapper::ComponentWrapper;
use crate::encyclopedia::Encyclopedia;
-use crate::template::Template;
use crate::roomtemplate::RoomTemplate;
pub struct Room<'a, 'b> {
world: World,
- dispatcher: Dispatcher<'a, 'b>,
- encyclopedia: Encyclopedia
+ dispatcher: Dispatcher<'a, 'b>
}
impl <'a, 'b>Room<'a, 'b> {
pub fn new(encyclopedia: Encyclopedia) -> Room<'a, 'b> {
let mut world = World::new();
+ world.insert(NewEntities{
+ templates: Vec::new(),
+ encyclopedia
+ });
let mut dispatcher = DispatcherBuilder::new()
.with(ControlInput, "controlinput", &[])
- .with(MakeFloor, "makefloor", &[])
+ .with(MakeFloor::default(), "makefloor", &[])
.with(Move, "move", &["makefloor", "controlinput"])
- .with(ClearControllers, "clearcontrollers", &["move"])
.with(View::default(), "view", &["move"])
+ .with(Create, "create", &["view", "controlinput"])
+ .with(Remove, "remove", &["view", "move"])
.build();
dispatcher.setup(&mut world);
@@ -59,8 +59,7 @@ impl <'a, 'b>Room<'a, 'b> {
Room {
world,
- dispatcher,
- encyclopedia
+ dispatcher
}
}
@@ -77,9 +76,7 @@ impl <'a, 'b>Room<'a, 'b> {
let y = (idx as i64) / width;
for template in templates {
- if let Err(msg) = self.add_entity(template, Pos{x, y}){
- println!("{}", msg);
- }
+ self.world.fetch_mut::<NewEntities>().templates.push((Pos{x, y}, template.clone()));
}
}
}
@@ -90,13 +87,6 @@ impl <'a, 'b>Room<'a, 'b> {
pub fn update(&mut self) {
self.dispatcher.dispatch(&mut self.world);
- let templates = self.world.remove::<NewEntities>().unwrap_or(NewEntities::default()).templates;
- self.world.insert(NewEntities::default());
- for (pos, template) in templates{
- if let Err(msg) = self.add_entity(&template, pos){
- println!("failed to add entity {:?}: {}", template, msg);
- }
- }
self.world.maintain();
}
@@ -104,18 +94,6 @@ impl <'a, 'b>Room<'a, 'b> {
self.world.fetch_mut::<Input>().actions = actions;
}
- pub fn add_entity(&mut self, template: &Template, pos: Pos) -> Result<Entity, &'static str> {
- let preentity = self.encyclopedia.construct(template)?;
- Ok(self.add_complist(&preentity, pos))
- }
-//
- pub fn add_complist(&mut self, template: &Vec<ComponentWrapper>, pos: Pos) -> Entity{
- let mut builder = self.world.create_entity();
- for comp in template {
- builder = comp.build(builder);
- }
- builder.with(Position::new(pos)).build()
- }
}