move platformes and scenery to /apps/demos/
This commit is contained in:
60
apps/demos/platformer/scenes/ingame.c
Normal file
60
apps/demos/platformer/scenes/ingame.c
Normal file
@ -0,0 +1,60 @@
|
||||
#include "ingame.h"
|
||||
#include "title.h"
|
||||
#include "scene.h"
|
||||
|
||||
#include "twn_game_api.h"
|
||||
#include "twn_vec.h"
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
|
||||
static void ingame_tick(State *state) {
|
||||
SceneIngame *scn = (SceneIngame *)state->scene;
|
||||
|
||||
world_drawdef(scn->world);
|
||||
player_calc(scn->player);
|
||||
|
||||
const Vec3 right = m_vec_norm(m_vec_cross(scn->cam.target, scn->cam.up));
|
||||
const float speed = 0.04f; /* TODO: put this in a better place */
|
||||
if (input_is_action_pressed("player_left"))
|
||||
scn->cam.pos = vec3_sub(scn->cam.pos, m_vec_scale(right, speed));
|
||||
|
||||
if (input_is_action_pressed("player_right"))
|
||||
scn->cam.pos = vec3_add(scn->cam.pos, m_vec_scale(right, speed));
|
||||
|
||||
if (input_is_action_pressed("player_forward"))
|
||||
scn->cam.pos = vec3_add(scn->cam.pos, m_vec_scale(scn->cam.target, speed));
|
||||
|
||||
if (input_is_action_pressed("player_backward"))
|
||||
scn->cam.pos = vec3_sub(scn->cam.pos, m_vec_scale(scn->cam.target, speed));
|
||||
|
||||
if (input_is_action_pressed("player_jump"))
|
||||
scn->cam.pos.y += speed;
|
||||
|
||||
if (input_is_action_pressed("player_run"))
|
||||
scn->cam.pos.y -= speed;
|
||||
}
|
||||
|
||||
|
||||
static void ingame_end(State *state) {
|
||||
SceneIngame *scn = (SceneIngame *)state->scene;
|
||||
player_destroy(scn->player);
|
||||
world_destroy(scn->world);
|
||||
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->world = world_create();
|
||||
new_scene->player = player_create(new_scene->world);
|
||||
|
||||
new_scene->cam = (Camera){ .pos = { 32, 0, 1 }, .up = { 0, 1, 0 }, .fov = (float)M_PI_2 };
|
||||
|
||||
return (Scene *)new_scene;
|
||||
}
|
30
apps/demos/platformer/scenes/ingame.h
Normal file
30
apps/demos/platformer/scenes/ingame.h
Normal file
@ -0,0 +1,30 @@
|
||||
#ifndef INGAME_H
|
||||
#define INGAME_H
|
||||
|
||||
#include "twn_game_api.h"
|
||||
|
||||
#include "../state.h"
|
||||
#include "scene.h"
|
||||
#include "../player.h"
|
||||
#include "../world.h"
|
||||
|
||||
|
||||
typedef struct SceneIngame {
|
||||
Scene base;
|
||||
|
||||
World *world;
|
||||
Player *player;
|
||||
|
||||
Camera cam;
|
||||
|
||||
/* TODO: put this in a better place */
|
||||
float yaw;
|
||||
float pitch;
|
||||
float roll;
|
||||
} SceneIngame;
|
||||
|
||||
|
||||
Scene *ingame_scene(State *state);
|
||||
|
||||
|
||||
#endif
|
8
apps/demos/platformer/scenes/scene.c
Normal file
8
apps/demos/platformer/scenes/scene.c
Normal file
@ -0,0 +1,8 @@
|
||||
#include "scene.h"
|
||||
#include "../state.h"
|
||||
|
||||
|
||||
void switch_to(State *state, Scene *(*scene_func)(State *)) {
|
||||
state->next_scene = scene_func(state);
|
||||
state->is_scene_switching = true;
|
||||
}
|
16
apps/demos/platformer/scenes/scene.h
Normal file
16
apps/demos/platformer/scenes/scene.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef SCENE_H
|
||||
#define SCENE_H
|
||||
|
||||
|
||||
typedef struct State State;
|
||||
typedef struct Scene {
|
||||
char *id;
|
||||
void (*tick)(State *);
|
||||
void (*end)(State *);
|
||||
} Scene;
|
||||
|
||||
|
||||
void switch_to(State *state, Scene *(*scene_func)(State *));
|
||||
|
||||
|
||||
#endif
|
75
apps/demos/platformer/scenes/title.c
Normal file
75
apps/demos/platformer/scenes/title.c
Normal file
@ -0,0 +1,75 @@
|
||||
#include "title.h"
|
||||
#include "ingame.h"
|
||||
#include "../world.h"
|
||||
#include "../player.h"
|
||||
|
||||
#include "twn_game_api.h"
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
static void title_tick(State *state) {
|
||||
SceneTitle *scn = (SceneTitle *)state->scene;
|
||||
(void)scn;
|
||||
|
||||
if (input_is_action_just_pressed("ui_accept")) {
|
||||
switch_to(state, ingame_scene);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
m_sprite("/assets/title.png", ((Rect) {
|
||||
((float)ctx.base_draw_w / 2) - ((float)320 / 2), 64, 320, 128 }));
|
||||
|
||||
|
||||
/* draw the tick count as an example of dynamic text */
|
||||
size_t text_str_len = snprintf(NULL, 0, "%lu", state->ctx->tick_count) + 1;
|
||||
char *text_str = cmalloc(text_str_len);
|
||||
snprintf(text_str, text_str_len, "%lu", state->ctx->tick_count);
|
||||
|
||||
const char *font = "fonts/kenney-pixel.ttf";
|
||||
int text_h = 32;
|
||||
int text_w = draw_text_width(text_str, text_h, font);
|
||||
|
||||
draw_rectangle(
|
||||
(Rect) {
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
.w = (float)text_w,
|
||||
.h = (float)text_h,
|
||||
},
|
||||
(Color) { 0, 0, 0, 255 }
|
||||
);
|
||||
draw_text(
|
||||
text_str,
|
||||
(Vec2){ 0, 0 },
|
||||
text_h,
|
||||
(Color) { 255, 255, 255, 255 },
|
||||
font
|
||||
);
|
||||
free(text_str);
|
||||
}
|
||||
|
||||
|
||||
static void title_end(State *state) {
|
||||
SceneTitle *scn = (SceneTitle *)state->scene;
|
||||
player_destroy(scn->player);
|
||||
world_destroy(scn->world);
|
||||
free(state->scene);
|
||||
}
|
||||
|
||||
|
||||
Scene *title_scene(State *state) {
|
||||
(void)state;
|
||||
|
||||
SceneTitle *new_scene = ccalloc(1, sizeof *new_scene);
|
||||
new_scene->base.tick = title_tick;
|
||||
new_scene->base.end = title_end;
|
||||
|
||||
new_scene->world = world_create();
|
||||
new_scene->player = player_create(new_scene->world);
|
||||
|
||||
return (Scene *)new_scene;
|
||||
}
|
21
apps/demos/platformer/scenes/title.h
Normal file
21
apps/demos/platformer/scenes/title.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef TITLE_H
|
||||
#define TITLE_H
|
||||
|
||||
#include "../state.h"
|
||||
#include "scene.h"
|
||||
#include "../player.h"
|
||||
#include "../world.h"
|
||||
|
||||
|
||||
typedef struct SceneTitle {
|
||||
Scene base;
|
||||
|
||||
World *world;
|
||||
Player *player;
|
||||
} SceneTitle;
|
||||
|
||||
|
||||
Scene *title_scene(State *state);
|
||||
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user