From 36bf0c3834273331e3c7d0755b81cb33ebb94130 Mon Sep 17 00:00:00 2001 From: yschl <ys037@hdm-stuttgart.de> Date: Mon, 15 Jan 2024 18:33:29 +0100 Subject: [PATCH] TileManager.java: Changed: tileMap to public static, Added: new Tile and 1 new TileType from TileFactory.java TileFactory.java: Added: new Tiletype "destructible" TileType.java: Added 2 new enumTypes GameSceneController.java: Changed: diffTileCount to 6, Added checkHealTile in renderContent Method Player.java: Added TileManager import, checkHealTile Method, Added new Code in attack Method to make a heart spawn when attacking an enemy, started using healPlayer Method NetworkPlayerTwo.java: Added checkHealTile Method and healPlayerMethod ITile.java: Added getDestruction Method IEntity.java: Added checkHealTile and healPlayer BackgroundTile.java: Added Methods for destruction type --- .../Controller/GameSceneController.java | 6 ++- .../battlearena/Model/Entity/IEntity.java | 6 ++- .../Model/Entity/NetworkPlayerTwo.java | 11 ++++ .../battlearena/Model/Entity/Player.java | 51 ++++++++++++++++-- .../battlearena/Model/Map/BackgroundTile.java | 10 +++- .../battlearena/Model/Map/ITile.java | 1 + .../battlearena/Model/Map/TileFactory.java | 20 ++++--- .../battlearena/Model/Map/TileManager.java | 14 ++--- .../battlearena/Model/Map/TileType.java | 5 +- .../resources/textures/map/finalheart.png | Bin 0 -> 403 bytes 10 files changed, 102 insertions(+), 22 deletions(-) create mode 100644 src/main/resources/textures/map/finalheart.png diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Controller/GameSceneController.java b/src/main/java/de/hdm_stuttgart/battlearena/Controller/GameSceneController.java index 24da67aa..dc9b88e8 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Controller/GameSceneController.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Controller/GameSceneController.java @@ -54,7 +54,7 @@ public class GameSceneController implements Initializable { "4 1 1 1 2 2 4 1 1 1 1 1 1 1 3 1 2 3 " + "4 1 1 1 2 2 4 1 3 3 3 1 1 1 3 2 2 3 " + "4 1 1 1 2 2 4 1 1 1 1 1 1 1 3 2 1 3 " + - "4 1 1 1 2 2 4 3 3 1 4 3 3 3 3 2 1 3 " + + "4 1 5 1 2 2 4 3 3 1 4 3 3 3 3 2 1 3 " + "4 1 1 1 1 1 1 2 2 2 1 1 1 1 1 1 1 3 " + "4 1 1 1 2 2 4 3 3 1 3 3 3 3 3 2 1 3 " + "4 1 1 1 1 2 4 3 3 1 3 3 3 3 3 1 1 3 " + @@ -67,7 +67,7 @@ public class GameSceneController implements Initializable { "4 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3"; int horizontalTileCount = 18; int verticalTileCount = 18; - int diffTileCount = 5; + int diffTileCount = 6; int scaledTileSize = 48; @@ -113,6 +113,8 @@ public class GameSceneController implements Initializable { tileManager.renderMap(); player.renderEntity(graphicsContext); enemy.renderEntity(graphicsContext); + player.checkHealTile(player, graphicsContext2D); + enemy.checkHealTile(enemy, graphicsContext2D); } public IEntity getEnemy() { diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/IEntity.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/IEntity.java index 81dc5653..f98ddeb7 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/IEntity.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/IEntity.java @@ -12,13 +12,17 @@ public interface IEntity { void updateEntityMovement(GameSceneController gameScene); + void checkHealTile(IEntity entity, GraphicsContext graphicsContext); + void attack(IEntity entity, GraphicsContext graphicsContext); void updateEntityWalkAnimation(); void renderEntity(GraphicsContext graphicsContext); - BoundingBox getBoxCollider(); + void healPlayer(int heal); + + BoundingBox getBoxCollider(); EntityDirection getEntityDirection(); diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/NetworkPlayerTwo.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/NetworkPlayerTwo.java index d42c173d..d045a108 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/NetworkPlayerTwo.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/NetworkPlayerTwo.java @@ -36,6 +36,11 @@ class NetworkPlayerTwo implements IEntity{ } + @Override + public void checkHealTile(IEntity entity, GraphicsContext graphicsContext) { + + } + @Override public void attack(IEntity entity, GraphicsContext graphicsContext) { @@ -52,6 +57,12 @@ class NetworkPlayerTwo implements IEntity{ graphicsContext.fillRect(300, 200, 48, 48); } + @Override + public void healPlayer(int heal) { + log.info("Network player health: " + health); + health -= heal; + } + @Override public BoundingBox getBoxCollider() { return boxCollider; diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/Player.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/Player.java index 44e99c33..0eaf4b2c 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/Player.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/Player.java @@ -4,6 +4,7 @@ import de.hdm_stuttgart.battlearena.Controller.Enum.PlayerMode; import de.hdm_stuttgart.battlearena.Controller.GameSceneController; import de.hdm_stuttgart.battlearena.Model.Inputs.InputHandler; +import de.hdm_stuttgart.battlearena.Model.Map.TileManager; import javafx.geometry.BoundingBox; import javafx.scene.canvas.GraphicsContext; import javafx.scene.image.Image; @@ -278,11 +279,31 @@ class Player implements IEntity { boxCollider = new BoundingBox(mapPosX + 15, mapPosY + 10, playerWidth, playerHeight); } + @Override + public void checkHealTile(IEntity entity, GraphicsContext graphicsContext){ + int xTile = entity.getMapPosX() / gameSceneController.getScaledTileSize(); + int yTile = entity.getMapPosY() / gameSceneController.getScaledTileSize(); + if(TileManager.tileMap[yTile][xTile +1 ] == 5){ + entity.healPlayer(1); + TileManager.tileMap[yTile][xTile +1] = 2; + log.info("Healed +1: " + health); + } + } @Override public void attack(IEntity entity, GraphicsContext graphicsContext) { BoundingBox hitBox; + int xTile = mapPosX / gameSceneController.getScaledTileSize(); + int yTile = mapPosY / gameSceneController.getScaledTileSize(); + //log.info(xTile); + //log.info(yTile); + //int xTile = (int) (Math.random() * (18 - 1)); //Get xTile Coordinate +1 /-1 of playerTile + //int yTile = (int) (Math.random() * (18 - 1)); //Get yTile Coordinate +1 /-1 of playerTile + double dropChance = 0.5; + double randomDropChance = Math.random() * 1; + //log.info(randomDropChance); + //Added and subtracted numbers from variables are the pixel insets of the player sprite int attackRange = 30; @@ -297,6 +318,12 @@ class Player implements IEntity { if (hitBox.intersects(entity.getBoxCollider())) { entity.gotHit(damage); graphicsContext.strokeText("Hit", 10, 10); + + if (((TileManager.tileMap[xTile][yTile] == 3) && randomDropChance > dropChance)|| + ((TileManager.tileMap[xTile][yTile] == 1) && randomDropChance > dropChance)) + TileManager.tileMap[yTile-1][xTile+1] = 5; + //TileManager.tileMap[yTile-2][xTile] = 5; + //TileManager.tileMap[yTile-3][xTile] = 5; } } else if (playerDirection == EntityDirection.DOWN) { hitBox = new BoundingBox(mapPosX + playerWidth, mapPosY + playerHeight, attackWidth, attackRange); @@ -305,6 +332,12 @@ class Player implements IEntity { if (hitBox.intersects(entity.getBoxCollider())) { entity.gotHit(damage); graphicsContext.strokeText("Hit", 10, 10); + + if (((TileManager.tileMap[xTile][yTile] == 3) && randomDropChance > dropChance)|| + ((TileManager.tileMap[xTile][yTile] == 1) && randomDropChance > dropChance)) + TileManager.tileMap[yTile-1][xTile+1] = 5; + //TileManager.tileMap[yTile-2][xTile] = 5; + //TileManager.tileMap[yTile-3][xTile] = 5; } } else if (playerDirection == EntityDirection.LEFT) { hitBox = new BoundingBox(mapPosX - attackWidth, mapPosY + ((double) playerHeight / 2), @@ -315,6 +348,12 @@ class Player implements IEntity { if (hitBox.intersects(entity.getBoxCollider())) { entity.gotHit(damage); graphicsContext.strokeText("Hit", 10, 10); + + if (((TileManager.tileMap[xTile][yTile] == 3) && randomDropChance > dropChance)|| + ((TileManager.tileMap[xTile][yTile] == 1) && randomDropChance > dropChance)) + TileManager.tileMap[yTile-1][xTile+1] = 5; + //TileManager.tileMap[yTile-2][xTile] = 5; + //TileManager.tileMap[yTile-3][xTile] = 5; } } else { hitBox = new BoundingBox(mapPosX + playerWidth + attackWidth, mapPosY + ((double) playerHeight / 2), @@ -325,6 +364,12 @@ class Player implements IEntity { if (hitBox.intersects(entity.getBoxCollider())) { entity.gotHit(damage); graphicsContext.strokeText("Hit", 10, 10); + + if (((TileManager.tileMap[xTile][yTile] == 3) && randomDropChance > dropChance)|| + ((TileManager.tileMap[xTile][yTile] == 1) && randomDropChance > dropChance)) + TileManager.tileMap[yTile-1][xTile+1] = 5; + //TileManager.tileMap[yTile-2][xTile] = 5; + //TileManager.tileMap[yTile-3][xTile] = 5; } } } @@ -393,8 +438,8 @@ class Player implements IEntity { public void gotHit(int damageDone) { health -= damageDone; } - - private void healPlayer(int healthRegenerated) { + @Override + public void healPlayer(int healthRegenerated) { int regeneratedHealth = health + healthRegenerated; if (regeneratedHealth < maxPlayerHealth) { @@ -402,7 +447,7 @@ class Player implements IEntity { } else { health = maxPlayerHealth; } - + log.info(health); } @Override diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/BackgroundTile.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/BackgroundTile.java index c7fd6bc3..b17fb79d 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/BackgroundTile.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/BackgroundTile.java @@ -10,11 +10,12 @@ class BackgroundTile implements ITile{ private static final Logger log = LogManager.getLogger(BackgroundTile.class); private final Image tileSprite; - private final boolean isWalkable; + private final boolean isWalkable, isDestructible; - public BackgroundTile(Image tileSprite, boolean isWalkable) { + public BackgroundTile(Image tileSprite, boolean isWalkable, boolean isDestructible) { this.tileSprite = tileSprite; this.isWalkable = isWalkable; + this.isDestructible = isDestructible; } @Override @@ -22,6 +23,11 @@ class BackgroundTile implements ITile{ log.debug("Collision type: " + isWalkable + " returned."); return isWalkable; } + @Override + public boolean getDestruction() { + log.debug("Collision type: " + isDestructible + " returned."); + return isWalkable; + } @Override public Image getTileSprite() { diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/ITile.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/ITile.java index f33750be..58f71108 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/ITile.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/ITile.java @@ -4,5 +4,6 @@ import javafx.scene.image.Image; public interface ITile { boolean getCollision(); + boolean getDestruction(); Image getTileSprite(); } \ No newline at end of file diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/TileFactory.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/TileFactory.java index 84278164..0da1a155 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/TileFactory.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/TileFactory.java @@ -9,13 +9,19 @@ public class TileFactory { private static final Logger log = LogManager.getLogger(TileFactory.class); - public static ITile createTile(TileType tileType, Image tileSprite) { - if (tileType == TileType.WALKABLE) { - log.debug("Tile with type: " + tileType + " created."); - return new BackgroundTile(tileSprite, true); - } else if (tileType == TileType.NON_WALKABLE) { - log.debug("Tile with type: " + tileType + " created."); - return new BackgroundTile(tileSprite, false); + public static ITile createTile(TileType tileType, TileType tileType2, Image tileSprite) { + if (tileType == TileType.WALKABLE && tileType2 == TileType.DESTRUCTIBLE) { + log.info("Tile with type: " + tileType + " " + tileType2 + " created."); + return new BackgroundTile(tileSprite, true, true); + }else if (tileType == TileType.WALKABLE && tileType2 == TileType.NON_DESTRUCTIBLE) { + log.info("Tile with type: " + tileType + " " + tileType2 +" created."); + return new BackgroundTile(tileSprite, true, false); + }else if (tileType == TileType.NON_WALKABLE && tileType2 == TileType.DESTRUCTIBLE) { + log.info("Tile with type: " + tileType + " " + tileType2 +" created."); + return new BackgroundTile(tileSprite, false, true); + }else if (tileType == TileType.NON_WALKABLE && tileType2 == TileType.NON_DESTRUCTIBLE) { + log.info("Tile with type: " + tileType + " " + tileType2 +" created."); + return new BackgroundTile(tileSprite, false, false); } log.error("TileType: " + tileType + " not supported!"); diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/TileManager.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/TileManager.java index 480478d5..0ac687da 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/TileManager.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/TileManager.java @@ -14,7 +14,7 @@ public class TileManager { private final GraphicsContext graphicsContext2D; private final ITile[] tileSet; - private final int[][] tileMap; + public static int[][] tileMap; private final int horizontalTileCount; private final int verticalTileCount; @@ -34,16 +34,18 @@ public class TileManager { private void createTiles() { try { - tileSet[0] = TileFactory.createTile(TileType.WALKABLE, + tileSet[0] = TileFactory.createTile(TileType.WALKABLE, TileType.NON_DESTRUCTIBLE, new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/map/Grass01.png")))); - tileSet[1] = TileFactory.createTile(TileType.WALKABLE, + tileSet[1] = TileFactory.createTile(TileType.WALKABLE, TileType.NON_DESTRUCTIBLE, new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/map/Grass02.png")))); - tileSet[2] = TileFactory.createTile(TileType.WALKABLE, + tileSet[2] = TileFactory.createTile(TileType.WALKABLE, TileType.NON_DESTRUCTIBLE, new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/map/Grass04.png")))); - tileSet[3] = TileFactory.createTile(TileType.NON_WALKABLE, + tileSet[3] = TileFactory.createTile(TileType.NON_WALKABLE, TileType.DESTRUCTIBLE, new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/map/Stone01.png")))); - tileSet[4] = TileFactory.createTile(TileType.NON_WALKABLE, + tileSet[4] = TileFactory.createTile(TileType.NON_WALKABLE, TileType.DESTRUCTIBLE, new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/map/Stone02.png")))); + tileSet[5] = TileFactory.createTile(TileType.WALKABLE,TileType.NON_DESTRUCTIBLE, + new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/map/finalheart.png")))); } catch (Exception e) { log.error(e); } diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/TileType.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/TileType.java index 17e95c7e..efe35788 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/TileType.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/TileType.java @@ -2,5 +2,8 @@ package de.hdm_stuttgart.battlearena.Model.Map; public enum TileType { WALKABLE, - NON_WALKABLE + NON_WALKABLE, + DESTRUCTIBLE, + NON_DESTRUCTIBLE + } \ No newline at end of file diff --git a/src/main/resources/textures/map/finalheart.png b/src/main/resources/textures/map/finalheart.png new file mode 100644 index 0000000000000000000000000000000000000000..b861b145ceb949abdd7222a2679fe8e10756340f GIT binary patch literal 403 zcmV;E0c`$>P)<h;3K|Lk000e1NJLTq000mG000mO1^@s6AM^iV00001b5ch_0Itp) z=>Px$O-V#SR5(wKld(zzQ4mGvrO_pYVlT1r0~GTCeuZ{+YHQ^;Xp>5ibe4jDpkkwy zg1u=Jn^a<opjInqcJDITJi#H5xASK1+_^I^-8`I?pWjo%mKM4;`I-7ZrgKVW#4Im% ztt|b@<VyhH5C~@iA78($zgASyEd%7~t-5!B=ujo)fIg`i%0~fNy1G4e266(RR@#-- z8uaG?VlZGEI|IAB*mw8#-q408hql!#%##01%$$M5L9~}~#}1CqZRLGrAFJy#Bm@|p z4FFEG*xVgtla&NWY;i^d0c54GozIs?&4h?=0Jw*KzgIYmoT&vENXT@3U+qC$8om)R zTDyOAV`2`Vts)_!3k2HIC>EorxA528BZJYnQ;jjO<=jjmu5xbU^aiLJ7cVPf+%$o? x;`IjdPGG}#(?TwZ<Q%iPJ&<tihw~q8egTb@%0=hGHQ@jN002ovPDHLkV1mH%v5^1( literal 0 HcmV?d00001 -- GitLab