#include "comp/entity.h" #include "comp/physical.h" #include "comp/visible.h" #include "comp/mechanical.h" #include "comp/fighter.h" #include "comp/particle.h" #include "anim.h" #include "aoe.h" #include "enemies.h" #include "game.h" #include "geometry.h" #include "level.h" #include "map.h" #include "pathfinding.h" #include "player.h" #include "render.h" #include #include #include #include #include #include #include #include #include #include #include #include #include int main_menu(void) { while(1) { dclear(C_BLACK); dprint(1, 1, C_WHITE, "Rogue Life"); dprint(1, 15, C_WHITE, "[1] Cavern"); dprint(1, 29, C_WHITE, "[2] Lab"); dupdate(); int key = getkey().key; if(key == KEY_EXIT) return -1; if(key == KEY_1) return 1; if(key == KEY_2) return 2; } } 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); int lv = main_menu(); level_t const *level = NULL; if(lv == -1) return 1; if(lv == 1) level = &level_lv1; if(lv == 2) level = &level_lv2; game_t game = { 0 }; camera_t *c = &game.camera; game_load(&game, level); 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; /* Coordinates of a point of interest */ int some_x, some_y; /* Game is paused */ bool paused; } debug; memset(&debug, 0, sizeof debug); //--- // Spawn player //--- player_t player_data = { .entity = NULL, .mechanical_limits = { .max_speed = fix(4.5), .friction = fix(0.7), .dash_speed = fix(20), .dash_duration = fix(1) / 8, .dash_cooldown = fix(1), }, .stat_model = { .HP = fix(1.5), .ATK = fix(1.5), .DEF = fix(1.5), .MAG = fix(1.5), }, .xp_level = 0, .xp_to_next_level = 0, .xp_current = 0, }; game.player_data = &player_data; entity_t *player = entity_make(physical, visible, mechanical, fighter); player_data.entity = player; 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->enemy_data = NULL; /* Initialize stats. This will level up to level 1 */ player_add_xp(&player_data, 0); player_f->HP = player_f->HP_max; player_p->x = fix(game.level->player_spawn_x) + fix(0.5); player_p->y = fix(game.level->player_spawn_y) + fix(0.5); player_p->facing = DOWN; player_p->hitbox = (rect){ -fix(5)/16, fix(5)/16, -fix(2)/16, fix(4)/16 }; player_m->limits = &player_data.mechanical_limits; player_v->sprite_plane = VERTICAL; 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; bool menu_stats_open = false; bool menu_inventory_open = 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; bool bullet_time = menu_stats_open || menu_inventory_open; /* This assumes the frame is not late; can do better with libprof */ fixed_t dt = fix(1) / FRAME_RATE; if(bullet_time) dt >>= 3; if(debug.paused) dt = 0; perf_render = prof_make(); prof_enter(perf_render); render_game(&game, debug.show_hitboxes); if(menu_stats_open) { render_window(48, 24, 300, 154); extern font_t font_rogue; font_t const *old_font = dfont(&font_rogue); anim_frame_render(62, 50, anims_player_Idle.start[RIGHT]); dprint(74, 39, C_WHITE, "Rogue"); dprint(60, 58, C_WHITE, "Level %d", player_data.xp_level); dprint(60, 72, C_WHITE, "XP to next level: %d/%d", player_data.xp_current, player_data.xp_to_next_level); dprint(60, 93, C_WHITE, "HP"); dprint(100, 93, C_WHITE, "%d/%d", player_f->HP, player_f->HP_max);; dprint(60, 108, C_WHITE, "ATK"); dprint(100, 108, C_WHITE, "%d", player_f->ATK); dprint(60, 123, C_WHITE, "MAG"); dprint(100, 123, C_WHITE, "%d", player_f->MAG); dprint(60, 138, C_WHITE, "DEF"); dprint(100, 138, C_WHITE, "%d", player_f->DEF); extern bopti_image_t img_hud_stars; int w = img_hud_stars.width; int h = img_hud_stars.height / 2; dsubimage(182, 94, &img_hud_stars, 0, 0, w, h, DIMAGE_NONE); dsubimage(182, 94, &img_hud_stars, 0, h+1, w * player_data.stat_model.HP / fix(5.0), h, DIMAGE_NONE); dsubimage(182, 109, &img_hud_stars, 0, 0, w, h, DIMAGE_NONE); dsubimage(182, 109, &img_hud_stars, 0, h+1, w * player_data.stat_model.ATK / fix(5.0), h, DIMAGE_NONE); dsubimage(182, 124, &img_hud_stars, 0, 0, w, h, DIMAGE_NONE); dsubimage(182, 124, &img_hud_stars, 0, h+1, w * player_data.stat_model.MAG / fix(5.0), h, DIMAGE_NONE); dsubimage(182, 139, &img_hud_stars, 0, 0, w, h, DIMAGE_NONE); dsubimage(182, 139, &img_hud_stars, 0, h+1, w * player_data.stat_model.DEF / fix(5.0), h, DIMAGE_NONE); dfont(old_font); } else if(menu_inventory_open) { render_window(48, 24, 300, 154); } /* kmalloc_arena_t *_uram = kmalloc_get_arena("_uram"); kmalloc_gint_stats_t *_uram_stats = kmalloc_get_gint_stats(_uram); dprint(1, 15, 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(player_data.mechanical_limits.max_speed)); dprint(15, 55, gray, "[frac] -/+ [X,0,T]"); dprint(3, 70, C_WHITE, "Friction: %g", f2double(player_data.mechanical_limits.friction)); dprint(15, 85, gray, "[F<>D] -/+ [log]"); dprint(3, 115, C_WHITE, "Dash speed: %g tiles/s", f2double(player_data.mechanical_limits.dash_speed)); dprint(15, 130, gray, "[(] -/+ [ln]"); dprint(3, 145, C_WHITE, "Dash duration: %g s", f2double(player_data.mechanical_limits.dash_duration)); dprint(15, 160, gray, "[)] -/+ [sin]"); dprint(3, 175, C_WHITE, "Dash cooldown: %g s", f2double(player_data.mechanical_limits.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)); } debug.some_x = game.level->spawner_count > 0 ? game.level->spawner_x[0] : game.map->width / 2; debug.some_y = game.level->spawner_count > 0 ? game.level->spawner_y[0] : game.map->height / 2; vec2 p = physical_pos(player); vec2 q = vec_i2f_center((ivec2){ debug.some_x, debug.some_y }); bool clear = raycast_clear_hitbox(game.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); fkey_button(6, "PAUSE", debug.paused ? fg : C_WHITE); } static bool record = false; if(keydown(KEY_F6) && keydown(KEY_ALPHA) && !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); 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_F6 && keydown(KEY_VARS)) debug.paused ^= 1; if(ev.key == KEY_XOT) player_data.mechanical_limits.max_speed += fix(1)/8; if(ev.key == KEY_FRAC) { player_data.mechanical_limits.max_speed -= fix(1)/8; if(player_data.mechanical_limits.max_speed < 0) player_data.mechanical_limits.max_speed = 0; } if(ev.key == KEY_LOG) { player_data.mechanical_limits.friction += fix(1) / 32; if(player_data.mechanical_limits.friction > 1) player_data.mechanical_limits.friction = 1; } if(ev.key == KEY_FD) { player_data.mechanical_limits.friction -= fix(1) / 32; if(player_data.mechanical_limits.friction <= 0) player_data.mechanical_limits.friction = 0; } if(ev.key == KEY_LN) player_data.mechanical_limits.dash_speed += fix(0.5); if(ev.key == KEY_LEFTP) { player_data.mechanical_limits.dash_speed -= fix(0.5); if(player_data.mechanical_limits.dash_speed <= 0) player_data.mechanical_limits.dash_speed = 0; } if(ev.key == KEY_SIN) player_data.mechanical_limits.dash_duration += fix(1) / 64; if(ev.key == KEY_RIGHTP) { player_data.mechanical_limits.dash_duration -= fix(1) / 64; if(player_data.mechanical_limits.dash_duration <= 0) player_data.mechanical_limits.dash_duration = 0; } if(ev.key == KEY_COS) player_data.mechanical_limits.dash_cooldown += fix(1) / 8; if(ev.key == KEY_COMMA) { player_data.mechanical_limits.dash_cooldown -= fix(1) / 8; if(player_data.mechanical_limits.dash_cooldown <= 0) player_data.mechanical_limits.dash_cooldown = 0; } #if 0 if(ev.key == KEY_PLUS) camera_zoom(c, c->zoom + 1); if(ev.key == KEY_MINUS) camera_zoom(c, c->zoom - 1); #endif if(ev.key == KEY_SHIFT) attack = true; /* Menus */ if(ev.key == KEY_F5 && !keydown(KEY_VARS)) menu_inventory_open = !menu_inventory_open; if(ev.key == KEY_F6 && !keydown(KEY_VARS) && !keydown(KEY_ALPHA)) menu_stats_open = !menu_stats_open; } /* 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(!debug.paused && 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, game.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(game.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); if(!f || !m || f->enemy_data == NULL || f->HP == 0) continue; enemy_ai(&game, e, dt); if(mechanical_moving(e)) visible_set_anim(e, f->enemy_data->anim_walking, 1); else visible_set_anim(e, f->enemy_data->anim_idle, 1); } /* 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_4(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_4(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; game_shake(&game, 5, fix(0.7)); } if(can_attack && keydown(KEY_F3)) { entity_t *aoe = aoe_make_attack_4( 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; game_shake(&game, 3, fix(1.3)); } if(can_attack && keydown(KEY_F4)) { entity_t *aoe = aoe_make_attack_4(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 buffs (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); // TODO: Kill out-of-bounds entities 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); for(int i = 0; i < game.entity_count; i++) { entity_t *e = game.entities[i]; fighter_t *f = getcomp(e, fighter); visible_t *v = getcomp(e, visible); if(f && f->enemy_data != NULL && v && !v->anim.frame) { visible_set_anim(e, f->enemy_data->anim_idle, 1); } } /* Reduce screenshake time */ game.screenshake_duration -= dt; if(game.screenshake_duration < 0) { game.screenshake_duration = 0; game.screenshake_amplitude = 0; } 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_current_wave_finished(&game)) game.time_victory = game.time_total; /* Next wave */ if(game.time_victory > 0 && game.time_total > game.time_victory + fix(game_current_wave(&game)->delay_after) && 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){ debug.some_x,debug.some_y }); 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; }