-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.cpp
More file actions
124 lines (103 loc) · 4.89 KB
/
Copy pathplayer.cpp
File metadata and controls
124 lines (103 loc) · 4.89 KB
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
#include "player.h"
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <optional>
void Player::move(int duration, std::optional<float> rotation, float rotationOffset, std::optional<float> slipperiness,
bool isSprinting, bool isSneaking, std::optional<int> speedEffect, std::optional<int> slowEffect,
State state) {
slipperiness = slipperiness.value_or(m_defaultGroundSlipperiness);
speedEffect = speedEffect.value_or(m_speedEffect);
slowEffect = slowEffect.value_or(m_slowEffect);
bool overrideRotation = false;
if (rotation.has_value()) {
overrideRotation = true;
rotation = rotation.value() + rotationOffset;
}
if (this->hasModifier(Modifiers::WATER)) {
slipperiness = 0.8f / 0.91f;
} else if (this->hasModifier(Modifiers::LAVA)) {
slipperiness = 0.5f / 0.91f;
}
if (rotationOffset == 45) this->inputs = "wa";
m_state = state;
if (((m_sneakDelay && m_previouslySneaking) || (!m_sneakDelay && isSneaking)) && this->hasModifier(Modifiers::LAVA))
m_state = State::AIRBORNE;
float sprintjumpBoost = m_sprintjumpBoost;
if (m_reverse) sprintjumpBoost *= -1;
for (int i = 0; i < duration; i++) {
this->update(overrideRotation, rotationOffset, isSprinting, isSneaking, slipperiness.value(),
rotation.value_or(0.0f), speedEffect.value(), slowEffect.value(), sprintjumpBoost);
}
}
float Player::getMovementMultiplier(float slipperiness, bool isSprinting, int16_t speed, int16_t slow) {
if (this->hasModifier(Modifiers::WATER) || this->hasModifier(Modifiers::LAVA)) {
return 0.02f;
} else if (m_state == State::AIRBORNE) {
if ((m_airSprintDelay && m_previouslySprinting) || (!m_airSprintDelay && isSprinting)) {
return 0.02f + 0.02f * 0.3f;
} else {
return 0.02f;
}
} else {
float multiplier = 0.1f;
if (speed > 0) multiplier *= 1.0f + (0.2f * speed);
if (slow > 0) multiplier *= 1.0f + std::fmax(-0.15f * speed, 0.0f);
if (isSprinting) multiplier *= 1.3f;
float drag = 0.91f * slipperiness;
return multiplier * (0.16277136f / (drag * drag * drag));
}
}
void Player::update(bool overrideRotation, float rotationOffset, bool isSprinting, bool isSneaking, float& slipperiness,
float rotation, int speed, int slow, float sprintjumpBoost) {
if (!overrideRotation) rotation = this->getAngle() + rotationOffset;
this->position.add(this->velocity);
if (this->hasModifier(Modifiers::SOULSAND)) this->velocity.scale(0.4);
Vector2<float> direction = this->movementValues();
this->velocity.scale(0.91 * m_previousSlipperiness);
if (m_inertiaAxis == 1) {
if (std::fabs(this->velocity.x) < m_inertiaThreshold || m_previouslyInWeb) this->velocity.x = 0.0f;
if (std::fabs(this->velocity.z) < m_inertiaThreshold || m_previouslyInWeb) this->velocity.z = 0.0f;
}
if (this->hasModifier(Modifiers::BLOCK)) direction.scale(0.2f);
if ((m_sneakDelay && m_previouslySneaking) || (!m_sneakDelay && isSneaking)) direction.scale(0.3f);
direction.scale(0.98f);
float multiplier = this->getMovementMultiplier(slipperiness, isSprinting, speed, slow);
m_previousSlipperiness = slipperiness;
if (m_state == State::JUMPING) {
m_state = State::AIRBORNE;
slipperiness = 1.0f;
if (isSprinting) {
float facing = rotation * 0.017453292f;
this->velocity.x -= double(this->mcsin(facing) * sprintjumpBoost);
this->velocity.z += double(this->mccos(facing) * sprintjumpBoost);
}
}
float distance = direction.sqrMagnitude();
if (distance > 0.0f) {
distance = std::sqrtf(distance);
distance = std::max(distance, 1.0f);
distance = multiplier / distance;
direction.scale(distance);
float sinYaw = this->mcsin(rotation * PI / 180.0f);
float cosYaw = this->mccos(rotation * PI / 180.0f);
this->velocity.x += direction.z * cosYaw - direction.x * sinYaw;
this->velocity.z += direction.x * cosYaw + direction.z * sinYaw;
}
if (this->hasModifier(Modifiers::WEB)) this->velocity.scale(0.25f);
if (this->hasModifier(Modifiers::LADDER)) {
this->velocity.x = std::clamp(this->velocity.x, 0.15, -0.15);
this->velocity.z = std::clamp(this->velocity.z, 0.15, -0.15);
}
m_previouslySprinting = isSprinting;
m_previouslySneaking = isSneaking;
m_previouslyInWeb = this->hasModifier(Modifiers::WEB);
m_lastTurn = rotation - m_lastRotation;
m_lastRotation = rotation;
}
std::ostream& operator<<(std::ostream& os, const Player& p) {
os << "Velocity: (" << std::fixed << std::setprecision(p.precision) << p.velocity.x << ", " << p.velocity.z << ")"
<< std::endl
<< "Position: (" << p.position.x << ", " << p.position.z << ")" << std::endl;
return os;
}