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-08-19 16:19:22 +00:00
|
|
|
|
|
|
|
#define STB_PERLIN_IMPLEMENTATION
|
|
|
|
#include <stb_perlin.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
|
|
|
|
|
|
|
world_drawdef(scn->world);
|
|
|
|
player_calc(scn->player);
|
2024-07-08 15:13:33 +00:00
|
|
|
|
2024-07-30 21:18:01 +00:00
|
|
|
if (input_is_mouse_captured(&ctx.input)) {
|
|
|
|
const float sensitivity = 0.6f; /* TODO: put this in a better place */
|
|
|
|
scn->yaw += (float)ctx.input.mouse_relative_position.x * sensitivity;
|
|
|
|
scn->pitch -= (float)ctx.input.mouse_relative_position.y * sensitivity;
|
|
|
|
scn->pitch = clampf(scn->pitch, -89.0f, 89.0f);
|
|
|
|
|
|
|
|
const float yaw_rad = scn->yaw * (float)DEG2RAD;
|
|
|
|
const float pitch_rad = scn->pitch * (float)DEG2RAD;
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
scn->cam.target = m_vec_norm(((Vec3){
|
2024-07-30 21:18:01 +00:00
|
|
|
cosf(yaw_rad) * cosf(pitch_rad),
|
|
|
|
sinf(pitch_rad),
|
|
|
|
sinf(yaw_rad) * cosf(pitch_rad)
|
|
|
|
}));
|
|
|
|
}
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
const Vec3 right = m_vec_norm(m_vec_cross(scn->cam.target, scn->cam.up));
|
2024-07-30 21:05:28 +00:00
|
|
|
const float speed = 0.04f; /* TODO: put this in a better place */
|
2024-07-29 12:21:39 +00:00
|
|
|
if (input_is_action_pressed(&ctx.input, "player_left"))
|
2024-09-23 17:43:16 +00:00
|
|
|
scn->cam.pos = vec3_sub(scn->cam.pos, m_vec_scale(right, speed));
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-07-29 12:21:39 +00:00
|
|
|
if (input_is_action_pressed(&ctx.input, "player_right"))
|
2024-09-23 17:43:16 +00:00
|
|
|
scn->cam.pos = vec3_add(scn->cam.pos, m_vec_scale(right, speed));
|
2024-07-30 21:05:28 +00:00
|
|
|
|
|
|
|
if (input_is_action_pressed(&ctx.input, "player_forward"))
|
2024-09-23 17:43:16 +00:00
|
|
|
scn->cam.pos = vec3_add(scn->cam.pos, m_vec_scale(scn->cam.target, speed));
|
2024-07-30 21:05:28 +00:00
|
|
|
|
|
|
|
if (input_is_action_pressed(&ctx.input, "player_backward"))
|
2024-09-23 17:43:16 +00:00
|
|
|
scn->cam.pos = vec3_sub(scn->cam.pos, m_vec_scale(scn->cam.target, speed));
|
2024-07-30 21:05:28 +00:00
|
|
|
|
2024-07-29 22:20:30 +00:00
|
|
|
if (input_is_action_pressed(&ctx.input, "player_jump"))
|
2024-08-21 13:55:34 +00:00
|
|
|
scn->cam.pos.y += speed;
|
2024-07-30 21:05:28 +00:00
|
|
|
|
|
|
|
if (input_is_action_pressed(&ctx.input, "player_run"))
|
2024-08-21 13:55:34 +00:00
|
|
|
scn->cam.pos.y -= speed;
|
2024-07-30 21:05:28 +00:00
|
|
|
|
|
|
|
/* toggle mouse capture with end key */
|
|
|
|
if (input_is_action_just_pressed(&ctx.input, "mouse_capture_toggle")) {
|
|
|
|
input_set_mouse_captured(&ctx.input, !input_is_mouse_captured(&ctx.input));
|
|
|
|
}
|
2024-07-29 12:21:39 +00:00
|
|
|
|
2024-09-21 17:07:05 +00:00
|
|
|
m_sprite(m_set(path, "/assets/9slice.png"),
|
2024-09-23 17:43:16 +00:00
|
|
|
m_set(rect, ((Rect){ 16, 16, 128, 128 })),
|
|
|
|
m_opt(texture_region, ((Rect){ 0, 0, (float)(ctx.tick_count % 48), (float)(ctx.tick_count % 48) })));
|
2024-07-31 21:23:32 +00:00
|
|
|
|
2024-07-30 12:30:35 +00:00
|
|
|
m_sprite(m_set(path, "/assets/light.png"),
|
2024-09-23 17:43:16 +00:00
|
|
|
m_set(rect, ((Rect){ 48, 64, 64, 64 })),
|
|
|
|
m_opt(color, ((Color){ 255, 0, 0, 255 })));
|
2024-07-29 12:21:39 +00:00
|
|
|
|
2024-07-30 12:30:35 +00:00
|
|
|
m_sprite(m_set(path, "/assets/light.png"),
|
2024-09-23 17:43:16 +00:00
|
|
|
m_set(rect, ((Rect){ 64, 64, 64, 64 })),
|
|
|
|
m_opt(color, ((Color){ 0, 255, 0, 255 })));
|
2024-07-27 14:55:57 +00:00
|
|
|
|
2024-07-30 12:30:35 +00:00
|
|
|
m_sprite(m_set(path, "/assets/light.png"),
|
2024-09-23 17:43:16 +00:00
|
|
|
m_set(rect, ((Rect){ 80, 64, 64, 64 })),
|
|
|
|
m_opt(color, ((Color){ 0, 0, 255, 255 })));
|
2024-07-27 14:55:57 +00:00
|
|
|
|
2024-07-30 12:30:35 +00:00
|
|
|
m_sprite(m_set(path, "/assets/player/baron-walk.png"),
|
2024-09-23 17:43:16 +00:00
|
|
|
m_set(rect, ((Rect){ 32, 32, 64, 64 })),
|
2024-07-30 12:30:35 +00:00
|
|
|
m_opt(rotation, (float)M_PI * 2 * (float)(ctx.tick_count % 64) / 64 ));
|
2024-07-27 14:55:57 +00:00
|
|
|
|
2024-07-30 12:30:35 +00:00
|
|
|
m_sprite(m_set(path, "/assets/player/baron-walk.png"),
|
2024-09-23 17:43:16 +00:00
|
|
|
m_set(rect, ((Rect){ 128, 32, 128, 64 })),
|
2024-07-30 12:30:35 +00:00
|
|
|
m_opt(rotation, (float)M_PI * 2 * (float)(ctx.tick_count % 64) / 64 ));
|
2024-07-27 13:55:38 +00:00
|
|
|
|
2024-08-21 13:55:34 +00:00
|
|
|
set_camera(&scn->cam);
|
2024-07-29 09:43:46 +00:00
|
|
|
|
2024-08-19 16:19:22 +00:00
|
|
|
#define TERRAIN_FREQUENCY 0.1f
|
|
|
|
|
|
|
|
for (int y = 64; y--;) {
|
2024-07-30 22:12:45 +00:00
|
|
|
for (int x = 64; x--;) {
|
2024-08-21 13:55:34 +00:00
|
|
|
float d0 = stb_perlin_noise3((float)x * TERRAIN_FREQUENCY, (float)y * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 20 - 6;
|
|
|
|
float d1 = stb_perlin_noise3((float)(x + 1) * TERRAIN_FREQUENCY, (float)y * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 20 - 6;
|
|
|
|
float d2 = stb_perlin_noise3((float)(x + 1) * TERRAIN_FREQUENCY, (float)(y - 1) * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 20 - 6;
|
|
|
|
float d3 = stb_perlin_noise3((float)x * TERRAIN_FREQUENCY, (float)(y - 1) * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 20 - 6;
|
2024-07-30 16:36:59 +00:00
|
|
|
|
|
|
|
unfurl_triangle("/assets/grass.gif",
|
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 },
|
|
|
|
(Vec2sh){ 1024, 768 },
|
|
|
|
(Vec2sh){ 1024, 0 },
|
|
|
|
(Vec2sh){ 0, 768 });
|
2024-07-30 16:36:59 +00:00
|
|
|
|
|
|
|
unfurl_triangle("/assets/grass.gif",
|
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 },
|
|
|
|
(Vec2sh){ 1024, 0 },
|
|
|
|
(Vec2sh){ 0, 0 },
|
|
|
|
(Vec2sh){ 0, 768 });
|
2024-07-30 16:36:59 +00:00
|
|
|
}
|
2024-08-19 16:19:22 +00:00
|
|
|
}
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
static void ingame_end(State *state) {
|
|
|
|
SceneIngame *scn = (SceneIngame *)state->scene;
|
2024-07-08 00:44:20 +00:00
|
|
|
player_destroy(scn->player);
|
|
|
|
world_destroy(scn->world);
|
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;
|
|
|
|
|
|
|
|
new_scene->world = world_create();
|
|
|
|
new_scene->player = player_create(new_scene->world);
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
new_scene->cam = (Camera){ .pos = { 32, 0, 1 }, .up = { 0, 1, 0 }, .fov = (float)M_PI_2 };
|
2024-08-21 13:55:34 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
audio_play_ex("music/mod65.xm", "soundtrack", (PlayAudioArgs){
|
2024-07-08 15:00:38 +00:00
|
|
|
.repeat = true,
|
2024-07-31 21:58:16 +00:00
|
|
|
.volume = 1.0f
|
2024-07-08 06:46:12 +00:00
|
|
|
});
|
|
|
|
|
2024-07-30 21:05:28 +00:00
|
|
|
input_set_mouse_captured(&ctx.input, true);
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
return (Scene *)new_scene;
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|