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