From 1466ba041abf5d2841e44dbb1a08f0afdbf5fe4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Thu, 9 Jun 2022 21:07:13 +0200 Subject: [PATCH] detect enemy --- resources/map/Maps.tiled-session | 12 ++++++------ src/Entity/Player.java | 30 +++++++++++++++++++++++------- src/Main/PokeMudry.java | 10 +--------- src/Main/Settings.java | 4 ++-- 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/resources/map/Maps.tiled-session b/resources/map/Maps.tiled-session index 16e8d38..e5b606d 100644 --- a/resources/map/Maps.tiled-session +++ b/resources/map/Maps.tiled-session @@ -197,8 +197,8 @@ "scale": 0.75, "selectedLayer": 1, "viewCenter": { - "x": 640.6666666666667, - "y": 640.6666666666666 + "x": 640, + "y": 640 } }, "desert.tmx#Desert": { @@ -212,8 +212,8 @@ "scale": 1.5, "selectedLayer": 1, "viewCenter": { - "x": 160.33333333333331, - "y": 160.33333333333331 + "x": 159.33333333333331, + "y": 160.66666666666663 } }, "test.tmx#test": { @@ -227,8 +227,8 @@ "scale": 1, "selectedLayer": 4, "viewCenter": { - "x": 320.5, - "y": 159.5 + "x": 351, + "y": 196 } }, "test_couloir.tmx#Dungeon_Tileset": { diff --git a/src/Entity/Player.java b/src/Entity/Player.java index 06c6d45..542bf8d 100644 --- a/src/Entity/Player.java +++ b/src/Entity/Player.java @@ -7,6 +7,7 @@ import com.badlogic.gdx.maps.tiled.TiledMapTile; import com.badlogic.gdx.math.Vector2; import Control.Controller; +import Main.PokeMudry; import Screen.ScreenMap; public class Player extends Character{ @@ -29,25 +30,34 @@ public class Player extends Character{ // Compute direction and next cell Vector nextCell = new Vector<>(); Player.Direction goalDirection = Player.Direction.NULL; + Vector2 nextPos = position; if (c.keyStatus.get(Input.Keys.RIGHT)) { goalDirection = Player.Direction.RIGHT; nextCell = sm.getTile(getPosition(), 1, 0); + nextPos.x+=sm.tileWidth; } else if (c.keyStatus.get(Input.Keys.LEFT)) { goalDirection = Player.Direction.LEFT; nextCell = sm.getTile(getPosition(), -1, 0); + nextPos.x-=sm.tileWidth; } else if (c.keyStatus.get(Input.Keys.UP)) { goalDirection = Player.Direction.UP; nextCell = sm.getTile(getPosition(), 0, 1); + nextPos.y+=sm.tileHeight; } else if (c.keyStatus.get(Input.Keys.DOWN)) { goalDirection = Player.Direction.DOWN; nextCell = sm.getTile(getPosition(), 0, -1); + nextPos.y-=sm.tileHeight; } // Is the move valid ? if (sm.isWalkable(nextCell)) { - setSpeed(sm.getSpeed(nextCell)); - go(goalDirection); + if (enemy(sm, nextPos)) { + System.out.println("It's a enemy !!"); + } else { + setSpeed(sm.getSpeed(nextCell)); + go(goalDirection); + } } else { // Face the wall turn(goalDirection); @@ -72,11 +82,17 @@ public class Player extends Character{ } } - private boolean enemy() { - //Vector enemies = PokeMudry.getEnemies; - //for (Enemy enemy : enemies) { - - //} + private boolean enemy(ScreenMap sm, Vector2 nextPos) { + Vector enemies = PokeMudry.getEnemies(); + for (Enemy enemy : enemies) { + boolean bMap = sm.map.equals(enemy.getMap()); + int pX = (int) nextPos.x/sm.tileWidth; + int pY = (int) nextPos.y/sm.tileHeight; + int eX = (int) enemy.position.x/sm.tileWidth; + int eY = (int) enemy.position.y/sm.tileHeight; + //System.out.println("Player: " + pX + " x " + pY + " - Enemy: " + eX + " x " + eY); + if(bMap && pX==eX && pY==eY) return true; + } return false; } diff --git a/src/Main/PokeMudry.java b/src/Main/PokeMudry.java index ae3b330..790e0df 100644 --- a/src/Main/PokeMudry.java +++ b/src/Main/PokeMudry.java @@ -13,17 +13,9 @@ import ch.hevs.gdx2d.desktop.PortableApplication; import ch.hevs.gdx2d.lib.GdxGraphics; public class PokeMudry extends PortableApplication { - - public final boolean ANDROID = false; - public final int PLAYERS = 1; - public static final int TIME = 10; // number of minutes for kill all enemy - - public static final int HEIGHT = 800; - public static final int width = 800; private ScreenPlayer sp; private Controller controller; - //private Player p1; private static Vector enemies = new Vector<>(); private static Vector entities = new Vector<>(); @@ -48,7 +40,7 @@ public class PokeMudry extends PortableApplication { controller.init(); entities.add((Entity) sp.p); enemies.add(new Enemy("Mudry", 10, 15, "lumberjack_sheet32", "desert")); - enemies.add(new Enemy("Pignat", 12, 15, "lumberjack_sheet32", "desert")); + enemies.add(new Enemy("Pignat", 5, 1, "lumberjack_sheet32", "test")); for (Enemy enemy : enemies) { entities.add(enemy); diff --git a/src/Main/Settings.java b/src/Main/Settings.java index cd99584..fe96e1e 100644 --- a/src/Main/Settings.java +++ b/src/Main/Settings.java @@ -2,8 +2,8 @@ package Main; public class Settings { - public final boolean ANDROID = false; - public final int PLAYERS = 1; + public static final boolean ANDROID = false; + public static final int PLAYERS = 1; public static final int TIME = 10; // number of minutes for kill all enemy public static final int SIDE = 800;