diff options
| author | troido <troido@protonmail.com> | 2020-02-23 01:18:17 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-23 01:18:17 +0100 |
| commit | 7262cfc53b4af978d6db1b91e3143200f906587f (patch) | |
| tree | f6aa651a3040a1389a9c4e82796f1c04b3c5198c /src | |
| parent | 522aad7889cd62e96af7c420789507ccbf5b7aaa (diff) | |
rooms have named locations to be used by portals
Diffstat (limited to 'src')
| -rw-r--r-- | src/components/mod.rs | 15 | ||||
| -rw-r--r-- | src/componentwrapper.rs | 13 | ||||
| -rw-r--r-- | src/defaultencyclopedia.rs | 4 | ||||
| -rw-r--r-- | src/playerstate.rs | 14 | ||||
| -rw-r--r-- | src/resources/mod.rs | 5 | ||||
| -rw-r--r-- | src/room.rs | 19 | ||||
| -rw-r--r-- | src/roomtemplate.rs | 14 | ||||
| -rw-r--r-- | src/systems/migrate.rs | 2 | ||||
| -rw-r--r-- | src/world.rs | 11 |
9 files changed, 72 insertions, 25 deletions
diff --git a/src/components/mod.rs b/src/components/mod.rs index 459590b..682af65 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -11,9 +11,15 @@ use specs::{ Component }; -use crate::{Pos, PlayerId, RoomId, Sprite}; -use crate::controls::Control; -use crate::template::Template; +use crate::{ + Pos, + PlayerId, + RoomId, + Sprite, + controls::Control, + template::Template, + playerstate::RoomPos +}; #[derive(Debug, Clone)] pub struct Position{ @@ -100,5 +106,6 @@ pub struct Serialise { #[derive(Component, Debug, Clone)] pub struct RoomExit { - pub destination: RoomId + pub destination: RoomId, + pub dest_pos: RoomPos } diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index fb2a80a..27e0783 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use specs::Builder; -use crate::{PlayerId, RoomId, Sprite}; +use crate::{PlayerId, RoomId, Sprite, playerstate::RoomPos}; use crate::components::{Visible, Blocking, Player, Floor, Item, Inventory, Health, Serialise, RoomExit}; use crate::parameter::{Parameter, ParameterType}; @@ -98,7 +98,16 @@ components!( Inventory (capacity: Int) {Inventory{items: Vec::new(), capacity: capacity as usize}}; Health (health: Int, maxhealth: Int) {Health{health, maxhealth}}; Serialise (template: Template) {Serialise{template}}; - RoomExit (destination: String) {RoomExit{destination: RoomId::from_str(&destination)}}; + RoomExit (destination: String, dest_pos: String) { + RoomExit { + destination: RoomId::from_str(&destination), + dest_pos: if dest_pos.is_empty() { + RoomPos::Unknown + } else { + RoomPos::Name(dest_pos) + } + } + }; ); diff --git a/src/defaultencyclopedia.rs b/src/defaultencyclopedia.rs index 260c5f7..7179989 100644 --- a/src/defaultencyclopedia.rs +++ b/src/defaultencyclopedia.rs @@ -112,9 +112,9 @@ pub fn default_encyclopedia() -> Encyclopedia { ] }, "portal": { - "arguments": [["destination", "string", null]], + "arguments": [["destination", "string", null], ["dest_pos", "string", ""]], "components": [ - ["RoomExit", {"destination": ["arg", "destination"]}], + ["RoomExit", {"destination": ["arg", "destination"], "dest_pos": ["arg", "dest_pos"]}], "Floor" ] }, diff --git a/src/playerstate.rs b/src/playerstate.rs index ff9565b..8e3d99f 100644 --- a/src/playerstate.rs +++ b/src/playerstate.rs @@ -10,25 +10,35 @@ use crate::{ Result, aerr, Sprite, - Encyclopedia + Encyclopedia, + Pos }; #[derive(Debug, Clone)] pub struct PlayerState { pub id: PlayerId, pub room: Option<RoomId>, + pub pos: RoomPos, pub inventory_capacity: usize, pub inventory: Vec<Template>, pub health: i64, pub maximum_health: i64 } +#[derive(Debug, Clone)] +pub enum RoomPos { + Pos(Pos), + Name(String), + Unknown +} + impl PlayerState { pub fn new(id: PlayerId) -> Self { Self{ id, room: None, + pos: RoomPos::Unknown, inventory: Vec::new(), inventory_capacity: 10, health: 9, @@ -40,6 +50,7 @@ impl PlayerState { Self { id, room: Some(room), + pos: RoomPos::Unknown, inventory, health, inventory_capacity, @@ -79,6 +90,7 @@ impl PlayerState { Value::String(name) => Some(RoomId::from_str(name)), _ => None }, + pos: RoomPos::Unknown, inventory: items, health: val.get("health").ok_or(aerr!("player json does not have health"))?.as_i64().ok_or(aerr!("player health not a number"))?, inventory_capacity: inventory.get("capacity").ok_or(aerr!("inventory does no have capacity"))?.as_i64().ok_or(aerr!("inventory capacity not a number"))? as usize, diff --git a/src/resources/mod.rs b/src/resources/mod.rs index 0e9a1e3..5524298 100644 --- a/src/resources/mod.rs +++ b/src/resources/mod.rs @@ -12,7 +12,8 @@ use crate::{ RoomId, util::Result, template::Template, - components::Visible + components::Visible, + playerstate::RoomPos }; @@ -87,6 +88,6 @@ pub struct Players { #[derive(Default)] pub struct Emigration { - pub emigrants: Vec<(PlayerId, RoomId)> + pub emigrants: Vec<(PlayerId, RoomId, RoomPos)> } diff --git a/src/room.rs b/src/room.rs index 2d48363..dd5c736 100644 --- a/src/room.rs +++ b/src/room.rs @@ -46,7 +46,7 @@ use crate::encyclopedia::Encyclopedia; use crate::roomtemplate::RoomTemplate; use crate::savestate::SaveState; use crate::template::Template; -use crate::playerstate::PlayerState; +use crate::playerstate::{PlayerState, RoomPos}; use crate::{Pos, PlayerId, RoomId, aerr}; use crate::util::Result; @@ -55,7 +55,8 @@ use crate::util::Result; pub struct Room<'a, 'b> { world: World, dispatcher: Dispatcher<'a, 'b>, - pub id: RoomId + pub id: RoomId, + places: HashMap<String, Pos> } impl <'a, 'b>Room<'a, 'b> { @@ -86,7 +87,8 @@ impl <'a, 'b>Room<'a, 'b> { Room { world, dispatcher, - id + id, + places: HashMap::new() } } @@ -106,6 +108,9 @@ impl <'a, 'b>Room<'a, 'b> { let _ = self.create_entity(template.clone().unsaved(), Pos{x, y}); } } + for (name, place) in &template.places { + self.places.insert(name.clone(), *place); + } } @@ -131,7 +136,11 @@ impl <'a, 'b>Room<'a, 'b> { pub fn add_player(&mut self, state: &PlayerState){ let pre_player = state.construct(&self.world.fetch::<NewEntities>().encyclopedia); - let spawn = self.world.fetch::<Spawn>().pos; + let spawn = match &state.pos { + RoomPos::Unknown => self.world.fetch::<Spawn>().pos, + RoomPos::Pos(pos) => *pos, + RoomPos::Name(name) => *self.places.get(name).unwrap() + }; let mut builder = self.world.create_entity(); let ent = builder.entity; for comp in pre_player { @@ -207,7 +216,7 @@ impl <'a, 'b>Room<'a, 'b> { Ok(()) } - pub fn emigrate(&mut self) -> Vec<(PlayerId, RoomId)> { + pub fn emigrate(&mut self) -> Vec<(PlayerId, RoomId, RoomPos)> { let emigrants = self.world.remove::<Emigration>().expect("World does not have Emigrating resource").emigrants; self.world.insert(Emigration::default()); emigrants diff --git a/src/roomtemplate.rs b/src/roomtemplate.rs index 49dd7af..84110b9 100644 --- a/src/roomtemplate.rs +++ b/src/roomtemplate.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use serde_json::Value; +use serde_json::{json, Value}; use crate::Pos; use crate::template::Template; use crate::{Result, aerr}; @@ -8,7 +8,8 @@ use crate::{Result, aerr}; pub struct RoomTemplate { pub size: (i64, i64), pub spawn: Pos, - pub field: Vec<Vec<Template>> + pub field: Vec<Vec<Template>>, + pub places: HashMap<String, Pos> } impl RoomTemplate { @@ -42,10 +43,17 @@ impl RoomTemplate { field[x + y * (size.0 as usize)] = mapping.get(&ch).ok_or(aerr!("char not found in mapping"))?.clone(); } } + + let mut places = HashMap::new(); + for (name, jsonpos) in jsonroom.get("places").unwrap_or(&json!({})).as_object().ok_or("places not an object")? { + places.insert(name.to_string(), Pos::from_json(jsonpos).ok_or("pos of places invalid")?); + } + Ok(RoomTemplate { size, spawn, - field + field, + places }) } } diff --git a/src/systems/migrate.rs b/src/systems/migrate.rs index aecffd0..80690f3 100644 --- a/src/systems/migrate.rs +++ b/src/systems/migrate.rs @@ -27,7 +27,7 @@ impl <'a> System<'a> for Migrate { for (player, position, _moved) in (&players, &positions, &moved).join() { for ent in ground.cells.get(&position.pos).unwrap() { if let Some(exit) = exits.get(*ent) { - emigration.emigrants.push((player.id.clone(), exit.destination.clone())); + emigration.emigrants.push((player.id.clone(), exit.destination.clone(), exit.dest_pos.clone())); break; } } diff --git a/src/world.rs b/src/world.rs index 05e6887..dad3f13 100644 --- a/src/world.rs +++ b/src/world.rs @@ -7,12 +7,12 @@ use crate::{ room::Room, worldloader::WorldLoader, persistence::PersistentStorage, - playerstate::PlayerState, + playerstate::{PlayerState, RoomPos}, encyclopedia::Encyclopedia, controls::Control, util::Result, aerr, - worldmessages::WorldMessage + worldmessages::WorldMessage, }; pub struct World<'a, 'b> { @@ -81,9 +81,10 @@ impl <'a, 'b>World<'a, 'b> { Ok(self.get_room_mut(&roomid)?.control_player(player, control)) } - fn migrate_player(&mut self, player: &PlayerId, destination: RoomId) -> Result<()> { + fn migrate_player(&mut self, player: &PlayerId, destination: RoomId, roompos: RoomPos) -> Result<()> { let mut state = self.discorporate_player(player)?; state.room = Some(destination); + state.pos = roompos; self.add_loaded_player(state) } @@ -100,8 +101,8 @@ impl <'a, 'b>World<'a, 'b> { for room in self.rooms.values_mut() { migrants.append(&mut room.emigrate()); } - for (player, destination) in migrants { - self.migrate_player(&player, destination).unwrap(); + for (player, destination, roompos) in migrants { + self.migrate_player(&player, destination, roompos).unwrap(); } } |
