From 2b1d4a2ce6382db1484227a5a256744b63c42416 Mon Sep 17 00:00:00 2001
From: Louis MALTERRE <malterre.louis@laposte.net>
Date: Thu, 8 Dec 2022 18:56:27 +0100
Subject: [PATCH] attaque monstres implementee, gestion des vies faite, plan
 uml mis a jour (globalement) LM

---
 MAVENProject/PlanUML.puml                     | 45 +++++++++++++++----
 MAVENProject/src/main/java/entity/Attack.java |  6 ++-
 .../src/main/java/entity/Character.java       | 12 +++--
 MAVENProject/src/main/java/entity/Entity.java |  8 ++++
 .../src/main/java/entity/Monster.java         | 17 +++++--
 MAVENProject/src/main/java/jeu/Jeu.java       |  8 ++--
 MAVENProject/src/main/java/jeu/Painter.java   |  4 +-
 7 files changed, 79 insertions(+), 21 deletions(-)

diff --git a/MAVENProject/PlanUML.puml b/MAVENProject/PlanUML.puml
index c16d686..615853f 100644
--- a/MAVENProject/PlanUML.puml
+++ b/MAVENProject/PlanUML.puml
@@ -14,6 +14,7 @@ engine.DrawingPanel <|-- engine.GraphicalInterface
 engine.GamePainter <|-- engine.DrawingPanel
 environment.Coordonnees <|-- entity.Entity
 entity.Character <|-- environment.Physique
+entity.Monster <|-- environnement.Physique
 
 loaders.CharacterLoader <|-- jeu.Jeu
 jeu.Painter <|-- loaders.CharacterLoader
