-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
148 lines (120 loc) · 3.04 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
#include "Player.h"
#include "Bullet.h"
#include "Game.h"
#include "Item.h"
#include "SDLWindow.h"
Player::Player() {
name = "Player/" + name;
collisionType = ICollideableType::Player;
Vector2f windowSize = sdl->getWindowSize();
box.size = windowSize * Player::DefaultPlayerSizePercent;
Vector2f playerCenter( windowSize.x/2, windowSize.y - box.size.y/2 );
box.setCenter( playerCenter );
animation = sdl->getAnimation( AnimationId::Player );
deathSound = SFXId::ExplodePlayer;
particleCloudProperties.setNumParticles( 25, 40 );
particleCloudProperties.setSizes( 16, 22 );
particleCloudProperties.initialDecay = .8;
}
void Player::update( float t ) {
Sprite::update( t );
velocity.x = 0; // player kill vel each frame
if( canPlayerRespawn() ) {
respawn();
}
}
void Player::die() {
lives--;
Sprite::die();
// swap out the player death sprite.
setAnimation( AnimationId::PlayerDie );
game->playerDie();
}
bool Player::canPlayerRespawn() {
return dead && animation.isEnded() && !isOutOfLives();
}
void Player::respawn() {
maxBullets = DefaultMaxBullets;
setAnimation( AnimationId::Player );
weapon = BulletType::PlayerNormal;
dead = 0;
}
void Player::addShield() {
if( shielded ) {
return; // player already shielded
}
shielded = true;
RectF shieldBox = box;
shieldBox.pad( box.size/2 );
auto shieldSprite = std::make_shared<Sprite>( shieldBox, AnimationId::Shield );
children.push_back( shieldSprite );
}
void Player::loseShield() {
// The player won't die if shielded, but the shield drops.
shielded = 0;
children.clear(); // drops the shield.
}
void Player::onHit( ICollideable *o ) {
switch( o->collisionType ) {
case ICollideableType::Bullet: {
Bullet *bullet = (Bullet*)o;
if( bullet->isFromInvader() ) {
if( shielded ) {
loseShield();
}
else {
die();
}
}
}
break;
case ICollideableType::Bunker:
break;
case ICollideableType::BunkerPiece:
break;
case ICollideableType::Invader:
die();
break;
case ICollideableType::Item: {
auto item = ((Item*)o)->shared_Item();
giveItem( item );
}
break;
case ICollideableType::Player:
break;
case ICollideableType::UFO:
break;
case ICollideableType::NotCollideable:
default:
error( "Colliding with non-collideable" );
break;
}
}
void Player::giveItem( shared_ptr<Item> item ) {
switch( item->character ) {
case AnimationId::ItemPlus1:
maxBullets++;
break;
case AnimationId::ItemShield:
addShield();
break;
case AnimationId::ItemSpread:
weapon = BulletType::PlayerSpread;
break;
case AnimationId::ItemThickLaser:
weapon = BulletType::PlayerThickLaser;
break;
default:
break;
}
sdl->playSound( SFXId::Blip );
}
void Player::tryShoot() {
game->tryShootBullet( weapon, box.midTop() );
}
void Player::setMovingLeft() {
velocity.x = -DefaultPlayerSpeed;
}
void Player::setMovingRight() {
velocity.x = +DefaultPlayerSpeed;
}