From 24f3d7d0daf7b05934b653787f16f4e0b70c9988 Mon Sep 17 00:00:00 2001 From: Louis MALTERRE <malterre.louis@laposte.net> Date: Tue, 29 Nov 2022 10:54:53 +0100 Subject: [PATCH] generalisation de collision a entity --- MAVENProject/PlanUML.puml | 4 +- .../src/main/java/entity/Character.java | 37 ++++++------------- MAVENProject/src/main/java/entity/Entity.java | 21 ++++++++++- .../src/main/java/environnement/HitBox.java | 6 +-- MAVENProject/src/main/java/map/Obstacle.java | 10 ++--- 5 files changed, 40 insertions(+), 38 deletions(-) diff --git a/MAVENProject/PlanUML.puml b/MAVENProject/PlanUML.puml index e402530..48d37f1 100644 --- a/MAVENProject/PlanUML.puml +++ b/MAVENProject/PlanUML.puml @@ -68,8 +68,8 @@ class entity.Character { - double impulsionSaut - HashMap tableCommand - int alpha -- double characterWIDTH -- double characterHEIGHT +- double entityWIDTH +- double entityHEIGHT - double vitesseActuX - double vitesseActuZ + void deplacements() diff --git a/MAVENProject/src/main/java/entity/Character.java b/MAVENProject/src/main/java/entity/Character.java index 1b33b1f..0c7bec7 100644 --- a/MAVENProject/src/main/java/entity/Character.java +++ b/MAVENProject/src/main/java/entity/Character.java @@ -15,22 +15,21 @@ public class Character extends Entity{ private double vitesseActuZ = 0; private final double impulsionSaut; private double masse; - private HitBox hitBox; private int alpha = 1; // gere les collisions avec le sol - private final double characterWIDTH = 30; // pour l'instant je les ai renommes, ca peut changer - private final double characterHEIGHT= 60; + private final double entityWIDTH = 30; // pour l'instant je les ai renommes, ca peut changer + private final double entityHEIGHT = 60; private Map<String,Double> tableCommande; //table des commandes du perso //private BufferedImage sprite; - private Obstacle obstacleBeneath = Jeu.getSol(); + public Character(Coordonnees c, double vitesseMax, double m, double impSaut){ - super(c,vitesseMax,null); // comme tous les entities ont une hitbox, j'ai factorise + super(c,vitesseMax); // comme tous les entities ont une hitbox, j'ai factorise this.masse = m; this.oldCoord = this.getCoord(); this.impulsionSaut = impSaut; - this.hitBox = new HitBox(c,characterHEIGHT,characterWIDTH); + this.setHitBox(new HitBox(c,entityHEIGHT,entityWIDTH)); this.tableCommande = new HashMap<>(); tableCommande.put("CommandX",(double)0); tableCommande.put("CommandZ",(double)0); @@ -44,14 +43,15 @@ public class Character extends Entity{ //on actualise les positions double newX,newZ; + //on se souvient de ses anciennes positions this.oldCoord = this.getCoord(); newX = tableCommande.get("CommandX")*delta + this.getCoord().getX(); newZ = ((1-alpha)*Physique.g - tableCommande.get("CommandZ"))/2*delta*delta + vitesseActuZ*delta + this.getCoord().getZ(); - if (newZ+characterHEIGHT/2 >= obstacleBeneath.getHitbox().getExtremites().get("HautGauche").getZ()){ - newZ = obstacleBeneath.getHitbox().getExtremites().get("HautGauche").getZ()-characterHEIGHT/2; + if (newZ+entityHEIGHT/2 >= obstacleBeneath.getHitbox().getExtremites().get("HautGauche").getZ()){ + newZ = obstacleBeneath.getHitbox().getExtremites().get("HautGauche").getZ()-entityHEIGHT/2; //alpha = 1; } @@ -99,13 +99,10 @@ public class Character extends Entity{ public void obstacleBeneathCharacter(){ Obstacle obsMin = Jeu.getSol(); - int compteur = 0; for (Obstacle obs : Jeu.getObstacleTable()){ - compteur++; if (obs != Jeu.getSol() && obs.getHitbox().getExtremites().get("HautDroite").getZ() >= this.getCoord().getZ() && HitBox.collisionDroite(hitBox, obs.getHitbox()) && HitBox.collisionGauche(hitBox, obs.getHitbox())){ if (obs.getHitbox().getExtremites().get("HautDroite").getZ() < obsMin.getHitbox().getExtremites().get("HautDroite").getZ()){ obsMin = obs; - System.out.println("obstacle = "+compteur); } } }obstacleBeneath = obsMin; @@ -129,9 +126,9 @@ public class Character extends Entity{ checkIfOnGround(); //checkIfNextToWall(); deplacements(); - //checkIfNextToWall(); + checkIfNextToWall(); resetCommand(); - this.hitBox = new HitBox(this.getCoord(),characterHEIGHT,characterWIDTH); // mise à jour crade pour l'instant + this.setHitBox(new HitBox(this.getCoord(),entityHEIGHT,entityWIDTH)); // mise à jour moins crade pour l'instant } public double getVitesseMax() { @@ -154,24 +151,14 @@ public class Character extends Entity{ } @Override public double getEntityWIDTH() { - return this.characterWIDTH; + return this.entityWIDTH; } @Override public double getEntityHEIGHT() { - return this.characterHEIGHT; + return this.entityHEIGHT; } public int getAlpha() { return alpha; } - - public HitBox getHitBox() { - return hitBox; - } - public Obstacle getObstacleBeneath() { - return obstacleBeneath; - } - public void setObstacleBeneath(Obstacle obstacleBeneath) { - this.obstacleBeneath = obstacleBeneath; - } } diff --git a/MAVENProject/src/main/java/entity/Entity.java b/MAVENProject/src/main/java/entity/Entity.java index 3b4dbe4..4efaaa3 100644 --- a/MAVENProject/src/main/java/entity/Entity.java +++ b/MAVENProject/src/main/java/entity/Entity.java @@ -2,6 +2,8 @@ package entity; import environnement.Coordonnees; import environnement.HitBox; +import jeu.Jeu; +import map.Obstacle; public abstract class Entity { private Coordonnees coord; @@ -12,10 +14,12 @@ public abstract class Entity { private final double entityWIDTH = 10; private final double entityHEIGHT = 10; - public Entity(Coordonnees c,double vitesseMax,HitBox hitBox){ + protected Obstacle obstacleBeneath = Jeu.getSol(); + + public Entity(Coordonnees c,double vitesseMax){ this.coord = c; this.vitesseMax = vitesseMax; - this.hitBox = hitBox; + this.hitBox = new HitBox(c, entityHEIGHT, entityWIDTH); } public void deplacements(){} public void attaque(){} @@ -34,5 +38,18 @@ public abstract class Entity { public Coordonnees getOldCoord() { return oldCoord; } + public HitBox getHitBox() { + return hitBox; + } + public Obstacle getObstacleBeneath() { + return obstacleBeneath; + } + public void setHitBox(HitBox hitBox) { + this.hitBox = hitBox; + } + public void setObstacleBeneath(Obstacle obstacleBeneath) { + this.obstacleBeneath = obstacleBeneath; + } + } diff --git a/MAVENProject/src/main/java/environnement/HitBox.java b/MAVENProject/src/main/java/environnement/HitBox.java index f8e5c4f..82c4257 100644 --- a/MAVENProject/src/main/java/environnement/HitBox.java +++ b/MAVENProject/src/main/java/environnement/HitBox.java @@ -38,16 +38,16 @@ public class HitBox { } public static Boolean collisionDroite(HitBox A, HitBox B){ - return !(A.extremites.get("HautGauche").getX() >= B.extremites.get("HautDroite").getX());// A est à droite de B + return !(A.extremites.get("HautGauche").getX() > B.extremites.get("HautDroite").getX());// A est à droite de B } public static Boolean collisionGauche(HitBox A, HitBox B){ - return !(A.extremites.get("HautDroite").getX() <= B.extremites.get("HautGauche").getX());// A est à gauche de B + return !(A.extremites.get("HautDroite").getX() < B.extremites.get("HautGauche").getX());// A est à gauche de B } public static Boolean collisionBas(HitBox A, HitBox B){ System.out.println(A.extremites.get("HautGauche").getZ()+" "+B.extremites.get("BasGauche").getZ()); return !(A.extremites.get("HautGauche").getZ() > B.extremites.get("BasGauche").getZ());// A est en dessous de B } public static Boolean collisionHaut(HitBox A, HitBox B){ - return !(A.extremites.get("BasGauche").getZ() <= B.extremites.get("HautGauche").getZ());// A est au dessus de B + return !(A.extremites.get("BasGauche").getZ() < B.extremites.get("HautGauche").getZ());// A est au dessus de B } } \ No newline at end of file diff --git a/MAVENProject/src/main/java/map/Obstacle.java b/MAVENProject/src/main/java/map/Obstacle.java index b155441..acd13a7 100644 --- a/MAVENProject/src/main/java/map/Obstacle.java +++ b/MAVENProject/src/main/java/map/Obstacle.java @@ -1,9 +1,7 @@ package map; import java.util.List; - -import entity.Character; - +import entity.Entity; import environnement.HitBox; import jeu.Jeu; @@ -42,7 +40,7 @@ public class Obstacle { return false; }*/ - public static Boolean collisionBas(Character charac){ + public static Boolean collisionBas(Entity charac){ if (charac.getObstacleBeneath() != Jeu.getSol()){ if (HitBox.collisionDroite(charac.getHitBox(),charac.getObstacleBeneath().getHitbox()) && HitBox.collisionGauche(charac.getHitBox(),charac.getObstacleBeneath().getHitbox()) && HitBox.collisionBas(charac.getObstacleBeneath().getHitbox(),charac.getHitBox())) { return true; @@ -53,7 +51,7 @@ public class Obstacle { return false; } - public static Boolean collisionGauche(Character charac,List<Obstacle> list){ + public static Boolean collisionGauche(Entity charac,List<Obstacle> list){ for (Obstacle obs : list){ if (HitBox.collisionGauche(charac.getHitBox(), obs.getHitbox()) && obs != Jeu.getSol() && !HitBox.collisionHaut(obs.getHitbox(),charac.getHitBox())) return true; @@ -61,7 +59,7 @@ public class Obstacle { return false; } - public static Boolean collisionDroite(Character charac,List<Obstacle> list){ + public static Boolean collisionDroite(Entity charac,List<Obstacle> list){ for (Obstacle obs : list){ if (HitBox.collisionDroite(charac.getHitBox(), obs.getHitbox()) && obs != Jeu.getSol() && !HitBox.collisionHaut(obs.getHitbox(),charac.getHitBox())) return true; -- GitLab