townengine/apps/testgame/scenes/ingame.c

138 lines
5.0 KiB
C
Raw Normal View History

2024-07-08 00:44:20 +00:00
#include "ingame.h"
#include "title.h"
#include "scene.h"
#include "townengine/game_api.h"
2024-07-08 06:46:12 +00:00
2024-07-08 00:44:20 +00:00
static void ingame_tick(struct state *state) {
struct scene_ingame *scn = (struct scene_ingame *)state->scene;
world_drawdef(scn->world);
player_calc(scn->player);
2024-07-08 15:13:33 +00:00
2024-07-30 16:36:59 +00:00
static t_camera cam = { .pos = { 32, 0, 1 }, .up = { 0, 1, 0 }, .fov = (float)M_PI_2 };
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;
cam.target = m_vec_norm(((t_fvec3){
cosf(yaw_rad) * cosf(pitch_rad),
sinf(pitch_rad),
sinf(yaw_rad) * cosf(pitch_rad)
}));
}
2024-07-30 21:05:28 +00:00
const t_fvec3 right = m_vec_norm(m_vec_cross(cam.target, cam.up));
const float speed = 0.04f; /* TODO: put this in a better place */
if (input_is_action_pressed(&ctx.input, "player_left"))
2024-07-30 21:05:28 +00:00
cam.pos = fvec3_sub(cam.pos, m_vec_scale(right, speed));
if (input_is_action_pressed(&ctx.input, "player_right"))
2024-07-30 21:05:28 +00:00
cam.pos = fvec3_add(cam.pos, m_vec_scale(right, speed));
if (input_is_action_pressed(&ctx.input, "player_forward"))
cam.pos = fvec3_add(cam.pos, m_vec_scale(cam.target, speed));
if (input_is_action_pressed(&ctx.input, "player_backward"))
cam.pos = fvec3_sub(cam.pos, m_vec_scale(cam.target, speed));
2024-07-29 22:20:30 +00:00
if (input_is_action_pressed(&ctx.input, "player_jump"))
2024-07-30 21:05:28 +00:00
cam.pos.y += speed;
if (input_is_action_pressed(&ctx.input, "player_run"))
cam.pos.y -= speed;
/* 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));
}
m_sprite(m_set(path, "/assets/player/baron-walk.png"),
m_set(rect, ((t_frect){ 256, 256, 48, 48 })),
m_opt(color, ((t_color){ 255, 255, 255, 255 })),
2024-07-31 22:25:23 +00:00
m_opt(stretch, false ),
m_opt(texture_origin, ((t_fvec2){ ctx.tick_count % 48, 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"),
m_set(rect, ((t_frect){ 48, 64, 64, 64 })),
m_opt(color, ((t_color){ 255, 0, 0, 255 })));
2024-07-30 12:30:35 +00:00
m_sprite(m_set(path, "/assets/light.png"),
m_set(rect, ((t_frect){ 64, 64, 64, 64 })),
m_opt(color, ((t_color){ 0, 255, 0, 255 })));
2024-07-30 12:30:35 +00:00
m_sprite(m_set(path, "/assets/light.png"),
m_set(rect, ((t_frect){ 80, 64, 64, 64 })),
m_opt(color, ((t_color){ 0, 0, 255, 255 })));
2024-07-30 12:30:35 +00:00
m_sprite(m_set(path, "/assets/player/baron-walk.png"),
m_set(rect, ((t_frect){ 32, 32, 64, 64 })),
m_opt(rotation, (float)M_PI * 2 * (float)(ctx.tick_count % 64) / 64 ));
2024-07-30 12:30:35 +00:00
m_sprite(m_set(path, "/assets/player/baron-walk.png"),
m_set(rect, ((t_frect){ 128, 32, 128, 64 })),
m_opt(rotation, (float)M_PI * 2 * (float)(ctx.tick_count % 64) / 64 ));
2024-07-27 13:55:38 +00:00
2024-07-30 12:30:35 +00:00
set_camera(&cam);
2024-07-30 22:12:45 +00:00
for (int y = 64; y--;)
for (int x = 64; x--;) {
2024-07-30 16:36:59 +00:00
float d0 = sample_perlin_2d((t_fvec2){x, y}, 0.2, 5) * 8 - 6;
float d1 = sample_perlin_2d((t_fvec2){x + 1, y}, 0.2, 5) * 8 - 6;
float d2 = sample_perlin_2d((t_fvec2){x + 1, y - 1}, 0.2, 5) * 8 - 6;
float d3 = sample_perlin_2d((t_fvec2){x, y - 1}, 0.2, 5) * 8 - 6;
unfurl_triangle("/assets/grass.gif",
(t_fvec3){ x, d0, y },
(t_fvec3){ x + 1, d1, y },
(t_fvec3){ x, d3, y - 1 },
(t_shvec2){ 0, 768 },
(t_shvec2){ 1024, 768 },
(t_shvec2){ 1024, 0 });
unfurl_triangle("/assets/grass.gif",
(t_fvec3){ x + 1, d1, y },
(t_fvec3){ x + 1, d2, y - 1 },
(t_fvec3){ x, d3, y - 1 },
(t_shvec2){ 1024, 0 },
(t_shvec2){ 0, 0 },
(t_shvec2){ 0, 768 });
}
2024-07-08 00:44:20 +00:00
}
static void ingame_end(struct state *state) {
struct scene_ingame *scn = (struct scene_ingame *)state->scene;
player_destroy(scn->player);
world_destroy(scn->world);
free(state->scene);
2024-07-08 00:44:20 +00:00
}
struct scene *ingame_scene(struct state *state) {
(void)state;
struct scene_ingame *new_scene = ccalloc(1, sizeof *new_scene);
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-07-31 21:58:16 +00:00
play_audio_ex("music/mod65.xm", "soundtrack", (t_play_audio_args){
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-07-08 00:44:20 +00:00
return (struct scene *)new_scene;
}