@@ -67,10 +68,7 @@ class entity.Character {
 - double masse
 - double impulsionSaut
 - HashMap tableCommand
-- int alpha
-- int nbLifePoints
-- double entityWIDTH
-- double entityHEIGHT
+
 - double vitesseActuX
 - double vitesseActuZ
 + void deplacements()
@@ -79,33 +77,62 @@ class entity.Character {
 + void attaque()
 + void evolveCharacter()
 + void collisionsGestion()
-+ int getNbLifePoints()
-+ void setNbLifePoints(int)
-+ int getAlpha()
++ List<Obstacles> obstacleInVoisinage()
++ void updateVoisinage()
++ Boolean death()
 + double getM()
 + double getVitesseActuX()
 + double getVitesseActuZ()
 }
 class entity.Monster{
+- double vitesseActuZ
+- Obstacle obstacleBeneath
++ List<Obstacle> obstacleInVoisinage()
++ void updateVoisinage()
++ Boolean death()
 + void deplacements()
 + void attaque()
++ void collisionsGestion()
++ static void evolveAllMonsters(List<Monster>)
++ void evolveMonster()
 }
 
 
 abstract class entity.Entity{
 - HitBox hitBox
+- HitBox voisinage
 - double vitesseMax
+- int nbFramesInvincible
+- int nbMaxFramesInvincible
+- int attackStat
+- int direction
+- List<PositionCollision> obstacleTableCollision
 - Coordonnees coord
+- Coordonnees oldCoord
+- int alpha
+- int nbLifePoints
 - double entityWIDTH
 - double entityHEIGHT
 + void deplacements()
-+ Coordonnes getCoord()
++ Coordonnees getCoord()
++ Coordonnees getOldCoord()
 + void attaque()
++ int getDirection()
++ int getNbLifePoints()
++ int getNbFramesInvincible()
++ void setNbLifePoints(int)
++ int getAlpha()
 }
 entity.Entity <|.. entity.Character
 entity.Entity <|.. entity.Monster
 
-
+class entity.Attack(){
+- int damage
+- Coordonnees coordCentre
+- HitBox hitBox
++ void dealDamage(Entity)
++ HitBox getHitBox()
+}
 
 class environment.Obstacle{
 }
diff --git a/MAVENProject/src/main/java/entity/Attack.java b/MAVENProject/src/main/java/entity/Attack.java
index cb35f38..bfedcf6 100644
--- a/MAVENProject/src/main/java/entity/Attack.java
+++ b/MAVENProject/src/main/java/entity/Attack.java
@@ -16,7 +16,11 @@ public class Attack {
     }
 
     public void dealDamage(Entity entity){
-        entity.setNbLifePoints(Math.max(entity.getNbLifePoints()-damage, 0));
+        if (entity.nbFramesInvincible == 0){//n'attaque que si l'entity n'est pas invincible
+            //le Math.max est la juste pour que la vie du perso ne tombe pas en-dessous de zero
+            entity.setNbLifePoints(Math.max(entity.getNbLifePoints()-damage, 0));
+            entity.nbFramesInvincible = entity.nbMaxFramesInvicible;//l'entity devient invincible qq frames
+        }
     }
 
     public HitBox getHitBox() {
diff --git a/MAVENProject/src/main/java/entity/Character.java b/MAVENProject/src/main/java/entity/Character.java
index 4ff2ff6..f0527ae 100644
--- a/MAVENProject/src/main/java/entity/Character.java
+++ b/MAVENProject/src/main/java/entity/Character.java
@@ -35,7 +35,8 @@ public class Character extends Entity{
         this.tableCommande = new HashMap<>();
         tableCommande.put("CommandX",(double)0);
         tableCommande.put("CommandZ",(double)0);
-        tableCommande.put("CommandAttack",(double)0); // 1 si attack, 0 sinon   
+        tableCommande.put("CommandAttack",(double)0); // 1 si attack, 0 sinon  
+        nbMaxFramesInvicible = 500; 
     }
 
     @Override
@@ -137,8 +138,8 @@ public class Character extends Entity{
         Attack characterAttack = new Attack(attackStat, attackCenter, new HitBox(attackCenter,entityHEIGHT,entityWIDTH+20));
         for (Monster monster : Jeu.getMonsterList()){
             if (HitBox.collision(characterAttack.getHitBox(), monster.getHitBox())){
-                //le Math.max est la juste pour que la vie du perso ne tombe pas en-dessous de zero
-                monster.setNbLifePoints(Math.max(monster.getNbLifePoints()-attackStat,0));
+                //si collision, l'attaque fait ses dmg
+                characterAttack.dealDamage(monster);
             }
         }
     }
@@ -175,10 +176,15 @@ public class Character extends Entity{
         updateVoisinage();
         this.setHitBox(new HitBox(this.getCoord(),entityHEIGHT,entityWIDTH)); // mise à jour moins crade pour l'instant
         if (tableCommande.get("CommandAttack") == 1) attaque();
+        updateNbFrameInvincible();
         resetCommand();
         
     }
 
+    private void updateNbFrameInvincible(){
+        nbFramesInvincible = Math.max(0, nbFramesInvincible-1);
+    }
+
     public double getVitesseActuX() {
         return vitesseActuX;
     }
diff --git a/MAVENProject/src/main/java/entity/Entity.java b/MAVENProject/src/main/java/entity/Entity.java
index 73d24c0..7d59585 100644
--- a/MAVENProject/src/main/java/entity/Entity.java
+++ b/MAVENProject/src/main/java/entity/Entity.java
@@ -26,6 +26,8 @@ public abstract class Entity {
     HitBox voisinage;
     int direction = 1; // direction selon l'axe des x, -1 pour la gauche et 1 pour la droite
     List<PositionCollision>  obstacleTableCollision = new ArrayList<>();
+    int nbFramesInvincible = 0;
+    int nbMaxFramesInvicible;
 
     
     public Entity(Coordonnees c,double vitesseMax,double width,double height,int atkStt){
@@ -41,6 +43,7 @@ public abstract class Entity {
         for (int i = 0;i<getObstacleTable().size();i++){
             obstacleTableCollision.add(NONE);
         }
+        nbMaxFramesInvicible = 100;
     }
     public void deplacements(){}
 
@@ -82,6 +85,8 @@ public abstract class Entity {
         return nbLifePoints;
     }
 
+    
+
     public void setNbLifePoints(int nbLifePoints) {
         this.nbLifePoints = nbLifePoints;
     }
@@ -117,6 +122,9 @@ public abstract class Entity {
     }
     public HitBox getVoisinage() {
         return voisinage;
+    }
+    public int getNbFramesInvincible() {
+        return nbFramesInvincible;
     }   
     
 }
diff --git a/MAVENProject/src/main/java/entity/Monster.java b/MAVENProject/src/main/java/entity/Monster.java
index 8887c1b..235f9ee 100644
--- a/MAVENProject/src/main/java/entity/Monster.java
+++ b/MAVENProject/src/main/java/entity/Monster.java
@@ -86,18 +86,29 @@ public class Monster extends Entity{
 
     
     public void attaque(){
-        if (HitBox.collision(this.hitBox, getGameCharacter().getHitBox())){
-            //le Math.max est la juste pour que la vie du perso ne tombe pas en-dessous de zero
-            getGameCharacter().setNbLifePoints(Math.max(getGameCharacter().getNbLifePoints()-attackStat,0));
+        if (direction*(this.coord.getX()-getGameCharacter().coord.getX()) <= 10 && Math.abs(this.coord.getZ()-getGameCharacter().coord.getZ()) <= entityHEIGHT){ //n'attaque que si perso pas invincible
+            //on cree l'attaque
+            Coordonnees coordAttack = new Coordonnees(this.coord.getX() + direction*entityWIDTH/2,this.coord.getZ());
+            Attack monsterAttack = new Attack(attackStat, coordAttack, new HitBox(coordAttack, entityHEIGHT, entityWIDTH+20));
+            //si il ya collision entre le perso et l'attaque
+            if (HitBox.collision(monsterAttack.getHitBox(), getGameCharacter().hitBox)){
+                //l'attaque fait ses degats
+                monsterAttack.dealDamage(getGameCharacter());
+            }
         }
     }
 
+    private void updateNbFrameInvincible(){
+        nbFramesInvincible = Math.max(0, nbFramesInvincible-1);
+    }
+
     public void evolveMonster(){ //evolution du monstre
         deplacements();
         collisionGestion(); // ca merde
         attaque();
         updateVoisinage();
         this.setHitBox(new HitBox(this.getCoord(),entityHEIGHT,entityWIDTH));
+        updateNbFrameInvincible();
         
     }
 
diff --git a/MAVENProject/src/main/java/jeu/Jeu.java b/MAVENProject/src/main/java/jeu/Jeu.java
index e39366e..1e6a2df 100644
--- a/MAVENProject/src/main/java/jeu/Jeu.java
+++ b/MAVENProject/src/main/java/jeu/Jeu.java
@@ -105,10 +105,10 @@ public class Jeu implements Game{
         //coffreTable.add(new Coffre(new HitBox(new Coordonnees(900, 685),30,30))); //table des coffres
 
         //on met des monstres, faut supp la pour table monstres
-        monsterList.add(new Monster(new Coordonnees(1000, sol.getHitbox().getExtremites().get("HautGauche").getZ()-500), 1000, 30, 60, 1,1));
-        monsterList.add(new Monster(new Coordonnees(500, sol.getHitbox().getExtremites().get("HautGauche").getZ()-500), 1000, 30, 60, 1,1));
-        monsterList.add(new Monster(new Coordonnees(100, sol.getHitbox().getExtremites().get("HautGauche").getZ()-500), 1000, 30, 60, 1,1));
-        monsterList.add(new Monster(new Coordonnees(300, sol.getHitbox().getExtremites().get("HautGauche").getZ()-500), 1000, 30, 60, 1,1));
+        monsterList.add(new Monster(new Coordonnees(1000, sol.getHitbox().getExtremites().get("HautGauche").getZ()-500), 1000, 30, 60, 1,5));
+        monsterList.add(new Monster(new Coordonnees(500, sol.getHitbox().getExtremites().get("HautGauche").getZ()-500), 1000, 30, 60, 1,5));
+        monsterList.add(new Monster(new Coordonnees(100, sol.getHitbox().getExtremites().get("HautGauche").getZ()-500), 1000, 30, 60, 1,5));
+        monsterList.add(new Monster(new Coordonnees(300, sol.getHitbox().getExtremites().get("HautGauche").getZ()-500), 1000, 30, 60, 1,5));
 
         //on charge le perso (a la fin du constructeur, cest important par rapport a obstacleTable, sinon y'a des pbs de "causalite")
         gameCharacterLoader = new CharacterLoader(characterSkinPath,new Character(new Coordonnees(300, 300), 1500, 1,600000));
diff --git a/MAVENProject/src/main/java/jeu/Painter.java b/MAVENProject/src/main/java/jeu/Painter.java
index 325a298..d9a82c3 100644
--- a/MAVENProject/src/main/java/jeu/Painter.java
+++ b/MAVENProject/src/main/java/jeu/Painter.java
@@ -52,13 +52,14 @@ public class Painter implements GamePainter{
 
 
     private void drawCharacter(Graphics2D crayon,Image imageCharac){
+        if (gameCharacter.getNbFramesInvincible() > 0)crayon.setColor(Color.black);
         crayon.fillRect(WIDTH/2-(int) gameCharacter.getEntityWIDTH()/2,WIDTH/2-(int) gameCharacter.getEntityHEIGHT()/2, (int) gameCharacter.getEntityWIDTH(),(int) gameCharacter.getEntityHEIGHT());
         crayon.drawRect(WIDTH/2-500,WIDTH/2-500, 1000,1000);
         if (gameCharacter.getDirection() == 1){
             crayon.drawImage(imageCharac, WIDTH/2- (int) gameCharacter.getEntityWIDTH()/2-10, HEIGHT/2 - (int) gameCharacter.getEntityHEIGHT()/2-3, null, null);
         }
         else{
-            crayon.drawImage(imageCharac, WIDTH/2- (int) gameCharacter.getEntityWIDTH()/2-10-20, HEIGHT/2 - (int) gameCharacter.getEntityHEIGHT()/2-3, null, null); 
+            crayon.drawImage(imageCharac, WIDTH/2- (int) gameCharacter.getEntityWIDTH()/2-10-30, HEIGHT/2 - (int) gameCharacter.getEntityHEIGHT()/2-3, null, null); 
         }
     }
 
@@ -163,6 +164,7 @@ public class Painter implements GamePainter{
             z = (int)monster.getCoord().getZ();
             crayon.drawRect(x-300 - xCam, z-300 - zCam, 600, 600);
             if (HitBox.collision(gameCharacter.getHitBox(),monster.getHitBox()))crayon.setColor(Color.red);
+            if (monster.getNbFramesInvincible() > 0)crayon.setColor(Color.black);
             crayon.fillRect(x-(int)monster.getEntityWIDTH()/2 - xCam, z-(int)monster.getEntityHEIGHT()/2 - zCam, (int)monster.getEntityWIDTH(), (int)monster.getEntityHEIGHT());
         }
     }
-- 
GitLab