-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.cpp
More file actions
169 lines (139 loc) · 4.1 KB
/
Copy pathplayer.cpp
File metadata and controls
169 lines (139 loc) · 4.1 KB
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
/****************************************************************************\
|* *|
|* player.cpp - part of Possum, a Space Shooter game. *|
|* *|
|* Copyright (C) 2021 Pete Favelle <32blit@ahnlak.com> *|
|* *|
|* This file is released under the MIT License; see LICENSE for details. *|
|* *|
|* The Player class encapsulates the whole player; his ship, lives, scores. *|
|* *|
\****************************************************************************/
/* System headers. */
/* Local headers. */
#include "possum.hpp"
#include "player.hpp"
/* Functions. */
/**
* Player()
*
* Constructor; just sets up some basic parameters.
*/
Player::Player( void )
{
/* Default our position the middle bottom of the screen. */
c_position.x = blit::screen.bounds.w / 2;
c_position.y = blit::screen.bounds.h - 12;
c_bank = BANK_NONE;
c_thrust = THRUST_CRUISE;
c_thrust_frame = 0;
/* All done. */
return;
}
/**
* ~Player()
*
* Destructor; simple tidying up - very little is required.
*/
Player::~Player( void )
{
/* All done. */
return;
}
/**
* update( time )
*
* Called every tick to update the player state, based on various inputs which
* depend on whether or not the game is active, and other factors.
*/
void Player::update( uint32_t p_time )
{
/* Check to see if there is movement input. */
if ( blit::buttons.state & blit::Button::DPAD_LEFT )
{
/* Moving left; set the bank angle to start with. */
c_bank = BANK_LEFT;
/* If we haven't hit the edge of the screen, move too. */
if ( c_position.x > 4 )
{
c_position.x--;
}
}
else if ( blit::buttons.state & blit::Button::DPAD_RIGHT )
{
/* Moving right then, again, always set the bank angle. */
c_bank = BANK_RIGHT;
/* If we haven't hit the edge of the screen, move too. */
if ( c_position.x < blit::screen.bounds.w - 4 )
{
c_position.x++;
}
}
else
{
/* We're not moving left or right, so we shouldn't bank. */
c_bank = BANK_NONE;
}
/* Vertical movement is a bit easier, because there's no banking. */
if ( blit::buttons.state & blit::Button::DPAD_UP )
{
/* Boost the thrusters, and move if there's space. */
c_thrust = THRUST_BOOST;
if ( c_position.y > 4 )
{
c_position.y--;
}
}
else if ( blit::buttons.state & blit::Button::DPAD_DOWN )
{
/* Drift down, turn the thrusters down too. */
c_thrust = THRUST_NONE;
if ( c_position.y < blit::screen.bounds.h - 8 )
{
c_position.y++;
}
}
else
{
/* We're just cruising. */
c_thrust = THRUST_CRUISE;
}
/* Keep the thrusters ticking, at a sensible framerate. */
if ( p_time > ( c_thrust_tick + 75 ) )
{
/* Keep moving through the frames. */
if ( ++c_thrust_frame >= 3 )
{
c_thrust_frame = 0;
}
/* And remember that we've ticked. */
c_thrust_tick = p_time;
}
/* Remember the last tick we were here. */
c_last_tick = p_time;
/* All done. */
return;
}
/**
* render()
*
* Called every tick to render the player; we always know where the ship is and
* what state it's in.
*/
void Player::render( void )
{
static blit::Point l_ship_offset( -4, -4 );
static blit::Point l_thrust_offset( -4, 4 );
/*
* The player position indicates the center of the ship; so, we just pick
* the right sprite based on the bank angle.
*/
blit::Rect l_ship_src( 8 * c_bank, 16, 8, 8 );
blit::screen.blit( g_ss_ships, l_ship_src, c_position + l_ship_offset );
/* The ship needs to have the right thrusters, too. */
blit::Rect l_thrust_src( 8 * ( 5 + c_thrust_frame ), 8 * c_thrust, 8, 8 );
blit::screen.blit( g_ss_misc, l_thrust_src, c_position + l_thrust_offset );
/* All done. */
return;
}
/* End of file player.cpp */