-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoverpup.cpp
90 lines (84 loc) · 2.78 KB
/
moverpup.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
#include "moverpup.h"
MoverPup::MoverPup(sdlxx::Sdl_renderer& renderer, int x_pos, int y_pos)
: Mover{renderer,
35,
50,
x_pos,
y_pos,
"resources/pupper.bmp" }
{
isPlayer = false;
isVillain = false;
isPupper = true;
move_speed = rand() % 9 + 2;
air_move_speed = move_speed - 1;
gravity = rand() % 5 + 1;
move_right = rand() % 2;
}
void MoverPup::update()
{
int jumpchance = rand() % 100;
if ((velocity.y == 0) && (jumpchance < 1)) {
velocity.y -= gravity * 10 - 1; // -1 so the puppers can't doublejump
}
Mover::update();
}
void MoverPup::collide(Game_object& rhs)
{
bool on_top = false;
if (rhs.isPlatform && velocity.y > 0) {
// We're falling
auto overlap_y = (position.y + height + velocity.y) - rhs.position.y;
// Check if the bottommost border is already past the objects topmost border
bool past_border;
if (((position.y + height - velocity.y) - rhs.position.y) > 0) {
past_border = true;
}
else {
past_border = false;
}
if (overlap_y > 0 && past_border == false) {
// We're starting to go through a platform.
// They're no longer jumping
//jumping = false;
//double_jump = false;
velocity.y = 0;
position.y = rhs.position.y - height;
on_top = true;
}
}
// If we're moving left or right and we hit a barrier move back to the last position of the x coordinate
if (rhs.isPlatform && move_speed > 0 && on_top == false) {
// We're moving to the right
// Overlap into the object
auto overlap_x = (position.x + width + move_speed) - rhs.position.x;
// Check if the leftmost border is already past the objects leftmost border
bool past_border;
if ((position.x - rhs.position.x) > 0) {
past_border = true;
}
else {
past_border = false;
}
if (overlap_x > 0 && past_border == false) {
position.x = rhs.position.x - width;
move_right = !move_right;
}
}
else if (rhs.isPlatform && move_speed < 0 && on_top == false) {
// We're moving to the left
auto overlap_x = (position.x + move_speed) - (rhs.position.x + rhs.width);
// Check if the rightmost border is already past the objects leftmost border
bool past_border;
if (((position.x + width) - (rhs.position.x + rhs.width)) < 0) {
past_border = true;
}
else {
past_border = false;
}
if (overlap_x < 0 && past_border == false) {
position.x = rhs.position.x + rhs.width;
move_right = !move_right;
}
}
}