gate frame fix
[carveJwlIkooP6JGAAIwe30JlM.git] / player.h
1 /*
2 * Copyright 2021-2022 (C) Mount0 Software, Harry Godden - All Rights Reserved
3 * -----------------------------------------------------------------------------
4 *
5 * Player head module
6 *
7 * -----------------------------------------------------------------------------
8 */
9
10 #ifndef PLAYER_H
11 #define PLAYER_H
12
13 #include "audio.h"
14 #include "common.h"
15 #include "world.h"
16 #include "skeleton.h"
17 #include "bvh.h"
18
19 static float
20 k_walkspeed = 7.0f, /* no longer used */
21 k_runspeed = 14.0f,
22 k_board_radius = 0.3f,
23 k_board_length = 0.45f,
24 k_board_allowance = 0.04f,
25 k_friction_lat = 8.8f,
26 k_friction_resistance = 0.01f,
27 k_max_push_speed = 16.0f,
28 k_push_accel = 10.0f,
29 k_push_cycle_rate = 8.0f,
30 k_steer_ground = 2.5f,
31 k_steer_air = 3.6f,
32 k_steer_air_lerp = 0.3f,
33 k_pump_force = 0.0f,
34 k_downforce = 5.0f,
35 k_jump_charge_speed = (1.0f/1.0f),
36 k_jump_force = 5.0f,
37 k_pitch_limit = 1.5f,
38 k_look_speed = 2.0f,
39 k_walk_accel = 5.0f,
40 k_walk_friction = 8.0f;
41
42 static int freecam = 0;
43 static int walk_grid_iterations = 1;
44 static float fc_speed = 10.0f;
45
46 /*
47 * -----------------------------------------------------------------------------
48 * Memory
49 * -----------------------------------------------------------------------------
50 */
51
52 static struct gplayer
53 {
54 /* Physics */
55 rigidbody collide_front, collide_back;
56
57 struct player_phys
58 {
59 rigidbody rb, rb_gate_frame;
60 float iY, siY; /* Yaw inertia */
61
62 v3f a, v_last, m, bob, vl;
63
64 /* Utility */
65 float vswitch, slip, slip_last,
66 reverse;
67
68 float grab, jump;
69 int in_air, on_board, jump_charge, jump_dir;
70
71 m3x3f vr,vr_pstep;
72 }
73 phys,
74 phys_gate_frame;
75
76 float pushing, push_time;
77 int is_dead;
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 m4x3f camera, camera_inverse;
93
94 /* animation */
95 double jump_time;
96 float fslide,
97 fdirz, fdirx,
98 fstand,
99 ffly,
100 fpush,
101 fairdir,
102 fsetup,
103 walk_timer,
104 fjump,
105 fonboard;
106
107 int step_phase;
108
109 /* player model */
110 struct player_model
111 {
112 glmesh mesh;
113 struct skeleton sk;
114 struct skeleton_anim *anim_stand,
115 *anim_highg,
116 *anim_slide,
117 *anim_air,
118 *anim_push, *anim_push_reverse,
119 *anim_ollie, *anim_ollie_reverse,
120 *anim_grabs, *anim_stop,
121 *anim_walk, *anim_run, *anim_idle;
122
123 u32 id_hip,
124 id_ik_hand_l,
125 id_ik_hand_r,
126 id_ik_elbow_l,
127 id_ik_elbow_r,
128 id_head;
129
130 v3f cam_pos;
131
132 struct ragdoll_part
133 {
134 u32 bone_id;
135 v3f offset;
136
137 u32 use_limits;
138 v3f limits[2];
139
140 rigidbody rb;
141 u32 parent;
142 }
143 *ragdoll;
144 u32 ragdoll_count;
145
146 int shoes[2];
147 }
148 mdl;
149 }
150 player =
151 {
152 .collide_front = { .type = k_rb_shape_sphere, .inf.sphere.radius = 0.3f },
153 .collide_back = { .type = k_rb_shape_sphere, .inf.sphere.radius = 0.3f }
154 };
155
156 /*
157 * API
158 */
159 static float *player_get_pos(void);
160 static void player_kill(void);
161 static float *player_cam_pos(void);
162 static void player_save_frame(void);
163 static void player_restore_frame(void);
164
165 /*
166 * Submodules
167 */
168 #include "player_physics.h"
169 #include "player_ragdoll.h"
170 #include "player_model.h"
171 #include "player_animation.h"
172 #include "player_audio.h"
173
174 /*
175 * -----------------------------------------------------------------------------
176 * Events
177 * -----------------------------------------------------------------------------
178 */
179
180 static void player_register(void) /* 0 */
181 {
182 player_model_register();
183 }
184
185 static void player_init(void) /* 1 */
186 {
187 player_model_init();
188
189 rb_init( &player.collide_front );
190 rb_init( &player.collide_back );
191 }
192
193 static void player_update(void) /* 2 */
194 {
195 struct player_phys *phys = &player.phys;
196
197 for( int i=0; i<player.land_log_count; i++ )
198 vg_line_cross( player.land_target_log[i],
199 player.land_target_colours[i], 0.25f);
200
201 if( vg_get_axis("grabl")>0.0f)
202 {
203 player.is_dead = 0;
204 player_restore_frame();
205
206 if( !phys->on_board )
207 {
208 player.angles[0] = atan2f( -phys->rb.forward[2],
209 -phys->rb.forward[0] );
210 }
211
212 player.mdl.shoes[0] = 1;
213 player.mdl.shoes[1] = 1;
214
215 world_routes_notify_reset();
216 }
217
218 if( vg_get_button_down( "switchmode" ) )
219 {
220 phys->on_board ^= 0x1;
221 }
222
223 if( (glfwGetKey( vg_window, GLFW_KEY_O ) || (phys->rb.co[1] < 0.0f)) &&
224 !player.is_dead)
225 {
226 player_ragdoll_copy_model( phys->rb.v );
227 player.is_dead = 1;
228 }
229
230 if( player.is_dead )
231 {
232 player_ragdoll_iter();
233 player_debug_ragdoll();
234
235 if( !freecam )
236 player_animate_death_cam();
237 }
238 else
239 {
240 player_do_motion();
241 player_animate();
242
243 if( !freecam )
244 player_animate_camera();
245 }
246
247 if( freecam )
248 player_freecam();
249
250 player_camera_update();
251 player_audio();
252 }
253
254 static void draw_player(void) /* 3 */
255 {
256 if( player.is_dead )
257 player_model_copy_ragdoll();
258
259 shader_viewchar_use();
260 vg_tex2d_bind( &tex_characters, 0 );
261 shader_viewchar_uTexMain( 0 );
262 shader_viewchar_uCamera( player.camera[3] );
263 shader_viewchar_uPv( vg_pv );
264 shader_link_standard_ub( _shader_viewchar.id, 2 );
265 glUniformMatrix4x3fv( _uniform_viewchar_uTransforms,
266 player.mdl.sk.bone_count,
267 0,
268 (float *)player.mdl.sk.final_mtx );
269
270 mesh_bind( &player.mdl.mesh );
271 mesh_draw( &player.mdl.mesh );
272 }
273
274 /*
275 * -----------------------------------------------------------------------------
276 * API implementation
277 * -----------------------------------------------------------------------------
278 */
279
280 static float *player_get_pos(void)
281 {
282 return player.phys.rb.co;
283 }
284
285 static void player_kill(void)
286 {
287 player.is_dead = 1;
288 player_ragdoll_copy_model( player.phys.rb.v );
289 }
290
291 static float *player_cam_pos(void)
292 {
293 return player.camera_pos;
294 }
295
296
297 #endif /* PLAYER_H */