add late flips
[carveJwlIkooP6JGAAIwe30JlM.git] / player_dead.c
1 #ifndef PLAYER_DEAD_C
2 #define PLAYER_DEAD_C
3
4 #include "player.h"
5 #include "gui.h"
6
7 static void player__dead_update(void){
8 player_ragdoll_iter( &localplayer.ragdoll );
9 }
10
11 static void player__dead_post_update(void){
12 struct ragdoll_part *part =
13 &localplayer.ragdoll.parts[ localplayer.id_hip-1 ];
14 struct player_dead *d = &player_dead;
15
16 v3f ext_co;
17 v4f ext_q;
18 rb_extrapolate( &part->obj.rb, ext_co, ext_q );
19
20 v3_lerp( d->co_lpf, ext_co, vg.time_frame_delta*4.0f, d->co_lpf );
21 v3_lerp( d->v_lpf, part->obj.rb.v, vg.time_frame_delta*4.0f, d->v_lpf );
22 v3_lerp( d->w_lpf, part->obj.rb.w, vg.time_frame_delta*4.0f, d->w_lpf );
23
24 v3_copy( d->co_lpf, localplayer.rb.co );
25 v3_zero( localplayer.rb.v );
26 v3_zero( localplayer.rb.w );
27
28 if( (skaterift.activity == k_skaterift_default) &&
29 button_down(k_srbind_dead_respawn) ){
30 ent_spawn *spawn = world_find_closest_spawn(
31 world_current_instance(), localplayer.rb.co );
32
33 if( spawn ){
34 v3_copy( spawn->transform.co, localplayer.rb.co );
35 player__reset();
36 srinput.state = k_input_state_resume;
37 }
38 else {
39 vg_error( "No spawns!\n" );
40 }
41 }
42 }
43
44 static void player__dead_animate(void){
45 struct player_dead *d = &player_dead;
46 struct player_dead_animator *animator = &d->animator;
47 struct player_ragdoll *rd = &localplayer.ragdoll;
48 struct skeleton *sk = &localplayer.skeleton;
49
50 m4x3f transforms[ 32 ];
51
52 /* root transform */
53 q_m3x3( localplayer.rb.q, transforms[0] );
54 v3_copy( localplayer.rb.co, transforms[0][3] );
55
56 v4_copy( localplayer.rb.q, animator->transforms[0].q );
57 v3_copy( localplayer.rb.co, animator->transforms[0].co );
58
59 /* colliders with bones transforms */
60 for( int i=0; i<rd->part_count; i++ ){
61 struct ragdoll_part *part = &rd->parts[i];
62
63 m4x3f mtx;
64
65 v4f q_int;
66 v3f co_int;
67
68 float substep = vg.time_fixed_extrapolate;
69 v3_lerp( part->prev_co, part->obj.rb.co, substep, co_int );
70 q_nlerp( part->prev_q, part->obj.rb.q, substep, q_int );
71 v4_copy( part->obj.rb.q, q_int );
72
73 q_m3x3( q_int, mtx );
74 v3_copy( co_int, mtx[3] );
75
76 m4x3_mul( mtx, part->inv_collider_mtx, transforms[part->bone_id] );
77 }
78
79 /* bones without colliders transforms */
80 for( u32 i=1; i<sk->bone_count; i++ ){
81 struct skeleton_bone *sb = &sk->bones[i];
82
83 if( sb->parent && !sb->collider ){
84 v3f delta;
85 v3_sub( sk->bones[i].co, sk->bones[sb->parent].co, delta );
86
87 m4x3f posemtx;
88 m3x3_identity( posemtx );
89 v3_copy( delta, posemtx[3] );
90
91 /* final matrix */
92 m4x3_mul( transforms[sb->parent], posemtx, transforms[i] );
93 }
94 }
95
96 /* measurements */
97 for( u32 i=1; i<sk->bone_count; i++ ){
98 struct skeleton_bone *sb = &sk->bones[i];
99
100 v3_zero( animator->transforms[i].co );
101 q_identity( animator->transforms[i].q );
102
103 m4x3f parent, inverse, local;
104 m3x3_identity( parent );
105 v3_sub( sk->bones[i].co, sk->bones[sb->parent].co, parent[3] );
106 m4x3_mul( transforms[ sb->parent ], parent, parent );
107 m4x3_invert_affine( parent, inverse );
108
109 v3f _s;
110 m4x3_mul( inverse, transforms[i], local );
111 m4x3_decompose( local, animator->transforms[i].co,
112 animator->transforms[i].q, _s );
113 }
114 }
115
116 static void player__dead_pose( void *_animator, player_pose *pose ){
117 struct player_dead_animator *animator = _animator;
118 struct player_ragdoll *rd = &localplayer.ragdoll;
119 struct skeleton *sk = &localplayer.skeleton;
120
121 pose->type = k_player_pose_type_fk_2;
122 pose->board.lean = 0.0f;
123
124 v3_copy( animator->transforms[0].co, pose->root_co );
125 v4_copy( animator->transforms[0].q, pose->root_q );
126
127 for( u32 i=1; i<sk->bone_count; i++ ){
128 v3_copy( animator->transforms[i].co, pose->keyframes[i-1].co );
129 v4_copy( animator->transforms[i].q, pose->keyframes[i-1].q );
130 v3_fill( pose->keyframes[i-1].s, 1.0f );
131 }
132 }
133
134 static void player__dead_post_animate(void){
135 localplayer.cam_velocity_influence = 1.0f;
136 }
137
138 static void player__dead_im_gui(void){
139 }
140
141 static void player__dead_transition(void){
142 localplayer.subsystem = k_player_subsystem_dead;
143 copy_localplayer_to_ragdoll( &localplayer.ragdoll, localplayer.rb.v );
144
145 struct ragdoll_part *part =
146 &localplayer.ragdoll.parts[ localplayer.id_hip-1 ];
147 v3_copy( part->obj.rb.co, player_dead.co_lpf );
148 v3_copy( part->obj.rb.v, player_dead.v_lpf );
149 v3_copy( part->obj.rb.w, player_dead.w_lpf );
150
151 gui_helper_clear();
152 vg_str str;
153
154 struct gui_helper *h;
155 if( (h = gui_new_helper(input_button_list[k_srbind_reset], &str) )){
156 vg_strcat( &str, "rewind" );
157
158 if( world_static.active_instance == k_world_purpose_hub )
159 h->greyed = 1;
160 }
161
162 if( gui_new_helper(input_button_list[k_srbind_dead_respawn], &str ))
163 vg_strcat( &str, "spawn" );
164 }
165
166 static void player__dead_animator_exchange( bitpack_ctx *ctx, void *data ){
167 struct player_dead_animator *animator = data;
168
169 for( u32 i=0; i<localplayer.skeleton.bone_count; i ++ ){
170 bitpack_qv3f( ctx, 24, -1024.0f, 1024.0f, animator->transforms[i].co );
171 bitpack_qquat( ctx, animator->transforms[i].q );
172 }
173 }
174
175 #endif /* PLAYER_DEAD_C */