#include "ingame.h" #include "title.h" #include "scene.h" #include "twn_game_api.h" #include "twn_vec.h" #define STB_PERLIN_IMPLEMENTATION #include #include #include static void ingame_tick(State *state) { SceneIngame *scn = (SceneIngame *)state->scene; input_action("player_left", CONTROL_A); input_action("player_right", CONTROL_D); input_action("player_forward", CONTROL_W); input_action("player_backward", CONTROL_S); input_action("player_jump", CONTROL_SPACE); input_action("player_run", CONTROL_LSHIFT); input_action("mouse_capture_toggle", CONTROL_ESCAPE); if (scn->mouse_captured) { const float sensitivity = 0.4f * (float)DEG2RAD; /* TODO: put this in a better place */ scn->yaw += (float)ctx.mouse_movement.x * sensitivity; scn->pitch -= (float)ctx.mouse_movement.y * sensitivity; scn->pitch = clampf(scn->pitch, (float)-M_PI * 0.49f, (float)M_PI * 0.49f); } DrawCameraFromPrincipalAxesResult dir_and_up = draw_camera_from_principal_axes(scn->pos, (float)M_PI_2, scn->roll, scn->pitch, scn->yaw); const Vec3 right = m_vec_norm(m_vec_cross(dir_and_up.direction, dir_and_up.up)); const float speed = 0.04f; /* TODO: put this in a better place */ if (input_action_pressed("player_left")) scn->pos = vec3_sub(scn->pos, m_vec_scale(right, speed)); if (input_action_pressed("player_right")) scn->pos = vec3_add(scn->pos, m_vec_scale(right, speed)); if (input_action_pressed("player_forward")) scn->pos = vec3_add(scn->pos, m_vec_scale(dir_and_up.direction, speed)); if (input_action_pressed("player_backward")) scn->pos = vec3_sub(scn->pos, m_vec_scale(dir_and_up.direction, speed)); if (input_action_pressed("player_jump")) scn->pos.y += speed; if (input_action_pressed("player_run")) scn->pos.y -= speed; /* toggle mouse capture with end key */ if (input_action_just_pressed("mouse_capture_toggle")) scn->mouse_captured = !scn->mouse_captured; input_mouse_captured(scn->mouse_captured); #define TERRAIN_FREQUENCY 0.1f for (int ly = 128; ly--;) { for (int lx = 128; lx--;) { float x = SDL_truncf(scn->pos.x + 64 - (float)lx); float y = SDL_truncf(scn->pos.z + 64 - (float)ly); float d0 = stb_perlin_noise3((float)x * TERRAIN_FREQUENCY, (float)y * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6; float d1 = stb_perlin_noise3((float)(x + 1) * TERRAIN_FREQUENCY, (float)y * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6; float d2 = stb_perlin_noise3((float)(x + 1) * TERRAIN_FREQUENCY, (float)(y - 1) * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6; float d3 = stb_perlin_noise3((float)x * TERRAIN_FREQUENCY, (float)(y - 1) * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6; draw_triangle("/assets/grass.png", (Vec3){ (float)x, d0, (float)y }, (Vec3){ (float)x + 1, d1, (float)y }, (Vec3){ (float)x, d3, (float)y - 1 }, (Vec2){ 128, 128 }, (Vec2){ 128, 0 }, (Vec2){ 0, 128 }); draw_triangle("/assets/grass.png", (Vec3){ (float)x + 1, d1, (float)y }, (Vec3){ (float)x + 1, d2, (float)y - 1 }, (Vec3){ (float)x, d3, (float)y - 1 }, (Vec2){ 128, 0 }, (Vec2){ 0, 0 }, (Vec2){ 0, 128 }); } } draw_skybox("/assets/miramar/miramar_*.tga"); draw_fog(0.9f, 1.0f, 0.05f, (Color){ 140, 147, 160, 255 }); } static void ingame_end(State *state) { free(state->scene); } Scene *ingame_scene(State *state) { (void)state; SceneIngame *new_scene = ccalloc(1, sizeof *new_scene); new_scene->base.tick = ingame_tick; new_scene->base.end = ingame_end; new_scene->mouse_captured = true; m_audio(m_set(path, "music/mod65.xm"), m_opt(channel, "soundtrack"), m_opt(repeat, true)); return (Scene *)new_scene; }