-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPlayer.cpp
46 lines (38 loc) · 1.25 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
#include "Configuration.h"
#include "Player.h"
#include "ProjectilePool.h"
#include <ncine/Application.h>
#include <ncine/IInputManager.h>
Player::Player(ProjectilePool *rocketPool, nc::Texture *playerTexture)
: lives_(0), score_(0), rocketPool_(rocketPool), sprite_(&nc::theApplication().rootNode(), playerTexture)
{
ASSERT(rocketPool_);
reset();
}
void Player::move(float frameTime)
{
sprite_.moveX(roundf(frameTime * Conf::PlayerSpeed));
// Clamping to window horizontal borders
if (sprite_.position().x < 0.0f)
sprite_.setPositionX(0.0f);
else if (sprite_.position().x >= nc::theApplication().width() - sprite_.width())
sprite_.setPositionX(nc::theApplication().width() - sprite_.width());
}
void Player::shoot()
{
// Check if enough time has passed before shooting again
if (lastShootTime_.secondsSince() >= Conf::PlayerShootTime)
{
rocketPool_->shoot(sprite_.position().x, sprite_.position().y + sprite_.height() * 0.5f);
lastShootTime_ = nc::TimeStamp::now();
}
}
void Player::reset()
{
// Centered at the bottom of the window
sprite_.setPositionX((nc::theApplication().width() - sprite_.width()) * 0.5f);
sprite_.setPositionY(sprite_.height());
lives_ = Conf::PlayerStartingLives;
// Ready to shoot
lastShootTime_ = nc::TimeStamp::now();
}