Newer
Older
/*
// Created by Tiny on 3/25/2021.
*/
#include <SDL2/SDL.h>
#include "move.h"
#include "personnage.h"
#include "collision.h"
#include "prof.h"
#include "DS.h"
#include "GAME.h"
#include "map.h"
/*!
* \fn void move_hero(Personnage * hero, SDL_Rect * hero_rect, Prof * teacher, DS * exam, MAP * map_game, int * close, int * result)
* \brief This function move the hero following his position on the map, it will change the close request and the result request if necessary
*
* \param [in] hero the hero pointer
* \param [in] hero_rect the pointer to structure rect of hero
* \param [in] teacher the teacher pointer
* \param [in] exam the exam pointer
* \param [in] map_game the map_game pointer
* \param [in] close the close request pointer
* \param [in] result the result pointer
*/
void move_hero(Personnage * hero, SDL_Rect * hero_rect, Prof * teacher, DS * exam, MAP * map_game, int * close, int * result)
{
//collision variable
int collision;
// determine position following inputs
if (hero->direction_personnage.up && !hero->direction_personnage.down && !hero->direction_personnage.right && !hero->direction_personnage.left) hero->y_pers -=
SPEED;
if (!hero->direction_personnage.up && hero->direction_personnage.down && !hero->direction_personnage.right && !hero->direction_personnage.left) hero->y_pers +=
SPEED;
if (!hero->direction_personnage.up && !hero->direction_personnage.down && !hero->direction_personnage.right && hero->direction_personnage.left) hero->x_pers -=
SPEED;
if (!hero->direction_personnage.up && !hero->direction_personnage.down && hero->direction_personnage.right && !hero->direction_personnage.left) hero->x_pers +=
SPEED;
//recalculate 4 points of hero
calcul_points_pers(hero);
// collision detection
collision = test_position(hero, teacher, exam,
map_game); // return 0 if nothing, 1 if wall, 2 if exam, 3 if prof, 4 if bounds
switch (collision)
{
case 0:
hero_rect->x = (int) hero->x_pers;
hero_rect->y = (int) hero->y_pers;
break;
case 1:
hero->y_pers = hero_rect->y;
hero->x_pers = hero_rect->x;
break;
case 2:
*close = 1;
*result = 1;
break;
case 3:
*close = 1;
*result = 0;
break;
case 4:
if (hero->x_pers <= 0) hero->x_pers = 2;
if (hero->y_pers <= 0) hero-> y_pers = 2;
if (hero->x_pers >= WINDOW_WIDTH - hero_rect->w) hero->x_pers = WINDOW_WIDTH - hero_rect->w - 2;
if (hero->y_pers >= WINDOW_HEIGHT - hero_rect->h) hero->y_pers = WINDOW_HEIGHT - hero_rect->h - 2;
hero_rect->x = (int) hero->x_pers;
hero_rect->y = (int) hero->y_pers;
break;
}
}
/*!
* \fn void move_hero(Personnage * hero, SDL_Rect * hero_rect, Prof * teacher, DS * exam, MAP * map_game, int * close, int * result)
* \brief This function move the hero following his position on the map, it will change the close request and the result request if necessary
*
* \param [in] prof the prof pointer
* \param [in] prof_rect the pointer to structure rect of prof
* \param [in] predirection the direction pointer
* \param [in] exam the exam pointer
* \param [in] map_game the map_game pointer
*/
void prof_move(Prof *prof, int * predirection, SDL_Rect * prof_rect, MAP * map, DS *exam){
int randdirect;
switch (*predirection)
{
case 0:
prof->y_prof -= prof->speed_prof;
break;
case 1:
prof->y_prof += prof->speed_prof;
break;
case 2:
prof->x_prof -= prof->speed_prof;
break;
case 3:
prof->x_prof += prof->speed_prof;
break;
}
// prof collision detection
int collision = test_position_prof(prof,map,exam); // return 0 if nothing, 1 if wall or bounds
// set the positions following collision variable
switch (collision)
{
case 0:
prof_rect->x = (int) prof->x_prof;
prof_rect->y = (int) prof->y_prof;
break;
case 1:
prof->y_prof = prof_rect->y;
prof->x_prof = prof_rect->x;
fflush(stdin);
do {
randdirect = rand()%4;
}
while(((randdirect == 0) && (*predirection == 1)) || ((randdirect == 1) && (*predirection == 0)) || ((randdirect == 2) && (*predirection == 3)) || ((randdirect == 3) && (*predirection == 2)));
*predirection = randdirect;
break;
}