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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
use crossterm::cursor::MoveTo;
use crossterm::{execute, queue, Output};
use std::io::{stdout, Write};
use crate::entities::{Character, Entity, Player};
use crate::tiling::{tile_to_str, Tile, TileGrid, TileType};
use crate::world::{apply_movement, Dungeon, Generatable, Level, Movement};
const PLAYER_SIGHT: usize = 3;
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.clear_los()
}
pub fn switch_level(&mut self, num_level: usize) {
self.level = num_level;
self.grid = Some(self.current_level().to_tilegrid().unwrap());
}
pub fn render_level(&self) {
let mut sout = stdout();
execute!(sout, MoveTo(0, 0)).unwrap();
for (linenum, line) in self.grid.as_ref().unwrap().raw_data().iter().enumerate() {
let linestr = line.iter().map(tile_to_str).collect::<Vec<&str>>();
let mut linestr2 = String::from("");
for chr in linestr {
linestr2.push_str(chr);
}
queue!(sout, Output(linestr2), MoveTo(0, linenum as u16)).unwrap();
sout.flush().unwrap();
}
}
fn render_entity(&self, entity: &dyn Entity) {
if !entity.is_visible() || !entity.is_dirty() {
return;
}
let dirt = entity.previous_location();
let background = self.grid.as_ref().unwrap().block_at(dirt.0, dirt.1);
let mut sout = stdout();
queue!(
sout,
MoveTo(dirt.0 as u16, dirt.1 as u16),
Output(tile_to_str(background)),
MoveTo(entity.location().0 as u16, entity.location().1 as u16),
Output(tile_to_str(entity.tile()))
)
.unwrap();
sout.flush().unwrap();
}
pub fn render_entities(&self) {
for e in self.current_level().entities.iter() {
self.render_entity(&**e);
}
}
pub fn render_player(&mut self) {
self.render_entity(&self.player);
}
fn ui_state_position(&self) -> MoveTo {
MoveTo(0, (self.dungeon.ysize()) as u16)
}
fn ui_notification_position(&self) -> MoveTo {
MoveTo(0, (self.dungeon.ysize() + 1) as u16)
}
pub fn render_ui(&self) {
let mut sout = stdout();
queue!(sout, self.ui_state_position(), Output(self.player.stats())).unwrap();
sout.flush().unwrap();
}
pub fn notify(&self, message: String) {
let mut sout = stdout();
queue!(
sout,
self.ui_notification_position(),
Output(" ".repeat(self.dungeon.xsize())),
self.ui_notification_position(),
Output(message)
)
.unwrap();
sout.flush().unwrap();
}
pub fn ui_help(&self) {
self.notify(String::from(
"quit: q, movement{up(k), down(j), left(h), right(l)}",
))
}
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,
}
}
fn clear_los(&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!"));
}
self.player.move_by(dir)?;
self.clear_los();
Ok(())
}
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);
self.render_level();
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);
self.render_level();
Ok(())
}
_ => Err(String::from("Not on stairs!")),
}
}
}
|