add jumping
[carveJwlIkooP6JGAAIwe30JlM.git] / player.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef PLAYER_H
6 #define PLAYER_H
7
8 #include "audio.h"
9 #include "common.h"
10 #include "world.h"
11 #include "skeleton.h"
12 #include "bvh.h"
13
14 static float
15 k_walkspeed = 20.0f, /* no longer used */
16 k_runspeed = 20.0f,
17 k_board_radius = 0.3f,
18 k_board_length = 0.45f,
19 k_board_allowance = 0.04f,
20 k_friction_lat = 8.8f,
21 k_friction_resistance = 0.01f,
22 k_max_push_speed = 16.0f,
23 k_push_accel = 10.0f,
24 k_push_cycle_rate = 8.0f,
25 k_steer_ground = 2.5f,
26 k_steer_air = 3.6f,
27 k_steer_air_lerp = 0.3f,
28 k_pump_force = 0.0f,
29 k_downforce = 5.0f,
30 k_walk_downforce = 8.0f,
31 k_jump_charge_speed = (1.0f/1.0f),
32 k_jump_force = 5.0f,
33 k_pitch_limit = 1.5f,
34 k_look_speed = 2.0f,
35 k_walk_accel = 150.0f,
36 k_walk_friction = 8.0f;
37
38 static int freecam = 0;
39 static int walk_grid_iterations = 1;
40 static float fc_speed = 10.0f;
41
42 /*
43 * -----------------------------------------------------------------------------
44 * Memory
45 * -----------------------------------------------------------------------------
46 */
47
48 static struct gplayer
49 {
50 /* Physics */
51 rigidbody collide_front, collide_back;
52
53 struct player_phys
54 {
55 rigidbody rb, rb_gate_frame;
56 float iY, siY; /* Yaw inertia */
57
58 v3f a, v_last, m, bob, vl;
59
60 /* Utility */
61 float vswitch, slip, slip_last,
62 reverse;
63
64 float grab, jump, pushing, push_time;
65 double start_push;
66 int in_air, on_board, jump_charge, jump_dir;
67
68 m3x3f vr,vr_pstep;
69 }
70 phys,
71 phys_gate_frame;
72
73 int is_dead;
74
75 v3f land_target;
76 v3f land_target_log[22];
77 u32 land_target_colours[22];
78 int land_log_count;
79
80 v3f handl_target, handr_target,
81 handl, handr;
82
83 /* Camera */
84 float air_blend;
85
86 v3f camera_pos, smooth_localcam;
87 v2f angles;
88 m4x3f camera, camera_inverse;
89
90 /* animation */
91 double jump_time;
92 float fslide,
93 fdirz, fdirx,
94 fstand,
95 ffly,
96 fpush,
97 fairdir,
98 fsetup,
99 walk_timer,
100 fjump,
101 fonboard;
102
103 int step_phase;
104
105 /* player model */
106 struct player_model
107 {
108 glmesh mesh;
109 struct skeleton sk;
110 struct skeleton_anim *anim_stand,
111 *anim_highg,
112 *anim_slide,
113 *anim_air,
114 *anim_push, *anim_push_reverse,
115 *anim_ollie, *anim_ollie_reverse,
116 *anim_grabs, *anim_stop,
117 *anim_walk, *anim_run, *anim_idle,
118 *anim_jump;
119
120 u32 id_hip,
121 id_ik_hand_l,
122 id_ik_hand_r,
123 id_ik_elbow_l,
124 id_ik_elbow_r,
125 id_head;
126
127 v3f cam_pos;
128
129 struct ragdoll_part
130 {
131 u32 bone_id;
132 v3f offset;
133
134 u32 use_limits;
135 v3f limits[2];
136
137 rigidbody rb;
138 u32 parent;
139 }
140 *ragdoll;
141 u32 ragdoll_count;
142
143 int shoes[2];
144 }
145 mdl;
146 }
147 player =
148 {
149 .collide_front = { .type = k_rb_shape_sphere, .inf.sphere.radius = 0.3f },
150 .collide_back = { .type = k_rb_shape_sphere, .inf.sphere.radius = 0.3f }
151 };
152
153 /*
154 * API
155 */
156 static float *player_get_pos(void);
157 static void player_kill(void);
158 static float *player_cam_pos(void);
159 static void player_save_frame(void);
160 static void player_restore_frame(void);
161
162 /*
163 * Submodules
164 */
165 #include "player_physics.h"
166 #include "player_ragdoll.h"
167 #include "player_model.h"
168 #include "player_animation.h"
169 #include "player_audio.h"
170
171 /*
172 * -----------------------------------------------------------------------------
173 * Events
174 * -----------------------------------------------------------------------------
175 */
176
177 static void player_init(void) /* 1 */
178 {
179 rb_init( &player.phys.rb );
180 rb_init( &player.collide_front );
181 rb_init( &player.collide_back );
182
183 vg_convar_push( (struct vg_convar){
184 .name = "walk_speed",
185 .data = &k_walkspeed,
186 .data_type = k_convar_dtype_f32,
187 .opt_f32 = { .clamp = 0 },
188 .persistent = 1
189 });
190
191 vg_convar_push( (struct vg_convar){
192 .name = "run_speed",
193 .data = &k_runspeed,
194 .data_type = k_convar_dtype_f32,
195 .opt_f32 = { .clamp = 0 },
196 .persistent = 1
197 });
198
199 vg_convar_push( (struct vg_convar){
200 .name = "walk_accel",
201 .data = &k_walk_accel,
202 .data_type = k_convar_dtype_f32,
203 .opt_f32 = { .clamp = 0 },
204 .persistent = 1
205 });
206
207 vg_convar_push( (struct vg_convar){
208 .name = "fc",
209 .data = &freecam,
210 .data_type = k_convar_dtype_i32,
211 .opt_i32 = { .min=0, .max=1, .clamp=1 },
212 .persistent = 1
213 });
214
215 vg_convar_push( (struct vg_convar){
216 .name = "fcs",
217 .data = &fc_speed,
218 .data_type = k_convar_dtype_f32,
219 .opt_f32 = { .clamp = 0 },
220 .persistent = 1
221 });
222
223 vg_function_push( (struct vg_cmd){
224 .name = "reset",
225 .function = reset_player
226 });
227
228 /* other systems */
229 vg_loader_highwater( player_model_init, player_model_free, NULL );
230 }
231
232 /* Deal with input etc */
233 static void player_update_pre(void)
234 {
235 struct player_phys *phys = &player.phys;
236
237 if( vg_get_button_down( "reset" ) )
238 {
239 player.is_dead = 0;
240 player_restore_frame();
241
242 if( !phys->on_board )
243 {
244 player.angles[0] = atan2f( -phys->rb.forward[2],
245 -phys->rb.forward[0] );
246 }
247
248 player.mdl.shoes[0] = 1;
249 player.mdl.shoes[1] = 1;
250
251 world_routes_notify_reset();
252 }
253
254 if( vg_get_button_down( "switchmode" ) )
255 {
256 phys->on_board ^= 0x1;
257
258 if( phys->on_board )
259 {
260 v3_muladds( phys->rb.v, phys->rb.forward, 0.2f, phys->rb.v );
261 }
262 }
263 }
264
265 static void player_update_fixed(void) /* 2 */
266 {
267 struct player_phys *phys = &player.phys;
268
269 if( player.is_dead )
270 {
271 player_ragdoll_iter();
272 }
273 else
274 {
275 player_do_motion();
276 }
277
278 player_audio(); /* FUTURE: can probably move this to post()
279 BUT, it uses deltas from fixed step physics,
280 AND this *might* be what we want for realtime
281 audio, anyway. */
282 }
283
284 static void player_update_post(void)
285 {
286 for( int i=0; i<player.land_log_count; i++ )
287 vg_line_cross( player.land_target_log[i],
288 player.land_target_colours[i], 0.25f);
289
290 if( player.is_dead )
291 {
292 player_debug_ragdoll();
293
294 if( !freecam )
295 player_animate_death_cam();
296 }
297 else
298 {
299 player_animate();
300
301 if( !freecam )
302 player_animate_camera();
303 }
304
305 if( freecam )
306 player_freecam();
307
308 player_camera_update();
309 }
310
311 static void draw_player(void) /* 3 */
312 {
313 if( player.is_dead )
314 player_model_copy_ragdoll();
315
316 shader_viewchar_use();
317 vg_tex2d_bind( &tex_characters, 0 );
318 shader_viewchar_uTexMain( 0 );
319 shader_viewchar_uCamera( player.camera[3] );
320 shader_viewchar_uPv( vg.pv );
321 shader_link_standard_ub( _shader_viewchar.id, 2 );
322 glUniformMatrix4x3fv( _uniform_viewchar_uTransforms,
323 player.mdl.sk.bone_count,
324 0,
325 (float *)player.mdl.sk.final_mtx );
326
327 mesh_bind( &player.mdl.mesh );
328 mesh_draw( &player.mdl.mesh );
329 }
330
331 /*
332 * -----------------------------------------------------------------------------
333 * API implementation
334 * -----------------------------------------------------------------------------
335 */
336
337 static float *player_get_pos(void)
338 {
339 return player.phys.rb.co;
340 }
341
342 static void player_kill(void)
343 {
344 player.is_dead = 1;
345 player_ragdoll_copy_model( player.phys.rb.v );
346 }
347
348 static float *player_cam_pos(void)
349 {
350 return player.camera_pos;
351 }
352
353
354 #endif /* PLAYER_H */