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 5b490adc8bf54a93797c501e974081b25cbc9b7b..513c8c606c6b4883bc6cb67f99b187c09332bf27 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Controller/GameSceneController.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Controller/GameSceneController.java @@ -1,10 +1,111 @@ package de.hdm_stuttgart.battlearena.Controller; +import de.hdm_stuttgart.battlearena.Model.Entity.EntityClass; +import de.hdm_stuttgart.battlearena.Model.Entity.EntityFactory; +import de.hdm_stuttgart.battlearena.Model.Entity.EntityType; +import de.hdm_stuttgart.battlearena.Model.Entity.IEntity; +import de.hdm_stuttgart.battlearena.Model.Inputs.InputHandler; +import de.hdm_stuttgart.battlearena.Model.Map.TileManager; + +import javafx.animation.AnimationTimer; +import javafx.fxml.FXML; +import javafx.fxml.Initializable; +import javafx.scene.canvas.Canvas; +import javafx.scene.canvas.GraphicsContext; + import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; -public class GameSceneController { +import java.net.URL; +import java.util.ResourceBundle; + +public class GameSceneController implements Initializable { private static final Logger log = LogManager.getLogger(GameSceneController.class); -} + @FXML + private Canvas canvas2D; + + private GraphicsContext graphicsContext2D; + + InputHandler inputHandler = InputHandler.getInstance(); + + IEntity player; + IEntity enemy; + + EntityClass playerClass = EntityClass.HUMAN; + EntityClass enemyClass = EntityClass.HUMAN; + + TileManager tileManager; + + //map data + String mapString = "4 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 " + + "4 1 2 1 1 1 1 2 2 2 1 1 1 1 1 1 1 3 " + + "4 2 1 1 1 1 1 2 2 2 1 1 1 1 2 1 1 3 " + + "4 1 1 1 2 2 4 3 3 1 4 3 3 3 3 2 2 3 " + + "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 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 " + + "4 1 1 1 1 2 4 3 3 1 3 3 3 3 3 2 2 3 " + + "4 1 1 1 1 1 4 3 3 1 3 3 3 3 3 1 2 3 " + + "4 1 2 1 1 1 1 2 2 2 1 1 1 1 1 1 1 3 " + + "4 1 1 2 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 2 3 " + + "4 1 1 1 2 2 4 3 3 1 3 3 3 3 3 2 2 3 " + + "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 scaledTileSize = 48; + + + @Override + public void initialize(URL url, ResourceBundle resourceBundle) { + graphicsContext2D = canvas2D.getGraphicsContext2D(); + graphicsContext2D.setImageSmoothing(false); + + player = EntityFactory.createEntity(EntityType.PLAYER, graphicsContext2D, inputHandler, playerClass, this); + enemy = EntityFactory.createEntity(EntityType.ENEMY_PLAYER, graphicsContext2D, inputHandler, enemyClass, this); + + tileManager = new TileManager(graphicsContext2D, diffTileCount, horizontalTileCount, verticalTileCount, mapString); + + AnimationTimer gameLoop = new AnimationTimer() { + @Override + public void handle(long l) { + graphicsContext2D.clearRect(0, 0, canvas2D.getWidth(), canvas2D.getHeight()); + renderContent(graphicsContext2D); + updateContent(); + } + }; + gameLoop.start(); + log.debug("Game loop started"); + } + + private void updateContent() { + player.updateEntityMovement(this); + enemy.updateEntityMovement(this); + player.attack(enemy, graphicsContext2D); + } + + private void renderContent(GraphicsContext graphicsContext) { + tileManager.renderMap(); + player.renderEntity(graphicsContext); + enemy.renderEntity(graphicsContext); + } + + public IEntity getEnemy() { + return enemy; + } + + public TileManager getTileManager() { + return tileManager; + } + + public int getScaledTileSize() { + return scaledTileSize; + } +} \ No newline at end of file diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Main/Main.java b/src/main/java/de/hdm_stuttgart/battlearena/Main/Main.java index 36394526c54f30d8ec087eef528385257ee9c5a0..d222017ae825e1a18482ab3913991df51d2df7bf 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Main/Main.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Main/Main.java @@ -1,5 +1,6 @@ package de.hdm_stuttgart.battlearena.Main; +import de.hdm_stuttgart.battlearena.Model.Inputs.InputHandler; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.geometry.Rectangle2D; @@ -20,6 +21,8 @@ public class Main extends Application { private static final Logger log = LogManager.getLogger(Main.class); Rectangle2D screen = Screen.getPrimary().getVisualBounds(); + InputHandler inputHandler = InputHandler.getInstance(); + public static void main(String[] args) { launch(args); } @@ -33,6 +36,9 @@ public class Main extends Application { Scene scene = new Scene(root); + scene.setOnKeyPressed(inputHandler::handleKeyPress); + scene.setOnKeyReleased(inputHandler::handleKeyRelease); + stage.setTitle("BattleArena"); stage.setScene(scene); stage.minHeightProperty().setValue(400); diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/CollisionHandler.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/CollisionHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..9d860c1af101e6002e51c846e70d219cee7772e1 --- /dev/null +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/CollisionHandler.java @@ -0,0 +1,69 @@ +package de.hdm_stuttgart.battlearena.Model.Entity; + +import de.hdm_stuttgart.battlearena.Controller.GameSceneController; +import de.hdm_stuttgart.battlearena.Model.Map.ITile; + +import javafx.geometry.BoundingBox; + +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.LogManager; + +public class CollisionHandler { + + private static final Logger log = LogManager.getLogger(CollisionHandler.class); + + public CollisionHandler() { + log.debug("CollisionHandler initialized"); + } + + public boolean handleBoxCollision(BoundingBox colliderA, BoundingBox colliderB) { + return colliderA.getMaxX() > colliderB.getMinX() && + colliderA.getMinX() < colliderB.getMaxX() && + colliderA.getMaxY() > colliderB.getMinY() && + colliderA.getMinY() < colliderB.getMaxY(); + } + + public boolean handleMapCollision(IEntity entity, int scaledTileSize, GameSceneController gameSceneController) { + int playerLeftX = (int) (entity.getBoxCollider().getMinX()); + int playerRightX = (int) (entity.getBoxCollider().getMinX() + entity.getBoxCollider().getWidth()); + int playerTopY = (int) (entity.getBoxCollider().getMinY()); + int playerBottomY = (int) (entity.getBoxCollider().getMinY() + entity.getBoxCollider().getHeight()); + + int leftColumn = playerLeftX / scaledTileSize; + int rightColumn = playerRightX / scaledTileSize; + int topRow = playerTopY / scaledTileSize; + int bottomRow = playerBottomY / scaledTileSize; + + int nextPlayerTop = (playerTopY - entity.getEntitySpeed()) / scaledTileSize; + int nextPlayerBottom = (playerBottomY + entity.getEntitySpeed()) / scaledTileSize; + int nextPlayerLeft = (playerLeftX - entity.getEntitySpeed()) / scaledTileSize; + int nextPlayerRight = (playerRightX + entity.getEntitySpeed()) / scaledTileSize; + + int[][] tileMap = gameSceneController.getTileManager().getTileMap(); + ITile[] tileSet = gameSceneController.getTileManager().getTileSet(); + int tileOne; + int tileTwo; + + switch (entity.getEntityDirection()) { + case UP: + tileOne = tileMap[nextPlayerTop][leftColumn]; + tileTwo = tileMap[nextPlayerTop][rightColumn]; + return tileSet[tileOne].getCollision() || tileSet[tileTwo].getCollision(); + case DOWN: + tileOne = tileMap[nextPlayerBottom][leftColumn]; + tileTwo = tileMap[nextPlayerBottom][rightColumn]; + return tileSet[tileOne].getCollision() || tileSet[tileTwo].getCollision(); + case LEFT: + tileOne = tileMap[topRow][nextPlayerLeft]; + tileTwo = tileMap[bottomRow][nextPlayerLeft]; + return tileSet[tileOne].getCollision() || tileSet[tileTwo].getCollision(); + case RIGHT: + tileOne = tileMap[topRow][nextPlayerRight]; + tileTwo = tileMap[bottomRow][nextPlayerRight]; + return tileSet[tileOne].getCollision() || tileSet[tileTwo].getCollision(); + default: + return true; + } + } + +} \ No newline at end of file diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/EnemyHandler.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/EnemyHandler.java index ea46bb6f0ff8235e038455fde192db494e789f94..c8fc714cc5aa2eefe30e616b5211fe76e6457ff2 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/EnemyHandler.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/EnemyHandler.java @@ -1,10 +1,86 @@ package de.hdm_stuttgart.battlearena.Model.Entity; +import de.hdm_stuttgart.battlearena.Controller.GameSceneController; + +import javafx.geometry.BoundingBox; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.paint.Color; + import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; -public class EnemyHandler { +class EnemyHandler implements IEntity{ private static final Logger log = LogManager.getLogger(EnemyHandler.class); -} + private final BoundingBox boxCollider = new BoundingBox(300, 200, 48, 48); + + private EntityDirection entityDirection = EntityDirection.DOWN; + + private int entitySpeed = 3; + + private int health = 10; + + @Override + public void initializeEntity() { + + } + + @Override + public void loadEntitySprites() { + + } + + @Override + public void updateEntityMovement(GameSceneController gameScene) { + + } + + @Override + public void attack(IEntity entity, GraphicsContext graphicsContext) { + + } + + @Override + public void updateEntityWalkAnimation() { + + } + + @Override + public void renderEntity(GraphicsContext graphicsContext) { + graphicsContext.setFill(Color.BLACK); + graphicsContext.fillRect(300, 200, 48, 48); + } + + @Override + public BoundingBox getBoxCollider() { + return boxCollider; + } + + @Override + public EntityDirection getEntityDirection() { + return entityDirection; + } + + @Override + public int getEntitySpeed() { + return 3; + } + + @Override + public int gotHit(int damageDone) { + log.debug(health); + return health -= damageDone; + } + + @Override + public int getMapPosX() { + return 0; + } + + @Override + public int getMapPosY() { + return 0; + } + +} \ No newline at end of file diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/EntityClass.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/EntityClass.java new file mode 100644 index 0000000000000000000000000000000000000000..03b800acd74ad72318dee1df4dbf6528c53bf6d6 --- /dev/null +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/EntityClass.java @@ -0,0 +1,8 @@ +package de.hdm_stuttgart.battlearena.Model.Entity; + +public enum EntityClass { + HUMAN, + HIGH_BORN, + LOW_BORN, + SENTINELS +} \ No newline at end of file diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/EntityDirection.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/EntityDirection.java new file mode 100644 index 0000000000000000000000000000000000000000..c86d3e0e25d51cf090cede9ef3a3167745be7d0a --- /dev/null +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/EntityDirection.java @@ -0,0 +1,8 @@ +package de.hdm_stuttgart.battlearena.Model.Entity; + +public enum EntityDirection { + UP, + DOWN, + LEFT, + RIGHT +} diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/EntityFactory.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/EntityFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..d5b4b382213c197e503a598e855940aa18821998 --- /dev/null +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/EntityFactory.java @@ -0,0 +1,27 @@ +package de.hdm_stuttgart.battlearena.Model.Entity; + +import de.hdm_stuttgart.battlearena.Controller.GameSceneController; +import de.hdm_stuttgart.battlearena.Model.Inputs.InputHandler; + +import javafx.scene.canvas.GraphicsContext; + +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.LogManager; + +public class EntityFactory { + + private static final Logger log = LogManager.getLogger(EntityFactory.class); + + public static IEntity createEntity(EntityType entityType, GraphicsContext gameScene, InputHandler inputHandler, EntityClass entityClass, GameSceneController gameSceneController) { + if (entityType == EntityType.PLAYER) { + log.debug("Entity " + entityType + " created"); + return new Player(gameScene, inputHandler, entityClass, gameSceneController); + } else if (entityType == EntityType.ENEMY_PLAYER) { + log.debug("Entity " + entityType + " created"); + return new EnemyHandler(); + } + + throw new IllegalArgumentException ("Entity type not supported " + entityType); + } + +} \ No newline at end of file diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/EntityType.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/EntityType.java new file mode 100644 index 0000000000000000000000000000000000000000..3e8560772b2e9f049e217bdeca99c30c24150e11 --- /dev/null +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Entity/EntityType.java @@ -0,0 +1,6 @@ +package de.hdm_stuttgart.battlearena.Model.Entity; + +public enum EntityType { + PLAYER, + ENEMY_PLAYER +} \ No newline at end of file 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 fd7bbc62dab3dc4952f7a50958d25bb42cf95b5a..92ca5b53906697c1bad96c2cbb59524b7bc2c40b 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 @@ -1,4 +1,33 @@ package de.hdm_stuttgart.battlearena.Model.Entity; +import de.hdm_stuttgart.battlearena.Controller.GameSceneController; +import javafx.geometry.BoundingBox; +import javafx.scene.canvas.GraphicsContext; + public interface IEntity { -} + + void initializeEntity(); + + void loadEntitySprites(); + + void updateEntityMovement(GameSceneController gameScene); + + void attack(IEntity entity, GraphicsContext graphicsContext); + + void updateEntityWalkAnimation(); + + void renderEntity(GraphicsContext graphicsContext); + + BoundingBox getBoxCollider(); + + EntityDirection getEntityDirection(); + + int getEntitySpeed(); + + int gotHit(int damageDone); + + int getMapPosX(); + + int getMapPosY(); + +} \ No newline at end of file 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 0384286b6f19eb3b2cd302cd23687b741eef2722..fffccf712e14e8a655bbc72622fae7e4c0d5d3b8 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 @@ -1,10 +1,347 @@ package de.hdm_stuttgart.battlearena.Model.Entity; +import de.hdm_stuttgart.battlearena.Controller.GameSceneController; +import de.hdm_stuttgart.battlearena.Model.Inputs.InputHandler; + +import javafx.geometry.BoundingBox; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.image.Image; + import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; -public class Player implements IEntity { +import java.util.Objects; + +class Player implements IEntity { private static final Logger log = LogManager.getLogger(Player.class); -} + InputHandler inputHandler; + CollisionHandler collisionHandler = new CollisionHandler(); + + GraphicsContext gameScene; + + GameSceneController gameSceneController; + + private BoundingBox boxCollider; + + private Image directionDownOne, + directionDownTwo, + directionUpOne, + directionUpTwo, + directionLeftOne, + directionLeftTwo, + directionRightOne, + directionRightTwo; + + private Image swordDown, + swordUp, + swordLeft, + swordRight; + + private final EntityClass entityClass; + + private int spriteCounter = 0; + private int spriteNumber = 1; + + private final int playerWidth = 19; + private final int playerHeight = 35; + + private int scaledTileSize; + + private int mapPosX; + private int mapPosY; + private int playerSpeed; + private EntityDirection playerDirection; + + private int health = 10; + private int damage = 1; + + public Player(GraphicsContext gameScene, InputHandler inputHandler, EntityClass entityClass, GameSceneController gameSceneController) { + this.gameScene = gameScene; + this.inputHandler = inputHandler; + this.entityClass = entityClass; + this.gameSceneController = gameSceneController; + + initializeEntity(); + loadEntitySprites(); + } + + @Override + public void initializeEntity() { + scaledTileSize = gameSceneController.getScaledTileSize(); + mapPosX = 60; + mapPosY = 60; + boxCollider = new BoundingBox(mapPosX+15,mapPosY+10, playerWidth, playerHeight); + playerSpeed = 5; + playerDirection = EntityDirection.DOWN; + } + + @Override + public void loadEntitySprites() { + try { + if (entityClass == EntityClass.HUMAN) { + loadHumanSprites(); + loadWeaponSprites(); + } else if (entityClass == EntityClass.HIGH_BORN) { + loadHighBornSprites(); + loadWeaponSprites(); + } else if (entityClass == EntityClass.LOW_BORN) { + loadLowBornSprites(); + loadWeaponSprites(); + } else { + loadSentinelsSprites(); + loadWeaponSprites(); + } + } catch (Exception e) { + log.error(e); + } + } + + private void loadHumanSprites() { + directionDownOne = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/HumanDown01.png"))); + directionDownTwo = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/HumanDown02.png"))); + directionUpOne = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/upOne.png"))); + directionUpTwo = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/upTwo.png"))); + directionLeftOne = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/leftOne.png"))); + directionLeftTwo = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/leftTwo.png"))); + directionRightOne = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/rightOne.png"))); + directionRightTwo = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/rightTwo.png"))); + } + + private void loadHighBornSprites() { + directionDownOne = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/HumanDown01.png"))); + directionDownTwo = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/HumanDown02.png"))); + directionUpOne = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/upOne.png"))); + directionUpTwo = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/upTwo.png"))); + directionLeftOne = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/leftOne.png"))); + directionLeftTwo = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/leftTwo.png"))); + directionRightOne = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/rightOne.png"))); + directionRightTwo = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/rightTwo.png"))); + } + + private void loadLowBornSprites() { + directionDownOne = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/HumanDown01.png"))); + directionDownTwo = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/HumanDown02.png"))); + directionUpOne = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/upOne.png"))); + directionUpTwo = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/upTwo.png"))); + directionLeftOne = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/leftOne.png"))); + directionLeftTwo = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/leftTwo.png"))); + directionRightOne = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/rightOne.png"))); + directionRightTwo = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/rightTwo.png"))); + } + + private void loadSentinelsSprites() { + directionDownOne = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/HumanDown01.png"))); + directionDownTwo = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/HumanDown02.png"))); + directionUpOne = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/upOne.png"))); + directionUpTwo = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/upTwo.png"))); + directionLeftOne = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/leftOne.png"))); + directionLeftTwo = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/leftTwo.png"))); + directionRightOne = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/rightOne.png"))); + directionRightTwo = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/player/rightTwo.png"))); + } + + private void loadWeaponSprites() { + swordUp = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/weapons/SwordUp.png"))); + swordDown = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/weapons/SwordDown.png"))); + swordLeft = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/weapons/SwordLeft.png"))); + swordRight = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/weapons/SwordRight.png"))); + } + + @Override + public void updateEntityMovement(GameSceneController gameScene) { + if (inputHandler.isMoveUp() || inputHandler.isMoveDown() || inputHandler.isMoveLeft() || inputHandler.isMoveRight()) { + if (inputHandler.isMoveUp()) { + playerDirection = EntityDirection.UP; + } else if (inputHandler.isMoveDown()) { + playerDirection = EntityDirection.DOWN; + } else if (inputHandler.isMoveLeft()) { + playerDirection = EntityDirection.LEFT; + } else { + playerDirection = EntityDirection.RIGHT; + } + + boolean isWalkableTile = collisionHandler.handleMapCollision(this, scaledTileSize, gameSceneController); + + int lastMapPosX = mapPosX; + int lastMapPosY = mapPosY; + + if (isWalkableTile) { + switch (playerDirection) { + case UP: + mapPosY -= playerSpeed; + break; + case DOWN: + mapPosY += playerSpeed; + break; + case LEFT: + mapPosX -= playerSpeed; + break; + case RIGHT: + mapPosX += playerSpeed; + } + } + + boolean isEntityCollision = boxCollider.intersects(gameScene.getEnemy().getBoxCollider()); + //boolean isEntityCollision = collisionHandler.handleBoxCollision(boxCollider, gameScene.getEnemy().getBoxCollider()); + + if (isEntityCollision) { + if (playerDirection == EntityDirection.UP) { + mapPosY = lastMapPosY + 30; + } else if (playerDirection == EntityDirection.DOWN) { + mapPosY = lastMapPosY - 30; + } else if (playerDirection == EntityDirection.LEFT) { + mapPosX = lastMapPosX + 30; + } else { + mapPosX = lastMapPosX - 30; + } + } + + boxCollider = new BoundingBox(mapPosX+15,mapPosY+10, playerWidth, playerHeight); + + updateEntityWalkAnimation(); + + } + } + + @Override + public void attack(IEntity entity, GraphicsContext graphicsContext) { + BoundingBox hitBox; + + //Added and subtracted numbers from variables are the pixel insets of the player sprite + + int attackRange = 30; + int attackWidth = 10; + + if (inputHandler.isAttack()){ + if (playerDirection == EntityDirection.UP) { + hitBox = new BoundingBox(mapPosX+playerWidth, mapPosY-10, attackWidth, attackRange); + graphicsContext.strokeRect(mapPosX+playerWidth, mapPosY-10, attackWidth, attackRange); + graphicsContext.drawImage(swordUp, mapPosX+8, mapPosY-10, 32, 32); + if (hitBox.intersects(entity.getBoxCollider())) { + entity.gotHit(damage); + graphicsContext.strokeText("Hit", 10, 10); + } + } else if (playerDirection == EntityDirection.DOWN) { + hitBox = new BoundingBox(mapPosX+playerWidth, mapPosY+playerHeight, attackWidth, attackRange); + graphicsContext.strokeRect(mapPosX+playerWidth, mapPosY+playerHeight, attackWidth, attackRange); + graphicsContext.drawImage(swordDown, mapPosX+8, mapPosY+playerHeight, 32, 32); + if (hitBox.intersects(entity.getBoxCollider())) { + entity.gotHit(damage); + graphicsContext.strokeText("Hit", 10, 10); + } + } else if (playerDirection == EntityDirection.LEFT) { + hitBox = new BoundingBox(mapPosX-attackWidth, mapPosY+((double) playerHeight /2), + attackRange, attackWidth); + graphicsContext.strokeRect(mapPosX-attackWidth, mapPosY+((double) playerHeight /2), + attackRange, attackWidth); + graphicsContext.drawImage(swordLeft, mapPosX-8, mapPosY+8, 32, 32); + if (hitBox.intersects(entity.getBoxCollider())) { + entity.gotHit(damage); + graphicsContext.strokeText("Hit", 10, 10); + } + } else { + hitBox = new BoundingBox(mapPosX+playerWidth+attackWidth, mapPosY+((double) playerHeight /2), + attackRange, attackWidth); + graphicsContext.strokeRect(mapPosX+playerWidth+attackWidth, mapPosY+((double) playerHeight /2), + attackRange, attackWidth); + graphicsContext.drawImage(swordRight, mapPosX+playerWidth+8, mapPosY+8, 32, 32); + if (hitBox.intersects(entity.getBoxCollider())) { + entity.gotHit(damage); + graphicsContext.strokeText("Hit", 10, 10); + } + } + } + } + + @Override + public void updateEntityWalkAnimation() { + spriteCounter++; + + if (spriteCounter > 10) { + if (spriteNumber == 1) { + spriteNumber = 2; + } else if (spriteNumber == 2) { + spriteNumber = 1; + } + spriteCounter = 0; + } + } + + @Override + public void renderEntity(GraphicsContext graphicsContext) { + Image playerSprite = null; + + //for debugging collision + graphicsContext.strokeRect(mapPosX+15,mapPosY+10, playerWidth, playerHeight); + + switch (playerDirection) { + case UP: + if (spriteNumber == 1) { + playerSprite = directionUpOne; + } + if (spriteNumber == 2) { + playerSprite = directionUpTwo; + } + break; + case DOWN: + if (spriteNumber == 1) { + playerSprite = directionDownOne; + } + if (spriteNumber == 2) { + playerSprite = directionDownTwo; + } + break; + case LEFT: + if (spriteNumber == 1) { + playerSprite = directionLeftOne; + } + if (spriteNumber == 2) { + playerSprite = directionLeftTwo; + } + break; + case RIGHT: + if (spriteNumber == 1) { + playerSprite = directionRightOne; + } + if (spriteNumber == 2) { + playerSprite = directionRightTwo; + } + break; + } + + graphicsContext.drawImage(playerSprite, mapPosX, mapPosY, 48, 48); + } + + @Override + public BoundingBox getBoxCollider() { + return boxCollider; + } + + @Override + public EntityDirection getEntityDirection() { + return playerDirection; + } + + @Override + public int getEntitySpeed() { + return playerSpeed; + } + + @Override + public int gotHit(int damageDone) { + return health -= damageDone; + } + + @Override + public int getMapPosX() { + return mapPosX; + } + + @Override + public int getMapPosY() { + return mapPosY; + } + +} \ No newline at end of file diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Inputs/InputHandler.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Inputs/InputHandler.java index 5772a4785203b4a51e58738825772f37c11816b3..8dfd2f3d3d51063e409488e71a25814030ed2f68 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Inputs/InputHandler.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Inputs/InputHandler.java @@ -1,5 +1,8 @@ package de.hdm_stuttgart.battlearena.Model.Inputs; +import javafx.scene.input.KeyCode; +import javafx.scene.input.KeyEvent; + import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; @@ -7,4 +10,75 @@ public class InputHandler { private static final Logger log = LogManager.getLogger(InputHandler.class); -} + private static final InputHandler inputHandler = new InputHandler(); + + private InputHandler(){} + + public static InputHandler getInstance() { + return inputHandler; + } + + private boolean moveUp, moveDown, moveLeft, moveRight, attack; + + public void handleKeyPress(KeyEvent event) { + KeyCode code = event.getCode(); + + switch (code) { + case W: + moveUp = true; + break; + case S: + moveDown = true; + break; + case A: + moveLeft = true; + break; + case D: + moveRight = true; + break; + case SPACE: + attack = true; + } + } + + public void handleKeyRelease(KeyEvent event) { + KeyCode code = event.getCode(); + + switch (code) { + case W: + moveUp = false; + break; + case S: + moveDown = false; + break; + case A: + moveLeft = false; + break; + case D: + moveRight = false; + break; + case SPACE: + attack = false; + } + } + + public boolean isMoveUp() { + return moveUp; + } + + public boolean isMoveDown() { + return moveDown; + } + + public boolean isMoveLeft() { + return moveLeft; + } + + public boolean isMoveRight() { + return moveRight; + } + + public boolean isAttack() { + return attack; + } +} \ No newline at end of file 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 0a702580fe7a00095752ce2f6141f25caa8a0344..c7fd6bc3ad273c2e2667a9398a33c94764947ea7 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 @@ -1,10 +1,31 @@ package de.hdm_stuttgart.battlearena.Model.Map; +import javafx.scene.image.Image; + import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; -public class BackgroundTile implements ITile{ +class BackgroundTile implements ITile{ private static final Logger log = LogManager.getLogger(BackgroundTile.class); -} + private final Image tileSprite; + private final boolean isWalkable; + + public BackgroundTile(Image tileSprite, boolean isWalkable) { + this.tileSprite = tileSprite; + this.isWalkable = isWalkable; + } + + @Override + public boolean getCollision() { + log.debug("Collision type: " + isWalkable + " returned."); + return isWalkable; + } + + @Override + public Image getTileSprite() { + log.debug("Image returned"); + return tileSprite; + } +} \ No newline at end of file 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 4364ed78f3face5dcca4f183e62c11b13e43a9fd..f33750bed5ede99e059da2a7fb4e31f33bb735ad 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 @@ -1,4 +1,8 @@ package de.hdm_stuttgart.battlearena.Model.Map; +import javafx.scene.image.Image; + public interface ITile { -} + boolean getCollision(); + Image getTileSprite(); +} \ No newline at end of file diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/TileFactorie.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/TileFactorie.java deleted file mode 100644 index 8409dac4ecfd80fb60f0d9fad2337efbfad775af..0000000000000000000000000000000000000000 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/TileFactorie.java +++ /dev/null @@ -1,10 +0,0 @@ -package de.hdm_stuttgart.battlearena.Model.Map; - -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.LogManager; - -public class TileFactorie { - - private static final Logger log = LogManager.getLogger(TileFactorie.class); - -} 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 new file mode 100644 index 0000000000000000000000000000000000000000..842781646eb4c59e1ba537d172414c7437b498b2 --- /dev/null +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/TileFactory.java @@ -0,0 +1,25 @@ +package de.hdm_stuttgart.battlearena.Model.Map; + +import javafx.scene.image.Image; + +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.LogManager; + +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); + } + + log.error("TileType: " + tileType + " not supported!"); + throw new IllegalArgumentException("TileType: " + tileType + " not supported!"); + } + +} \ No newline at end of file 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 c57a773e4dec2aeb5b0b7da0adfeedb0b6d537e2..480478d51823c0a4992de930e4eea5f2b73f82d8 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 @@ -1,10 +1,91 @@ package de.hdm_stuttgart.battlearena.Model.Map; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.image.Image; + import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import java.util.Objects; + public class TileManager { private static final Logger log = LogManager.getLogger(TileManager.class); -} + private final GraphicsContext graphicsContext2D; + private final ITile[] tileSet; + private final int[][] tileMap; + + private final int horizontalTileCount; + private final int verticalTileCount; + + public TileManager(GraphicsContext graphicsContext2D, int diffTileCount, + int horizontalTileCount, int verticalTileCount, String mapString) { + this.graphicsContext2D = graphicsContext2D; + this.horizontalTileCount = horizontalTileCount; + this.verticalTileCount = verticalTileCount; + + tileSet = new BackgroundTile[diffTileCount]; + tileMap = new int[horizontalTileCount][verticalTileCount]; + + createTiles(); + generateMap(mapString, horizontalTileCount, verticalTileCount); + } + + private void createTiles() { + try { + tileSet[0] = TileFactory.createTile(TileType.WALKABLE, + new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/map/Grass01.png")))); + tileSet[1] = TileFactory.createTile(TileType.WALKABLE, + new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/map/Grass02.png")))); + tileSet[2] = TileFactory.createTile(TileType.WALKABLE, + new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/map/Grass04.png")))); + tileSet[3] = TileFactory.createTile(TileType.NON_WALKABLE, + new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/map/Stone01.png")))); + tileSet[4] = TileFactory.createTile(TileType.NON_WALKABLE, + new Image(Objects.requireNonNull(getClass().getResourceAsStream("/textures/map/Stone02.png")))); + } catch (Exception e) { + log.error(e); + } + } + + private void generateMap(String mapString, int horizontalTileCount, int verticalTileCount) { + String[] tileSet = mapString.split(" "); + + int stringIndex = 0; + + for (int column = 0; column < horizontalTileCount; column++) { + for (int row = 0; row < verticalTileCount; row++) { + tileMap[column][row] = Integer.parseInt(tileSet[stringIndex]); + stringIndex++; + } + } + } + + public void renderMap() { + int currentTile; + int scaledTileSize = 48; //todo: later replace "48" with value from gameScene. + int tilePosX; + int tilePosY; + + for (int mapColumn = 0; mapColumn < horizontalTileCount; mapColumn++) { + for (int mapRow = 0; mapRow < verticalTileCount; mapRow++) { + currentTile = tileMap[mapRow][mapColumn]; + + tilePosX = mapColumn * scaledTileSize; + tilePosY = mapRow * scaledTileSize; + + graphicsContext2D.drawImage(tileSet[currentTile].getTileSprite(), tilePosX, tilePosY, + scaledTileSize, scaledTileSize); + } + } + } + + public int[][] getTileMap() { + return tileMap; + } + + public ITile[] getTileSet() { + return tileSet; + } +} \ No newline at end of file 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 new file mode 100644 index 0000000000000000000000000000000000000000..17e95c7e93fc890a5f993cc53b7eb6cfd1f4bd6b --- /dev/null +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Map/TileType.java @@ -0,0 +1,6 @@ +package de.hdm_stuttgart.battlearena.Model.Map; + +public enum TileType { + WALKABLE, + NON_WALKABLE +} \ No newline at end of file diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Client.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Client.java new file mode 100644 index 0000000000000000000000000000000000000000..87189a54b8a83db73c96f74eee8e46702322b5f4 --- /dev/null +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Client.java @@ -0,0 +1,51 @@ +package de.hdm_stuttgart.battlearena.Model.Multiplayer; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.net.*; +import java.io.*; +import java.util.Arrays; + +public class Client { + private Socket clientSocket; + private PrintWriter out; + private BufferedReader in; + + + private static final Logger log = LogManager.getLogger(Client.class); + + public void startConnection(String ip, int port) throws IOException { + clientSocket = new Socket(ip, port); + out = new PrintWriter(clientSocket.getOutputStream(), true); + in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + } + + public int[] sendcords(int[] cords) throws IOException { + String message = String.join(",", Arrays.stream(cords).mapToObj(String::valueOf).toArray(String[]::new)); + out.println(message); + log.info("Sent coordinates: " + message); + String resp = in.readLine(); + return convertStringToArray(resp); + } + + public static int[] convertStringToArray(String inputString) { + // Remove brackets and split by comma + String[] parts = inputString.substring(1, inputString.length() - 1).split(","); + + // Convert each part to integer + int[] result = new int[parts.length]; + for (int i = 0; i < parts.length; i++) { + result[i] = Integer.parseInt(parts[i].trim()); + } + + return result; + } + + + public void stopConnection() throws IOException { + in.close(); + out.close(); + clientSocket.close(); + } +} diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/ConnectionHandler.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/ConnectionHandler.java index 95967c90b0b30ad5c9832df6cb003e38f1463a3f..67529ff1e0e3eb4cc9f2b0f01ef637a2190415e7 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/ConnectionHandler.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/ConnectionHandler.java @@ -1,10 +1,88 @@ package de.hdm_stuttgart.battlearena.Model.Multiplayer; -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.IOException; +import java.util.Arrays; +import java.util.concurrent.TimeUnit; public class ConnectionHandler { + private static int pX = 0; + private static int pY = 0; + private int enemyX; + private int enemyY; + private static int playerID = 0; + private static int[] coordinates = {pX,pY,playerID}; + private static final Logger log = LogManager.getLogger(ConnectionHandler.class); + public static void main(String[] args) throws IOException, InterruptedException { + + Client client = new Client(); + client.startConnection("localhost", 4444); + while (true){ + int[] cords = client.sendcords(coordinates); + System.out.println("Enemy X: " + cords[0]); + System.out.println("Enemy Y: " + cords[1]); + System.out.println("Your PlayerID is " + cords[2]); + pX++; + pY++; + playerID = cords[2]; + //Assign the Values to the message to send: + coordinates[0] = pX; + coordinates[1] = pY; + coordinates[2] = playerID; + + TimeUnit.SECONDS.sleep(1); + } + + //stop the connection: + //client.stopConnection(); + } + + class ConnectionThread implements Runnable { + public void run() { + + try { + Thread.sleep(250); + } catch (InterruptedException e){ + log.error("Error in Thread.sleep()"); + } + } + } + + public int getpX() { + return pX; + } + + public void setpX(int pX) { + this.pX = pX; + } + + public int getpY() { + return pY; + } + + public void setpY(int pY) { + this.pY = pY; + } + + public int getEnemyX() { + return enemyX; + } + + public void setEnemyX(int enemyX) { + this.enemyX = enemyX; + } + + public int getEnemyY() { + return enemyY; + } + + public void setEnemyY(int enemyY) { + this.enemyY = enemyY; + } } + diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/GameServer.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/GameServer.java deleted file mode 100644 index 6a4e74cfa64ea68224b76119f3943de4ee62457d..0000000000000000000000000000000000000000 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/GameServer.java +++ /dev/null @@ -1,10 +0,0 @@ -package de.hdm_stuttgart.battlearena.Model.Multiplayer; - -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.LogManager; - -public class GameServer { - - private static final Logger log = LogManager.getLogger(GameServer.class); - -} diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Server.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Server.java new file mode 100644 index 0000000000000000000000000000000000000000..e9226a37ba2b57a3d708a2f022e243b8f66d68da --- /dev/null +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Server.java @@ -0,0 +1,148 @@ +package de.hdm_stuttgart.battlearena.Model.Multiplayer; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.net.*; +import java.io.*; +import java.util.Arrays; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +public class Server { + + private static final Logger log = LogManager.getLogger(Server.class); + private ServerSocket serverSocket; + private static boolean isServerRunning = true; + private static int px; + private static int py; + + private static int enemyx = 0; + private static int enemyy = 0; + private static int playerID; + private static int playercount = 0; + private static int[] cords; + + private static int[] returncords = {enemyx,enemyy,playerID}; + + public static void main(String[] args) throws IOException { + Server server = new Server(); + log.info("server starting..."); + server.start("localhost",4444); + } + + public void start(String localhost, int port) throws IOException { + serverSocket = new ServerSocket(port); + log.info("server started!"); + while (true){ + new ServerHandler(serverSocket.accept()).start(); + } + } + + private static class ServerHandler extends Thread { + private final Socket clientSocket; + private PrintWriter out; + private BufferedReader in; + + public ServerHandler(Socket socket) { + this.clientSocket = socket; + } + + public void run() { + ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); + + executorService.scheduleAtFixedRate(() -> { + try { + BufferedReader localIn = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + PrintWriter localOut = new PrintWriter(clientSocket.getOutputStream(), true); + + String inputLine = localIn.readLine(); + + if (inputLine != null) { + // Assuming that the input line is a comma-separated list of integers + String[] coordinates = inputLine.split(","); + cords = new int[coordinates.length]; + for (int i = 0; i < coordinates.length; i++) { + cords[i] = Integer.parseInt(coordinates[i]); + } + + //first connect of a client: + if(playercount == 0){ + //first player to connect + playercount++; + returncords[2] = 1; + + } else if(playercount == 1){ + //second player to connect + if(cords[2] == 1){ + returncords[2] = 1; + } else { + playercount++; + returncords[2] = 2; + } + + } else if(playercount == 2){ + + //check which client has connected + if (cords[2] == 1) { //player + px = cords[0]; + py = cords[1]; + + //set the cords to return: + returncords[0] = enemyx; + returncords[1] = enemyy; + + //set playerID: + returncords[2] = 1; + + } else if(cords[2] == 2) { //enemy + enemyx = cords[0]; + enemyy = cords[1]; + + //set the cords to return: + returncords[0] = px; + returncords[1] = py; + + //set playerID: + returncords[2] = 2; + + } + } + + localOut.println(Arrays.toString(returncords)); + localOut.flush(); + } + + log.info("Player X / Y : " + px + " / " + py + " Enemy X / Y : " + enemyx + " / " + enemyy); + + + //check if server was shut down: + if (!isServerRunning) { + executorService.shutdown(); + try { + if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) { + executorService.shutdownNow(); + } + } catch (InterruptedException e) { + log.error("Error waiting for executor service termination: " + e.getMessage()); + } + } + + } catch (IOException e) { + throw new RuntimeException(e); + } + }, 0, 250, TimeUnit.MILLISECONDS); + } + } + + public void stopServer() { + isServerRunning = false; + try { + serverSocket.close(); // Close the server socket to break out of accept() in the main thread + } catch (IOException e) { + log.error("Error closing server socket: " + e.getMessage()); + } + } + +} diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index c37776eeb147449e3fa1dbb6aaf650b8c5f12637..13a3d1d34ef25e809dd1dd1467218739070f8357 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -4,7 +4,12 @@ module gui { requires javafx.media; requires org.apache.logging.log4j; + opens de.hdm_stuttgart.battlearena to javafx.fxml; + opens de.hdm_stuttgart.battlearena.Controller to javafx.fxml; exports de.hdm_stuttgart.battlearena.Main; exports de.hdm_stuttgart.battlearena.Controller; -} + exports de.hdm_stuttgart.battlearena.Model.Entity; + exports de.hdm_stuttgart.battlearena.Model.Inputs; + exports de.hdm_stuttgart.battlearena.Model.Map; +} \ No newline at end of file diff --git a/src/main/resources/fxml/GameScene.fxml b/src/main/resources/fxml/GameScene.fxml index b8a97acf0ac20383de9eb2086ae9b0c3e6e370a3..f046db58280a8ea339f035b1879ac8e6e73b8115 100644 --- a/src/main/resources/fxml/GameScene.fxml +++ b/src/main/resources/fxml/GameScene.fxml @@ -1,8 +1,15 @@ <?xml version="1.0" encoding="UTF-8"?> +<?import javafx.scene.canvas.Canvas?> <?import javafx.scene.layout.AnchorPane?> - - -<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.hdm_stuttgart.battlearena.Controller.GameSceneController"> - +<?import javafx.scene.layout.StackPane?> + +<AnchorPane prefHeight="864.0" prefWidth="864.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.hdm_stuttgart.battlearena.Controller.GameSceneController"> + <children> + <StackPane prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> + <children> + <Canvas fx:id="canvas2D" height="864.0" width="864.0" /> + </children> + </StackPane> + </children> </AnchorPane> diff --git a/src/main/resources/textures/map/Grass01.png b/src/main/resources/textures/map/Grass01.png new file mode 100644 index 0000000000000000000000000000000000000000..e835d4676d2f80105626ece4d99b485a30b289fe Binary files /dev/null and b/src/main/resources/textures/map/Grass01.png differ diff --git a/src/main/resources/textures/map/Grass02.png b/src/main/resources/textures/map/Grass02.png new file mode 100644 index 0000000000000000000000000000000000000000..144fad81f75b681454b58768081394cb40aaeead Binary files /dev/null and b/src/main/resources/textures/map/Grass02.png differ diff --git a/src/main/resources/textures/map/Grass04.png b/src/main/resources/textures/map/Grass04.png new file mode 100644 index 0000000000000000000000000000000000000000..4ce012b5eac6c41488efe96c5f66eb43c28c98ce Binary files /dev/null and b/src/main/resources/textures/map/Grass04.png differ diff --git a/src/main/resources/textures/map/Stone01.png b/src/main/resources/textures/map/Stone01.png new file mode 100644 index 0000000000000000000000000000000000000000..b6cf13f66633060803264739b889f64c32877a0e Binary files /dev/null and b/src/main/resources/textures/map/Stone01.png differ diff --git a/src/main/resources/textures/map/Stone02.png b/src/main/resources/textures/map/Stone02.png new file mode 100644 index 0000000000000000000000000000000000000000..2ac41bee77062960aa2d8cfc921f66e45de629ba Binary files /dev/null and b/src/main/resources/textures/map/Stone02.png differ diff --git a/src/main/resources/textures/map/mapPlaceholder.png b/src/main/resources/textures/map/mapPlaceholder.png deleted file mode 100644 index ca3f707dff2cf59fbcf284b896a786fafa5e9180..0000000000000000000000000000000000000000 Binary files a/src/main/resources/textures/map/mapPlaceholder.png and /dev/null differ diff --git a/src/main/resources/textures/player/HumanDown01.png b/src/main/resources/textures/player/HumanDown01.png new file mode 100644 index 0000000000000000000000000000000000000000..ee8c9024a240bfec9f28cdecffff3060d3eb769e Binary files /dev/null and b/src/main/resources/textures/player/HumanDown01.png differ diff --git a/src/main/resources/textures/player/HumanDown02.png b/src/main/resources/textures/player/HumanDown02.png new file mode 100644 index 0000000000000000000000000000000000000000..3a96754826bed31e6baad1d519a71c2e97680821 Binary files /dev/null and b/src/main/resources/textures/player/HumanDown02.png differ diff --git a/src/main/resources/textures/player/downOne.png b/src/main/resources/textures/player/downOne.png new file mode 100644 index 0000000000000000000000000000000000000000..d1e261cc01b3a32bf937536716162ec5c61d0d50 Binary files /dev/null and b/src/main/resources/textures/player/downOne.png differ diff --git a/src/main/resources/textures/player/downTwo.png b/src/main/resources/textures/player/downTwo.png new file mode 100644 index 0000000000000000000000000000000000000000..9a14543c8d06be035ea8048428faeaa218faec32 Binary files /dev/null and b/src/main/resources/textures/player/downTwo.png differ diff --git a/src/main/resources/textures/player/leftOne.png b/src/main/resources/textures/player/leftOne.png new file mode 100644 index 0000000000000000000000000000000000000000..13cc7b495f2647d10917ebda3046cd8af32f9553 Binary files /dev/null and b/src/main/resources/textures/player/leftOne.png differ diff --git a/src/main/resources/textures/player/leftTwo.png b/src/main/resources/textures/player/leftTwo.png new file mode 100644 index 0000000000000000000000000000000000000000..cbed81ae3a90ef128cb1bd913329c211c69030bd Binary files /dev/null and b/src/main/resources/textures/player/leftTwo.png differ diff --git a/src/main/resources/textures/player/rightOne.png b/src/main/resources/textures/player/rightOne.png new file mode 100644 index 0000000000000000000000000000000000000000..5d26f27d4b44906c9c17e0625329e56407c86d8d Binary files /dev/null and b/src/main/resources/textures/player/rightOne.png differ diff --git a/src/main/resources/textures/player/rightTwo.png b/src/main/resources/textures/player/rightTwo.png new file mode 100644 index 0000000000000000000000000000000000000000..7cf375e6ef6a51ed56f485a03bb5988e21a7ed37 Binary files /dev/null and b/src/main/resources/textures/player/rightTwo.png differ diff --git a/src/main/resources/textures/player/upOne.png b/src/main/resources/textures/player/upOne.png new file mode 100644 index 0000000000000000000000000000000000000000..0e3134b32646f8587e59c68a0c62a9c8697876e1 Binary files /dev/null and b/src/main/resources/textures/player/upOne.png differ diff --git a/src/main/resources/textures/player/upTwo.png b/src/main/resources/textures/player/upTwo.png new file mode 100644 index 0000000000000000000000000000000000000000..2f698853e288792ce6879baa96d7999552fd17fe Binary files /dev/null and b/src/main/resources/textures/player/upTwo.png differ diff --git a/src/main/resources/textures/weapons/SwordDown.png b/src/main/resources/textures/weapons/SwordDown.png new file mode 100644 index 0000000000000000000000000000000000000000..dba0a85781e2e79ac38fc7004e892f7db82e04b5 Binary files /dev/null and b/src/main/resources/textures/weapons/SwordDown.png differ diff --git a/src/main/resources/textures/weapons/SwordLeft.png b/src/main/resources/textures/weapons/SwordLeft.png new file mode 100644 index 0000000000000000000000000000000000000000..f83bcf7fa199d1e4fd97352a9688a78b6a3df757 Binary files /dev/null and b/src/main/resources/textures/weapons/SwordLeft.png differ diff --git a/src/main/resources/textures/weapons/SwordRight.png b/src/main/resources/textures/weapons/SwordRight.png new file mode 100644 index 0000000000000000000000000000000000000000..35bf135d41218bbef6c6409ab4bc99efad373754 Binary files /dev/null and b/src/main/resources/textures/weapons/SwordRight.png differ diff --git a/src/main/resources/textures/weapons/SwordSwingDown.png b/src/main/resources/textures/weapons/SwordSwingDown.png new file mode 100644 index 0000000000000000000000000000000000000000..3d160e5d8d518ca696860cdc77a90b11d2e7aec8 Binary files /dev/null and b/src/main/resources/textures/weapons/SwordSwingDown.png differ diff --git a/src/main/resources/textures/weapons/SwordSwingLeft.png b/src/main/resources/textures/weapons/SwordSwingLeft.png new file mode 100644 index 0000000000000000000000000000000000000000..75dd39ee517e0714684c979e9e5052096fda3eaf Binary files /dev/null and b/src/main/resources/textures/weapons/SwordSwingLeft.png differ diff --git a/src/main/resources/textures/weapons/SwordSwingRight.png b/src/main/resources/textures/weapons/SwordSwingRight.png new file mode 100644 index 0000000000000000000000000000000000000000..a78877e88fdb0b55ffa4b5bb77f036f00f8793c1 Binary files /dev/null and b/src/main/resources/textures/weapons/SwordSwingRight.png differ diff --git a/src/main/resources/textures/weapons/SwordSwingUp.png b/src/main/resources/textures/weapons/SwordSwingUp.png new file mode 100644 index 0000000000000000000000000000000000000000..62ece9f708265ca4b3a9238c8437999825070f71 Binary files /dev/null and b/src/main/resources/textures/weapons/SwordSwingUp.png differ diff --git a/src/main/resources/textures/weapons/SwordUp.png b/src/main/resources/textures/weapons/SwordUp.png new file mode 100644 index 0000000000000000000000000000000000000000..3c6ba9f3c3170669b0e24a4bcda8ebad952f42ca Binary files /dev/null and b/src/main/resources/textures/weapons/SwordUp.png differ