-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
143 lines (118 loc) · 4.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
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
133
134
135
136
137
138
139
140
141
142
143
#include "Player.h"
#include "ResourceManager.h"
Player::Player() {} //unused constructor
Player::Player(Level* newWorld) {
jumpActivated = false;
digActivated = false;
world = newWorld;
xPos = 0.;
yPos = 0.;
xVel = 0.;
yVel = 0.;
anim = 0;
direction = 1;
levelFlags = new int[20];
for (int i = 0; i < 20; i++) levelFlags[i] = 0;
sprite = sf::Sprite(ResourceManager::SplunkSprites);
sprite.setOrigin(16.,16.);
sprite.setTextureRect(sf::IntRect(0,0,32,32));
while (world->colAt(xPos,yPos)) {
xPos = (float)(rand() % (int)world->getPixelSize().x);
yPos = (float)(rand() % (int)world->getPixelSize().y);
}
}
sf::Vector2f Player::getPosition() { return sf::Vector2f(xPos,yPos); }
int Player::getFlag(int levelNum) { return levelFlags[levelNum]; }
void Player::setPosition(float newX, float newY) {
xPos = newX;
yPos = newY;
}
void Player::setPosition(sf::Vector2f posV) {
xPos = posV.x;
yPos = posV.y;
}
void Player::setWorld(Level* newWorld) {
world = newWorld;
}
void Player::setFlag(int levelNum, int flag) {
levelFlags[levelNum] = levelFlags[levelNum] | flag;
}
int Player::logic() {
//climb
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z)) {
if (yVel < 5 && yVel > -5) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) yVel = -1.;
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) yVel = 1.;
else yVel = 0;
}
else yVel += .35;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) xVel = -1.;
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) xVel = 1.;
else xVel = 0;
}
else if (world->colAt(xPos,yPos + 16)) {
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && !jumpActivated) {
yVel = -8;
jumpActivated = true;
anim = 0;
sprite.setTextureRect(sf::IntRect(32,0,32,32));
}
//left or right
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
xVel = -3.5;
direction = -1;
anim = ++anim % 40;
sprite.setTextureRect(sf::IntRect(anim/10*32,0,32,32));
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
xVel = 3.5;
direction = 1;
anim = ++anim % 40;
sprite.setTextureRect(sf::IntRect(anim/10*32,0,32,32));
}
else {
xVel = 0;
anim = 0;
sprite.setTextureRect(sf::IntRect(0,0,32,32));
}
}
else {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && xVel >= 0) xVel = -1., direction = -1;
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && xVel <= 0) xVel = 1., direction = 1;
yVel += .35;
anim = 0;
sprite.setTextureRect(sf::IntRect(32,0,32,32));
}
//dig check
if (sf::Keyboard::isKeyPressed(sf::Keyboard::X) && !digActivated) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) world->activateAt(xPos-32.,yPos);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) world->activateAt(xPos+32.,yPos);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) world->activateAt(xPos,yPos-32.);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) world->activateAt(xPos,yPos+32.);
else world->activateAt(xPos,yPos);
digActivated = true;
}
//reset activated flags
if (!sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) jumpActivated = false;
if (!sf::Keyboard::isKeyPressed(sf::Keyboard::X)) digActivated = false;
//physics
for (int i = abs(yVel); i > 0; i--) {
if(!world->colAt(xPos, yPos + abs(yVel)/yVel*16)) yPos += abs(yVel)/yVel;
else {
yVel = 0;
break;
}
while (yVel == 0 && world->colAt(xPos,yPos+16)) yPos -= 1.;
}
for (int i = abs(xVel); i > 0; i--) {
if(!world->colAt(xPos + abs(xVel)/xVel*5, yPos)) xPos += abs(xVel)/xVel;
else break;
}
//prepare to draw
sprite.setScale(direction,1);
sprite.setPosition(xPos,yPos);
return 0;
}
void Player::draw(sf::RenderTarget& target, sf::RenderStates states) const {
target.draw(sprite);
}