-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.rs
261 lines (221 loc) · 6.04 KB
/
engine.rs
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
use prelude::*;
use peripherals::*;
use assets::*;
pub struct Engine<P: Peripherals> {
pub peripherals: P,
state: State,
}
impl<P: Peripherals> Engine<P> {
pub fn new(peripherals: P) -> Engine<P> {
let mut player = Player::new();
Engine{
peripherals: peripherals,
state: State::Playing{ level_state: LevelState::new(&mut player) },
}
}
pub fn step(&mut self) {
clear_screen(&mut self.peripherals);
match &self.state {
State::GameOver{ score } => {
},
State::Playing{ mut level_state } => {
self.state = level_state.step(self);
}
}
}
fn read_button(&mut self) -> bool {
self.peripherals.get_button()
}
}
#[derive(Debug, Clone, Copy)]
enum Dir {
Up,
Down,
Left,
Right,
}
fn fill_screen(p: &mut impl Peripherals, value: u8) {
for x in 0..SCREEN_WIDTH {
for stripe in 0..SCREEN_HEIGHT / 8 {
p.set_stripe(x, stripe, value)
}
}
}
fn clear_screen(p: &mut impl Peripherals) {
fill_screen(p, 0x00)
}
fn draw_sprite(p: &mut impl Peripherals, sprite: ProgMem<[u8]>, pos: (u8, u8)) {
let (x0, y0) = pos;
for i in 0..sprite.len() {
let dx = i as u8;
let mut col = sprite.load_at(i);
for dy in 0..8 {
p.set_pixel(x0 + dx, y0 + dy, col & 1 != 0);
col >>= 1;
}
}
}
#[derive(Clone, Copy)]
struct Entity {
pos: (u8, u8),
d: Dir,
}
fn decode_pos(pos: (u8, u8)) -> ((u8, u8), (u8, u8)) {
let (x, y) = pos;
let xblock = (x - BLOCK_X_START) / BLOCK_WIDTH;
let xoff = (x - BLOCK_X_START) % BLOCK_WIDTH;
let yblock = (y - BLOCK_Y_START) / BLOCK_HEIGHT;
let yoff = (y - BLOCK_Y_START) % BLOCK_HEIGHT;
((xblock, yblock), (xoff, yoff))
}
fn apply_move(pos: (u8, u8), d: (i8, i8)) -> (u8, u8) {
let (x, y) = pos;
let (dx, dy) = d;
(x.saturating_add_signed(dx), y.saturating_add_signed(dy))
}
fn blocked_by_walls(level: &LevelState, e: Entity) -> Option<(u8, u8)> {
let Entity{pos: pos@(_, y), .. } = e;
let ((xblock, yblock), (xoff, yoff)) = decode_pos(pos);
let (dx, dy) = (-1, 0);
let blocked =
if (xoff != 1 && dy != 0) || (yoff != 1 && dx != 0) {
// Middle of a block
true
} else if dx != 0 && xoff == 1 {
// Check vertical walls when moving horizontally
let row = level.walls.vertical_walls[yblock as usize];
if dx < 0 {
if xblock == 0 {
if yblock == 2 && level.portals_open {
return Some((BLOCK_X_END - BLOCK_WIDTH + 1, y));
} else {
true
}
} else {
// Wall to the left
row & (1 << (xblock - 1)) != 0
}
} else {
// Wall to the right
row & (1 << xblock) != 0
}
} else {
false
};
if !blocked {
Some(apply_move(e.pos, (dx, dy)))
} else {
None
}
}
fn move_bullet(level: &LevelState, bullet: &mut Option<Entity>) {
if let Some(e) = bullet {
match blocked_by_walls(level, *e) {
Some(pos) => { e.pos = pos },
None => { *bullet = None },
}
}
}
enum State {
Playing {
level_state: LevelState,
},
GameOver {
score: u16,
},
}
#[derive(Clone, Copy)]
struct LevelState {
walls: Walls,
player: Player,
monsters: [Option<BasicMonster>; 8],
portals_open: bool,
}
impl LevelState {
fn new(player: &mut Player) -> Self {
let layout = LEVEL.load();
LevelState {
walls: layout.into(),
portals_open: true,
player: *player,
monsters: [None; 8],
}
}
fn draw<P: Peripherals>(&mut self, engine: &mut Engine<P>) {
self.player.draw(&mut engine.peripherals);
for monster in self.monsters.iter() {
if let Some(monster) = monster {
monster.draw(&mut engine.peripherals);
}
}
}
fn step<P: Peripherals>(&mut self, engine: &mut Engine<P>) -> State {
self.draw(engine);
let pressed = engine.read_button();
let mut player = self.player;
player.action(&self, pressed);
let mut monsters = self.monsters;
let mut monster_count: u8 = 0;
for monster_slot in monsters.iter_mut() {
if monster_slot.is_some() {
monster_count += 1;
}
}
self.player = player;
self.monsters = monsters;
if self.player.lives == u8::MAX {
return State::GameOver{ score: 0 };
}
if monster_count == 0 {
State::Playing{ level_state: Self::new(&mut self.player) }
} else {
State::Playing{ level_state: *self }
}
}
}
#[derive(Clone, Copy)]
struct BasicMonster {
bullet: Option<Entity>,
}
impl BasicMonster {
fn draw(&self, p: &mut impl Peripherals) {
if let Some(ref e) = self.bullet {
draw_sprite(p, MONSTER_BULLET, e.pos);
}
}
}
#[derive(Clone, Copy)]
struct Player {
e: Entity,
lives: u8,
#[cfg(feature = "score")] score: u16,
bullet: Option<Entity>,
}
impl Player {
fn new() -> Player {
Player {
e: Self::start_pos(),
lives: 2,
#[cfg(feature = "score")] score: 0,
bullet: None,
}
}
fn start_pos() -> Entity {
Entity {
pos: (40, 40),
d: Dir::Left,
}
}
fn draw(&self, p: &mut impl Peripherals) {
let Player{ bullet, .. } = self;
if let Some(ref e) = bullet {
draw_sprite(p, PLAYER_BULLET, e.pos);
}
}
fn action(&mut self, level: &LevelState, pressed: bool) {
move_bullet(level, &mut self.bullet);
if pressed && self.bullet.is_none() {
self.bullet = Some(self.e);
}
}
}