summaryrefslogtreecommitdiff
path: root/src/room.rs
blob: 28abfa7ec6762febd4102019325a3e5b4606ef41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

use std::collections::HashMap;

use specs::{
	World,
	WorldExt,
	DispatcherBuilder,
	Dispatcher,
	Join
};

use super::controls::Action;
use super::worldmessages::WorldMessage;
use super::resources::{
	Size,
	Output,
	Input,
	NewEntities,
	Spawn
};
use super::systems::{
	moving::Move,
	registernew::RegisterNew,
	controlinput::ControlInput,
	view::View,
	remove::Remove,
	create::Create,
	take::Take
};
use crate::components::{Position, Serialise};
use crate::encyclopedia::Encyclopedia;
use crate::roomtemplate::RoomTemplate;
use crate::savestate::SaveState;
use crate::template::Template;
use crate::{Pos, PlayerId};



pub struct Room<'a, 'b> {
	world: World,
	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
		});
		world.register::<Serialise>();
		
		let mut dispatcher = DispatcherBuilder::new()
			.with(RegisterNew::default(), "registernew", &[])
			.with(ControlInput, "controlinput", &[])
			.with(Take, "take", &["controlinput"])
			.with(Move, "move", &["registernew", "controlinput"])
			.with(View::default(), "view", &["move"])
			.with(Create, "create", &["view", "controlinput"])
			.with(Remove, "remove", &["view", "move"])
			.build();
		
		dispatcher.setup(&mut world);
		
		
		Room {
			world,
			dispatcher
		}
	}
	
	pub fn load_from_template(&mut self, template: &RoomTemplate) {
	
		let (width, height) = template.size;
		self.world.fetch_mut::<Size>().width = width;
		self.world.fetch_mut::<Size>().height = height;
		
		self.world.fetch_mut::<Spawn>().pos = template.spawn;
		
		for (idx, templates) in template.field.iter().enumerate() {
			let x = (idx as i64) % width;
			let y = (idx as i64) / width;
			
			for template in templates {
				self.create_entity(template.clone().unsaved(), Pos{x, y});
			}
		}
	}
	
	pub fn view(&self) -> HashMap<PlayerId, WorldMessage> {
		self.world.fetch::<Output>().output.clone()
	}
	
	pub fn update(&mut self) {
		self.dispatcher.dispatch(&self.world);
		self.world.maintain();
	}
	
	pub fn set_input(&mut self, actions: Vec<Action>){
		self.world.fetch_mut::<Input>().actions = actions;
	}
	
	pub fn save(&self) -> SaveState {
		let positions = self.world.read_component::<Position>();
		let serialisers = self.world.write_component::<Serialise>();
		let mut state = SaveState::new();
		for (pos, serialiser) in (&positions, &serialisers).join() {
			state.changes.entry(pos.pos).or_insert(Vec::new()).push(serialiser.template.clone());
		}
		state
	}
	
	pub fn load_saved(&mut self, state: &SaveState) {
		for (pos, templates) in state.changes.iter() {
			for template in templates {
				self.create_entity(template.clone(), *pos);
			}
		}
	}
	
	fn create_entity(&mut self, template: Template, pos: Pos){
		self.world.fetch_mut::<NewEntities>().templates.push((pos, template));
	}
}