Skip to content
Snippets Groups Projects
Commit 178e5dc1 authored by antoinemeyer5's avatar antoinemeyer5
Browse files

fix deplacement personnage

parent 5efe0f14
Branches
No related tags found
No related merge requests found
#include "gest_event.h" #include "gest_event.h"
int Keys[NB_KEYS]; int Keys[NB_KEYS];
void initKeys(){ void initKeys()
{
int i; int i;
for(i = 0; i < NB_KEYS; i++) for (i = 0; i < NB_KEYS; i++)
{ {
Keys[i] = 0; Keys[i] = 1;
} }
} }
void gestMenu(){ void gestMenu()
{
SDL_Event event; SDL_Event event;
while (SDL_PollEvent(&event)){ while (SDL_PollEvent(&event))
switch(event.type) {
{ switch (event.type)
case SDL_QUIT: {
running = 0; case SDL_QUIT:
break; running = 0;
break;
case SDL_KEYUP:
switch (event.key.keysym.sym) case SDL_KEYUP:
{ switch (event.key.keysym.sym)
case SDLK_ESCAPE: {
running = 0; case SDLK_ESCAPE:
continue; running = 0;
continue;
default:
continue; default:
} continue;
break;
default:
continue;
} }
break;
default:
continue;
}
} }
SDL_Delay(5); SDL_Delay(5);
} }
void gestGame()
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
running = 0;
break;
case SDL_MOUSEMOTION:
if (event.motion.xrel > 0)
{
player.angle += 0.01;
if (player.angle > 2 * pi)
player.angle -= 2 * pi;
}
if (event.motion.xrel < 0)
{
player.angle -= 0.01;
if (player.angle < 0)
player.angle += 2 * pi;
}
if (event.motion.yrel > 0)
{
player.viewAngle -= 1;
}
if (event.motion.yrel < 0)
{
player.viewAngle += 1;
}
player.deltax = cos(player.angle);
player.deltay = sin(player.angle);
break;
case SDL_KEYUP:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
running = 0;
continue;
case SDLK_z:
// avance
Keys[2] = 1;
continue;
case SDLK_d:
// va à droite
Keys[3] = 1;
continue;
case SDLK_s:
// va en arrière
Keys[0] = 1;
continue;
case SDLK_q:
// va à gauche
Keys[1] = 1;
continue;
default:
break;
}
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_z:
Keys[2] = 0;
continue;
void gestGame(){ case SDLK_d:
SDL_Event event; Keys[3] = 0;
while (SDL_PollEvent(&event)){ continue;
switch(event.type)
{ case SDLK_s:
case SDL_QUIT: Keys[0] = 0;
running = 0; continue;
break;
case SDLK_q:
case SDL_MOUSEMOTION: Keys[1] = 0;
if (event.motion.xrel > 0){ continue;
player.angle += 0.01;
if (player.angle > 2*pi) player.angle -= 2*pi; default:
} break;
if (event.motion.xrel < 0){
player.angle -= 0.01;
if (player.angle < 0) player.angle += 2*pi;
}
if (event.motion.yrel > 0){
player.viewAngle -= 1;
}
if (event.motion.yrel < 0){
player.viewAngle += 1;
}
player.deltax = cos(player.angle);
player.deltay = sin(player.angle);
case SDL_KEYUP:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
running = 0;
continue;
case SDLK_z:
Keys[2] = 1;
continue;
case SDLK_d:
Keys[3] = 1;
continue;
case SDLK_s:
Keys[0] = 1;
continue;
case SDLK_q:
Keys[1] = 1;
continue;
default:
break;
}
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_z:
Keys[2] = 0;
continue;
case SDLK_d:
Keys[3] = 0;
continue;
case SDLK_s:
Keys[0] = 0;
continue;
case SDLK_q:
Keys[1] = 0;
continue;
default:
break;
}
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT){
player.isHitting = 1;
}
break;
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT){
player.isHitting = 0;
}
break;
default:
break;
} }
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT)
{
player.isHitting = 1;
}
break;
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT)
{
player.isHitting = 0;
}
break;
default:
break;
}
} }
managePlayer(); managePlayer();
SDL_Delay(5); SDL_Delay(5);
} }
void *EventLoop(void *arg)
void *EventLoop(void *arg){ {
while(running){ while (running)
switch(game_state){ {
case MENU : gestMenu();break; switch (game_state)
case GAME : gestGame();break; {
default:printf("game state fault");break; case MENU:
gestMenu();
break;
case GAME:
gestGame();
break;
default:
printf("game state fault");
break;
} }
} }
return NULL; return NULL;
} }
\ No newline at end of file \ No newline at end of file
...@@ -6,12 +6,13 @@ player_t ennemy; ...@@ -6,12 +6,13 @@ player_t ennemy;
player_t ball; player_t ball;
int * landingPoint; int *landingPoint;
int * lastHitPoint; int *lastHitPoint;
void initPlayer(){ void initPlayer()
player.x= 6*BLOCK_SIZE; {
player.y= 6*BLOCK_SIZE; player.x = 6 * BLOCK_SIZE;
player.y = 6 * BLOCK_SIZE;
player.h = 2 * BLOCK_SIZE; player.h = 2 * BLOCK_SIZE;
player.w = 2 * BLOCK_SIZE; player.w = 2 * BLOCK_SIZE;
player.angle = 0; player.angle = 0;
...@@ -29,51 +30,59 @@ void initPlayer(){ ...@@ -29,51 +30,59 @@ void initPlayer(){
player.HPMax = 3; player.HPMax = 3;
player.currentHP = player.HPMax; player.currentHP = player.HPMax;
player.coins = 0; player.coins = 0;
player.angle=0; player.angle = 0;
player.deltax = 0; player.deltax = 1;
player.deltay = 1; player.deltay = 0;
player.viewAngle = 0; player.viewAngle = 0;
ball.isHit = 0; ball.isHit = 0;
ball.speed = 0; ball.speed = 0;
ball.angle = -pi; ball.angle = -pi;
} }
int * generateLandingPoint(){ int *generateLandingPoint()
int * landingPoint = malloc(sizeof(int) * 2); {
int randomLength = rand() % (int)((MAP_WIDTH/2)*BLOCK_SIZE) + (MAP_WIDTH/2)*BLOCK_SIZE; int *landingPoint = malloc(sizeof(int) * 2);
int randomLength = rand() % (int)((MAP_WIDTH / 2) * BLOCK_SIZE) + (MAP_WIDTH / 2) * BLOCK_SIZE;
int randomPointX = randomLength * cos(player.angle) + player.x; int randomPointX = randomLength * cos(player.angle) + player.x;
int randomPointY = randomLength * sin(player.angle) + player.y; int randomPointY = randomLength * sin(player.angle) + player.y;
landingPoint[0] = randomPointX/BLOCK_SIZE; landingPoint[0] = randomPointX / BLOCK_SIZE;
landingPoint[1] = randomPointY/BLOCK_SIZE; landingPoint[1] = randomPointY / BLOCK_SIZE;
//printf("landing point: %d %d\n", landingPoint[0], landingPoint[1]); // printf("landing point: %d %d\n", landingPoint[0], landingPoint[1]);
return landingPoint; return landingPoint;
} }
int * allocLastHitPoint(){ int *allocLastHitPoint()
int * lastHitPoint = (int *)malloc(sizeof(int) * 2); {
int *lastHitPoint = (int *)malloc(sizeof(int) * 2);
lastHitPoint[0] = 0; lastHitPoint[0] = 0;
lastHitPoint[1] = 0; lastHitPoint[1] = 0;
return lastHitPoint; return lastHitPoint;
} }
void freeIntList(int * list){ void freeIntList(int *list)
if (list != NULL){ {
if (list != NULL)
{
free(list); free(list);
} }
} }
void hitBall(){ void hitBall()
//printf("map edges: %d %d\n", BLOCK_SIZE * MAP_WIDTH/2, BLOCK_SIZE *MAP_HEIGHT/2); {
//printf("ray1: %d %d\n", ray1[0], ray1[1]); // printf("map edges: %d %d\n", BLOCK_SIZE * MAP_WIDTH/2, BLOCK_SIZE *MAP_HEIGHT/2);
int angleMin = RD * atan2((MAP_WIDTH/2)*BLOCK_SIZE - player.x, player.y); // printf("ray1: %d %d\n", ray1[0], ray1[1]);
int angleMax = 90 + RD * atan2((MAP_WIDTH/2)*BLOCK_SIZE - player.x, MAP_HEIGHT * BLOCK_SIZE - player.y); int angleMin = RD * atan2((MAP_WIDTH / 2) * BLOCK_SIZE - player.x, player.y);
int currAngle = (int) ((player.angle) * RD +90) %360; int angleMax = 90 + RD * atan2((MAP_WIDTH / 2) * BLOCK_SIZE - player.x, MAP_HEIGHT * BLOCK_SIZE - player.y);
//printf("player angle: %d\n",(int) ((player.angle) * RD +90) %360); int currAngle = (int)((player.angle) * RD + 90) % 360;
//printf("distance to ball: %f\n", sqrt(pow(ball.x - player.x, 2) + pow(ball.y - player.y, 2))/BLOCK_SIZE); // printf("player angle: %d\n",(int) ((player.angle) * RD +90) %360);
if (sqrt(pow(player.x - ball.x, 2) + pow(player.y - ball.y, 2))/BLOCK_SIZE < HIT_RANGE){ // printf("distance to ball: %f\n", sqrt(pow(ball.x - player.x, 2) + pow(ball.y - player.y, 2))/BLOCK_SIZE);
if (currAngle < angleMax && currAngle > angleMin){ if (sqrt(pow(player.x - ball.x, 2) + pow(player.y - ball.y, 2)) / BLOCK_SIZE < HIT_RANGE)
//printf("hit\n"); {
if (player.isHitting){ if (currAngle < angleMax && currAngle > angleMin)
{
// printf("hit\n");
if (player.isHitting)
{
freeIntList(landingPoint); freeIntList(landingPoint);
freeIntList(lastHitPoint); freeIntList(lastHitPoint);
lastHitPoint = allocLastHitPoint(); lastHitPoint = allocLastHitPoint();
...@@ -87,52 +96,61 @@ void hitBall(){ ...@@ -87,52 +96,61 @@ void hitBall(){
ball.speed = HIT_FORCE; ball.speed = HIT_FORCE;
ball.isHit = 1; ball.isHit = 1;
} }
//printf("valid hit\n"); // printf("valid hit\n");
} }
else { else
//printf("unvalid hit\n"); {
// printf("unvalid hit\n");
} }
} }
//} //}
} }
void updateBall(){ void updateBall()
{
ball.x = ball.x + ball.speed * cos(ball.angle); ball.x = ball.x + ball.speed * cos(ball.angle);
ball.y = ball.y + ball.speed * sin(ball.angle); ball.y = ball.y + ball.speed * sin(ball.angle);
if (ball.isHit){ if (ball.isHit)
{
// landingPoint est déjà / BLOCK_SIZE de base // landingPoint est déjà / BLOCK_SIZE de base
ball.z = lagrangeInterpolation(ball.x/BLOCK_SIZE, lastHitPoint[0]/BLOCK_SIZE, lastHitPoint[1]/BLOCK_SIZE, 15 , 2*player.h/BLOCK_SIZE, landingPoint[0], 0); ball.z = lagrangeInterpolation(ball.x / BLOCK_SIZE, lastHitPoint[0] / BLOCK_SIZE, lastHitPoint[1] / BLOCK_SIZE, 15, 2 * player.h / BLOCK_SIZE, landingPoint[0], 0);
if(ball.z > 0){ if (ball.z > 0)
{
printf("param. lagrange : xp=%f, xd=%d, yd=%d, xf=%d, yf=%d, xt=%d, yt=%d\n", printf("param. lagrange : xp=%f, xd=%d, yd=%d, xf=%d, yf=%d, xt=%d, yt=%d\n",
ball.x/BLOCK_SIZE, ball.x / BLOCK_SIZE,
lastHitPoint[0]/BLOCK_SIZE, lastHitPoint[0] / BLOCK_SIZE,
lastHitPoint[1]/BLOCK_SIZE, lastHitPoint[1] / BLOCK_SIZE,
15, 15,
2*player.h/BLOCK_SIZE, 2 * player.h / BLOCK_SIZE,
landingPoint[0], landingPoint[0],
0 0);
);
printf("ballZ: %f\n", ball.z); printf("ballZ: %f\n", ball.z);
} }
} }
} }
void manageMovement()
void manageMovement(){ {
// z : keys[2] : avance
// s : keys[0] : arrière
// q : keys[1] : gauche
// d : keys[3] : droite
float x_increment = (Keys[0] - Keys[2]) * player.deltax + (Keys[3] - Keys[1]) * sin(player.angle); float x_increment = (Keys[0] - Keys[2]) * player.deltax + (Keys[3] - Keys[1]) * sin(player.angle);
float y_increment = (Keys[0] - Keys[2]) * player.deltay + (Keys[1] - Keys[3]) * cos(player.angle); float y_increment = (Keys[0] - Keys[2]) * player.deltay + (Keys[1] - Keys[3]) * cos(player.angle);
float newpos_x = (player.x + x_increment)/BLOCK_SIZE; float newpos_x = (player.x + x_increment) / BLOCK_SIZE;
float newpos_y = (player.y + y_increment)/BLOCK_SIZE; float newpos_y = (player.y + y_increment) / BLOCK_SIZE;
if (newpos_x >= 0 && newpos_x < MAP_WIDTH && newpos_y >= 0 && newpos_y < MAP_HEIGHT){ if (newpos_x >= 0 && newpos_x < MAP_WIDTH && newpos_y >= 0 && newpos_y < MAP_HEIGHT)
if (map[(int)newpos_y][(int)newpos_x] != 1){ {
if (map[(int)newpos_y][(int)newpos_x] != 1)
{
player.x += x_increment * MOVEMENT_SPEED; player.x += x_increment * MOVEMENT_SPEED;
player.y += y_increment * MOVEMENT_SPEED; player.y += y_increment * MOVEMENT_SPEED;
} }
} }
} }
void managePlayer(){ void managePlayer()
{
manageMovement(); manageMovement();
hitBall(); hitBall();
updateBall(); updateBall();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment