#include "comp/entity.h" #include "comp/physical.h" #include "comp/visible.h" #include "comp/mechanical.h" #include "comp/fighter.h" #include "anim.h" #include "aoe.h" #include "enemies.h" #include "game.h" #include "geometry.h" #include "level.h" #include "map.h" #include "particles.h" #include "pathfinding.h" #include "render.h" #include #include #include #include #include #include #include #include #include #include #include #include int main(void) { /* Enable %f etc. in printf()-like functions */ __printf_enable_fp(); /* Enable %D for decimal fixed-point in printf()-like functions */ __printf_enable_fixed(); /* Initialize the PRNG */ srand(0xc0ffee + 1); /* Initialize the benchmarking/profiling library */ prof_init(); /* Open the USB connection (for screenshots with fxlink) */ usb_interface_t const *interfaces[] = { &usb_ff_bulk, NULL }; usb_open(interfaces, GINT_CALL_NULL); game_t game = { 0 }; map_t *map = &game.map; camera_t *c = &game.camera; game_load(&game, &lv_1); struct { /* Show variables */ bool show_vars; /* Show hitboxes */ bool show_hitboxes; /* Show BFS field around player */ bool show_bfs_field; pfg_all2one_t bfs_field; /* Show path to some fixed cell */ bool show_path; pfg_path_t grid_path; pfc_path_t continuous_path; /* Show performance metrics */ bool show_perf; } debug; memset(&debug, 0, sizeof debug); //--- // Spawn player //--- mechanical_limits_t ml_player = { .max_speed = fix(4.5), .friction = fix(0.7), .dash_speed = fix(20), .dash_duration = fix(1) / 8, .dash_cooldown = fix(1), }; entity_t *player = enemy_make(ENEMY_BAT, 2); physical_t *player_p = getcomp(player, physical); visible_t *player_v = getcomp(player, visible); mechanical_t *player_m = getcomp(player, mechanical); fighter_t *player_f = getcomp(player, fighter); player_f->combo_length = 2; player_f->combo_next = 0; player_f->combo_delay = fix(0); player_f->identity = 0; player_f->HP_max += 100; player_f->HP += 100; player_p->x = fix(9.5); player_p->y = fix(4.5); player_p->hitbox = (rect){ -fix(5)/16, fix(5)/16, -fix(2)/16, fix(4)/16 }; player_m->limits = &ml_player; player_v->z = 0; player_v->shadow_size = 4; visible_set_anim(player, &anims_player_Idle, 1); game_add_entity(&game, player); game.player = player; //--- // Main loop //--- /* Start a timer at the FRAME_RATE setting to schedule new frames */ volatile int frame_tick = 1; int timer_id = timer_configure(TIMER_ANY, 1000000 / FRAME_RATE, GINT_CALL_SET(&frame_tick)); if(timer_id >= 0) timer_start(timer_id); bool stop = false; prof_t perf_render, perf_simul; uint32_t time_render=0, time_simul=0; while(!stop) { while(!frame_tick) sleep(); frame_tick = 0; bool attack = false; /* This assumes the frame is not late; can do better with libprof */ fixed_t dt = fix(1) / FRAME_RATE; perf_render = prof_make(); prof_enter(perf_render); render_game(&game, debug.show_hitboxes); /* kmalloc_arena_t *_uram = kmalloc_get_arena("_uram"); kmalloc_gint_stats_t *_uram_stats = kmalloc_get_gint_stats(_uram); dprint(1, 1, C_WHITE, "Memory: %d", _uram_stats->used_memory); */ /* Developer/tweaking menu */ if(debug.show_vars) { uint32_t *vram = (void *)gint_vram; for(int y = 0; y < 224; y++) { for(int i = 0; i < 396/4; i++) vram[i] = (vram[i] & 0xf7def7de) >> 1; vram += 396/2; } uint16_t gray = C_RGB(16, 16, 16); dprint(3, 40, C_WHITE, "Player speed: %g tiles/s", f2double(ml_player.max_speed)); dprint(15, 55, gray, "[frac] -/+ [X,0,T]"); dprint(3, 70, C_WHITE, "Friction: %g", f2double(ml_player.friction)); dprint(15, 85, gray, "[F<>D] -/+ [log]"); dprint(3, 115, C_WHITE, "Dash speed: %g tiles/s", f2double(ml_player.dash_speed)); dprint(15, 130, gray, "[(] -/+ [ln]"); dprint(3, 145, C_WHITE, "Dash duration: %g s", f2double(ml_player.dash_duration)); dprint(15, 160, gray, "[)] -/+ [sin]"); dprint(3, 175, C_WHITE, "Dash cooldown: %g s", f2double(ml_player.dash_cooldown)); dprint(15, 190, gray, "[,] -/+ [cos]"); } if(debug.show_path) { if(debug.grid_path.points) for(int i = 0; i < debug.grid_path.length; i++) { ivec2 j = camera_map2screen(c, vec_i2f_center(debug.grid_path.points[i])); ivec2 k = camera_map2screen(c, vec_i2f_center(debug.grid_path.points[i+1])); dline(j.x, j.y, k.x, k.y, C_RGB(31, 0, 31)); } if(debug.continuous_path.points) for(int i = 0; i < debug.continuous_path.length; i++) { ivec2 j = camera_map2screen(c, debug.continuous_path.points[i]); ivec2 k = camera_map2screen(c, debug.continuous_path.points[i+1]); dline(j.x, j.y, k.x, k.y, C_RGB(0, 31, 31)); } vec2 p = physical_pos(player); vec2 q = vec_i2f_center((ivec2){ 6, 9 }); bool clear = raycast_clear_hitbox(map, p, q, player_p->hitbox); ivec2 j = camera_map2screen(c, p); ivec2 k = camera_map2screen(c, q); dline(j.x, j.y, k.x, k.y, clear ? C_GREEN : C_RED); extern int rch_c1, rch_c2; extern vec2 rch_p1[64], rch_p2[64]; for(int k = 0; k < rch_c1 && k < 64; k++) { ivec2 i = camera_map2screen(c, rch_p1[k]); dline(i.x-2, i.y, i.x+2, i.y, C_RGB(0, 31, 31)); dline(i.x, i.y-2, i.x, i.y+2, C_RGB(0, 31, 31)); } for(int k = 0; k < rch_c2 && k < 64; k++) { ivec2 i = camera_map2screen(c, rch_p2[k]); dline(i.x-2, i.y, i.x+2, i.y, C_RGB(0, 31, 31)); dline(i.x, i.y-2, i.x, i.y+2, C_RGB(0, 31, 31)); } } if(debug.show_bfs_field && game.paths_to_player.direction) { render_pfg_all2one(&game.paths_to_player, c); } if(debug.show_perf) { dprint(1, 15, C_WHITE, "Render: %.3D ms", time_render); dprint(1, 29, C_WHITE, "Simul: %.3D ms", time_simul); } if(keydown(KEY_VARS)) { int fg = C_RGB(31, 31, 0); fkey_button(1, "PARAMS", debug.show_vars ? fg : C_WHITE); fkey_button(2, "HITBOX", debug.show_hitboxes ? fg : C_WHITE); fkey_button(3, "BFS", debug.show_bfs_field ? fg : C_WHITE); fkey_button(4, "PATHS", debug.show_path ? fg : C_WHITE); fkey_button(5, "PERF", debug.show_perf ? fg : C_WHITE); } static bool record = false; if(keydown(KEY_F6) && !keydown(KEY_VARS) && usb_is_open()) { record = !record; } if(record) { usb_fxlink_videocapture(false); } /* Instead of dupdate(); for accurate performance measurements. Leaving the DMA running during the simulation affects the results in extreme proportions (can turn 1 ms of simulation into 11 ms measured) */ r61524_display(gint_vram, 0, 224, R61524_DMA_WAIT); prof_leave(perf_render); time_render = prof_time(perf_render); //--- perf_simul = prof_make(); prof_enter(perf_simul); game_spawn_enemies(&game); game_sort_particles(&game); key_event_t ev; while((ev = pollevent()).type != KEYEV_NONE) { if(ev.type == KEYEV_UP) continue; if(ev.key == KEY_MENU) gint_osmenu(); if(ev.key == KEY_EXIT) stop = true; /* Debug settings */ if(ev.key == KEY_F1 && keydown(KEY_VARS)) debug.show_vars ^= 1; if(ev.key == KEY_F2 && keydown(KEY_VARS)) debug.show_hitboxes ^= 1; if(ev.key == KEY_F3 && keydown(KEY_VARS)) debug.show_bfs_field ^= 1; if(ev.key == KEY_F4 && keydown(KEY_VARS)) debug.show_path ^= 1; if(ev.key == KEY_F5 && keydown(KEY_VARS)) debug.show_perf ^= 1; if(ev.key == KEY_XOT) ml_player.max_speed += fix(1)/8; if(ev.key == KEY_FRAC) { ml_player.max_speed -= fix(1)/8; if(ml_player.max_speed < 0) ml_player.max_speed = 0; } if(ev.key == KEY_LOG) { ml_player.friction += fix(1) / 32; if(ml_player.friction > 1) ml_player.friction = 1; } if(ev.key == KEY_FD) { ml_player.friction -= fix(1) / 32; if(ml_player.friction <= 0) ml_player.friction = 0; } if(ev.key == KEY_LN) ml_player.dash_speed += fix(0.5); if(ev.key == KEY_LEFTP) { ml_player.dash_speed -= fix(0.5); if(ml_player.dash_speed <= 0) ml_player.dash_speed = 0; } if(ev.key == KEY_SIN) ml_player.dash_duration += fix(1) / 64; if(ev.key == KEY_RIGHTP) { ml_player.dash_duration -= fix(1) / 64; if(ml_player.dash_duration <= 0) ml_player.dash_duration = 0; } if(ev.key == KEY_COS) ml_player.dash_cooldown += fix(1) / 8; if(ev.key == KEY_COMMA) { ml_player.dash_cooldown -= fix(1) / 8; if(ml_player.dash_cooldown <= 0) ml_player.dash_cooldown = 0; } if(ev.key == KEY_PLUS) camera_zoom(c, c->zoom + 1); if(ev.key == KEY_MINUS) camera_zoom(c, c->zoom - 1); if(ev.key == KEY_SHIFT) attack = true; } /* Camera movement */ #if 0 fixed_t vx = CAMERA_SPEED_X; fixed_t vy = CAMERA_SPEED_Y; if(keydown(KEY_4) || keydown(KEY_7) || keydown(KEY_1)) camera_move(c, -fmul(dt, vx), 0); if(keydown(KEY_6) || keydown(KEY_9) || keydown(KEY_3)) camera_move(c, fmul(dt, vx), 0); if(keydown(KEY_8) || keydown(KEY_7) || keydown(KEY_9)) camera_move(c, 0, -fmul(dt, vy)); if(keydown(KEY_2) || keydown(KEY_1) || keydown(KEY_3)) camera_move(c, 0, fmul(dt, vy)); #endif /* Player movement */ if(player_f->HP > 0) { int dir = -1; if(keydown(KEY_UP)) dir = UP; if(keydown(KEY_DOWN)) dir = DOWN; if(keydown(KEY_LEFT)) dir = LEFT; if(keydown(KEY_RIGHT)) dir = RIGHT; if(keydown(KEY_F1) && !keydown(KEY_VARS)) { int dash_dir = (dir >= 0) ? dir : player_p->facing; mechanical_dash(player, dash_dir); } mechanical_move4(player, dir, dt, map); if(dir >= 0) visible_set_anim(player, &anims_player_Walking, 1); else visible_set_anim(player, &anims_player_Idle, 1); } /* Directions to reach the player from anywhere on the grid */ pfg_all2one_free(&game.paths_to_player); game.paths_to_player = pfg_bfs(map, vec_f2i(physical_pos(player))); /* Enemy AI */ if(player_f->HP > 0) for(int i = 0; i < game.entity_count; i++) { entity_t *e = game.entities[i]; fighter_t *f = getcomp(e, fighter); mechanical_t *m = getcomp(e, mechanical); physical_t *p = getcomp(e, physical); if(!f || !m || f->identity == 0 || f->HP == 0) continue; /* Go within 1 block of the player */ vec2 direction = { 0, 0 }; vec2 pos = physical_pos(e); bool in_range = dist2(pos, physical_pos(player)) <= fix(1); if(!in_range) { pfg_path_t path = pfg_bfs_inwards(&game.paths_to_player, vec_f2i(pos)); if(path.points) { direction = pfc_shortcut_one(&path, pos, physical_pos(player), getcomp(e, physical)->hitbox); pfg_path_free(&path); } if(direction.x && direction.y) { direction.x -= pos.x; direction.y -= pos.y; } } mechanical_move(e, direction, dt, map); if(direction.x > 0) p->facing = RIGHT; else if(direction.x < 0) p->facing = LEFT; else if(p->x < player_p->x) p->facing = RIGHT; else p->facing = LEFT; if(mechanical_moving(e)) visible_set_anim(e, enemies[f->identity]->anim_walking, 1); else visible_set_anim(e, enemies[f->identity]->anim_idle, 1); if(in_range && !f->current_attack) { /* Enemy attack */ int facing = frdir((vec2){ player_p->x - p->x, player_p->y - p->y }); entity_t *aoe = aoe_make_attack(AOE_HIT, e, facing); game_add_entity(&game, aoe); f->current_attack = aoe; f->attack_follows_movement = true; } } /* Player attack */ if(player_f->HP > 0 && attack && !player_f->current_attack) { int hit_number=0, effect=AOE_SLASH; /* If hitting within .25s of the previous hit ending, combo! */ if(abs(player_f->combo_delay) < fix(0.25)) hit_number = player_f->combo_next; player_f->combo_next = (hit_number + 1) % player_f->combo_length; if(hit_number == 0) effect = AOE_SLASH; if(hit_number == 1) effect = AOE_IMPALE; entity_t *aoe = aoe_make_attack(effect, player, player_p->facing); game_add_entity(&game, aoe); visible_set_anim(player, &anims_player_Attack, 2); player_f->current_attack = aoe; player_f->attack_follows_movement = true; player_f->combo_delay = getcomp(aoe, aoe)->lifetime; } /* Player skills */ bool can_attack = player_f->HP > 0 && !player_f->current_attack && !keydown(KEY_VARS); if(can_attack && keydown(KEY_F2)) { entity_t *aoe = aoe_make_attack(AOE_SHOCK, player, player_p->facing); game_add_entity(&game, aoe); visible_set_anim(player, &anims_player_Attack, 2); player_f->current_attack = aoe; player_f->attack_follows_movement = true; } if(can_attack && keydown(KEY_F3)) { entity_t *aoe = aoe_make_attack( AOE_JUDGEMENT, player, player_p->facing); game_add_entity(&game, aoe); visible_set_anim(player, &anims_player_Attack, 2); player_f->current_attack = aoe; player_f->attack_follows_movement = false; } if(can_attack && keydown(KEY_F4)) { entity_t *aoe = aoe_make_attack(AOE_BULLET, player, player_p->facing); game_add_entity(&game, aoe); visible_set_anim(player, &anims_player_Attack, 2); player_f->current_attack = aoe; player_f->attack_follows_movement = false; } /* Ideas for additional skills: - Freeze enemies - Barrier around player - Teleport - Time manipulation - Player buuffs (short but strong) or wide area debuggs Ideas for items: - Healing potion - Equipment - XP boosts - Weaker but longer-lasting buffs */ game_update_animations(&game, dt); game_update_aoes(&game, dt); game_update_particles(&game, dt); game_remove_dead_entities(&game); player_f->combo_delay -= dt; /* Reset default anims */ if(!player_v->anim.frame) visible_set_anim(player, &anims_player_Idle, 1); game.time_total += dt; game.time_wave += dt; if(game.time_defeat == 0 && player_f->HP == 0) game.time_defeat = game.time_total; if(game.time_victory == 0 && player_f->HP > 0 && game.entity_count==1) game.time_victory = game.time_total; /* Next wave */ if(game.time_victory > 0 && game.time_total > game.time_victory+fix(2) && game.wave < game.level->wave_count) game_next_wave(&game); /* Visual pathfinding debug */ if(debug.show_path) { pfg_path_free(&debug.grid_path); pfc_path_free(&debug.continuous_path); vec2 target = vec_i2f_center((ivec2){ 6, 9 }); debug.grid_path = pfg_bfs_outwards(&game.paths_to_player, vec_f2i(target)); debug.continuous_path = pfc_shortcut_full(&debug.grid_path, physical_pos(player), target, player_p->hitbox); } prof_leave(perf_simul); time_simul = prof_time(perf_simul); } timer_stop(timer_id); prof_quit(); usb_close(); return 1; }