-
Notifications
You must be signed in to change notification settings - Fork 0
/
stickmanplayer.cpp
116 lines (96 loc) · 3.62 KB
/
stickmanplayer.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
#include "stickmanplayer.h"
#include "coordinate.h"
StickmanPlayer::StickmanPlayer(Coordinate* position, std::string name)
: Player(position, name),
offset_position(new Coordinate(0, 0, position->getFrameHeight(), position->getFrameWidth())),
physics_body(PhysicsBody(offset_position)),
collider(RectCollider(new Coordinate(0, 0, position->getFrameHeight(), position->getFrameWidth()),
new Coordinate(0, 0, position->getFrameHeight(), position->getFrameWidth()))),
jump_height(150),
gravity(-9.8 * 200), // Assume 100 pixels = 1m
jump_count(0),
frame_number(1),
counter(0),
m_lives(0),
m_current_level(1) {
physics_body.setYAcceleration(gravity);
}
StickmanPlayer::~StickmanPlayer() {
delete offset_position;
}
void StickmanPlayer::jump() {
if (jump_count < max_jump_count) {
jump_count += 1;
physics_body.setYVelocity(std::sqrt(-2 * gravity * jump_height));
}
}
void StickmanPlayer::landed() {
jump_count = 0;
physics_body.setYVelocity(0);
}
void StickmanPlayer::checkGroundCollision() {
if (offset_position->getYCoordinate() <= 0) {
offset_position->setYCoordinateToZero(0);
landed();
}
}
void StickmanPlayer::onCollision(Entity* other) {
std::cout << other->getName() << std::endl;
}
void StickmanPlayer::update(bool paused, double time_since_last_frame) {
// Physics updates
physics_body.apply(time_since_last_frame / 1000.0);
checkGroundCollision();
// Keep position coordinate and collider coordinates in sync with the Config
this->getPosition()->setXCoordinateToZero(Config::config()->getStickman()->getXPosition() - (Config::config()->getStickman()->getWidth() * 0.5) + offset_position->getXCoordinate());
this->getPosition()->setYCoordinateToZero(Config::config()->getStickman()->getHeight() + offset_position->getYCoordinate());
this->collider.getV1()->setXCoordinateToZero(this->getPosition()->getXCoordinate());
this->collider.getV1()->setYCoordinateToZero(this->getPosition()->getYCoordinate());
this->collider.getV2()->setXCoordinateToZero(this->getPosition()->getXCoordinate() + Config::config()->getStickman()->getWidth());
this->collider.getV2()->setYCoordinateToZero(this->getPosition()->getYCoordinate() - Config::config()->getStickman()->getHeight());
//Used to slow down the animation
if (counter == 2) {
frame_number++;
counter = 0;
}
//Once the frame is the last, reset
if (frame_number > 9) {
frame_number = 1;
}
//Only increment the counter if its not paused
if (!paused) {
counter++;
}
}
void StickmanPlayer::render(QPainter& painter) {
QPixmap sprite = Config::config()->getStickman()->getPixmap(frame_number);
painter.drawPixmap(this->getPosition()->getQtRenderingXCoordinate(),
this->getPosition()->getQtRenderingYCoordinate(),
Config::config()->getStickman()->getWidth(),
Config::config()->getStickman()->getHeight(),
sprite);
}
// stgae 3
// self explanatory getters and setters
void StickmanPlayer::setGravity(double gravity) {
this->gravity = gravity;
}
int StickmanPlayer::getLives() {
return m_lives;
}
void StickmanPlayer::setLives(int lives) {
this->m_lives = lives;
}
// decrease life when hitting an obstacle
void StickmanPlayer::loseLife() {
m_lives--;
}
int StickmanPlayer::getCurrentLevel() {
return m_current_level;
}
void StickmanPlayer::setCurrentLevel(int level) {
m_current_level = level;
}
double StickmanPlayer::getGravity() {
return gravity;
}