diff --git a/MAVENProject/src/main/java/entity/Character.java b/MAVENProject/src/main/java/entity/Character.java index 5a74d517766f7a63aa3f56a60ff2e6c9fa984bf4..d3c27838c0ad1094f209e3b6fc8a25495f7e727a 100644 --- a/MAVENProject/src/main/java/entity/Character.java +++ b/MAVENProject/src/main/java/entity/Character.java @@ -1,7 +1,9 @@ package entity; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import environnement.Coordonnees; @@ -21,7 +23,6 @@ public class Character extends Entity{ private double vitesseActuZ = 0; private final double impulsionSaut; private double masse; - private int alpha = 1; // gere les collisions avec le sol private Map<String,Double> tableCommande; //table des commandes du perso @@ -30,8 +31,7 @@ public class Character extends Entity{ public Character(Coordonnees c, double vitesseMax, double m, double impSaut){ - - super(c,vitesseMax,30,60); // comme tous les entities ont une hitbox, j'ai factorise + super(c,vitesseMax,30,60,1); // comme tous les entities ont une hitbox, j'ai factorise this.masse = m; this.impulsionSaut = impSaut; @@ -60,8 +60,6 @@ public class Character extends Entity{ alpha = 1; } - System.out.println(newZ-this.getCoord().getZ()); - this.setCoord(new Coordonnees(newX, newZ)); // on set les nouvelles coordonnees //on actualise les vitesses @@ -71,12 +69,13 @@ public class Character extends Entity{ } public void collisionGestion(){ + List<Obstacle> listeObstaclesInVoisinage = obstacleInVoisinage(); //stocke l'obstacle avec lequel il y a eu collision HashMap<Obstacle,PositionCollision> collisionsPresentes = new HashMap<Obstacle,PositionCollision>(); //recupere les collisions - for (int i=0;i<getObstacleTable().size();i++) { - collisionsPresentes.put(getObstacleTable().get(i),collisionObstacle(this.getHitBox(), getObstacleTable().get(i).getHitbox(), i)); + for (int i=0;i<listeObstaclesInVoisinage.size();i++) { + collisionsPresentes.put(listeObstaclesInVoisinage.get(i),collisionObstacle(this.getHitBox(), listeObstaclesInVoisinage.get(i).getHitbox(), i)); } //on set alpha a 0 (on suppose qu'il est en l'air) @@ -99,6 +98,7 @@ public class Character extends Entity{ //on gere les collisions laterales et celle du haut for (int i=0;i<collisionsPresentes.size();i++){ if (collisionsPresentes.get(getObstacleTable().get(i)) == DROITE && this.vitesseActuX < 0){ + System.out.println("coucou"); this.setCoord(new Coordonnees(getObstacleTable().get(i).getHitbox().getExtremites().get("HautDroite").getX()+entityWIDTH/2, this.getCoord().getZ())); } else if (collisionsPresentes.get(getObstacleTable().get(i)) == GAUCHE && this.vitesseActuX > 0){ @@ -108,13 +108,27 @@ public class Character extends Entity{ this.vitesseActuZ = 0; this.setCoord(new Coordonnees(this.getCoord().getX(), getObstacleTable().get(i).getHitbox().getExtremites().get("BasGauche").getZ()+entityHEIGHT/2+1)); } - } + } + } - + public List<Obstacle> obstacleInVoisinage(){ + List<Obstacle> liste = new ArrayList<>(); + for (Obstacle obs : getObstacleTable()){ + if (HitBox.collision(voisinage, obs.getHitbox())){ + liste.add(obs); + } + } + return liste; } + @Override public void attaque(){ - + for (Monster monster : Jeu.getMonsterList()){ + if (HitBox.collision(this.hitBox, 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)); + } + } } public void getGameCommand(){ @@ -134,7 +148,6 @@ public class Character extends Entity{ } } - public void resetCommand(){ for (String key : tableCommande.keySet()){ tableCommande.replace(key, (double)0); @@ -145,8 +158,10 @@ public class Character extends Entity{ getGameCommand(); deplacements(); collisionGestion(); - resetCommand(); + updateVoisinage(); this.setHitBox(new HitBox(this.getCoord(),entityHEIGHT,entityWIDTH)); // mise à jour moins crade pour l'instant + resetCommand(); + } public double getVitesseActuX() { @@ -160,12 +175,12 @@ public class Character extends Entity{ public double getM() { return masse; } + public double getImpulsionSaut() { return impulsionSaut; } - public int getAlpha() { - return alpha; + public void updateVoisinage(){ + voisinage = new HitBox(coord, 600, 600); } - } diff --git a/MAVENProject/src/main/java/entity/Entity.java b/MAVENProject/src/main/java/entity/Entity.java index 20f6c572adff58095d24571cc6a165c2317178ff..c26b7bca7e14d8be279b81c8c92027092cef543a 100644 --- a/MAVENProject/src/main/java/entity/Entity.java +++ b/MAVENProject/src/main/java/entity/Entity.java @@ -5,31 +5,33 @@ import environnement.HitBox; public abstract class Entity { - private Coordonnees coord; + protected Coordonnees coord; protected Coordonnees oldCoord; - final double vitesseMax; + protected final double vitesseMax; protected HitBox hitBox; - protected int nbLifePoints = 3; + protected int attackStat; // nb de pv enleves par attaque + protected int maxLifePoints = 3; + protected int nbLifePoints = maxLifePoints; protected final double entityWIDTH; protected final double entityHEIGHT; + protected int alpha = 1; // gere les collisions avec le sol + protected HitBox voisinage; - - public int getNbLifePoints() { - return nbLifePoints; - } - public void setNbLifePoints(int nbLifePoints) { - this.nbLifePoints = nbLifePoints; - } - public Entity(Coordonnees c,double vitesseMax,double width,double height){ + + public Entity(Coordonnees c,double vitesseMax,double width,double height,int atkStt){ this.coord = c; + this.attackStat = atkStt; this.oldCoord = this.coord; this.vitesseMax = vitesseMax; this.entityHEIGHT = height; this.entityWIDTH = width; this.hitBox = new HitBox(c, entityHEIGHT, entityWIDTH); + this.voisinage = new HitBox(c, 600, 600); } public void deplacements(){} + public void attaque(){} + public Coordonnees getCoord(){ return this.coord; } // Pour retourner les coordonées @@ -48,6 +50,12 @@ public abstract class Entity { public HitBox getHitBox() { return hitBox; } + public int getNbLifePoints() { + return nbLifePoints; + } + public void setNbLifePoints(int nbLifePoints) { + this.nbLifePoints = nbLifePoints; + } public void setHitBox(HitBox hitBox) { this.hitBox = hitBox; @@ -55,7 +63,19 @@ public abstract class Entity { public double getVitesseMax() { return vitesseMax; } - - + public int getMaxLifePoints() { + return maxLifePoints; + } + public int getAlpha() { + return alpha; + } + public int getAttackStat() { + return attackStat; + } + + public void updateVoisinage(){ + + } + } diff --git a/MAVENProject/src/main/java/entity/Monster.java b/MAVENProject/src/main/java/entity/Monster.java index b9426ca3d99b3153f27699c8a744071d73f444cc..865d77a5bb38b53fce5451c215652bc0d5a9f8aa 100644 --- a/MAVENProject/src/main/java/entity/Monster.java +++ b/MAVENProject/src/main/java/entity/Monster.java @@ -1,14 +1,52 @@ package entity; import environnement.Coordonnees; +import environnement.HitBox; + +import static jeu.Jeu.getGameCharacter; + +import java.util.List; public class Monster extends Entity{ - private int attackStat; //nombre de points de vie enleves par une attaque + int direction = -1; // direction selon l'axe des x, -1 pour la gauche et 1 pour la droite + + public Monster(Coordonnees c, double vitesseMax,double width,double height,int atkStt,int nbLP) { + super(c, vitesseMax,width,height,atkStt); + maxLifePoints = nbLP; + nbLifePoints = maxLifePoints; + } + + public void deplacements(){ + double delta = Math.pow(10, -3); + + //on se souvient de ses anciennes positions + oldCoord = coord; + + //temporaire, pour faire un deplacement cyclique + if (coord.getX() < 0)direction = 1; + else if (coord.getX() >= 1000)direction = -1; + //****************** + + double newX = coord.getX()+direction*vitesseMax*delta; + setCoord(new Coordonnees(newX, coord.getZ())); + } + + 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)); + } + } + + public void evolveMonster(){ //evolution du monstre + deplacements(); + attaque(); + this.setHitBox(new HitBox(this.getCoord(),entityHEIGHT,entityWIDTH)); + } - public Monster(Coordonnees c, double vitesseMax,double width,double height,int atkStt) { - super(c, vitesseMax,width,height); - attackStat = atkStt; + public static void evolveAllMonsters(List<Monster> liste){ // evolution de tous les monstres + for (Monster monster : liste)monster.evolveMonster(); } } diff --git a/MAVENProject/src/main/java/jeu/Jeu.java b/MAVENProject/src/main/java/jeu/Jeu.java index c0685f7767e4e41a0db206a8dd8c0364b2166936..d33bb27acb1edcdfc704cc08a763aed146535f1f 100644 --- a/MAVENProject/src/main/java/jeu/Jeu.java +++ b/MAVENProject/src/main/java/jeu/Jeu.java @@ -10,6 +10,7 @@ import java.util.List; import java.util.Map; import entity.Character; +import entity.Monster; import engine.Game; import environnement.Coordonnees; import environnement.HitBox; @@ -55,7 +56,9 @@ public class Jeu implements Game{ private static List<Obstacle> obstacleTable = new ArrayList<>(); private static List<PositionCollision> obstacleTableCollisionCharac= new ArrayList<>(); + private static List<Monster> monsterList = new ArrayList<>(); + public Jeu(String source) { @@ -111,6 +114,8 @@ public class Jeu implements Game{ obstacleTableCollisionCharac.add(NONE); obstacleTableCollisionCharac.add(NONE); + monsterList.add(new Monster(new Coordonnees(1000, sol.getHitbox().getExtremites().get("HautGauche").getZ()-30), 1000, 30, 60, 1,3)); + } @Override @@ -130,6 +135,9 @@ public class Jeu implements Game{ //perso evolue dans le jeu (en actualisant ses coordonnees) gameCharacter.evolveCharacter(); + //monstres evoluent dans le jeu + Monster.evolveAllMonsters(monsterList); + System.out.println(gameCharacter.getNbLifePoints()); //animation du perso dans le jeu (definir la bonne frame a cet instant) animation(Attack, Right, Left); @@ -142,25 +150,6 @@ public class Jeu implements Game{ return false; } - - public static Map<String, Boolean> getDirectionJeu() { - return directionJeu; - } - - public static Obstacle getSol() { - return sol; - } - - public static List<Obstacle> getObstacleTable() { - return obstacleTable; - } - public static List<PositionCollision> getObstacleTableCollisionCharac() { - return obstacleTableCollisionCharac; - } - public static void setObstacleTableCollisionCharac(List<PositionCollision> obstacleTableCollisionCharac) { - Jeu.obstacleTableCollisionCharac = obstacleTableCollisionCharac; - } - private void animation(Boolean Attack,Boolean Right,Boolean Left){ if (Attack && !characterAttacking) { movement = "character_attack"; @@ -203,5 +192,32 @@ public class Jeu implements Game{ gameCharacterLoader.setSkinPath(characterSkinPath); gameCharacterLoader.refreshImage(); } + + public static Map<String, Boolean> getDirectionJeu() { + return directionJeu; + } + + public static Obstacle getSol() { + return sol; + } + + public static List<Obstacle> getObstacleTable() { + return obstacleTable; + } + public static List<PositionCollision> getObstacleTableCollisionCharac() { + return obstacleTableCollisionCharac; + } + public static void setObstacleTableCollisionCharac(List<PositionCollision> obstacleTableCollisionCharac) { + Jeu.obstacleTableCollisionCharac = obstacleTableCollisionCharac; + } + public static Character getGameCharacter() { + return gameCharacter; + } + + public static List<Monster> getMonsterList() { + return monsterList; + } + + } \ No newline at end of file diff --git a/MAVENProject/src/main/java/jeu/Painter.java b/MAVENProject/src/main/java/jeu/Painter.java index 8c8fe705124000e37c4a4b6a7d00312ab790644b..3d822af156533331024b271e4368c81ffa3caefd 100644 --- a/MAVENProject/src/main/java/jeu/Painter.java +++ b/MAVENProject/src/main/java/jeu/Painter.java @@ -1,6 +1,7 @@ package jeu; import engine.GamePainter; +import entity.Monster; import environnement.Coordonnees; import environnement.HitBox; import loaders.ObstacleLoader; @@ -34,24 +35,16 @@ public class Painter implements GamePainter{ Graphics2D crayon = (Graphics2D) im.getGraphics(); crayon.setColor(Color.blue); drawCharacter(crayon,gameCharacterLoader.getImageCharacter()); - /*drawSol(crayon); - if (!collision(gameCharacter.getHitBox(), test.getHitbox())){ - crayon.setColor(Color.blue); - } - else { - crayon.setColor(Color.red); - } - drawObstacle(crayon,test); + //drawQuadrillage(crayon);*/ drawMap(crayon); drawSol(crayon); drawObstacleBis(crayon,test,"plateforme.png"); drawLifePoints(crayon); + drawMonsters(crayon); } - - private void drawCharacter(Graphics2D crayon,Image imageCharac){ int x = (int) gameCharacter.getCoord().getX(); int y = (int) gameCharacter.getCoord().getZ(); @@ -64,11 +57,17 @@ public class Painter implements GamePainter{ String fn = "Frames_perso"; URL url = Jeu.class.getClassLoader().getResource(fn); assert url != null; + int placeX = -10; String beginPath = url.toString().substring(6)+"/"; Image filledHeart = new ImageIcon(beginPath+"character_health2.png").getImage(); Image emptyHeart = new ImageIcon(beginPath+"character_health1.png").getImage(); for (int i=1;i<=gameCharacter.getNbLifePoints();i++){ - crayon.drawImage(filledHeart, 45*i-10, 10, null, null); + placeX += 45; + crayon.drawImage(filledHeart, placeX, 10, null, null); + } + for (int i=1;i<=gameCharacter.getMaxLifePoints()-gameCharacter.getNbLifePoints();i++){ + placeX += 45; + crayon.drawImage(emptyHeart, placeX, 10, null, null); } } @@ -105,6 +104,7 @@ public class Painter implements GamePainter{ drawObstacle(crayon,obs); } } + private void drawObstacleBis(Graphics2D crayon, Obstacle obstacle, String nom){ ObstacleLoader loader = new ObstacleLoader(obstacle,nom); HitBox hitBox = loader.getObstacle().getHitbox(); @@ -113,6 +113,19 @@ public class Painter implements GamePainter{ crayon.drawImage(loader.getImageObstacle(), x - (int) hitBox.getWidth()/2,z - (int) hitBox.getHeight()/2,null,null); } + private void drawMonsters(Graphics2D crayon){ + int x; + int z; + crayon.setColor(Color.blue); + for (Monster monster : getMonsterList()){ + crayon.setColor(Color.blue); + x = (int)monster.getCoord().getX(); + z = (int)monster.getCoord().getZ(); + if (HitBox.collision(gameCharacter.getHitBox(),monster.getHitBox()))crayon.setColor(Color.red); + crayon.fillRect(x-(int)monster.getEntityWIDTH()/2, z-(int)monster.getEntityHEIGHT()/2, (int)monster.getEntityWIDTH(), (int)monster.getEntityHEIGHT()); + } + } + @Override public int getWidth() { return WIDTH;