#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;

    input_action("player_left", "A");
    input_action("player_right", "D");
    input_action("player_forward", "W");
    input_action("player_backward", "S");
    input_action("player_jump", "SPACE");
    input_action("player_run", "LSHIFT");

    draw_camera_2d((Vec2){ scn->player->rect.x + scn->player->rect.w / 2 - ctx.resolution.x / 2,
                           scn->player->rect.y + scn->player->rect.h / 2 - ctx.resolution.y / 2 },
        0, 1
    );

    world_drawdef(scn->world);
    player_calc(scn->player);
}


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 = calloc(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);

    return (Scene *)new_scene;
}