-
Notifications
You must be signed in to change notification settings - Fork 1
/
Character.cpp
executable file
·132 lines (114 loc) · 4.35 KB
/
Character.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
127
128
129
130
131
132
#include <math.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include "Character.h"
#include "Defines.h"
Character::Character(sf::Texture & healthBarForegroundTexture, sf::Texture & healthBarBackgroundTexture, int maxHealth, int damage, float charSpeed) : maxHealth(maxHealth), health(maxHealth), damage(damage), charSpeed(charSpeed) {
healthBarBackgroundSprite = sf::Sprite(healthBarBackgroundTexture);
healthBarBackgroundSprite.setOrigin(healthBarBackgroundSprite.getGlobalBounds().width / 2.0f, healthBarBackgroundSprite.getGlobalBounds().height);
healthBarForegroundSprite = sf::Sprite(healthBarForegroundTexture);
playerCircle = sf::CircleShape(46.0f);
playerCircle.setOrigin(playerCircle.getRadius(), playerCircle.getRadius());
}
Character::~Character() {
// nothing to clean up as of yet...
}
void Character::operator=(const Character & other) {
maxHealth = other.maxHealth;
health = other.health;
damage = other.damage;
charSpeed = other.charSpeed;
}
void Character::draw(sf::RenderWindow & window) {
playerCircle.setPosition(playerSprite.getPosition());
healthBarBackgroundSprite.setPosition(getCenter() - sf::Vector2f(0.0f, healthBarBackgroundSprite.getGlobalBounds().height * 2.0f));
healthBarForegroundSprite.setScale(health / (float)maxHealth, 1.0f);
healthBarForegroundSprite.setPosition(healthBarBackgroundSprite.getGlobalBounds().left, healthBarBackgroundSprite.getGlobalBounds().top);
window.draw(healthBarForegroundSprite);
window.draw(healthBarBackgroundSprite);
window.draw(playerSprite);
}
void Character::move(const sf::RenderWindow & window, const sf::Keyboard::Key releasedKey) {
// stop movement if player is dead
if (getIsDead()) {
return;
}
// player rotation
sf::Vector2i mousePos = (sf::Vector2i)window.mapPixelToCoords(sf::Mouse::getPosition(window));
sf::Vector2i charPos = (sf::Vector2i)playerSprite.getPosition();
sf::Vector2i diff = mousePos - charPos;
float angle = (float)atan2(diff.y, diff.x) * (180 / PI);
playerSprite.setRotation(angle);
// normal movement
if (sf::Keyboard::isKeyPressed(moveUpKey)) {
playerSprite.setPosition(playerSprite.getPosition() + sf::Vector2f(0.0f, -charSpeed)); // up
}
if (sf::Keyboard::isKeyPressed(moveDownKey)) {
playerSprite.setPosition(playerSprite.getPosition() + sf::Vector2f(0.0f, charSpeed)); // down
}
if (sf::Keyboard::isKeyPressed(moveLeftKey)) {
playerSprite.setPosition(playerSprite.getPosition() + sf::Vector2f(-charSpeed, 0.0f)); // left
}
if (sf::Keyboard::isKeyPressed(moveRightKey)) {
playerSprite.setPosition(playerSprite.getPosition() + sf::Vector2f(charSpeed, 0.0f)); // right
}
}
sf::Vector2f Character::getCenter() {
return sf::Vector2f(playerSprite.getGlobalBounds().left + playerSprite.getGlobalBounds().width / 2.0f, playerSprite.getGlobalBounds().top + playerSprite.getGlobalBounds().height / 2.0f);
}
sf::Packet Character::chainDataToPacket(sf::Packet & packet, std::string value) {
return (packet << value << "Character" << maxHealth << health << damage << playerSprite.getPosition().x << playerSprite.getPosition().y << playerSprite.getRotation()) ? packet : sf::Packet();
}
sf::Packet Character::extractPacketToData(sf::Packet & packet) {
sf::Vector2f pos;
float rotation;
packet >> maxHealth >> health >> damage >> pos.x >> pos.y >> rotation;
playerSprite.setPosition(pos);
playerSprite.setRotation(rotation);
return packet;
}
bool Character::getIsDead() {
if (isPlayer && isDead) {
return true;
}
// increase death counter if needed
if (isPlayer && health <= 0 && !isDead) {
isDead = true;
respawnTimer.restart();
return true;
}
return false;
}
float Character::getTimeAsDead() {
return respawnTimer.getElapsedTime().asSeconds();
}
sf::CircleShape Character::getCollisionCircle() {
return playerCircle;
}
sf::Int16 Character::takeDamage(sf::Int16 damage) {
if (isDead) {
return -1;
}
health -= damage;
if (health < 0) {
health = 0;
}
return health;
}
sf::Int16 Character::getDamage() {
return damage;
}
sf::Int16 Character::getHealth() {
return health;
}
sf::Sprite* Character::getPlayerSprite() {
return &playerSprite;
}
float Character::getCharSpeed() {
return charSpeed;
}
void Character::setPosition(sf::Vector2f position) {
playerCircle.setPosition(position);
playerSprite.setPosition(position);
}