-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer_controller.cpp
226 lines (183 loc) · 6.05 KB
/
player_controller.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "player_controller.h"
using namespace godot;
void PlayerController::_register_methods() {
register_method("_physics_process", &PlayerController::_physics_process);
register_method("_ready", &PlayerController::_ready);
}
PlayerController::PlayerController() {
}
PlayerController::~PlayerController() {
}
/**
* Init
*/
void PlayerController::_init() {
time_passed = 0.0;
animation_tree_value = 0.0;
}
Vector3 PlayerController::adjust_facing(Vector3 p_facing, Vector3 p_target, float p_step, float p_adjust_rate, Vector3 current_gn) {
Vector3 n = p_target;
Vector3 t = n.cross(current_gn).normalized();
float x = n.dot(p_facing);
float y = t.dot(p_facing);
int ang = atan2(y, x);
if (abs(ang) < 0.001) {
return p_facing;
}
int s;
if (ang > 0) {
s = 1;
} else if(ang == 0) {
s = 0;
} else {
s = -1;
}
ang = ang * s;
int turn = ang * p_adjust_rate * p_step;
int a;
if (ang < turn) {
a = ang;
} else {
a = turn;
}
ang = (ang - a) * s;
return (n * cos(ang) + t * sin(ang)) * p_facing.length();
}
void PlayerController::_ready() {
init_translation = get_translation();
init_rotation = get_rotation();
animation_player = (AnimationPlayer *) get_node("AnimationPlayer");
animation_tree = (AnimationTree *) get_node("AnimationTree");
animation_tree -> set("parameters/blend_position", animation_tree_value);
// play_animation("idle");
}
void PlayerController::_physics_process(float delta) {
Vector3 lv = linear_velocity;
Vector3 g = Vector3(0, -9.8, 0);
lv += g * delta;
int anim = ANIM_FLOOR;
Vector3 up = -g.normalized();
float vv = up.dot(lv);
Vector3 hv = lv - up * vv;
Vector3 hdir = hv.normalized();
float hspeed = hv.length();
Vector3 dir = Vector3();
/*
Spatial *camera = (Spatial *) get_node("target/Camera");
Transform cam_xform = camera -> get_global_transform();*/
Input* input = Input::get_singleton();
bool up_input = input -> is_action_pressed("ui_up");
bool down_input = input -> is_action_pressed("ui_down");
bool right_input = input -> is_action_pressed("ui_right");
bool left_input = input -> is_action_pressed("ui_left");
/*
bool right_input_just = input -> is_action_just_pressed("ui_right");
bool left_input_just = input -> is_action_just_pressed("ui_left");
if(right_input_just) {
Vector3 current_rot = get_rotation();
set_rotation(Vector3(0, current_rot.y - M_PI/2, 0));
} else if(left_input_just) {
Vector3 current_rot = get_rotation();
set_rotation(Vector3(0, current_rot.y + M_PI/2, 0));
}*/
if (up_input) {
// dir += -cam_xform.basis[2];
dir += get_transform().basis.z.normalized() * 2;
}
if (down_input) {
// dir += cam_xform.basis[2];
dir += -get_transform().basis.z.normalized() * 2;
}
if (left_input) {
// dir += -cam_xform.basis[0];
dir += get_transform().basis.x.normalized() * 2;
}
if (right_input) {
dir += -get_transform().basis.x.normalized() * 2;
// dir += cam_xform.basis[0];
}
if (down_input || up_input || left_input || right_input) {
if (animation_tree_value < 1.0) {
animation_tree_value += 0.04;
}
// play_animation("walking");
} else {
// play_animation("idle");
if(animation_tree_value > 0.0) {
animation_tree_value -= 0.04;
}
}
animation_tree -> set("parameters/blend_position", animation_tree_value);
bool jump_attempt = input -> is_action_pressed("jump");
bool shoot_attempt = input -> is_action_pressed("shoot");
Vector3 target_dir = (dir - up * dir.dot(up)).normalized();
if (is_on_floor()) {
bool sharp_turn = hspeed > 0.1 and (acos(target_dir.dot(hdir))) * 180/M_PI > sharp_turn_threshold;
if (dir.length() > 0.1 and !sharp_turn) {
if(hspeed > 0.001) {
hdir = adjust_facing(hdir, target_dir, delta, 1.0/hspeed * turn_speed, up);
facing_dir = hdir;
} else {
hdir = target_dir;
}
if (hspeed < max_speed) {
hspeed += accel * delta;
}
} else {
hspeed -= deaccel * delta;
if (hspeed < 0) {
hspeed = 0;
}
}
hv = hdir * hspeed;
if (!jumping && jump_attempt) {
vv = 7.0;
jumping = true;
}
} else {
if (vv > 0) {
anim = ANIM_AIR_UP;
} else {
anim = ANIM_AIR_DOWN;
}
if (dir.length() > 0.1) {
hv += target_dir * (accel * 0.2) * delta;
if (hv.length() > max_speed) {
hv = hv.normalized() * max_speed;
}
} else {
if (air_idle_deaccel) {
hspeed = hspeed - (deaccel * 0.2) * delta;
if (hspeed < 0) {
hspeed = 0;
}
hv = hdir * hspeed;
}
}
}
if (jumping and vv) {
jumping = false;
}
lv = hv + up * vv;
if (is_on_floor()) {
movement_dir = lv;
}
linear_velocity = move_and_slide(lv, -g.normalized());
}
void PlayerController::play_animation(String animation) {
if (animation == "walking") {
animation_player -> play("lp_guy|Run");
} else if(animation == "idle") {
animation_player -> play("lp_guy|Idle");
}
}
void PlayerController::spawn_bullet(float speed) {
ResourceLoader *resource_loader = ResourceLoader::get_singleton();
Ref<PackedScene> res = resource_loader->load("res://ModelScenes/Bullet.tscn");
KinematicBody* body = (KinematicBody *) res -> instance();
body -> set_translation(get_transform().origin + Vector3(1.409, 1.255, 0));
body -> set("bullet_direction", Vector3(1, 0, 0));
body -> set("bullet_speed", speed);
body -> set_scale(Vector3(0.1, 0.1, 0.1));
get_parent() ->add_child(body);
}