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

fix deplacement personnage

parent 5efe0f14
No related branches found
No related tags found
No related merge requests found
#include "gest_event.h"
int Keys[NB_KEYS];
void initKeys(){
void initKeys()
{
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;
while (SDL_PollEvent(&event)){
switch(event.type)
{
case SDL_QUIT:
running = 0;
break;
case SDL_KEYUP:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
running = 0;
continue;
default:
continue;
}
break;
default:
continue;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
running = 0;
break;
case SDL_KEYUP:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
running = 0;
continue;
default:
continue;
}
break;
default:
continue;
}
}
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(){
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);
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 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;
}
}
managePlayer();
SDL_Delay(5);
}
void *EventLoop(void *arg){
while(running){
switch(game_state){
case MENU : gestMenu();break;
case GAME : gestGame();break;
default:printf("game state fault");break;
void *EventLoop(void *arg)
{
while (running)
{
switch (game_state)
{
case MENU:
gestMenu();
break;
case GAME:
gestGame();
break;
default:
printf("game state fault");
break;
}
}
return NULL;
}
\ No newline at end of file
}
\ No newline at end of file
......@@ -6,12 +6,13 @@ player_t ennemy;
player_t ball;
int * landingPoint;
int * lastHitPoint;
int *landingPoint;
int *lastHitPoint;
void initPlayer(){
player.x= 6*BLOCK_SIZE;
player.y= 6*BLOCK_SIZE;
void initPlayer()
{
player.x = 6 * BLOCK_SIZE;
player.y = 6 * BLOCK_SIZE;
player.h = 2 * BLOCK_SIZE;
player.w = 2 * BLOCK_SIZE;
player.angle = 0;
......@@ -29,51 +30,59 @@ void initPlayer(){
player.HPMax = 3;
player.currentHP = player.HPMax;
player.coins = 0;
player.angle=0;
player.deltax = 0;
player.deltay = 1;
player.angle = 0;
player.deltax = 1;
player.deltay = 0;
player.viewAngle = 0;
ball.isHit = 0;
ball.speed = 0;
ball.angle = -pi;
}
int * generateLandingPoint(){
int * landingPoint = malloc(sizeof(int) * 2);
int randomLength = rand() % (int)((MAP_WIDTH/2)*BLOCK_SIZE) + (MAP_WIDTH/2)*BLOCK_SIZE;
int *generateLandingPoint()
{
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 randomPointY = randomLength * sin(player.angle) + player.y;
landingPoint[0] = randomPointX/BLOCK_SIZE;
landingPoint[1] = randomPointY/BLOCK_SIZE;
//printf("landing point: %d %d\n", landingPoint[0], landingPoint[1]);
landingPoint[0] = randomPointX / BLOCK_SIZE;
landingPoint[1] = randomPointY / BLOCK_SIZE;
// printf("landing point: %d %d\n", landingPoint[0], landingPoint[1]);
return landingPoint;
}
int * allocLastHitPoint(){
int * lastHitPoint = (int *)malloc(sizeof(int) * 2);
int *allocLastHitPoint()
{
int *lastHitPoint = (int *)malloc(sizeof(int) * 2);
lastHitPoint[0] = 0;
lastHitPoint[1] = 0;
return lastHitPoint;
}
void freeIntList(int * list){
if (list != NULL){
void freeIntList(int *list)
{
if (list != NULL)
{
free(list);
}
}
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]);
int angleMin = RD * atan2((MAP_WIDTH/2)*BLOCK_SIZE - player.x, player.y);
int angleMax = 90 + RD * atan2((MAP_WIDTH/2)*BLOCK_SIZE - player.x, MAP_HEIGHT * BLOCK_SIZE - player.y);
int currAngle = (int) ((player.angle) * RD +90) %360;
//printf("player angle: %d\n",(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);
if (sqrt(pow(player.x - ball.x, 2) + pow(player.y - ball.y, 2))/BLOCK_SIZE < HIT_RANGE){
if (currAngle < angleMax && currAngle > angleMin){
//printf("hit\n");
if (player.isHitting){
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]);
int angleMin = RD * atan2((MAP_WIDTH / 2) * BLOCK_SIZE - player.x, player.y);
int angleMax = 90 + RD * atan2((MAP_WIDTH / 2) * BLOCK_SIZE - player.x, MAP_HEIGHT * BLOCK_SIZE - player.y);
int currAngle = (int)((player.angle) * RD + 90) % 360;
// printf("player angle: %d\n",(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);
if (sqrt(pow(player.x - ball.x, 2) + pow(player.y - ball.y, 2)) / BLOCK_SIZE < HIT_RANGE)
{
if (currAngle < angleMax && currAngle > angleMin)
{
// printf("hit\n");
if (player.isHitting)
{
freeIntList(landingPoint);
freeIntList(lastHitPoint);
lastHitPoint = allocLastHitPoint();
......@@ -87,52 +96,61 @@ void hitBall(){
ball.speed = HIT_FORCE;
ball.isHit = 1;
}
//printf("valid hit\n");
// printf("valid hit\n");
}
else {
//printf("unvalid hit\n");
else
{
// printf("unvalid hit\n");
}
}
//}
}
void updateBall(){
void updateBall()
{
ball.x = ball.x + ball.speed * cos(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
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){
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)
{
printf("param. lagrange : xp=%f, xd=%d, yd=%d, xf=%d, yf=%d, xt=%d, yt=%d\n",
ball.x/BLOCK_SIZE,
lastHitPoint[0]/BLOCK_SIZE,
lastHitPoint[1]/BLOCK_SIZE,
15,
2*player.h/BLOCK_SIZE,
landingPoint[0],
0
);
ball.x / BLOCK_SIZE,
lastHitPoint[0] / BLOCK_SIZE,
lastHitPoint[1] / BLOCK_SIZE,
15,
2 * player.h / BLOCK_SIZE,
landingPoint[0],
0);
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 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_y = (player.y + y_increment)/BLOCK_SIZE;
if (newpos_x >= 0 && newpos_x < MAP_WIDTH && newpos_y >= 0 && newpos_y < MAP_HEIGHT){
if (map[(int)newpos_y][(int)newpos_x] != 1){
float newpos_x = (player.x + x_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 (map[(int)newpos_y][(int)newpos_x] != 1)
{
player.x += x_increment * MOVEMENT_SPEED;
player.y += y_increment * MOVEMENT_SPEED;
}
}
}
void managePlayer(){
void managePlayer()
{
manageMovement();
hitBall();
updateBall();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment