2024-07-08 00:44:20 +00:00
|
|
|
#include "ingame.h"
|
|
|
|
#include "title.h"
|
|
|
|
#include "scene.h"
|
|
|
|
|
2024-09-16 13:17:00 +00:00
|
|
|
#include "twn_game_api.h"
|
2024-10-07 15:37:44 +00:00
|
|
|
#include "twn_vec.h"
|
2024-08-19 16:19:22 +00:00
|
|
|
|
|
|
|
#define STB_PERLIN_IMPLEMENTATION
|
|
|
|
#include <stb_perlin.h>
|
2024-10-06 21:00:36 +00:00
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2024-07-08 06:46:12 +00:00
|
|
|
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
static void ingame_tick(State *state) {
|
|
|
|
SceneIngame *scn = (SceneIngame *)state->scene;
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-10-22 17:32:17 +00:00
|
|
|
input_bind_action_control("player_left", CONTROL_A);
|
|
|
|
input_bind_action_control("player_right", CONTROL_D);
|
|
|
|
input_bind_action_control("player_forward", CONTROL_W);
|
|
|
|
input_bind_action_control("player_backward", CONTROL_S);
|
|
|
|
input_bind_action_control("player_jump", CONTROL_SPACE);
|
|
|
|
input_bind_action_control("player_run", CONTROL_LSHIFT);
|
|
|
|
input_bind_action_control("mouse_capture_toggle", CONTROL_ESCAPE);
|
|
|
|
|
|
|
|
if (scn->mouse_captured) {
|
2024-10-28 09:34:48 +00:00
|
|
|
const float sensitivity = 0.4f * (float)DEG2RAD; /* TODO: put this in a better place */
|
2024-10-12 17:24:47 +00:00
|
|
|
scn->yaw += (float)ctx.mouse_movement.x * sensitivity;
|
|
|
|
scn->pitch -= (float)ctx.mouse_movement.y * sensitivity;
|
2024-10-28 09:34:48 +00:00
|
|
|
scn->pitch = clampf(scn->pitch, (float)-M_PI * 0.49f, (float)M_PI * 0.49f);
|
2024-07-30 21:18:01 +00:00
|
|
|
}
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-10-28 09:34:48 +00:00
|
|
|
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));
|
2024-07-30 21:05:28 +00:00
|
|
|
const float speed = 0.04f; /* TODO: put this in a better place */
|
2024-10-08 07:12:30 +00:00
|
|
|
if (input_is_action_pressed("player_left"))
|
2024-10-28 09:34:48 +00:00
|
|
|
scn->pos = vec3_sub(scn->pos, m_vec_scale(right, speed));
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-10-08 07:12:30 +00:00
|
|
|
if (input_is_action_pressed("player_right"))
|
2024-10-28 09:34:48 +00:00
|
|
|
scn->pos = vec3_add(scn->pos, m_vec_scale(right, speed));
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-10-08 07:12:30 +00:00
|
|
|
if (input_is_action_pressed("player_forward"))
|
2024-10-28 09:34:48 +00:00
|
|
|
scn->pos = vec3_add(scn->pos, m_vec_scale(dir_and_up.direction, speed));
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-10-08 07:12:30 +00:00
|
|
|
if (input_is_action_pressed("player_backward"))
|
2024-10-28 09:34:48 +00:00
|
|
|
scn->pos = vec3_sub(scn->pos, m_vec_scale(dir_and_up.direction, speed));
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-10-08 07:12:30 +00:00
|
|
|
if (input_is_action_pressed("player_jump"))
|
2024-10-28 09:34:48 +00:00
|
|
|
scn->pos.y += speed;
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-10-08 07:12:30 +00:00
|
|
|
if (input_is_action_pressed("player_run"))
|
2024-10-28 09:34:48 +00:00
|
|
|
scn->pos.y -= speed;
|
2024-07-30 21:05:28 +00:00
|
|
|
|
|
|
|
/* toggle mouse capture with end key */
|
2024-10-22 17:32:17 +00:00
|
|
|
if (input_is_action_just_pressed("mouse_capture_toggle"))
|
|
|
|
scn->mouse_captured = !scn->mouse_captured;
|
|
|
|
|
|
|
|
input_set_mouse_captured(scn->mouse_captured);
|
2024-07-29 12:21:39 +00:00
|
|
|
|
2024-08-19 16:19:22 +00:00
|
|
|
#define TERRAIN_FREQUENCY 0.1f
|
|
|
|
|
2024-10-01 12:31:18 +00:00
|
|
|
for (int ly = 64; ly--;) {
|
|
|
|
for (int lx = 64; lx--;) {
|
2024-10-28 09:34:48 +00:00
|
|
|
float x = SDL_truncf(scn->pos.x + 32 - (float)lx);
|
|
|
|
float y = SDL_truncf(scn->pos.z + 32 - (float)ly);
|
2024-10-01 12:31:18 +00:00
|
|
|
|
2024-09-26 18:02:56 +00:00
|
|
|
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;
|
2024-07-30 16:36:59 +00:00
|
|
|
|
2024-10-07 14:53:09 +00:00
|
|
|
draw_triangle("/assets/grass.png",
|
2024-09-23 17:43:16 +00:00
|
|
|
(Vec3){ (float)x, d0, (float)y },
|
|
|
|
(Vec3){ (float)x + 1, d1, (float)y },
|
|
|
|
(Vec3){ (float)x, d3, (float)y - 1 },
|
2024-10-01 17:52:19 +00:00
|
|
|
(Vec2){ 128, 128 },
|
|
|
|
(Vec2){ 128, 0 },
|
|
|
|
(Vec2){ 0, 128 });
|
2024-07-30 16:36:59 +00:00
|
|
|
|
2024-10-07 14:53:09 +00:00
|
|
|
draw_triangle("/assets/grass.png",
|
2024-09-23 17:43:16 +00:00
|
|
|
(Vec3){ (float)x + 1, d1, (float)y },
|
|
|
|
(Vec3){ (float)x + 1, d2, (float)y - 1 },
|
|
|
|
(Vec3){ (float)x, d3, (float)y - 1 },
|
2024-10-01 17:52:19 +00:00
|
|
|
(Vec2){ 128, 0 },
|
|
|
|
(Vec2){ 0, 0 },
|
|
|
|
(Vec2){ 0, 128 });
|
2024-07-30 16:36:59 +00:00
|
|
|
}
|
2024-08-19 16:19:22 +00:00
|
|
|
}
|
2024-09-26 18:02:56 +00:00
|
|
|
|
2024-10-07 14:53:09 +00:00
|
|
|
draw_skybox("/assets/miramar/miramar_*.tga");
|
2024-10-12 17:24:47 +00:00
|
|
|
draw_fog(0.9f, 1.0f, 0.05f, (Color){ 140, 147, 160, 255 });
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
static void ingame_end(State *state) {
|
2024-07-27 22:44:39 +00:00
|
|
|
free(state->scene);
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
Scene *ingame_scene(State *state) {
|
2024-07-08 00:44:20 +00:00
|
|
|
(void)state;
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
SceneIngame *new_scene = ccalloc(1, sizeof *new_scene);
|
2024-07-08 00:44:20 +00:00
|
|
|
new_scene->base.tick = ingame_tick;
|
|
|
|
new_scene->base.end = ingame_end;
|
|
|
|
|
2024-10-22 17:32:17 +00:00
|
|
|
new_scene->mouse_captured = true;
|
2024-08-21 13:55:34 +00:00
|
|
|
|
2024-10-07 12:23:04 +00:00
|
|
|
m_audio(m_set(path, "music/mod65.xm"),
|
2024-10-07 12:21:44 +00:00
|
|
|
m_opt(channel, "soundtrack"),
|
|
|
|
m_opt(repeat, true));
|
2024-07-08 06:46:12 +00:00
|
|
|
|
2024-10-08 07:12:30 +00:00
|
|
|
input_set_mouse_captured(true);
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
return (Scene *)new_scene;
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|