diff options
| author | troido <troido@protonmail.com> | 2020-04-02 15:10:53 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-04-02 15:10:53 +0200 |
| commit | 1e8fe74740fc4fb11b660415b2db182c6a63a3c2 (patch) | |
| tree | df077b33f0aa5da69d3377672589838dee604356 | |
| parent | 4f9932074a8f0390d5cb6072b4e419c7ab08ffed (diff) | |
short value for flags; radish plants now have occupied flag
| -rw-r--r-- | content/encyclopediae/default_encyclopedia.json | 63 | ||||
| -rw-r--r-- | src/assemblage.rs | 76 |
2 files changed, 81 insertions, 58 deletions
diff --git a/content/encyclopediae/default_encyclopedia.json b/content/encyclopediae/default_encyclopedia.json index 46bb64e..9b625b9 100644 --- a/content/encyclopediae/default_encyclopedia.json +++ b/content/encyclopediae/default_encyclopedia.json @@ -1,24 +1,24 @@ { "assemblages": { "wall": { - "components": [["Flags", {"flags": ["strings", ["Blocking"]]}]], "sprite": "wall", - "height": 2 + "height": 2, + "flags": ["Blocking"] }, "rock": { - "components": [["Flags", {"flags": ["strings", ["Blocking"]]}]], "sprite": "rock", - "height": 10 + "height": 10, + "flags": ["Blocking"] }, "tree": { - "components": [["Flags", {"flags": ["strings", ["Blocking"]]}]], "sprite": "tree", - "height": 3 + "height": 3, + "flags": ["Blocking"] }, "fence": { - "components": [["Flags", {"flags": ["strings", ["Blocking"]]}]], "sprite": "fence", - "height": 1 + "height": 1, + "flags": ["Blocking"] }, "grass": { "components": [ @@ -34,9 +34,9 @@ ]], "height": ["float", 0.1], "name": ["string", "grass"] - }], - ["Flags", {"flags": ["strings", ["Floor", "Soil"]]}] - ] + }] + ], + "flags": ["Floor", "Soil"] }, "greengrass": { "components": [ @@ -48,26 +48,24 @@ ]], "height": ["float", 0.1], "name": ["string", "grass"] - }], - ["Flags", {"flags": ["strings", ["Floor", "Soil"]]}] - ] + }] + ], + "flags": ["Floor", "Soil"] }, "ground": { - "components": [["Flags", {"flags": ["strings", ["Floor", "Soil"]]}]], "sprite": "ground", - "height": 0.1 + "height": 0.1, + "flags": ["Floor", "Soil"] }, "floor": { - "components": [["Flags", {"flags": ["strings", ["Floor"]]}]], "sprite": "floor", - "height": 0.1 + "height": 0.1, + "flags": ["Floor"] }, "bridge": { - "components": [ - ["Flags", {"flags": ["strings", ["Floor"]]}] - ], "sprite": "bridge", - "height": 0.1 + "height": 0.1, + "flags": ["Floor"] }, "water": { "components": [], @@ -108,20 +106,20 @@ "portal": { "arguments": [["destination", "string"], ["dest_pos", "string", ""]], "components": [ - ["RoomExit", {"destination": ["arg", "destination"], "dest_pos": ["arg", "dest_pos"]}], - ["Flags", {"flags": ["strings", ["Floor"]]}] - ] + ["RoomExit", {"destination": ["arg", "destination"], "dest_pos": ["arg", "dest_pos"]}] + ], + "flags": ["Floor"] }, "builtwall": { "arguments": [["health", "int", 100]], "components": [ - ["Flags", {"flags": ["strings", ["Blocking"]]}], ["Health", {"health": ["arg", "health"], "maxhealth": ["int", 100]}], "Mortal" ], "sprite": "wall", "height": 2, - "extract": {"health": ["Health", "health"]} + "extract": {"health": ["Health", "health"]}, + "flags": ["Blocking"] }, "spiketrap": { "components": [["Trap", {"damage": ["int", 8]}]], @@ -187,13 +185,14 @@ ["Interactable", {"action": ["string", "harvest"]}], "Mortal", ["Loot", {"loot": ["lootlist", [["radishseed", 0.92], ["radishseed", 0.20], ["radishes", 0.8], ["radishes", 0.4]]]}] - ] + ], + "flags": ["Occupied"] }, "radishseed": { "sprite": "seed", "height": 0.2, "name": "radishseed", - "item": ["build", ["plantedradishseed", ["Floor", "Soil"], ["Occupied"]]] + "item": ["build", ["plantedradishseed", ["Floor", "Soil"], ["Occupied", "Blocking"]]] }, "plantedradishseed": { "arguments": [["target_time", "int", 0]], @@ -209,7 +208,8 @@ ], "extract": { "target_time": ["Grow", "target_time"] - } + }, + "flags": ["Occupied"] }, "radishseedling": { "arguments": [["target_time", "int", 0]], @@ -225,7 +225,8 @@ ], "extract": { "target_time": ["Grow", "target_time"] - } + }, + "flags": ["Occupied"] }, "radishes": { "sprite": "food", diff --git a/src/assemblage.rs b/src/assemblage.rs index ffb1420..95988cc 100644 --- a/src/assemblage.rs +++ b/src/assemblage.rs @@ -80,41 +80,20 @@ impl Assemblage { Ok(()) } - pub fn from_json(val: &Value) -> Result<Self>{ - let mut assemblage = Self { - arguments: Self::parse_definition_arguments(val.get("arguments").unwrap_or(&json!([])))?, - components: Self::parse_definition_components(val.get("components").unwrap_or(&json!([])))?, - save: val.get("save").unwrap_or(&json!(true)).as_bool().ok_or(aerr!("assemblage save not a bool"))?, - extract: val - .get("extract") - .unwrap_or(&json!({})) - .as_object().ok_or(aerr!("assemblage extract not a bool"))? - .into_iter() - .map(|(argname, val)| { - Ok(( - argname.to_string(), - ComponentType::from_str( - val - .get(0).ok_or(aerr!("index 0 not in extract value"))? - .as_str().ok_or(aerr!("extract component name not a string"))? - ).ok_or(aerr!("extract invalid component name"))?, - val.get(1) - .ok_or(aerr!("index 1 not in extract value"))? - .as_str().ok_or(aerr!("extract member name not a string"))?.to_string() - )) - }) - .collect::<Result<Vec<(String, ComponentType, String)>>>()? - }; + fn common_short_definitions(val: &Value) -> Result<Vec<(ComponentType, HashMap<String, ComponentParameter>)>> { + let mut components = Vec::new(); + let name = if let Some(nameval) = val.get("name") { Some(nameval.as_str().ok_or(aerr!("name not a string"))?.to_string()) } else {None}; + // visible component is so common that shortcuts are very helpful if let Some(spritename) = val.get("sprite") { let sprite = spritename.as_str().ok_or(aerr!("sprite not a string"))?.to_string(); let height = val .get("height").ok_or(aerr!("defining a sprite requires also defining a height"))? .as_f64().ok_or(aerr!("height not a float"))?; - assemblage.components.push(( + components.push(( ComponentType::Visible, hashmap!( "name".to_string() => ComponentParameter::Constant( @@ -129,9 +108,10 @@ impl Assemblage { ) )); } + // item component is common too if let Some(action) = val.get("item") { - assemblage.components.push(( + components.push(( ComponentType::Item, hashmap!( "ent".to_string() => ComponentParameter::TemplateSelf, @@ -146,6 +126,48 @@ impl Assemblage { ) )); } + + // and so is flags + if let Some(flags) = val.get("flags") { + components.push(( + ComponentType::Flags, + hashmap!( + "flags".to_string() => ComponentParameter::Constant( + Parameter::from_typed_json(ParameterType::Strings, flags).ok_or(aerr!("failed to parse flags"))? + ) + ) + )); + } + + Ok(components) + } + + pub fn from_json(val: &Value) -> Result<Self>{ + let mut assemblage = Self { + arguments: Self::parse_definition_arguments(val.get("arguments").unwrap_or(&json!([])))?, + components: Self::parse_definition_components(val.get("components").unwrap_or(&json!([])))?, + save: val.get("save").unwrap_or(&json!(true)).as_bool().ok_or(aerr!("assemblage save not a bool"))?, + extract: val + .get("extract") + .unwrap_or(&json!({})) + .as_object().ok_or(aerr!("assemblage extract not a bool"))? + .into_iter() + .map(|(argname, val)| { + Ok(( + argname.to_string(), + ComponentType::from_str( + val + .get(0).ok_or(aerr!("index 0 not in extract value"))? + .as_str().ok_or(aerr!("extract component name not a string"))? + ).ok_or(aerr!("extract invalid component name"))?, + val.get(1) + .ok_or(aerr!("index 1 not in extract value"))? + .as_str().ok_or(aerr!("extract member name not a string"))?.to_string() + )) + }) + .collect::<Result<Vec<(String, ComponentType, String)>>>()? + }; + assemblage.components.append(&mut Self::common_short_definitions(val)?); assemblage.validate()?; Ok(assemblage) } |
