/
jump.cpp
140 lines (114 loc) · 3.04 KB
/
jump.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
#include <iostream>
#include <cmath>
#include "jump.h"
// if not interrupted, jump takes 700 milisseconds
// jump max high is 74 px
#define JUMP_INITIAL_SPEED 5.375
#include "game.h"
extern game gameControl;
classjump::classjump() : started(false)
{
JUMP_ACCELERATION = 0.25;
JUMP_LIMIT = (TILESIZE*3)-6;
JUMP_LIMIT = 240;
state = NOJUMP;
jumps_number = 0;
start_terrain_type = TERRAIN_UNBLOCKED;
}
void classjump::start(bool bigjump_mode, int terrain_type)
{
started = true;
state = JUMPUP;
is_bigjump = bigjump_mode;
start_terrain_type = terrain_type;
start_big_jump = bigjump_mode;
if (start_terrain_type == TERRAIN_WATER) {
is_bigjump = true;
}
if (is_bigjump == true) {
acceleration = JUMP_ACCELERATION * 0.4;
} else {
acceleration = JUMP_ACCELERATION;
}
acceleration = acceleration * gameControl.get_fps_speed_multiplier();
jumps_number++;
speed = -JUMP_INITIAL_SPEED;
//std::cout << "CLASSJUMP::START::speed: " << speed << std::endl;
moved = 0;
}
bool classjump::is_started()
{
return started;
}
void classjump::execute(int terrain_type)
{
if (!started) {
return;
}
// if big jump was caused only because of water, acceleration changes if you are still in water or not
if (start_terrain_type == TERRAIN_WATER && start_big_jump == false) {
if (terrain_type == TERRAIN_WATER) {
acceleration = JUMP_ACCELERATION * 0.4;
} else {
acceleration = JUMP_ACCELERATION * 0.7;
}
}
//std::cout << "CLASSJUMP::EXECUTE[#1]::speed: " << speed << std::endl;
speed += acceleration;
moved += std::abs((double)speed);
//std::cout << "CLASSJUMP::EXECUTE[#2]::speed: " << speed << std::endl;
if (state == JUMPUP) {
if (speed >= 0) {
state = JUMPDOWN;
} else if (is_bigjump == false && std::abs((double)moved) > JUMP_LIMIT) { // hardcoded limit of 3 tiles
state = JUMPDOWN;
std::cout << "OBJUMP RESET SPEED #3" << std::endl;
speed = 0;
}
} else {
if (speed > GRAVITY_MAX_SPEED) { // do not surpass the speed limit
speed = GRAVITY_MAX_SPEED;
}
}
}
void classjump::interrupt()
{
if (!started) {
return;
}
if (state != JUMPUP) {
state = JUMPDOWN;
std::cout << "OBJUMP RESET SPEED #4" << std::endl;
speed = 0;
return;
}
// do not interrupt jump if it already passed 80% of total height
if (is_bigjump == false && moved >= JUMP_LIMIT*0.8) {
return;
}
state = JUMPDOWN;
speed = 0;
}
void classjump::finish()
{
jumps_number = 0;
state = NOJUMP;
speed = 0;
started = false;
}
float classjump::get_speed()
{
return speed * gameControl.get_fps_speed_multiplier();
}
short classjump::get_jumps_number()
{
return jumps_number;
}
void classjump::set_jump_acceleration(double value)
{
JUMP_ACCELERATION = value;
}
void classjump::set_jump_limit(int value)
{
JUMP_LIMIT = value;
}