Repository navigation
Expand file tree
/
Copy pathEntity.cpp
More file actions
168 lines (136 loc) · 5.4 KB
/
Copy pathEntity.cpp
File metadata and controls
168 lines (136 loc) · 5.4 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "Entity.hpp"
Entity::Entity(const char *skinpath) {
Entity::pos = Vec2(0, 0);
Entity::feetOffset = Vec2(0, 0);
Entity::headOffset = Vec2(0, 0);
Entity::vel = Vec2(0, 0);
Entity::lastvel = Vec2(0, 0);
Entity::acc = Vec2(0, 0);
Entity::maxvel = Vec2(0, 0);
Entity::jumpvel = 0;
Entity::jumpstrength = 0;
Entity::jumpstate = Entity::JumpState::STANDING;
Entity::walkstate = Entity::WalkState::STANDING;
Entity::walkdir = Entity::WalkState::RIGHT;
if (!Entity::skin.loadFromFile(skinpath)) {
puts("could not load entity skin");
}
}
void Entity::addLimb(Limb body) {
Entity::body = body;
}
void Entity::addChildLimb(Limb body) {
Entity::body.addChild(body);
}
void Entity::setPos(Vec2 pos) {
Entity::pos = pos;
}
void Entity::jump(float factor) {
if (Entity::jumpstate == Entity::JumpState::STANDING) {
Entity::jumpvel = Entity::jumpstrength * factor;
}
}
void Entity::walk(Entity::WalkState::Direction dir) {
Entity::walkstate = Entity::WalkState::WALKING;
Entity::walkdir = dir;
}
void Entity::physicsHandleJumping(Grid& grid) {
unsigned int dist_to_ceil_left = (unsigned int)grid.raycast(Entity::pos + (Entity::headOffset * Vec2(-1.0, 1.0)) + grid.offset, Vec2(0.0, -1.0), 12.0);
unsigned int dist_to_ceil_right = (unsigned int)grid.raycast(Entity::pos + (Entity::headOffset * Vec2( 1.0, 1.0)) + grid.offset, Vec2(0.0, -1.0), 12.0);
unsigned int dist_to_ceil = Util::min(dist_to_ceil_left, dist_to_ceil_right);
if (dist_to_ceil < Entity::jumpvel) {
/* We hit a block...ouch */
Entity::jumpvel = 0.0;
Entity::vel.y = 1.5;
Entity::jumpstate = Entity::JumpState::FALLING;
} else {
Entity::jumpstate = Entity::JumpState::JUMPING;
Entity::vel.y = Entity::jumpvel;
Entity::pos.y -= Entity::vel.y;
Entity::jumpvel *= 0.85;
if (Entity::jumpvel < 0.25) {
Entity::jumpstate = Entity::JumpState::FALLING;
Entity::jumpvel = 0.0;
}
}
}
void Entity::physicsHandleFalling(Grid& grid) {
unsigned int dist_to_floor_left = (unsigned int)grid.raycast(Entity::pos + (Entity::feetOffset * Vec2(-1.0, 1.0)) + grid.offset, Vec2(0.0, 1.0), 64.0);
unsigned int dist_to_floor_right = (unsigned int)grid.raycast(Entity::pos + (Entity::feetOffset * Vec2( 1.0, 1.0)) + grid.offset, Vec2(0.0, 1.0), 64.0);
unsigned int dist_to_floor = Util::min(dist_to_floor_left, dist_to_floor_right);
if ((float)dist_to_floor > Entity::vel.y) {
/* Fall faster! */
Entity::vel.y += 0.225 + (Entity::vel.y / Entity::maxvel.y) * 0.1;
/* Limit y-velocity to maximal velocity */
Util::limit(Entity::vel.y, Entity::maxvel.y);
/* Update position */
Entity::pos.y += Entity::vel.y;
/* Update JumpState */
Entity::jumpstate = Entity::JumpState::FALLING;
} else {
/* Move distance to floor, Stop falling */
Entity::vel.y = 0.0;
Entity::pos.y += (float)dist_to_floor;
Entity::jumpstate = Entity::JumpState::STANDING;
}
}
void Entity::physicsHandleWalking(Grid& grid) {
/* If standing and movement is close to 0, set to 0 */
if (Entity::walkstate != Entity::WalkState::WALKING && abs(Entity::vel.x) < 0.001) {
Entity::vel.x = 0.0;
}
/* When walking, update position */
if (Entity::walkstate == Entity::WalkState::WALKING) {
Entity::vel.x += Entity::acc.x * Entity::walkdir;
}
/* Don't move faster than maximum speed */
if (fabs(Entity::vel.x) > Entity::maxvel.x) {
const float sign = fabs(Entity::vel.x) / Entity::vel.x;
Entity::vel.x = Entity::maxvel.x * sign;
}
/* If no longer walking, apply friction */
if (Entity::walkstate != Entity::WalkState::WALKING) {
Block& standing_on = grid.atPoint(Entity::pos + (Entity::feetOffset * Vec2(0.0, 1.0)) + grid.offset);
float current_friction = 0.785;
if (standing_on.id > 0) {
current_friction = standing_on.friction;
}
Entity::vel.x *= current_friction;
}
/* Handle walking/collision right */
const float distance = 16.0;
const Vec2 dir_right(1.0, 0.0);
float rays_right[] = {
grid.raycast(Entity::pos + grid.offset , dir_right, distance, 9999.0),
grid.raycast(Entity::pos + grid.offset + (Entity::headOffset * Vec2(0.0, 1.0)) + Vec2(0.0, 2.0), dir_right, distance, 9999.0),
grid.raycast(Entity::pos + grid.offset + (Entity::feetOffset * Vec2(0.0, 1.0)) - Vec2(0.0, 2.0), dir_right, distance, 9999.0)
};
unsigned int rays_right_min = Util::min((unsigned int)rays_right[0], (unsigned int)rays_right[1], (unsigned int)rays_right[2]);
if (rays_right_min < distance) {
if (Entity::walkdir == Entity::WalkState::RIGHT) {
Entity::vel.x = 0;
}
}
/* Handle walking/collision left */
const Vec2 dir_left(-1.0, 0.0);
float rays_left[] = {
grid.raycast(Entity::pos + grid.offset , dir_left, distance, 9999.0),
grid.raycast(Entity::pos + grid.offset + (Entity::headOffset * Vec2(0.0, 1.0)) + Vec2(0.0, 2.0), dir_left, distance, 9999.0),
grid.raycast(Entity::pos + grid.offset + (Entity::feetOffset * Vec2(0.0, 1.0)) - Vec2(0.0, 2.0), dir_left, distance, 9999.0)
};
unsigned int rays_left_min = Util::min((unsigned int)rays_left[0], (unsigned int)rays_left[1], (unsigned int)rays_left[2]);
if (rays_left_min < distance) {
if (Entity::walkdir == Entity::WalkState::LEFT) {
Entity::vel.x = 0;
}
}
Entity::pos.x += Entity::vel.x;
}
void Entity::physicsUpdate(Grid& grid) {
if (Entity::jumpvel > 0.0) {
Entity::physicsHandleJumping(grid);
} else {
Entity::physicsHandleFalling(grid);
}
Entity::physicsHandleWalking(grid);
}