El Octavio  1.0
This is a video game about adventures.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
GameClass.cpp
Go to the documentation of this file.
1#include "../Header files/GameClass.h";
2#include "../Header files/El Octavo-Functions.h"
3#include "../Header files/Menu.h"
4#include "../Header files/Precompile.h"
5
6namespace vars {
9 bool jumped = false;
10 int keyTime = 0;
11}
12
13using namespace vars;
14
15//consturctors - setup variables
16Player::Player(Texture* texture, Vector2u imageCount, float switchTime, float speed)
17{
18 this->imageCount = imageCount;
19 this->switchTime = switchTime;
20 this->speed = speed;
21 this->totalTime = 0.0f;
22 this->currentImage.x = 0;
23 this->ramped = false;
24
25 this->uvRect.width = texture->getSize().x / float(imageCount.x);
26 this->uvRect.height = texture->getSize().y / float(imageCount.y);
27
28 //movement mechanic
29 this->faceLeft = true;
30 this->row = 0;
31
32 this->body.setSize(Vector2f(81.0f, 129.0f));
33 this->body.setOrigin(81.0f / 2, 129.0f / 2);
34 this->body.setPosition(1280 / 2, 720 / 2);
35 this->body.setTexture(texture);
36}
37
38//destructor
40{
41}
42
43//update for displaying character
44void Player::update(int row, float deltaTime, bool faceLeft)
45{
46 currentImage.y = row;
47 totalTime += deltaTime;
48
49 if (totalTime >= switchTime)
50 {
51 totalTime -= switchTime;
52 currentImage.x++;
53
54 if (currentImage.x >= imageCount.x)
55 {
56 currentImage.x = 0;
57 }
58 }
59
60 uvRect.top = currentImage.y * uvRect.height;
61
62 if (faceLeft)
63 {
64 uvRect.left = (currentImage.x + 1) * abs(uvRect.width);
65 uvRect.width = -abs(uvRect.width);
66 }
67 else
68 {
69 uvRect.left = currentImage.x * uvRect.width;
70 uvRect.width = abs(uvRect.width);
71 }
72}
73
75{
76 adventureBgImage.move(-(225.0f * deltaTime), 0.f);
77}
78
80{
81 if (keyTime < 1)
82 {
83 keyTime++;
84 }
85
86 //WASD moving
87 if (keyTime >= 1)
88 {
89 if (Keyboard::isKeyPressed(Keyboard::D))
90 {
92 keyTime = 0;
93 }
94 }
95}
96
97
98//Update for moving character
100{
101 velocity.x = 0;
102 jumped = false;
103
104 if (body.getPosition().x >= 35.0f)
105 {
106 if (Keyboard::isKeyPressed(Keyboard::A)) {
107 if (soundWalk.getStatus() == 0 && isAudioRunning(audioToggle) == true) {
108 soundWalk.play();
109 }
110
111 velocity.x -= abs(speed - 50.0f) * deltaTime;
112 }
113 }
114
115 if (Keyboard::isKeyPressed(Keyboard::D))
116 {
117 if (soundWalk.getStatus() == 0 && isAudioRunning(audioToggle) == true) {
118 soundWalk.play();
119 }
120
121 velocity.x += abs(speed - 50.0f) * deltaTime;
122 }
123
124 if (!Keyboard::isKeyPressed(Keyboard::D) && !Keyboard::isKeyPressed(Keyboard::A) && !Keyboard::isKeyPressed(Keyboard::W) && !Keyboard::isKeyPressed(Keyboard::S)) {
125 if (soundWalk.getStatus() == 2 && isAudioRunning(audioToggle) == true) {
126 soundWalk.stop();
127 }
128 }
129
130
131 if (toggle) {
132 if (Keyboard::isKeyPressed(Keyboard::Space)) {
133
135 if (currTime.asSeconds() >= 1.0f && !jumped) {
136 if (isAudioRunning(audioToggle) == true)
137 {
138 soundJump.play();
139 }
140
143 jump(deltaTime, 600);
144 jumped = true;
145 }
146 }
147
148 if (body.getPosition().x >= window.getSize().x / 2)
149 {
151 if (Keyboard::isKeyPressed(Keyboard::D))
152 {
153 velocity.x = 0.000001;
154 }
155 }
156 }
157 else {
158 velocity.y = 0;
159 if (Keyboard::isKeyPressed(Keyboard::W))
160 {
161 if (soundWalk.getStatus() == 0 && isAudioRunning(audioToggle) == true) {
162 soundWalk.play();
163 }
164
165 velocity.y -= abs(speed - 35.0f) * deltaTime;
166 }
167
168 if (Keyboard::isKeyPressed(Keyboard::S))
169 {
170 if (soundWalk.getStatus() == 0 && isAudioRunning(audioToggle) == true) {
171 soundWalk.play();
172 }
173
174 velocity.y += abs(speed - 35.0f) * deltaTime;
175 }
176 }
177
178
179 if (velocity.x == 0.0f)
180 {
181 row = 0;
182 }
183 else
184 {
185 row = 1;
186 if (velocity.x > 0.0f)
187 {
188 faceLeft = true;
189 }
190
191 else
192 {
193 faceLeft = false;
194 }
195 }
196
197 update(row, deltaTime, faceLeft);
199
200 if (toggle) {
201
202 if (checkCollideWithGround(body) && !ramped) {
203 body.setPosition(body.getPosition().x, 490.0f);
204 //cout << "set" << endl;
205 }
206
207 velocity.y += 160.0f * deltaTime;
208
209 if (checkCollideWithGround(body) && !jumped && !ramped) {
210 velocity.y = 0;
211 }
212
213
214 float bodyY = body.getPosition().y;
215 float bodyX = body.getPosition().x;
216
217
218 // idk how this works and why it works
219 // please kill me
220 // this took 2 hours
221
222 for (int i = 0; i < 2; i++) {
223 if (blocks[i].checkForCollision(body)) {
224 float hitboxX = blocks[i].hitbox.getPosition().x;
225 float hitboxY = blocks[i].hitbox.getPosition().y;
226 float hitboxSizeX = blocks[i].hitbox.getSize().x;
227 float hitboxSizeY = blocks[i].hitbox.getSize().y;
228
229 if (bodyX > hitboxX + hitboxSizeX && bodyY > hitboxY) {
230 velocity.x = 0.f;
231 body.setPosition(hitboxX + hitboxSizeX + 40.5f, bodyY);
232 }
233 else if (bodyX < hitboxX && bodyY > hitboxY) {
234 velocity.x = 0.f;
235 body.setPosition(hitboxX - 40.5f, bodyY);
236 }
237
238 if ((bodyY < hitboxY + hitboxSizeY && not bodyX < hitboxX) or (bodyY < hitboxY + hitboxSizeY && not bodyX > hitboxX + hitboxSizeX)) {
239 if (!jumped) {
240 velocity.y = 0.f;
241 }
242 jumped = false;
243
244 body.setPosition(bodyX, hitboxY - 64.0f);
245 }
246 else if (((bodyY > hitboxY && not bodyX < hitboxX) or (bodyY > hitboxY && not bodyX < bodyX >= hitboxX + hitboxSizeX)) && i != 0) {
247 if (jumped) {
248 velocity.y = 0.f;
249 }
250 jumped = false;
251
252 body.setPosition(bodyX, hitboxY + hitboxSizeY + 64.5f);
253 }
254 }
255 }
256
257
258 // ok this is taking way too long i give up
259 // ramp collision = good enough
260
262 //cout << "ez mi e" << endl;
263 //if (bodyY == 490) {
264 // body.setPosition(bodyX, 480);
265 // bodyY = body.getPosition().y;
266 // //cout << bodyY << endl;
267 //}
268 if (Keyboard::isKeyPressed(Keyboard::D)) {
269 if (-velocity.x < 999) {
270 velocity.x = abs(speed - 500.0f) * deltaTime;
271 }
272
273 }
274 else if (Keyboard::isKeyPressed(Keyboard::A)) {
275 if (velocity.x > 999) {
276 velocity.x = -abs(speed - 50.0f) * deltaTime;
277 }
278 }
279 velocity.y = -velocity.x;
280 ramped = true;
281 //cout << velocity.y << " " << velocity.x << endl;
282 }
283 else if (bodyX > getRampPos().x + 110 && (bodyX - (getRampPos().x + 110) < 30.0f) && bodyY > 450 && !ramped) {
284 cout << bodyY << endl;
285 velocity.x = 0.f;
286 body.setPosition(bodyX + 0.75f, bodyY);
287 }
288 else {
289 if (ramped) {
290 ramped = false;
291 }
292 }
293 }
294
296
297}
298
300{
301 window.draw(body);
302}
303
304void Player::jump(float deltaTime, float jumpHeight) {
305 velocity.y = -sqrtf((160.0f * jumpHeight) * 0.016);
306 cout << deltaTime << endl;
307}
bool checkCollideWithRamp(RectangleShape &body)
Vector2f getRampPos()
bool checkCollideWithGround(RectangleShape &body)
bool audioToggle
Definition: Menu.cpp:6
bool isAudioRunning(bool &audioToggle)
Definition: Menu.cpp:216
RectangleShape hitbox
void moveCharacter(int &keyTime, RenderWindow &window, Sprite &adventureBgImage, float &deltaTime)
Definition: GameClass.cpp:79
RectangleShape body
Definition: GameClass.h:22
void jump(float deltaTime, float jumpHeight)
Definition: GameClass.cpp:304
void updateMovement(float deltaTime, RenderWindow &window, Sprite &adventureBgImage, Sound &soundWalk, Sound &soundJump, bool &toggle, CollisionBlock blocks[], int blocksSize)
Definition: GameClass.cpp:99
float jumpY
Definition: GameClass.h:23
void draw(RenderWindow &window)
Definition: GameClass.cpp:299
IntRect uvRect
Definition: GameClass.h:21
~Player()
Definition: GameClass.cpp:39
void update(int row, float deltaTime, bool faceLeft)
Definition: GameClass.cpp:44
void moveCameraFirstStage(Sprite &image, float &deltaTime)
Definition: GameClass.cpp:74
Vector2f velocity
Definition: GameClass.h:24
Player(Texture *texture, Vector2u imageCount, float switchTime, float speed)
Definition: GameClass.cpp:16
Utility class that measures the elapsed time.
Definition: Clock.hpp:42
Time restart()
Restart the clock.
Time getElapsedTime() const
Get the elapsed time.
T width
Width of the rectangle.
Definition: Rect.hpp:159
T height
Height of the rectangle.
Definition: Rect.hpp:160
T left
Left coordinate of the rectangle.
Definition: Rect.hpp:157
T top
Top coordinate of the rectangle.
Definition: Rect.hpp:158
void setSize(const Vector2f &size)
Set the size of the rectangle.
const Vector2f & getSize() const
Get the size of the rectangle.
void draw(const Drawable &drawable, const RenderStates &states=RenderStates::Default)
Draw a drawable object to the render target.
Window that can serve as a target for 2D drawing.
virtual Vector2u getSize() const
Get the size of the rendering region of the window.
void setTextureRect(const IntRect &rect)
Set the sub-rectangle of the texture that the shape will display.
void setTexture(const Texture *texture, bool resetRect=false)
Change the source texture of the shape.
Regular sound that can be played in the audio environment.
Definition: Sound.hpp:46
void play()
Start or resume playing the sound.
Status getStatus() const
Get the current status of the sound (stopped, paused, playing)
void stop()
stop playing the sound
Drawable representation of a texture, with its own transformations, color, etc.
Definition: Sprite.hpp:48
Image living on the graphics card that can be used for drawing.
Definition: Texture.hpp:49
Vector2u getSize() const
Return the size of the texture.
Represents a time value.
Definition: Time.hpp:41
float asSeconds() const
Return the time value as a number of seconds.
void setPosition(float x, float y)
set the position of the object
void setOrigin(float x, float y)
set the local origin of the object
void move(float offsetX, float offsetY)
Move the object by a given offset.
const Vector2f & getPosition() const
get the position of the object
T x
X coordinate of the vector.
Definition: Vector2.hpp:75
T y
Y coordinate of the vector.
Definition: Vector2.hpp:76
Vector2< float > Vector2f
Definition: Vector2.hpp:255
CollisionBlock blocks[2]
Texture texture
Definition: Menu.cpp:17
Time currTime
Definition: GameClass.cpp:8
int keyTime
Definition: GameClass.cpp:10
Clock clock
Definition: GameClass.cpp:7
bool jumped
Definition: GameClass.cpp:9