forked from filoper/ms-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhysicsObject.h
152 lines (116 loc) · 4.18 KB
/
PhysicsObject.h
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
144
145
146
147
148
149
150
151
152
// This file is part of the continued Journey MMORPG client
// Copyright (C) 2015-2019 Daniel Allendorf, Ryan Payton
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include "../../Constants.h"
#include "../../Template/Interpolated.h"
#include "../../Template/Point.h"
namespace ms {
// Structure that contains all properties for movement calculations
struct MovingObject {
Linear<double> x;
Linear<double> y;
double hspeed = 0.0;
double vspeed = 0.0;
void normalize() {
x.normalize();
y.normalize();
}
void move() {
x += hspeed;
y += vspeed;
}
void set_x(double d) { x.set(d); }
void set_y(double d) { y.set(d); }
void limitx(double d) {
x = d;
hspeed = 0.0;
}
void limity(double d) {
y = d;
vspeed = 0.0;
}
void movexuntil(double d, uint16_t delay) {
if (delay) {
double hdelta = d - x.get();
hspeed = Constants::TIMESTEP * hdelta / delay;
}
}
void moveyuntil(double d, uint16_t delay) {
if (delay) {
double vdelta = d - y.get();
vspeed = Constants::TIMESTEP * vdelta / delay;
}
}
bool hmobile() const { return hspeed != 0.0; }
bool vmobile() const { return vspeed != 0.0; }
bool mobile() const { return hmobile() || vmobile(); }
double crnt_x() const { return x.get(); }
double crnt_y() const { return y.get(); }
double next_x() const { return x + hspeed; }
double next_y() const { return y + vspeed; }
int16_t get_x() const {
double rounded = std::round(x.get());
return static_cast<int16_t>(rounded);
}
int16_t get_y() const {
double rounded = std::round(y.get());
return static_cast<int16_t>(rounded);
}
int16_t get_last_x() const {
double rounded = std::round(x.last());
return static_cast<int16_t>(rounded);
}
int16_t get_last_y() const {
double rounded = std::round(y.last());
return static_cast<int16_t>(rounded);
}
Point<int16_t> get_position() const { return { get_x(), get_y() }; }
int16_t get_absolute_x(double viewx, float alpha) const {
double interx = x.normalized() ? std::round(x.get()) : x.get(alpha);
return static_cast<int16_t>(std::round(interx + viewx));
}
int16_t get_absolute_y(double viewy, float alpha) const {
double intery = y.normalized() ? std::round(y.get()) : y.get(alpha);
return static_cast<int16_t>(std::round(intery + viewy));
}
Point<int16_t> get_absolute(double viewx, double viewy, float alpha) const {
return { get_absolute_x(viewx, alpha), get_absolute_y(viewy, alpha) };
}
};
// Structure that contains all properties necessary for physics calculations
struct PhysicsObject : public MovingObject {
// Determines which physics engine to use
enum Type { NORMAL, ICE, SWIMMING, FLYING, FIXATED };
enum Flag { NOGRAVITY = 0x0001, TURNATEDGES = 0x0002, CHECKBELOW = 0x0004 };
Type type = Type::NORMAL;
int32_t flags = 0;
uint16_t fhid = 0;
double fhslope = 0.0;
int8_t fhlayer = 0;
double groundbelow = 0.0;
bool onground = true;
bool enablejd = false;
double hforce = 0.0;
double vforce = 0.0;
double hacc = 0.0;
double vacc = 0.0;
bool is_flag_set(Flag f) { return (flags & f) != 0; }
bool is_flag_not_set(Flag f) { return !is_flag_set(f); }
void set_flag(Flag f) { flags |= f; }
void clear_flag(Flag f) { flags &= ~f; }
void clear_flags() { flags = 0; }
};
} // namespace ms