/*
** EPITECH PROJECT, 2018
** task01
** File description:
** I do task
*/
#include "game/game.h"
#include "game/core.h"

void collision_pik(sfml_t *sfml)
{
	uint8_t *map = sfml->scene->level->map;
	int x = sfml->scene->player->x;
	int y = sfml->scene->player->y;
	int width = sfml->scene->level->width;

	if (map[(x + 8 >> 6) + (y >> 6) * width] == ID_PIK_LEFT
	|| map[(x + 8 >> 6) + (y + 64 >> 6) * width] == ID_PIK_LEFT
	|| map[(x + 56 >> 6) + (y >> 6) * width] == ID_PIK_RIGHT
	|| map[(x + 56 >> 6) + (y + 64 >> 6) * width] == ID_PIK_RIGHT
	|| map[(x + 8 >> 6) + (y + 16 >> 6) * width] == ID_PIK_DOWN
	|| map[(x + 56 >> 6) + (y + 16 >> 6) * width] == ID_PIK_DOWN
	|| map[(x + 8 >> 6) + (y + 48 >> 6) * width] == ID_PIK_UP
	|| map[(x + 56 >> 6) + (y + 48 >> 6) * width] == ID_PIK_UP){
		sfml->scene->player->action = PLAYER_DEAD;
		sfml->scene->player->dead_counter = 0;
	}
}

void collision_player_run(scene_t *scene)
{
	uint8_t *map = scene->level->map;
	int x = scene->player->x;
	int y = scene->player->y;

	if (map[(x + 8 >> 6) + (y + 1 >> 6) * scene->level->width] == ID_GROUND
	|| map[(x + 8 >> 6) + (y + 63 >> 6) *
	scene->level->width] == ID_GROUND){
		scene->player->x = ((scene->player->x - 8) >> 6 << 6) + 55;
		scene->player->action &= ~(PLAYER_RUN | PLAYER_JUMP);
		scene->player->action |= PLAYER_IDLE;
		scene->player->dx = 0;
		return;
	}
	if (map[(x + 54 >> 6) + (y + 1 >> 6) * scene->level->width] == ID_GROUND
	|| map[(x + 54 >> 6) +
	(y + 63 >> 6) * scene->level->width] == ID_GROUND){
		scene->player->x = ((scene->player->x - 8) >> 6 << 6) + 9;
		scene->player->action &= ~(PLAYER_RUN | PLAYER_JUMP);
		scene->player->action |= PLAYER_IDLE;
		scene->player->dx = 0;
	}
}

void collision_player_jump(scene_t *scene)
{
	uint8_t *map = scene->level->map;
	int x = scene->player->x;
	int y = scene->player->y;

	if (map[(x + 9 >> 6) + (y >> 6) * scene->level->width] == ID_GROUND
	|| map[(x + 53 >> 6) + (y >> 6) * scene->level->width] == ID_GROUND){
		scene->player->y = (scene->player->y >> 6 << 6) + 64;
		scene->player->counter_jump = PLAYER_MAX_JUMP;
		scene->player->dy = 1;
	}
	if (map[(x + 53 >> 6) + (y + 64 >> 6) *
	scene->level->width] == ID_GROUND
	|| map[(x + 9 >> 6) + (y + 64 >> 6) *
	scene->level->width] == ID_GROUND){
		scene->player->y = ((scene->player->y - 64) >> 6 << 6) + 64;
		scene->player->action &= ~(PLAYER_CANT_JUMP | PLAYER_JUMP);
		scene->player->counter_jump = 0;
		scene->player->dy = 1;
	}
}

void collision_wall(sfml_t *sfml)
{
	if (sfml->scene->wall->x + WALL_SIZE + 16 > sfml->scene->player->x){
		sfml->scene->player->action = PLAYER_DEAD;
		sfml->scene->player->dead_counter = 0;
	}
	if (sfml->scene->wall->x + WALL_SIZE > sfml->scene->level->width << 6)
		sfml->scene->wall->x =
		(sfml->scene->level->width << 6) - WALL_SIZE;
}
