summaryrefslogtreecommitdiff
path: root/src/room.rs
blob: 8341eba0b4e6ef937257f6db1eb05f61f6551407 (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

use std::collections::HashMap;

use specs::{
	World,
	WorldExt,
	Builder,
	DispatcherBuilder,
	Dispatcher,
	Entity
};

use super::controls::Action;
use super::pos::Pos;
use super::components::Position;
use super::assemblages::Assemblage;
use super::worldmessages::WorldMessage;
use super::resources::{
	Size,
	Output,
	Input,
	NewEntities
};
use super::systems::{
	moving::Move,
	clearcontrols::ClearControllers,
	makefloor::MakeFloor,
	controlinput::ControlInput,
	view::View
};



pub struct Room<'a, 'b> {
	world: World,
	dispatcher: Dispatcher<'a, 'b>
}

impl <'a, 'b>Room<'a, 'b> {

	pub fn new(size: (i32, i32)) -> Room<'a, 'b> {
		let (width, height) = size;
		let mut world = World::new();
		world.insert(Size{width, height});
		world.insert(Input{actions: Vec::new()});
		world.insert(Output{output: HashMap::new()});
		
		let mut dispatcher = DispatcherBuilder::new()
			.with(ControlInput, "controlinput", &[])
			.with(MakeFloor, "makefloor", &[])
			.with(Move, "move", &["makefloor", "controlinput"])
			.with(ClearControllers, "clearcontrollers", &["move"])
			.with(View::default(), "view", &["move"])
			.build();
		
		dispatcher.setup(&mut world);
		
		Room {
			world,
			dispatcher
		}
	}
	
	pub fn view(&self) -> HashMap<String, WorldMessage> {
		self.world.fetch::<Output>().output.clone()
	}
	
	pub fn update(&mut self) {
		self.dispatcher.dispatch(&mut self.world);
		let assemblages = self.world.remove::<NewEntities>().unwrap_or(NewEntities{assemblages: Vec::new()}).assemblages;
		self.world.insert(NewEntities{assemblages: Vec::new()});
		for (pos, assemblage) in assemblages{
			assemblage.build(self.world.create_entity()).with(Position::new(pos)).build();
		}
		self.world.maintain();
	}
	
	pub fn get_size(&self) -> (i32, i32) {
		let Size{width, height} = *self.world.fetch::<Size>();
		(width, height)
	}
	
	pub fn set_input(&mut self, actions: Vec<Action>){
		self.world.fetch_mut::<Input>().actions = actions;
	}
	
	pub fn add_obj(&mut self, template: &dyn Assemblage, (x, y): (i32, i32)) -> Entity {
		template.build(self.world.create_entity()).with(Position::new(Pos{x, y})).build()
	}
}