-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
75 lines (61 loc) · 1.82 KB
/
Player.cpp
File metadata and controls
75 lines (61 loc) · 1.82 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
#include "Player.h"
#include "GameComponents.h"
#include "Sounds.h"
#include <algorithm>
#include <numbers>
Player::Player(const Sprite& sprite, const Position position, const Size size) noexcept : GameComponent(sprite)
{
this->position = position;
this->size = size;
}
void Player::move() noexcept
{
// Horizontal movement
position.x += IsKeyDown('A') ? -7 : IsKeyDown('D') ? 7 : 0;
// Vertical movement
position.y += IsKeyDown('W') ? -7 : IsKeyDown('S') ? 7 : 0;
float mx, my;
GetMousePos(mx, my);
// Compute angle between cursor and the player's position
this->direction.x = this->position.x - mx;
this->direction.y = this->position.y - my;
this->direction.normalize();
this->angle = this->direction.getAngle();
GameComponents::get().getCanvas().moveIn(*this);
// Compute bounding box
this->recomputeBoundingBox();
}
void Player::shoot(const Time time)
{
static auto& bullets = GameComponents::get().getBullets();
static auto bulletToFire = bullets.begin();
static int counter = 0;
if (IsKeyDown(VK_LBUTTON))
{
if (counter == 0)
{
bulletToFire->setDirection(this->direction);
bulletToFire->setPosition(this->position);
bulletToFire->visible = true;
bullets.next(bulletToFire);
GameComponents::get().getSound().shoot();
counter = 15;
}
else
{
--counter;
}
}
else
{
counter = 0;
}
}
void Player::tick(const Time time)
{
this->move();
this->shoot(time);
// this->angle = std::numbers::pi + sin(time * 0.1) * 0.1;
DrawBoundingBox(minmax.first.x, minmax.first.y, minmax.second.x, minmax.second.y);
DrawSprite(sprite, position.x, position.y, size.width, size.height, angle, 0xffffffff);
}