This repository has been archived by the owner on Mar 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
128 lines (106 loc) · 3.16 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "Player.h"
#include<iostream>
#include "Path.h"
#include <windows.h>
Player::Player(const char* backgroundBitmap, int xPosition, int yPosition, const int frames, const int levels, CONTROL_SOURCE controlSource)
: Hero(backgroundBitmap, xPosition, yPosition, frames, levels),controlSource(controlSource)
{
Path path;
int x = 10;
if (xPosition >= 100)
x = GetSystemMetrics(SM_CXSCREEN)-365;
healthBar = new HealthBar(path.OBJECT_HEALTH_BAR,x,10,10);
}
Player::~Player()
{
delete healthBar;
std::cout << "DESTRUKTOR Z PLAYER" << std::endl;
}
void Player::show()
{
this->healthBar->readHp(this);
this->healthBar->show();
al_draw_bitmap_region(this->objectBitmap, this->shiftX, this->shiftY, this->bitmapWidth / frames, this->bitmapHeight / levels, this->xPosition, this->yPosition, 0);
this->gun->showBullet();
}
void Player::move(ALLEGRO_EVENT events, float backgroundXPosition, int backgroundWidth, int backgroundHeight)
{
ALLEGRO_KEYBOARD_STATE keyState;
al_get_keyboard_state(&keyState);
if (controlSource == CONTROL_SOURCE::ARROW_CONTROL) {
if (al_key_down(&keyState, ALLEGRO_KEY_DOWN)) {
this->yPosition += this->moveSpeed;
animation(events);
}
else if (al_key_down(&keyState, ALLEGRO_KEY_UP)) {
this->yPosition -= this->moveSpeed;
animation(events);
}
else if (al_key_down(&keyState, ALLEGRO_KEY_LEFT)) {
this->xPosition -= this->moveSpeed;
this->shiftY = this->getBitmapHeight() / 2;
animation(events);
}
else if (al_key_down(&keyState, ALLEGRO_KEY_RIGHT)) {
this->xPosition += this->moveSpeed;
this->shiftY = 0;
animation(events);
}
}
else {
if (al_key_down(&keyState, ALLEGRO_KEY_S)) {
this->yPosition += this->moveSpeed;
animation(events);
}
else if (al_key_down(&keyState, ALLEGRO_KEY_W)) {
this->yPosition -= this->moveSpeed;
animation(events);
}
else if (al_key_down(&keyState, ALLEGRO_KEY_A)) {
this->xPosition -= this->moveSpeed;
this->shiftY = this->getBitmapHeight() / 2;
animation(events);
}
else if (al_key_down(&keyState, ALLEGRO_KEY_D)) {
this->xPosition += this->moveSpeed;
this->shiftY = 0;
animation(events);
}
}
useBorders(backgroundXPosition, backgroundWidth, backgroundHeight);
}
void Player::makeShot(ALLEGRO_EVENT events)
{
if (controlSource == CONTROL_SOURCE::ARROW_CONTROL) {
if (events.keyboard.keycode == ALLEGRO_KEY_SPACE) {
Hero::makeShot(events);
}
}
else {
if (events.keyboard.keycode == ALLEGRO_KEY_F) {
Hero::makeShot(events);
}
}
}
void Player::addHealth()
{
if(this->hp<=90)
this->hp += 10;
}
HealthBar* Player::getHealthBar()
{
return this->healthBar;
}
void Player::useBorders(float backgroundXPosition, int backgroundWidth, int backgroundHeight)
{
if (backgroundXPosition == 0 && this->getXposition() <= backgroundXPosition) {
this->setXposition(backgroundXPosition);
}
if (this->getXposition() <= backgroundXPosition - (backgroundWidth / 2)) {
this->setXposition(backgroundXPosition - (backgroundWidth / 2));
}
if (this->getYposition() <= 0)
this->setYposition(0);
if (this->getYposition() >= backgroundHeight - (this->getBitmapHeight() / 2))
this->setYposition(backgroundHeight - (this->getBitmapHeight() / 2));
}