-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
80 lines (63 loc) · 1.57 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
#include "SpaceInvaderMainScene.h"
#include "Player.h"
#define PLAYER_SPEED 25
#define LASER_SPEED 40
void Player::tick ( SpaceInvaderMainScene * game )
{
processMovement ( game );
processCombat ( game );
}
void Player::processMovement ( SpaceInvaderMainScene * game )
{
bool leftInputPressed = game->m_keyboardInput.isPressed ( VK_LEFT );
bool rightInputPressed = game->m_keyboardInput.isPressed ( VK_RIGHT );
if ( leftInputPressed && rightInputPressed )
{
return;
}
float movement = 0;
if ( leftInputPressed )
{
movement = PLAYER_SPEED * game->getDeltaTime() * -1;
//translateByGridUnit ( -1, 0 );
}
else if ( rightInputPressed )
{
movement = PLAYER_SPEED * game->getDeltaTime() * 1;
//translateByGridUnit ( 1, 0 );
}
translate ( movement, 0 );
// prevent the player going off the edge of the screen
if ( getGridX ( ) >= game->getScreenWidth ( ) )
{
setGridX ( game->getScreenWidth ( ) - 1 );
}
else if ( getGridX ( ) < 0 )
{
setGridX ( 0 );
}
}
void Player::processCombat ( SpaceInvaderMainScene * game )
{
bool fireInputPressed = game->m_keyboardInput.isPressed ( VK_SPACE );
if ( ! fireInputPressed )
{
return;
}
// player trying to fire weapon.
if ( game->getGameTime() > m_lastFireTime + fireTimeout )
{
auto laser = game->getAvilableLaser ( );
if ( laser == nullptr )
{
// no available lasers
return;
}
// can fire
m_lastFireTime = game->getGameTime();
Vector2Int startPosition;
startPosition.X = getGridX ( ) + 2;
startPosition.Y = getGridY ( ) - 1;
laser->launch ( startPosition, LASER_SPEED );
}
}