-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
62 lines (50 loc) · 2.05 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
#include "Player.h"
#include "Game.h"
Player::Player(Game* game)
{
this->game = game;
radius = (spr->width / 2) * (spr->width / 2);
position = {static_cast<float>((game->ScreenWidth() - (spr->width / 2))), static_cast<float>((game->ScreenHeight() - 50))};
}
void Player::Update(float fElapsedTime)
{
position.y += fElapsedTime * game->scrollSpeed;
// handle input
if (game->GetKey(olc::W).bHeld) position.y -= (speed + game->scrollSpeed) * fElapsedTime;
if (game->GetKey(olc::S).bHeld) position.y += speed * fElapsedTime;
if (game->GetKey(olc::A).bHeld) position.x -= speed * fElapsedTime;
if (game->GetKey(olc::D).bHeld) position.x += speed * fElapsedTime;
// clamp to edges
if (position.y < 0) position.y = 0.0f;
if (position.x < 0) position.x = 0.0f;
if (position.y + spr->height > game->ScreenHeight()) position.y = game->ScreenHeight() - spr->height;
if (position.x + spr->width > game->ScreenWidth()) position.x = game->ScreenWidth() - spr->width;
HandleBullets(fElapsedTime);
game->DrawSprite(position, spr);
//olc::vf2d centre = {position.x + spr->width/2, position.y+spr->height/2};
//game->DrawCircle(centre, radius);
}
void Player::HandleBullets(float fElapsedTime)
{
// check input to create new bullets based on cooldown
bulletCooldown += fElapsedTime;
if (bulletCooldown >= bulletTriggerTime) {
if (game->GetKey(olc::SPACE).bHeld) {
bullets.emplace_back(Bullet({position.x + (spr->width / 2), position.y}));
//bullets.push_back({position.x + (spr->width / 2), position.y});
}
bulletCooldown = 0.0f;
}
// remove bullets if they're off screen or have hit an enemy
bullets.remove_if([&](const Bullet& b) {return b.position.y <= 0 || b.isDead;});
// draw bullets
for (auto& b : bullets) {
// update position of bullet
b.position.y -= (speed * 1.5) * fElapsedTime;
game->FillCircle(b.position, 3, olc::GREEN);
}
}
void Player::AdjustHealth(float healthModifier)
{
health += healthModifier;
}