update overlay to have ps controls too
[carveJwlIkooP6JGAAIwe30JlM.git] / world_map.c
1 #include "skaterift.h"
2 #include "world_map.h"
3 #include "world.h"
4 #include "input.h"
5 #include "gui.h"
6 #include "menu.h"
7 #include "scene.h"
8
9 struct world_map world_map;
10
11 static void world_map_get_dir( v3f dir ){
12 /* idk */
13 dir[0] = -sqrtf(0.5f);
14 dir[2] = sqrtf(0.5f);
15 dir[1] = 1.0f;
16 v3_normalize(dir);
17 }
18
19 static void world_map_get_plane( v4f plane ){
20 world_instance *world = &world_static.instances[ world_map.world_id ];
21 f32 h = localplayer.rb.co[1];
22 if( world_map.world_id != world_static.active_instance )
23 h = (world->scene_geo.bbx[0][1] + world->scene_geo.bbx[1][1]) * 0.5f;
24
25 v4_copy( (v4f){0.0f,1.0f,0.0f,h}, plane );
26 }
27
28 static void respawn_world_to_plane_pos( v3f pos, v2f plane_pos ){
29 v3f dir;
30 world_map_get_dir( dir );
31 v3_negate(dir,dir);
32 v4f plane;
33 world_map_get_plane( plane );
34
35 v3f co;
36 f32 t = ray_plane( plane, pos, dir );
37 v3_muladds( pos, dir, t, co );
38 plane_pos[0] = co[0];
39 plane_pos[1] = co[2];
40 }
41
42 static void respawn_map_draw_icon( vg_camera *cam,
43 enum gui_icon icon, v3f pos ){
44 v4f v;
45 v3_copy( pos, v );
46 v[3] = 1.0f;
47 m4x4_mulv( cam->mtx.pv, v, v );
48 v2_divs( v, v[3], v );
49
50 gui_draw_icon( icon, (v2f){ v[0]*0.5f+0.5f,v[1]*0.5f+0.5f }, 1.0f );
51 }
52
53 void world_map_pre_update(void)
54 {
55 if( skaterift.activity != k_skaterift_world_map ) return;
56
57 if( button_down( k_srbind_mback ) ){
58 gui_helper_clear();
59 srinput.state = k_input_state_resume;
60 skaterift.activity = k_skaterift_menu;
61 menu.page = 0xffffffff;
62 menu_open_page( "Main Menu", k_ent_menuitem_stack_append );
63 return;
64 }
65
66 if( button_down( k_srbind_maccept ) ){
67 skaterift.activity = k_skaterift_default;
68 srinput.state = k_input_state_resume;
69
70 if( world_map.spawn ){
71 world_static.active_instance = world_map.world_id;
72 player__spawn( world_map.spawn );
73 }
74 return;
75 }
76
77 world_instance *world = &world_static.instances[ world_map.world_id ];
78 v3f *bbx = world->scene_geo.bbx;
79 f32 *pos = world_map.plane_pos;
80
81 v2f steer;
82 joystick_state( k_srjoystick_steer, steer );
83 v2_normalize_clamp( steer );
84
85 m2x2f rm;
86 m2x2_create_rotation( rm, -0.25f*VG_PIf );
87 m2x2_mulv( rm, steer, steer );
88
89 v2_muladds( pos, steer, vg.time_frame_delta * 200.0f, pos );
90 v2_minv( (v2f){ bbx[1][0], bbx[1][2] }, pos, pos );
91 v2_maxv( (v2f){ bbx[0][0], bbx[0][2] }, pos, pos );
92
93 /* update camera */
94 vg_camera *cam = &world_map.cam;
95 v3f dir;
96 world_map_get_dir(dir);
97
98 v4f plane;
99 world_map_get_plane( plane );
100
101 v3f co = { pos[0], plane[3]*plane[1], pos[1] };
102 v3_muladds( co, dir, world_map.boom_dist, cam->pos );
103
104 vg_line_cross( co, VG__RED, 10.0f );
105
106 cam->angles[0] = 0.25f * VG_PIf;
107 cam->angles[1] = 0.25f * VG_PIf;
108 cam->farz = 5000.0f;
109 cam->nearz = 10.0f;
110 cam->fov = 40.0f;
111
112 vg_camera_update_transform( cam );
113 vg_camera_update_view( cam );
114 vg_camera_update_projection( cam );
115 vg_camera_finalize( cam );
116
117 /* pick spawn */
118 world_map.spawn = NULL;
119 f32 closest2 = INFINITY;
120
121 for( u32 i=0; i<mdl_arrcount(&world->ent_spawn); i++ ){
122 ent_spawn *spawn = mdl_arritm(&world->ent_spawn,i);
123
124 v4f v;
125 v3_copy( spawn->transform.co, v );
126 v[3] = 1.0f;
127 m4x4_mulv( cam->mtx.pv, v, v );
128 v2_divs( v, v[3], v );
129
130 f32 d2 = v2_length2(v);
131 if( d2 < closest2 ){
132 world_map.spawn = spawn;
133 closest2 = d2;
134 }
135 }
136
137 /* icons
138 * ---------------------*/
139 for( u32 i=0; i<mdl_arrcount(&world->ent_challenge); i++ ){
140 ent_challenge *challenge = mdl_arritm( &world->ent_challenge, i );
141
142 enum gui_icon icon = k_gui_icon_exclaim_2d;
143 if( challenge->status )
144 icon = k_gui_icon_tick_2d;
145
146 respawn_map_draw_icon( cam, icon, challenge->transform.co );
147 }
148
149 for( u32 i=0; i<mdl_arrcount(&world->ent_skateshop); i++ ){
150 ent_skateshop *shop = mdl_arritm( &world->ent_skateshop, i );
151 if( shop->type == k_skateshop_type_boardshop ){
152 respawn_map_draw_icon( cam, k_gui_icon_board, shop->transform.co );
153 }
154 else if( shop->type == k_skateshop_type_worldshop ){
155 respawn_map_draw_icon( cam, k_gui_icon_world, shop->transform.co );
156 }
157 }
158
159 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
160 ent_gate *gate = mdl_arritm( &world->ent_gate, i );
161 if( gate->flags & k_ent_gate_nonlocal ){
162 respawn_map_draw_icon( cam, k_gui_icon_rift, gate->co[0] );
163 }
164 }
165
166 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
167 ent_route *route = mdl_arritm( &world->ent_route, i );
168
169 v4f colour;
170 v4_copy( route->colour, colour );
171 v3_muls( colour, 1.6666f, colour );
172 gui_icon_setcolour( colour );
173 respawn_map_draw_icon( cam, k_gui_icon_rift_run_2d,
174 route->board_transform[3] );
175 }
176
177 for( u32 i=0; i<mdl_arrcount(&world->ent_glider); i ++ ){
178 ent_glider *glider = mdl_arritm( &world->ent_glider, i );
179
180 v4f colour = { 1,1,1,1 };
181
182 if( !(glider->flags & 0x1) )
183 v3_muls( colour, 0.5f, colour );
184 gui_icon_setcolour( colour );
185
186 respawn_map_draw_icon( cam, k_gui_icon_glider, glider->transform.co );
187 }
188 }
189
190 void world_map_enter(void)
191 {
192 skaterift.activity = k_skaterift_world_map;
193 world_map.world_id = world_static.active_instance;
194
195 world_instance *world = &world_static.instances[ world_map.world_id ];
196 v3f *bbx = world->scene_geo.bbx;
197
198 respawn_world_to_plane_pos( localplayer.rb.co, world_map.plane_pos );
199 world_map.boom_dist = 400.0f;
200 world_map.home_select = 0;
201
202 gui_helper_clear();
203
204 vg_str text;
205 if( gui_new_helper( input_joy_list[k_srjoystick_steer], &text ) )
206 vg_strcat( &text, "move" );
207
208 if( gui_new_helper( input_button_list[k_srbind_maccept], &text ) )
209 vg_strcat( &text, "spawn" );
210
211 if( gui_new_helper( input_button_list[k_srbind_mback], &text ) )
212 vg_strcat( &text, "exit" );
213 }