forked from Barnold1953/GraphicsTutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
84 lines (66 loc) · 2.35 KB
/
Copy pathPlayer.cpp
File metadata and controls
84 lines (66 loc) · 2.35 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
76
77
78
79
80
81
82
83
84
#include "Player.h"
#include <SDL/SDL.h>
#include <Bengine/ResourceManager.h>
#include "Gun.h"
Player::Player() :
_currentGunIndex(-1) {
// Empty
}
Player::~Player() {
// Empty
}
void Player::init(float speed, glm::vec2 pos, Bengine::InputManager* inputManager, Bengine::Camera2D* camera, std::vector<Bullet>* bullets) {
_speed = speed;
_position = pos;
_inputManager = inputManager;
_bullets = bullets;
_camera = camera;
_color.r = 255;
_color.g = 255;
_color.b = 255;
_color.a = 255;
_health = 150;
m_textureID = Bengine::ResourceManager::getTexture("Textures/player.png").id;
}
void Player::addGun(Gun* gun) {
// Add the gun to his inventory
_guns.push_back(gun);
// If no gun equipped, equip gun.
if (_currentGunIndex == -1) {
_currentGunIndex = 0;
}
}
void Player::update(const std::vector<std::string>& levelData,
std::vector<Human*>& humans,
std::vector<Zombie*>& zombies,
float deltaTime) {
if (_inputManager->isKeyDown(SDLK_w)) {
_position.y += _speed * deltaTime;
} else if (_inputManager->isKeyDown(SDLK_s)) {
_position.y -= _speed * deltaTime;
}
if (_inputManager->isKeyDown(SDLK_a)) {
_position.x -= _speed * deltaTime;
} else if (_inputManager->isKeyDown(SDLK_d)) {
_position.x += _speed * deltaTime;
}
if (_inputManager->isKeyDown(SDLK_1) && _guns.size() >= 0) {
_currentGunIndex = 0;
} else if (_inputManager->isKeyDown(SDLK_2) && _guns.size() >= 1) {
_currentGunIndex = 1;
} else if (_inputManager->isKeyDown(SDLK_3) && _guns.size() >= 2) {
_currentGunIndex = 2;
}
glm::vec2 mouseCoords = _inputManager->getMouseCoords();
mouseCoords = _camera->convertScreenToWorld(mouseCoords);
glm::vec2 centerPosition = _position + glm::vec2(AGENT_RADIUS);
m_direction = glm::normalize(mouseCoords - centerPosition);
if (_currentGunIndex != -1) {
_guns[_currentGunIndex]->update(_inputManager->isKeyDown(SDL_BUTTON_LEFT),
centerPosition,
m_direction,
*_bullets,
deltaTime);
}
collideWithLevel(levelData);
}