summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/assemblage.rs4
-rw-r--r--src/componentwrapper.rs1
-rw-r--r--src/encyclopedia.rs4
-rw-r--r--src/main.rs13
-rw-r--r--src/player.rs13
-rw-r--r--src/resources.rs7
-rw-r--r--src/room.rs21
-rw-r--r--src/systems/controlinput.rs8
-rw-r--r--src/systems/create.rs15
9 files changed, 42 insertions, 44 deletions
diff --git a/src/assemblage.rs b/src/assemblage.rs
index 879cd3e..a4179dd 100644
--- a/src/assemblage.rs
+++ b/src/assemblage.rs
@@ -3,7 +3,7 @@ use std::collections::HashMap;
use serde_json::{Value, json};
use super::componentparameter::ComponentParameter;
use super::parameter::{Parameter, ParameterType};
-use super::componentwrapper::{ComponentWrapper, ComponentType};
+use super::componentwrapper::{ComponentWrapper, ComponentType, PreEntity};
#[derive(Debug, PartialEq, Clone)]
pub struct Assemblage {
@@ -92,7 +92,7 @@ impl Assemblage {
Ok(arguments)
}
- pub fn instantiate(&self, args: &Vec<Parameter>, kwargs: &HashMap<String, Parameter>) -> Result<Vec<ComponentWrapper>, &'static str>{
+ pub fn instantiate(&self, args: &Vec<Parameter>, kwargs: &HashMap<String, Parameter>) -> Result<PreEntity, &'static str>{
let mut components: Vec<ComponentWrapper> = Vec::new();
let arguments = self.prepare_arguments(args, kwargs)?;
for (comptype, compparams) in &self.components {
diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs
index e821b8b..23e104b 100644
--- a/src/componentwrapper.rs
+++ b/src/componentwrapper.rs
@@ -88,6 +88,7 @@ components!(
Player [name: String] parameters {Player::new(parameters.remove("name")?.as_string()?)}
);
+pub type PreEntity = Vec<ComponentWrapper>;
diff --git a/src/encyclopedia.rs b/src/encyclopedia.rs
index 5cdf90c..6851a06 100644
--- a/src/encyclopedia.rs
+++ b/src/encyclopedia.rs
@@ -2,7 +2,7 @@
use std::collections::HashMap;
use serde_json::Value;
use crate::assemblage::Assemblage;
-use crate::componentwrapper::ComponentWrapper;
+use crate::componentwrapper::PreEntity;
use crate::template::Template;
#[derive(Default, Clone)]
@@ -20,7 +20,7 @@ impl Encyclopedia {
Ok(Encyclopedia{items})
}
- pub fn construct(&self, template: &Template) -> Result<Vec<ComponentWrapper>, &'static str> {
+ pub fn construct(&self, template: &Template) -> Result<PreEntity, &'static str> {
let assemblage = self.items.get(&template.name).ok_or("unknown assemblage name")?;
assemblage.instantiate(&template.args, &template.kwargs)
}
diff --git a/src/main.rs b/src/main.rs
index d2cfd13..322a13b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -23,6 +23,7 @@ mod componentparameter;
mod encyclopedia;
mod template;
mod roomtemplate;
+mod player;
pub use self::pos::Pos;
use self::gameserver::GameServer;
@@ -220,18 +221,6 @@ fn default_assemblages() -> Encyclopedia {
"height": ["float", 0.1]
}]
]
- },
- "player": {
- "arguments": [["name", "string", null]],
- "components": [
- ["Visible", {
- "sprite": ["string", "player"],
- "height": ["float", 1.0]
- }],
- ["Player", {
- "name": ["arg", "name"]
- }]
- ]
}
})).unwrap()
}
diff --git a/src/player.rs b/src/player.rs
new file mode 100644
index 0000000..142a6fe
--- /dev/null
+++ b/src/player.rs
@@ -0,0 +1,13 @@
+
+use crate::components::{Player, Visible};
+use crate::componentwrapper::{ComponentWrapper, PreEntity};
+
+pub fn make_player(name: &str) -> PreEntity {
+ vec![
+ ComponentWrapper::Visible(Visible {
+ sprite: "player".to_string(),
+ height: 1.0
+ }),
+ ComponentWrapper::Player(Player::new(name.to_string()))
+ ]
+}
diff --git a/src/resources.rs b/src/resources.rs
index 15d93c4..82c7787 100644
--- a/src/resources.rs
+++ b/src/resources.rs
@@ -4,10 +4,8 @@ use specs::Entity;
use super::pos::Pos;
use super::controls::Action;
-// use super::oldassemblage::Assemblage;
use super::worldmessages::WorldMessage;
-use super::template::Template;
-use crate::encyclopedia::Encyclopedia;
+use crate::componentwrapper::PreEntity;
#[derive(Default)]
@@ -38,6 +36,5 @@ pub struct Ground {
#[derive(Default)]
pub struct NewEntities {
- pub templates: Vec<(Pos, Template)>,
- pub encyclopedia: Encyclopedia
+ pub ents: Vec<(Pos, PreEntity)>
}
diff --git a/src/room.rs b/src/room.rs
index 84a1040..c179f61 100644
--- a/src/room.rs
+++ b/src/room.rs
@@ -28,22 +28,20 @@ use super::systems::{
};
use crate::encyclopedia::Encyclopedia;
use crate::roomtemplate::RoomTemplate;
+use crate::template::Template;
pub struct Room<'a, 'b> {
world: World,
- dispatcher: Dispatcher<'a, 'b>
+ dispatcher: Dispatcher<'a, 'b>,
+ encyclopedia: Encyclopedia
}
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", &[])
@@ -59,7 +57,8 @@ impl <'a, 'b>Room<'a, 'b> {
Room {
world,
- dispatcher
+ dispatcher,
+ encyclopedia
}
}
@@ -76,11 +75,19 @@ impl <'a, 'b>Room<'a, 'b> {
let y = (idx as i64) / width;
for template in templates {
- self.world.fetch_mut::<NewEntities>().templates.push((Pos{x, y}, template.clone()));
+ if let Err(msg) = self.add_entity(Pos{x, y}, template) {
+ println!("{}", msg);
+ }
}
}
}
+ pub fn add_entity(&mut self, pos: Pos, template: &Template) -> Result<(), &'static str>{
+ let pre_entity = self.encyclopedia.construct(template)?;
+ self.world.fetch_mut::<NewEntities>().ents.push((pos, pre_entity));
+ Ok(())
+ }
+
pub fn view(&self) -> HashMap<String, WorldMessage> {
self.world.fetch::<Output>().output.clone()
}
diff --git a/src/systems/controlinput.rs b/src/systems/controlinput.rs
index 0771b1e..712c98b 100644
--- a/src/systems/controlinput.rs
+++ b/src/systems/controlinput.rs
@@ -14,11 +14,7 @@ use specs::{
use crate::components::{Controller, Player, Removed};
use crate::controls::{Control, Action};
use crate::resources::{Input, NewEntities, Spawn};
-use crate::hashmap;
-use crate::template::Template;
-use crate::parameter::Parameter;
-// use crate::assemblages::Player;
pub struct ControlInput;
@@ -48,9 +44,9 @@ impl <'a> System<'a> for ControlInput {
for action in &input.actions {
match action {
Action::Join(name) => {
- new.templates.push((
+ new.ents.push((
spawn.pos,
- Template::new("player", hashmap!("name".to_string() => Parameter::String(name.to_string())))
+ crate::player::make_player(name)
));
}
Action::Leave(name) => {leaving.insert(name);}
diff --git a/src/systems/create.rs b/src/systems/create.rs
index 35ef747..054dc1f 100644
--- a/src/systems/create.rs
+++ b/src/systems/create.rs
@@ -34,18 +34,13 @@ impl <'a> System<'a> for Create {
new.remove(ent);
}
}
- for (pos, template) in &new_entities.templates {
+ for (pos, comps) in &new_entities.ents {
let mut builder = updater.create_entity(&entities);
- match new_entities.encyclopedia.construct(template) {
- Ok(comps) => {
- for comp in comps {
- builder = comp.build(builder);
- }
- builder.with(Position::new(*pos)).with(New).build();
- },
- Err(msg) => {println!("{}", msg);}
+ for comp in comps {
+ builder = comp.build(builder);
}
+ builder.with(Position::new(*pos)).with(New).build();
}
- new_entities.templates.clear();
+ new_entities.ents.clear();
}
}