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