-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
73 lines (53 loc) · 1.19 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
#include "Player.h"
#include "Input.h"
#include "Bullet.h"
Player::Player(): GameObject(), speed(200.0f), reloadTime(1.0f)
{
tag = Tag::PlayerTag;
}
Player::Player(glm::vec2 pos, Texture2D sprite, glm::vec3 color):
GameObject(pos, glm::vec2(50.0f, 50.0f), sprite, color), speed(200.0f), reloadTime(1.0f)
{
tag = Tag::PlayerTag;
}
void Player::Init()
{
AddObserver(world);
canFire = false;
}
void Player::Fire()
{
Bullet* bullet = new Bullet(Position + glm::vec2(0.0f, -40.0f), Sprite, glm::vec2(0.0f, -1.0f), Tag::BulletFromPlayerTag);
//NOT GOOD
bullet->AddWorld(world);
bullet->Init();
bullet->Spawn();
world->AddGameObject(bullet);
countTime = reloadTime;
canFire = false;
}
void Player::AddWorld(nohaGame* world)
{
this->world = world;
}
void Player::Update(float dt) {
float velocity = speed * dt;
if (countTime > 0.0f) {
countTime -= dt;
}
else {
countTime = 0.0f;
canFire = true;
}
if (Input::IsPressed(GLFW_KEY_RIGHT))
{
Position.x += velocity;
}
if (Input::IsPressed(GLFW_KEY_LEFT)) {
Position.x -= velocity;
}
if (Input::IsPressed(GLFW_KEY_SPACE) && canFire) {
Fire();
}
Position.x = glm::clamp(Position.x, 0.0f, static_cast<float>(SCREEN_WIDTH));
}