From bbc523a8b4cccd49caddd7f2be5c8e191764b3a6 Mon Sep 17 00:00:00 2001
From: belkhiritaha <belkhiri.taha53@gmail.com>
Date: Tue, 28 Jun 2022 17:26:26 +0200
Subject: [PATCH] ajout affichage de la balle

---
 .../jeu_appren_par_renfo/src/player.c         |  6 ++++
 .../jeu_appren_par_renfo/src/player.h         |  1 +
 .../jeu_appren_par_renfo/src/render.c         | 28 +++++++++++++++++++
 3 files changed, 35 insertions(+)

diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/player.c b/travail_de_groupe/jeu_appren_par_renfo/src/player.c
index 83c93a2..ac6ae73 100644
--- a/travail_de_groupe/jeu_appren_par_renfo/src/player.c
+++ b/travail_de_groupe/jeu_appren_par_renfo/src/player.c
@@ -4,6 +4,8 @@ player_t player;
 
 player_t ennemy;
 
+player_t ball;
+
 void initPlayer(){
     player.x= 6*BLOCK_SIZE;
     player.y= 6*BLOCK_SIZE;
@@ -13,6 +15,10 @@ void initPlayer(){
     ennemy.w = 2 * BLOCK_SIZE;
     ennemy.x = 20 * BLOCK_SIZE;
     ennemy.y = 15 * BLOCK_SIZE;
+    ball.h = 0.5 * BLOCK_SIZE;
+    ball.w = 0.5 * BLOCK_SIZE;
+    ball.x = 10 * BLOCK_SIZE;
+    ball.y = 10 * BLOCK_SIZE;
     player.speed = 1;
     player.isMoving = 0;
     player.HPMax = 3;
diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/player.h b/travail_de_groupe/jeu_appren_par_renfo/src/player.h
index 2fbe0dc..947f3bc 100644
--- a/travail_de_groupe/jeu_appren_par_renfo/src/player.h
+++ b/travail_de_groupe/jeu_appren_par_renfo/src/player.h
@@ -29,6 +29,7 @@ typedef struct player{
 
 extern player_t player;
 extern player_t ennemy;
+extern player_t ball;
 
 void initPlayer();
 void managePlayer();
diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/render.c b/travail_de_groupe/jeu_appren_par_renfo/src/render.c
index eada6fa..af7310f 100644
--- a/travail_de_groupe/jeu_appren_par_renfo/src/render.c
+++ b/travail_de_groupe/jeu_appren_par_renfo/src/render.c
@@ -427,6 +427,33 @@ void drawEnnemy(){
     }
 }
 
+void drawBall(){
+    float ballAngle = atan2((ball.y + ball.w/2)  - (player.y + player.w/2) , (ball.x + ball.w/2) - (player.x + player.w/2));
+    if (ballAngle < 0) ballAngle += 2*pi;
+    if (ballAngle > 2*pi) ballAngle -= 2*pi;
+    float ballDistance = sqrt((ball.x - player.x)*(ball.x - player.x) + (ball.y - player.y)*(ball.y - player.y)) * BLOCK_SIZE;
+    float ballBaseWidth = BLOCK_SIZE/2;
+    float ballDistanceX = ballDistance * cos(ballAngle - player.angle) * BLOCK_SIZE;
+    float ballDistanceY = ballDistance * fabs(sin(ballAngle - player.angle)) * BLOCK_SIZE;
+    float scaledBallWidth = ballBaseWidth / sqrt(3);
+    int ballWidth = 25;
+    int ballHeight = 25;
+
+    if (ballAngle >= player.angle - (FOV_ANGLE)/2 * DR && ballAngle <= player.angle + (FOV_ANGLE)/2 * DR){
+        rect.x = screenDimension.w/2 + (screenDimension.w * tan(ballAngle - player.angle)) * sqrt(3) * 0.5;
+        rect.w = (ballWidth * screenDimension.w) / (ballDistance/BLOCK_SIZE);
+        rect.h = (ballHeight * screenDimension.h)/(ballDistance/BLOCK_SIZE);
+        rect.y = (screenDimension.h/2 + player.viewAngle) - rect.h/5;
+
+        destRect.x = 0;
+        destRect.y = 0;
+        destRect.w = 64;
+        destRect.h = 64;
+        printf("%d %d %d %d\n", rect.x, rect.y, rect.w, rect.h); 
+        SDL_RenderCopy(renderer, playerTexture, &destRect, &rect);
+    }
+}
+
 void drawSkyAndGround(){
     SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
     SDL_RenderFillRect(renderer, NULL);
@@ -503,6 +530,7 @@ void drawGame(){
     drawHorizentalRays();
     drawEnnemy();
     drawVerticalRays();
+    drawBall();
     drawMap2D(map);
     drawFPS();
     SDL_RenderPresent(renderer);
-- 
GitLab