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
129
130
131
132
133
134
135
136
137
138
|
use crate::entities::{Character, Entity};
use crate::tiling::{Tile, TileGrid, TileType};
use crate::world::{apply_movement, Dungeon, Generatable, Level, Movement};
const PLAYER_SIGHT: usize = 1;
pub struct State {
pub player: Character,
dungeon: Dungeon,
level: usize,
grid: Option<TileGrid>,
}
impl State {
pub fn new(player: Character, dungeon: Dungeon) -> State {
State {
player,
dungeon,
level: 0,
grid: None,
}
}
pub fn init(&mut self) {
self.dungeon.generate();
self.switch_level(0);
self.player.place(self.current_level().start_point());
self.fog_of_war();
}
pub fn get_grid(&self) -> Option<&TileGrid> {
self.grid.as_ref()
}
pub fn get_player(&self) -> &Character {
&self.player
}
pub fn switch_level(&mut self, num_level: usize) {
self.level = num_level;
self.grid = Some(self.current_level().to_tilegrid().unwrap());
self.fog_of_war();
}
pub fn current_level(&self) -> &Level {
&self.dungeon.levels[self.level]
}
pub fn current_level_mut(&mut self) -> &mut Level {
&mut self.dungeon.levels[self.level]
}
fn can_step_on(tile: &Tile) -> bool {
match tile.get_type() {
TileType::Floor => true,
TileType::StairsDown => true,
TileType::StairsUp => true,
_ => false,
}
}
pub fn fog_of_war(&mut self) {
{
let grid = self.grid.as_mut().unwrap();
grid.clear_fog_of_war(self.player.location(), PLAYER_SIGHT);
}
for i in 0..self.current_level().entities.len() {
let loc = *self.current_level().entities[i].location();
if self
.grid
.as_ref()
.unwrap()
.block_at(loc.0, loc.1)
.is_visible()
&& !self.current_level().entities[i].is_visible()
{
self.current_level_mut().entities[i].visibility(true);
}
}
}
pub fn move_player(&mut self, dir: Movement) -> Result<(), String> {
let grid = match &self.grid {
Some(g) => g,
None => return Err(String::from("No level loaded!")),
};
let loc = apply_movement(*self.player.location(), dir)?;
// Is the new location colliding with anything?
if !State::can_step_on(grid.block_at(loc.0, loc.1)) {
return Err(String::from("Can't move entity!"));
}
let ret = self.player.move_by(dir);
self.fog_of_war();
ret
}
pub fn down_stairs(&mut self) -> Result<(), String> {
let grid = match &self.grid {
Some(g) => g,
None => return Err(String::from("No level loaded!")),
};
if self.level == self.dungeon.depth() - 1 {
return Err(String::from("Already at the bottom level"));
}
let loc = self.player.location();
match grid.block_at(loc.0, loc.1).get_type() {
TileType::StairsDown => {
self.switch_level(self.level + 1);
Ok(())
}
_ => Err(String::from("Not on stairs!")),
}
}
pub fn up_stairs(&mut self) -> Result<(), String> {
let grid = match &self.grid {
Some(g) => g,
None => return Err(String::from("No level loaded!")),
};
if self.level == 0 {
return Err(String::from("Already at the top level"));
}
let loc = self.player.location();
match grid.block_at(loc.0, loc.1).get_type() {
TileType::StairsUp => {
self.switch_level(self.level - 1);
Ok(())
}
_ => Err(String::from("Not on stairs!")),
}
}
}
|