fork/training-mode-ce

git clone https://git.julia.blue/fork/training-mode-ce

Alex Harrisonsmall eggs event cleanup33f399a

nix
8.6 KiB296 linesraw
1#include "../MexTK/mex.h"
2#include "eggs.h"
3
4static int egg_counter = 0;
5static int high_score = 0;
6static Vec3 coll_pos, last_coll_pos;
7static GOBJ *egg_gobj;
8static CmSubject *cam;
9static GOBJ *hud_score_gobj, *hud_best_gobj;
10static JOBJ *hud_score_jobj, *hud_best_jobj;
11static int canvas;
12static Text *hud_score_text, *hud_best_text;
13
14void Exit(GOBJ *menu) 
15{
16    stc_match->state = 3;
17    Match_EndVS();
18}
19
20void Retry(GOBJ *menu) 
21{
22    stc_match->end_kind = MATCHENDKIND_RETRY;
23    stc_match->state = 3;
24    Match_EndVS();
25}
26
27float RandomRange(float low, float high)
28{
29    return low + (high - low) * HSD_Randf();
30}
31
32void ChangeHitDisplay(GOBJ *menu_gobj, int value)
33{
34    // loop through all fighters
35    GOBJ *this_fighter = (*stc_gobj_lookup)[MATCHPLINK_FIGHTER];
36    while (this_fighter != 0)
37    {
38        // get data
39        FighterData *this_fighter_data = this_fighter->userdata;
40
41        // toggle
42        this_fighter_data->show_hit = value;
43
44        // get next fighter
45        this_fighter = this_fighter->next;
46    }
47}
48
49void StartFreePractice(GOBJ *gobj) {
50    Options_Main[OPT_FREEPRACTICE].disable = 1;
51    Options_Main[OPT_DAMAGETHRESHOLD].disable = 0;
52    Options_Main[OPT_SCALE].disable = 0;
53    Options_Main[OPT_VELOCITY].disable = 0;
54    Options_Main[OPT_COLLISION].disable = 0;
55    stc_match->match.timer = MATCH_TIMER_COUNTUP;
56}
57
58void Egg_OnChangeSize(GOBJ *menu, int value)
59{
60    if (egg_gobj != 0)
61    {
62        Item_Destroy(egg_gobj);
63        egg_gobj = Egg_Spawn();
64    }
65}
66
67GOBJ *Egg_Spawn(void)
68{
69    float min_ground_width = 8;
70    float camera_left = Stage_GetCameraLeft();
71    float camera_right = Stage_GetCameraRight();
72    float camera_top = Stage_GetCameraTop();
73    float camera_bottom = Stage_GetCameraBottom();
74    int is_ground = 0;
75
76    while (1)
77    {
78        // pick a random point on the stage via raycast down from random point
79        float x = RandomRange(camera_left, camera_right);
80        float from_y = RandomRange(camera_bottom, camera_top);
81        float to_y = from_y - 1000;
82        Vec3  line_unk;
83        int line_index, line_kind;
84
85        // check main ground collision
86        is_ground = GrColl_RaycastGround(&coll_pos, &line_index, &line_kind, &line_unk, -1, -1, -1, 0, 
87            x, from_y, x, to_y, 0);
88        if (is_ground == 0)
89            continue; // try a new random point
90        
91        // check distance from last collision
92        float distance = Math_Vec3Distance(&coll_pos, &last_coll_pos);
93        if (distance < 25.f)
94            continue; 
95
96        // check if too close to right end of the stage
97        Vec3 near_pos;
98        float near_x = x + min_ground_width;
99        is_ground = GrColl_RaycastGround(&near_pos, &line_index, &line_kind, &line_unk, -1, -1, -1, 0, 
100            near_x, from_y, near_x, to_y, 0);
101        if (is_ground == 0)
102            continue; 
103
104        // check if too close to left end of the stage
105        near_x = x - min_ground_width;
106        is_ground = GrColl_RaycastGround(&near_pos, &line_index, &line_kind, &line_unk, -1, -1, -1, 0, 
107            near_x, from_y, near_x, to_y, 0);
108        if (is_ground == 0)
109            continue; 
110        
111        break; 
112    }
113    
114    // random Y velocity on spawn
115    Vec3 rand_velocity = {0, (2 + HSD_Randf()) * Options_Main[OPT_VELOCITY].val, 0};
116    SpawnItem item_egg = {
117        .it_kind = ITEM_EGG,
118        .pos = coll_pos,
119        .pos2 = coll_pos,
120        .vel = rand_velocity,
121    };
122
123    last_coll_pos = coll_pos;
124
125    return Item_CreateItem2(&item_egg);
126}
127
128int Egg_OnTakeDamage(GOBJ *gobj)
129{
130    // gfx and sfx
131    ItemData *egg_data = egg_gobj->userdata;
132    int damage = egg_data->dmg.recent;
133    if (damage >= Options_Main[OPT_DAMAGETHRESHOLD].val){
134        Effect_SpawnSync(1232, gobj, egg_data->pos);
135        Item_PlayOnDestroySFXAgain(egg_data, 244, 127, 64);
136        
137        // manage old and new egg
138        Egg_Destroy(gobj);
139        egg_gobj = Egg_Spawn();
140        egg_counter++;
141    }
142    return 0;
143}
144
145void Event_Init(GOBJ *gobj)
146{
147    // initialize egg camera subject
148    cam = CameraSubject_Alloc();
149    cam->boundleft_proj = -10;
150    cam->boundright_proj = 10;
151    cam->boundtop_proj = 10;
152    cam->boundbottom_proj = -10;
153
154    // hud setup
155    hud_score_gobj = GObj_Create(0, 0, 0);
156    GObj_AddObject(hud_score_gobj, 3, hud_score_jobj);
157    GObj_AddGXLink(hud_score_gobj, GXLink_Common, GXLINK_HUD, 80);
158
159    hud_best_gobj = GObj_Create(0, 0, 0);
160    GObj_AddObject(hud_best_gobj, 3, hud_best_jobj);
161    GObj_AddGXLink(hud_best_gobj, GXLink_Common, GXLINK_HUD, 80);
162    canvas = Text_CreateCanvas(2, 0, 0, 0, 0, GXLINK_HUD, 81, 19);
163    
164    // Create text objects
165    hud_score_text = Text_CreateText(2, canvas);
166    hud_score_text->kerning = 1;
167    hud_score_text->align = 0;
168    hud_score_text->use_aspect = 1;
169    hud_score_text->trans.X = 50;
170    hud_score_text->trans.Y = 25;
171    hud_score_text->viewport_scale.X = 1;
172    hud_score_text->viewport_scale.Y = 1;
173    
174    hud_best_text = Text_CreateText(2, canvas);
175    hud_best_text->kerning = 1;
176    hud_best_text->align = 0;
177    hud_best_text->use_aspect = 1;
178    hud_best_text->trans.X = 50;
179    hud_best_text->trans.Y = 40;
180    hud_best_text->scale.X = 0.35;
181    hud_best_text->scale.Y = 0.35;
182    hud_best_text->viewport_scale.X = 1;
183    hud_best_text->viewport_scale.Y = 1;
184
185    // dummy text
186    Text_AddSubtext(hud_score_text, 0, 0, "-");
187    Text_AddSubtext(hud_best_text, 0, 0, "-");
188    
189    high_score = Events_GetSavedScore(stc_memcard->EventBackup.event);
190
191    stc_match->end_kind = MATCHENDKIND_NONE;
192}
193
194void Event_Think(GOBJ *event)
195{
196    // reset stale moves
197    for (int i = 0; i < 6; i++)
198    {
199        // check if fighter exists
200        GOBJ *fighter = Fighter_GetGObj(i);
201        if (fighter != 0)
202        {
203            // reset stale move table
204            int *stale_move_table = Fighter_GetStaleMoveTable(i);
205            memset(stale_move_table, 0, 0x2C);
206        }
207    }
208
209    GOBJ *hmn = Fighter_GetGObj(0);
210    FighterData *hmn_data = hmn->userdata;
211
212    // wait for first frame of player control to spawn first egg
213    if (egg_gobj == 0 && hmn_data->flags.input_enable)
214    {   
215        egg_gobj = Egg_Spawn();
216    }
217
218    // when egg exists
219    if (egg_gobj != 0) 
220    {
221        // don't allow holding or nudging
222        ItemData *egg_data = egg_gobj->userdata;
223        egg_data->can_hold = 0;
224        egg_data->can_nudge = 0;
225        egg_data->self_vel.X = 0;
226
227        // apply scaling changes
228        JOBJ *egg_jobj = Item_GetBoneJOBJ(egg_gobj, 0);
229        egg_jobj->scale.X = EggOptions_Size[Options_Main[OPT_SCALE].val];
230        egg_jobj->scale.Y = EggOptions_Size[Options_Main[OPT_SCALE].val];
231        egg_jobj->scale.Z = EggOptions_Size[Options_Main[OPT_SCALE].val];
232        egg_data->scale = EggOptions_Size[Options_Main[OPT_SCALE].val];
233
234        // show egg hurtbox
235        egg_data->show_hit = Options_Main[OPT_COLLISION].val;
236        egg_data->show_model = !Options_Main[OPT_COLLISION].val;
237
238        // set this callback on every frame or else it gets overwritten
239        egg_data->it_func->OnTakeDamage = Egg_OnTakeDamage;
240
241        // update camera position
242        cam->cam_pos.X = egg_data->pos.X;
243        cam->cam_pos.Y = egg_data->pos.Y + 15; // add so it's not awkwardly low
244        cam->cam_pos.Z = egg_data->pos.Z;
245    }
246
247    // Update egg count, hide when paused
248    if (Pause_CheckStatus(1) != 2)
249    {
250        Text_SetText(hud_score_text, 0, "%d", egg_counter);
251        if (stc_match->match.timer == MATCH_TIMER_COUNTUP)
252        {
253            Text_SetText(hud_best_text, 0, "Free Practice");
254            Text_SetColor(hud_best_text, 0, &text_gold);
255        }
256        else if (high_score > 0)
257        {
258            Text_SetText(hud_best_text, 0, "Best: %d", high_score);
259            Text_SetColor(hud_best_text, 0, &text_white);
260        }
261        else
262        {
263            Text_SetText(hud_best_text, 0, "");
264        }
265    } 
266    else
267    {
268        Text_SetText(hud_score_text, 0, "");
269        Text_SetText(hud_best_text, 0, "");
270    }
271
272    // After 1 minute and not in free practice
273    if (stc_match->time_frames == 3600 && stc_match->match.timer != MATCH_TIMER_COUNTUP)
274    {   
275        int event_id = stc_memcard->EventBackup.event;
276        Events_SetEventAsPlayed(event_id);
277        Match_SetEndGraphic(MATCHENDKIND_VSGAME);
278
279        // high score logic
280        int high_score = Events_GetSavedScore(event_id);
281        if (egg_counter > high_score)
282        {
283            MatchInfo_0x0010_store(0x9c40); // Makes "new record" audio happen, IDK why
284            Match_SetPostMatchSFX(325);
285            Events_StoreEventScore(event_id, egg_counter);
286        }
287        else
288        {
289            Match_SetPostMatchSFX(324);
290        }
291
292        Match_EndImmediate();
293    }
294}
295
296EventMenu *Event_Menu = &Menu_Main;