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

static void player_jump(sfml_t *sfml)
{
	if (sfml->key & (PRES_SPACE | PRES_UP)
	&& (sfml->scene->player->dy <= 0
	|| !(sfml->scene->player->action & PLAYER_CANT_JUMP))
	&& sfml->scene->player->counter_jump < PLAYER_MAX_JUMP){
		if (!(sfml->scene->player->action & PLAYER_CANT_JUMP)){
			sfml->scene->player->action |= PLAYER_CANT_JUMP;
			sfml->scene->player->dy = -8;
		}
		else
			sfml->scene->player->dy -= 2;
		sfml->scene->player->counter_jump++;
	}
	if (sfml->scene->player->dy < PLAYER_FALL_MAX)
		sfml->scene->player->dy++;
	sfml->scene->player->y += sfml->scene->player->dy;
	collision_player_jump(sfml->scene);
	if (sfml->scene->player->action & PLAYER_CANT_JUMP){
		sfml->scene->player->action &= ~(PLAYER_IDLE | PLAYER_RUN);
		sfml->scene->player->action |= PLAYER_JUMP;
	}
}

static void move_player(sfml_t *sfml)
{
	if (sfml->key & PRES_LEFT){
		if (sfml->scene->player->dx < PLAYER_SPEED)
			sfml->scene->player->dx++;
		sfml->scene->player->action &= ~(PLAYER_JUMP | PLAYER_IDLE);
		sfml->scene->player->action |= PLAYER_LEFT | PLAYER_RUN;
	}
	if (sfml->key & PRES_RIGHT){
		if (sfml->scene->player->dx > -PLAYER_SPEED)
			sfml->scene->player->dx--;
		sfml->scene->player->action &=
		~(PLAYER_LEFT | PLAYER_JUMP | PLAYER_IDLE);
		sfml->scene->player->action |= PLAYER_RUN;
	}
	if (!(sfml->key & PRES_LEFT) && !(sfml->key & PRES_RIGHT)){
		sfml->scene->player->action &= ~(PLAYER_RUN | PLAYER_RUN);
		sfml->scene->player->action |= PLAYER_IDLE;
		sfml->scene->player->frame_counter = 0;
		sfml->scene->player->dx /= 2;
	}
}

void update_player(sfml_t *sfml)
{
	sfml->scene->player->frame_counter =
	(sfml->scene->player->frame_counter + 1) % 20;
	move_player(sfml);
	sfml->scene->player->x += sfml->scene->player->dx;
	collision_player_run(sfml->scene);
	collision_pik(sfml);
	collision_princess(sfml);
	collision_stone(sfml);
	player_jump(sfml);
}

void update_wall(sfml_t *sfml)
{
	if (sfml->scene->player->x == sfml->scene->wall->start_x
		&& sfml->scene->player->y == sfml->scene->wall->start_y)
		return;
	sfml->scene->wall->x += WALL_SPEED;
	if (!(sfml->scene->player->action & PLAYER_DEAD))
		collision_wall(sfml);
}
