summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-02-06 21:20:09 +0100
committertroido <troido@protonmail.com>2020-02-06 21:20:09 +0100
commit45ed2cd201c79fae1c61a4f6f2982a9f5cfbceca (patch)
tree01036ea1ebe1502cfe0645b283dd9595a8b94fae /src
parentc18970e8d1003a7b4b3b95b8ade07226bd235f0f (diff)
grass now also loaded from json
Diffstat (limited to 'src')
-rw-r--r--src/componentparameter.rs27
-rw-r--r--src/main.rs17
2 files changed, 37 insertions, 7 deletions
diff --git a/src/componentparameter.rs b/src/componentparameter.rs
index a2f7b70..491486a 100644
--- a/src/componentparameter.rs
+++ b/src/componentparameter.rs
@@ -1,12 +1,14 @@
use std::collections::HashMap;
+use rand::Rng;
use serde_json::Value;
use crate::parameter::{Parameter, ParameterType};
#[derive(Debug, PartialEq)]
pub enum ComponentParameter {
Constant(Parameter),
- Argument(String)
+ Argument(String),
+ Random(Vec<ComponentParameter>)
}
impl ComponentParameter {
@@ -18,6 +20,10 @@ impl ComponentParameter {
Self::Argument(argname) => {
Some(arguments.get(argname.as_str())?.clone())
}
+ Self::Random(options) => {
+ let r = rand::thread_rng().gen_range(0, options.len());
+ options[r].evaluate(arguments)
+ }
}
}
@@ -32,6 +38,14 @@ impl ComponentParameter {
let argname = paramvalue.as_str().ok_or("argument parameter not a string")?.to_string();
Ok(Self::Argument(argname))
},
+ "random" => {
+ let optionvalues = paramvalue.as_array().ok_or("random argument not a a string")?;
+ let mut options = Vec::new();
+ for option in optionvalues {
+ options.push(Self::from_json(option)?)
+ }
+ Ok(Self::Random(options))
+ },
_ => Err("unknown compparam type")
}
}
@@ -40,7 +54,16 @@ impl ComponentParameter {
pub fn get_type(&self, arguments: &Vec<(String, ParameterType, Option<Parameter>)>) -> Result<ParameterType, &'static str>{
Ok(match self {
Self::Constant(param) => param.paramtype(),
- Self::Argument(argname) => arguments.iter().find(|(n, _t, _d)| n == argname).ok_or("unknown argument name")?.1
+ Self::Argument(argname) => arguments.iter().find(|(n, _t, _d)| n == argname).ok_or("unknown argument name")?.1,
+ Self::Random(options) => {
+ let typ: ParameterType = options.get(0).ok_or("random has no options")?.get_type(arguments)?;
+ for param in options {
+ if param.get_type(arguments)? != typ {
+ return Err("inconsistent parameter types");
+ }
+ }
+ typ
+ }
})
}
}
diff --git a/src/main.rs b/src/main.rs
index 545245c..1dda2d6 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -69,6 +69,7 @@ fn gen_room<'a, 'b>(width: i32, height: i32) -> Room<'a, 'b> {
let mut room = Room::new((width, height));
let assemblages = default_assemblages();
let wall = assemblages["wall"].instantiate(Vec::new(), HashMap::new()).unwrap();
+ let grass = &assemblages["grass"];
for x in 0..width {
room.add_complist(&wall, (x, 0));
room.add_complist(&wall, (x, height - 1));
@@ -79,7 +80,7 @@ fn gen_room<'a, 'b>(width: i32, height: i32) -> Room<'a, 'b> {
}
for x in 1..width-1 {
for y in 1..height-1 {
- room.add_obj(&Grass::new(), (x, y));
+ room.add_complist(&grass.instantiate(Vec::new(), HashMap::new()).unwrap(), (x, y));
}
}
room
@@ -98,12 +99,18 @@ fn default_assemblages() -> HashMap<String, Template> {
]
},
"grass": {
- "arguments": [
- ["sprite", "string", "grass1"]
- ],
+ "arguments": [],
"components": [
["Visible", {
- "sprite": ["arg", "sprite"],
+ "sprite": ["random", [
+ ["string", "grass1"],
+ ["string", "grass2"],
+ ["string", "grass3"],
+ ["string", "grass1"],
+ ["string", "grass2"],
+ ["string", "grass3"],
+ ["string", "ground"]
+ ]],
"height": ["float", 0.1]
}]
]