Skip to content
Snippets Groups Projects
  • Schlütter Yannik's avatar
    36bf0c38
    TileManager.java: Changed: tileMap to public static, Added: new Tile and 1 new... · 36bf0c38
    Schlütter Yannik authored
    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
    36bf0c38
    History
    TileManager.java: Changed: tileMap to public static, Added: new Tile and 1 new...
    Schlütter Yannik authored
    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
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
GameSceneController.java 4.95 KiB
package de.hdm_stuttgart.battlearena.Controller;

import de.hdm_stuttgart.battlearena.Controller.Enum.GameMode;
import de.hdm_stuttgart.battlearena.Controller.Enum.GameState;
import de.hdm_stuttgart.battlearena.Controller.Enum.PlayerMode;
import de.hdm_stuttgart.battlearena.Model.DataStorage.Classes.RuntimeInfo;
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;

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();

    RuntimeInfo runtimeInfo = RuntimeInfo.getInstance();

    GameMode gameMode = runtimeInfo.getGameMode();

    IEntity player;
    IEntity enemy;

    EntityClass playerOneClass = runtimeInfo.getPlayerOneClass();
    EntityClass playerTwoClass = runtimeInfo.getPlayerTwoClass();

    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 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 " +
                        "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 = 6;
    int scaledTileSize = 48;


    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        graphicsContext2D = canvas2D.getGraphicsContext2D();
        graphicsContext2D.setImageSmoothing(false);

        player = EntityFactory.createEntity(EntityType.PLAYER, graphicsContext2D, inputHandler,
                playerOneClass, this, PlayerMode.PLAYER_ONE);
        if (gameMode == GameMode.NETWORK) {
            enemy = EntityFactory.createEntity(EntityType.NETWORK_PLAYER_TWO, graphicsContext2D, inputHandler,
                    playerTwoClass, this, PlayerMode.PLAYER_TWO);
        } else {
            enemy = EntityFactory.createEntity(EntityType.PLAYER, graphicsContext2D, inputHandler,
                    playerTwoClass, this, PlayerMode.PLAYER_TWO);
        }

        tileManager = new TileManager(graphicsContext2D, diffTileCount, horizontalTileCount, verticalTileCount, mapString);

        runtimeInfo.setGameState(GameState.PLAYING);

        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);
        enemy.attack(player, graphicsContext2D);
    }

    private void renderContent(GraphicsContext graphicsContext) {
        tileManager.renderMap();
        player.renderEntity(graphicsContext);
        enemy.renderEntity(graphicsContext);
        player.checkHealTile(player, graphicsContext2D);
        enemy.checkHealTile(enemy, graphicsContext2D);
    }

    public IEntity getEnemy() {
        return enemy;
    }

    public IEntity getPlayer() {
        return player;
    }

    public TileManager getTileManager() {
        return tileManager;
    }

    public int getScaledTileSize() {
        return scaledTileSize;
    }
}