/*
** EPITECH PROJECT, 2018
** task01
** File description:
** I do task
*/
#include <stdlib.h>
#include "lib/my_memory.h"
#include "game/memory.h"
#include "game/game.h"
#include "game/core.h"

static void active_object(scene_t *scene, uint8_t object)
{
	if (object & SCENE_LOAD_PLAYER)
		scene->player = init_player(scene->level);
	if (object & SCENE_LOAD_THROWER)
		scene->thrower = init_thrower(scene->level);
	if (object & SCENE_LOAD_MESSAGE_INTRO)
		scene->message = load_message(scene, MESSAGE_INTRO);
	if (object & SCENE_LOAD_MESSAGE_GAME)
		scene->message = load_message(scene, MESSAGE_GAME);
	if (object & SCENE_LOAD_WALL)
		scene->wall = init_wall(scene->player);
	scene->camera = init_camera(scene);
}

static void scene_reset(scene_t *scene)
{
	scene->wall = NULL;
	scene->player = NULL;
	scene->camera = NULL;
	scene->message = NULL;
	scene->thrower = NULL;
}

scene_t *new_scene(char *map_name, uint8_t object)
{
	scene_t *scene;

	scene = (scene_t*)malloc(sizeof(scene_t));
	if (scene == NULL)
		return (NULL);
	scene_reset(scene);
	scene->level = load_map(map_name);
	if (scene->level != NULL && !check_map(scene))
		active_object(scene, object);
	if ((scene->player == NULL && object & SCENE_LOAD_PLAYER)
	|| (scene->wall == NULL && object & SCENE_LOAD_WALL)
	|| scene->camera == NULL){
		free_scene(scene);
		return (NULL);
	}
	if (scene->player != NULL)
		game_camera_update(scene, SCREEN_WIDTH, SCREEN_HEIGHT);
	return (scene);
}
