generated from 32blit/32blit-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
293 lines (240 loc) · 6.03 KB
/
Player.cpp
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Player.cpp - part of SokoBlit
*
* Copyright (c) 2021 Pete Favelle / fsqaured limited <[email protected]>
*
* The Player class handles the player on an individual level. This is where
* we work out things like animations, position and suchlike.
*
* This software is distributed under the MIT License. See LICENSE for details.
*/
/* System headers. */
#include <cstring>
/* Local headers. */
#include "32blit.hpp"
#include "sokoblit.hpp"
#include "Player.hpp"
#include "assets_font.hpp"
/* Functions. */
/*
* Player - constructor, basic initialisation. Takes the co-ordinates of the
* starting location, in tiles.
*/
Player::Player( uint16_t p_x, uint16_t p_y )
{
/* Save the location we've been given. */
c_location.x = p_x;
c_location.y = p_y;
/* Load the font we use to count progress. */
c_font = new blit::Font( a_font );
/* And set some defaults. */
c_direction = DIR_DOWN;
c_animation = 0;
c_steps = 0;
c_blocked = false;
c_pushing = false;
/* All done! */
return;
}
/*
* ~Player - destructor, just tidy up what we allocated.
*/
Player::~Player( void )
{
/* All done. */
return;
}
/*
* moving - returns a boolean flag indicating if the player is in motion.
*/
bool Player::moving( void )
{
/* Pretty simple access method. */
return c_steps > 0;
}
/*
* pushing - returns a boolean flag indicating if the player is shoving a crate.
*/
bool Player::pushing( void )
{
/* Pretty simple access method. */
return c_pushing;
}
/*
* direction - which way is the player facing?
*/
direction_t Player::facing( void )
{
return c_direction;
}
/*
* render - draws the player onto the screen; assumes that the screen has an
* appropriate spritesheet with sprites in the right place!
*/
void Player::render( void )
{
blit::Rect l_sprite = blit::Rect( 0, 4, 2, 2 );
blit::Point l_location = c_location * 8;
blit::Point l_crate_loc;
char l_buffer[32];
/* Work out the correct rectangle to blit, based on the direction. Also the */
/* precise location is offset if we're still moving. */
switch( c_direction )
{
case DIR_DOWN:
if ( !c_blocked )
{
l_location.y -= c_steps;
}
l_crate_loc = l_location + blit::Point( 0, 16 );
break;
case DIR_LEFT:
l_sprite.x = 6;
l_sprite.y = 6;
if ( !c_blocked )
{
l_location.x += c_steps;
}
l_crate_loc = l_location - blit::Point( 16, 0 );
break;
case DIR_UP:
l_sprite.y = 6;
if ( !c_blocked )
{
l_location.y += c_steps;
}
l_crate_loc = l_location - blit::Point( 0, 16 );
break;
case DIR_RIGHT:
l_sprite.x = 6;
if ( !c_blocked )
{
l_location.x -= c_steps;
}
l_crate_loc = l_location + blit::Point( 16, 0 );
break;
}
/* Now the animation steps, which are just along the X axis. */
l_sprite.x += ( ( c_steps % 3 ) * 2 );
/* And just send the right sprite to the right location. */
blit::screen.sprite( l_sprite, l_location );
/* And the crate, if we're pushing that. */
if ( c_pushing )
{
blit::screen.sprite( blit::Rect( 4, 0, 2, 2 ), l_crate_loc );
}
/* Lastly, write the current time and number of moves to the top. */
blit::screen.pen = blit::Pen( 154, 235, 0, 255 );
snprintf( l_buffer, 30, "Time:%02d:%02d.%d",
c_deciseconds / 600,
( c_deciseconds % 600 ) / 10,
( c_deciseconds % 10 ) );
blit::screen.text( l_buffer, *c_font, blit::Point( blit::screen.bounds.w -1, 1 ),
true, blit::TextAlign::top_right );
snprintf( l_buffer, 30, "Moves:%d", c_moves );
blit::screen.text( l_buffer, *c_font, blit::Point( 1, 1 ),
true, blit::TextAlign::top_left );
/* All done. */
return;
}
/*
* update - called every tick to keep the animation running and location
* changing when the player is moving.
*/
void Player::update( void )
{
static uint8_t l_delay = 2;
static uint8_t l_count = 10;
/* Horrible hack here to keep time, in tenths of seconds, without getting */
/* into complications about whether or not we're active. */
if ( l_count > 0 )
{
l_count--;
}
else
{
c_deciseconds++;
l_count = 10;
}
if ( l_delay > 0 )
{
l_delay--;
return;
}
l_delay = 2;
/* Only need to do stuff if the steps are there. */
if ( c_steps > 0 )
{
/* Just decrement the steps. */
c_steps -= 2;
}
/* If we've reached the end of the movement, clear the blocked flag. */
if ( 0 == c_steps )
{
c_blocked = false;
c_pushing = false;
}
/* That's it, all done. */
return;
}
/*
* location - returns the (tile-based) location of the player.
*/
blit::Point Player::location( void )
{
/* Simple access method. */
return c_location;
}
/*
* move - starts the player moving in the direction specified. If the blocked
* flag is true, we run the animation but simply don't move.
*/
void Player::move( direction_t p_direction, bool p_blocked, bool p_pushing )
{
/* Check that the new location makes sense, and switch to it. */
if ( !p_blocked )
{
/* Sanity check that we're within the board. */
switch( p_direction )
{
case DIR_DOWN:
if ( c_location.y < 28 )
{
c_location.y += 2;
}
else return;
break;
case DIR_LEFT:
if ( c_location.x > 1 )
{
c_location.x -= 2;
}
else return;
break;
case DIR_UP:
if ( c_location.y > 1 )
{
c_location.y -= 2;
}
else return;
break;
case DIR_RIGHT:
if ( c_location.x < 38 )
{
c_location.x += 2;
}
else return;
break;
}
/* And keep track of the move count. */
c_moves++;
}
/* And then remember what direction we're moving... */
c_direction = p_direction;
c_steps = 16;
c_blocked = p_blocked;
c_pushing = p_pushing;
/* All done. */
return;
}
/* End of file Game.cpp */