Skip to content

Commit

Permalink
#29 détection de colision entre joueur et IAs
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidForlen committed Feb 7, 2021
1 parent 46daec3 commit 986d1a9
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 24 deletions.
28 changes: 28 additions & 0 deletions src/games/paperDino/Cell.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.ArrayList;
import java.util.List;

import games.paperDino.entities.StationaryEntity;
import games.paperDino.entities.dynamic.Dino;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.state.StateBasedGame;
Expand Down Expand Up @@ -51,8 +53,34 @@ public boolean isWalkable(){
return true;
}

/**
* @return S'il est possible de marcher sur la cellule. Il suffit d'une piece non walkable pour rendre une cellule non walkable.
*/
public boolean isWalkable(Dino dino){
for (Piece piece : this.pieces) {
if (!piece.isWalkable()){
if(piece.getEntity() instanceof StationaryEntity || piece.getEntity().getClass().equals(dino.getClass())){ // Si l'obstacle est statique ou de même type que le dino voulant faire le déplacement, on ne peut pas marcher dessus
return false;
}
}
}
return true;
}

public List<Piece> getPieces() {
return this.pieces;
}

/**
* @return le premier Dino trouvé sur cette Cell
*/
public Dino whoIsOnThisCell(){
for (Piece piece : this.pieces) {
if (piece.getEntity() instanceof Dino){
return (Dino) piece.getEntity();
}
}
return null;
}

}
21 changes: 20 additions & 1 deletion src/games/paperDino/Grid.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package games.paperDino;

import games.paperDino.entities.dynamic.Dino;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.state.StateBasedGame;
Expand Down Expand Up @@ -77,9 +78,27 @@ public int getWidth(){
public boolean canMoveToCell(int[] position){
int i = position[0];
int j = position[1];
if(i < 0 || i >= this.getHeight() || j < 0 || j >= this.getWidth() || !cells[i][j].isWalkable()){
if(!cellExists(position) || !cells[i][j].isWalkable()){
return false;
}
return true;
}


/**
* @param position
* @return si la cellule existe (si la position n'est pas hors de Grid)
*/
public boolean cellExists(int [] position){
int i = position[0];
int j = position[1];
if(i < 0 || i >= this.getHeight() || j < 0 || j >= this.getWidth()){
return false;
}
return true;
}

public Cell getCell(int[] position){
return cells[position[0]][position[1]];
}
}
3 changes: 3 additions & 0 deletions src/games/paperDino/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,7 @@ public Player getPlayer() {
return this.player;
}

public void killPlayer(){
System.out.println("Collision avec le joueur ! Day over");
}
}
23 changes: 0 additions & 23 deletions src/games/paperDino/entities/DynamicEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,9 @@ public void update(GameContainer container, StateBasedGame game, int delta) {
}
}

/**
* Déplace l'entité vers une case en fonction du vecteur direction
* @param direction vecteur direction (i,j)
* return time before next move (in miliseconds)
*/
public int move(int[] direction) {


int[] newPosition = this.getPosition();
newPosition[0] += direction[0];
newPosition[1] += direction[1];

Grid grid = this.getWorld().getGrid();

if(!grid.canMoveToCell(newPosition)){
return 0; // Le mouvement est impossible et est donc annulé
}

this.setPosition(newPosition);
return this.defaultCountdown; //TODO : ajouter cooldown avant prochaine action
}

abstract public int checkInput(GameContainer container, int delta);

public int getDefaultCountdown() {
return defaultCountdown;
}

}
25 changes: 25 additions & 0 deletions src/games/paperDino/entities/dynamic/Dino.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package games.paperDino.entities.dynamic;

import games.paperDino.Grid;
import org.newdawn.slick.Image;

import games.paperDino.Piece;
Expand All @@ -12,4 +13,28 @@ public Dino(World world, Image sprite, int[] position) {
super(world, sprite, position);
}

public int move(int[] direction) {

int[] newPosition = this.getPosition();
newPosition[0] += direction[0];
newPosition[1] += direction[1];

Grid grid = this.getWorld().getGrid();

Dino dinoOnNewPositionCell = null;

if (grid.cellExists(newPosition)) {
dinoOnNewPositionCell = grid.getCell(newPosition).whoIsOnThisCell();
if (dinoOnNewPositionCell != null && !dinoOnNewPositionCell.getClass().equals(this.getClass())) { // S'il y a un dino sur la cellule newPosition et qu'il est de type différent cela signifie qu'un IA entre en colision avec le joueur
this.getWorld().killPlayer();
}
}

if (!grid.canMoveToCell(newPosition)) {
return 0; // Le mouvement est impossible et est donc annulé
}

this.setPosition(newPosition);
return this.getDefaultCountdown(); //TODO : ajouter cooldown avant prochaine action
}
}

0 comments on commit 986d1a9

Please sign in to comment.