Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/main/java/dk/sdu/mmmi/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dk.sdu.mmmi.modulemon.BattleScene.BattleView;
import dk.sdu.mmmi.modulemon.BattleSimulation.BattleSimulation;
import dk.sdu.mmmi.modulemon.Collision.CollisionProcessing;
import dk.sdu.mmmi.modulemon.CustomBattleView.CustomBattleView;
import dk.sdu.mmmi.modulemon.Game;
import dk.sdu.mmmi.modulemon.Interaction.InteractProcessing;
import dk.sdu.mmmi.modulemon.MCTSBattleAI.MCTSBattleAIFactory;
Expand Down Expand Up @@ -30,9 +31,10 @@ public static void main(String[] args) throws IOException, URISyntaxException {
var settings = new Settings();
var monsterRegistry = new MonsterRegistry();
var battleMonsterProcessor = new BattleMonsterProcessor();
var battleAI = new MCTSBattleAIFactory();
//var battleAI = new dk.sdu.mmmi.modulemon.BattleAI.BattleAIFactory();
// var battleAI = new dk.sdu.mmmi.modulemon.SimpleAI.BattleAIFactory(); // Uncomment for Simple AI

var battleAI = new dk.sdu.mmmi.modulemon.BattleAI.BattleAIFactory();
var mctsBattleAI = new MCTSBattleAIFactory();
var simpleBattleAI = new dk.sdu.mmmi.modulemon.SimpleAI.BattleAIFactory(); // Uncomment for Simple AI

var battleSimulation = new BattleSimulation();
battleSimulation.setAIFactory(battleAI);
Expand All @@ -43,6 +45,16 @@ public static void main(String[] args) throws IOException, URISyntaxException {
battle.setSettingsService(settings);
battle.setMonsterRegistry(monsterRegistry);

var customBattle = new CustomBattleView();
customBattle.setSettings(settings);
customBattle.setBattleSimulation(battleSimulation);
customBattle.setBattleView(battle);
customBattle.setMonsterRegistry(monsterRegistry);
customBattle.addBattleAI(battleAI);
customBattle.addBattleAI(mctsBattleAI);
customBattle.addBattleAI(simpleBattleAI);


// Map stuff
var map = new MapView();
map.setSettingsService(settings);
Expand Down Expand Up @@ -77,6 +89,7 @@ public static void main(String[] args) throws IOException, URISyntaxException {
var game = new Game();
game.setSettingsService(settings);
game.addGameViewServiceList(battle);
game.addGameViewServiceList(customBattle);
game.addGameViewServiceList(map);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public void setSettingsService(IGameSettings settings) {
public void removeSettingsService(IGameSettings settings) {
this.settings = null;
}

@Override
public String toString() {
return "Minimax";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,7 @@ public void removeMonsterProcessor(IBattleMonsterProcessor monsterProcessor) {
}

public void setAIFactory(IBattleAIFactory factory) {
this.AIFactory = factory;
}

public void removeAIFactory(IBattleAIFactory factory) {
this.AIFactory = null;
this.AI = null;
this.AIFactory = factory;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,305 @@
package dk.sdu.mmmi.modulemon.CustomBattleView;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import dk.sdu.mmmi.modulemon.CommonMonster.IMonster;
import dk.sdu.mmmi.modulemon.Game;
import dk.sdu.mmmi.modulemon.common.AssetLoader;
import dk.sdu.mmmi.modulemon.common.data.GameData;
import dk.sdu.mmmi.modulemon.common.drawing.DrawingUtils;
import dk.sdu.mmmi.modulemon.common.drawing.Position;
import dk.sdu.mmmi.modulemon.common.drawing.Rectangle;
import dk.sdu.mmmi.modulemon.common.drawing.TextUtils;
import dk.sdu.mmmi.modulemon.common.services.IGameSettings;

public class CustomBattleScene {

// LibGDX Drawing stuff
private String teamAAIText = "";
private boolean showTeamAAIArrow = false;
private Color teamATextColor = Color.WHITE;
private boolean showTeamBAIArrow = false;
private String teamBAIText = "";
private Color teamBTextColor = Color.WHITE;
private Color startBattleColor = Color.WHITE;

private Position errorPosition = new Position(0,0);
private float errorSize = 0;
private float errorOpacity = 0;
private String errorText = "";
private ShapeRenderer shapeRenderer;
private SpriteBatch spriteBatch;

private boolean isTeamA = false;
private int changingMonsterIndex = -1;
private int selectedMonsterIndex = -1;

public static final Color SelectColor = Color.valueOf("2a75bb");

private int screenWidth = 1280;
private int screenHeight = 720;

private Rectangle teamAContainer;
private Rectangle teamBContainer;
private Rectangle[] teamAMonsterBoxes;
private Rectangle[] teamBMonsterBoxes;
private IMonster[] teamAMonsters;
private IMonster[] teamBMonsters;

public CustomBattleScene(IGameSettings settings) {
spriteBatch = new SpriteBatch();
shapeRenderer = new ShapeRenderer();

teamAContainer = DrawingUtils.createRectangle(Rectangle.class, 0, 0, 0, 0);
teamBContainer = DrawingUtils.createRectangle(Rectangle.class, 0, 0, 0, 0);
int teamSize = 6;
teamAMonsters = new IMonster[teamSize];
teamBMonsters = new IMonster[teamSize];
teamAMonsterBoxes = new Rectangle[teamSize];
teamBMonsterBoxes = new Rectangle[teamSize];

for (int i = 0; i < teamSize; i++) {
teamAMonsterBoxes[i] = DrawingUtils.createRectangle(settings, 0, 0, 0, 0);
teamBMonsterBoxes[i] = DrawingUtils.createRectangle(settings, 0, 0, 0, 0);
}
}

private static TextUtils text = TextUtils.getInstance();

public void draw(GameData gameData) {
float dt = gameData.getDelta();
screenWidth = gameData.getDisplayWidth();
screenHeight = gameData.getDisplayHeight();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
spriteBatch.setProjectionMatrix(Game.cam.combined);
spriteBatch.begin();

text.setCoordinateMode(TextUtils.CoordinateMode.CENTER);
final int textTopMargin = 60;
text.drawTitleFont(spriteBatch, "Custom Battle", Color.valueOf("ffcb05"), gameData.getDisplayWidth() / 2f, gameData.getDisplayHeight() - textTopMargin);
text.setCoordinateMode(TextUtils.CoordinateMode.TOP_LEFT); // Reset
spriteBatch.end();

//DRAW THE BOXES

if (teamBContainer.getWidth() <= 0) {
calculateBoxSizes(gameData);
}

spriteBatch.begin();
shapeRenderer.setAutoShapeType(true);
shapeRenderer.setColor(Color.WHITE);

Gdx.gl.glEnable(GL20.GL_BLEND); //Alows for opacity
if (gameData.getCamera() != null)
shapeRenderer.setProjectionMatrix(gameData.getCamera().combined);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);

teamAContainer.draw(shapeRenderer, dt);
teamBContainer.draw(shapeRenderer, dt);

for (int i = 0; i < teamAMonsterBoxes.length; i++) {
if(selectedMonsterIndex == i){
if(isTeamA){
teamAMonsterBoxes[i].setBorderColor(SelectColor);
teamAMonsterBoxes[i].setBorderWidth(4);
}else{
teamBMonsterBoxes[i].setBorderColor(SelectColor);
teamBMonsterBoxes[i].setBorderWidth(4);
}
}else{
teamAMonsterBoxes[i].setBorderColor(Color.BLACK);
teamAMonsterBoxes[i].setBorderWidth(2);
teamBMonsterBoxes[i].setBorderColor(Color.BLACK);
teamBMonsterBoxes[i].setBorderWidth(2);
}
teamAMonsterBoxes[i].draw(shapeRenderer, dt);
teamBMonsterBoxes[i].draw(shapeRenderer, dt);
}

spriteBatch.end();
shapeRenderer.end();

spriteBatch.begin();
final int gapAboveContainersText = 20;
text.setCoordinateMode(TextUtils.CoordinateMode.CENTER);
text.drawNormalBoldRoboto(spriteBatch, "Your Team", Color.WHITE, teamAContainer.getX() + teamAContainer.getWidth() / 2f, teamAContainer.getY() + teamAContainer.getHeight() + gapAboveContainersText);
text.drawNormalBoldRoboto(spriteBatch, "Opponent Team", Color.WHITE, teamBContainer.getX() + teamBContainer.getWidth() / 2f, teamBContainer.getY() + teamBContainer.getHeight() + gapAboveContainersText);

for (int i = 0; i < teamAMonsterBoxes.length; i++) {
drawMonsterWithName(teamAMonsters[i], teamAMonsterBoxes[i], changingMonsterIndex == i && isTeamA);
drawMonsterWithName(teamBMonsters[i], teamBMonsterBoxes[i], changingMonsterIndex == i && !isTeamA);
}


final float arrowSpacingFromEdge = 20;
final float controllerSelectorY = teamAContainer.getY() - gapAboveContainersText;

if(showTeamAAIArrow) {
text.drawNormalRoboto(spriteBatch, "<", Color.WHITE, teamAContainer.getX() + arrowSpacingFromEdge, controllerSelectorY);
text.drawNormalRoboto(spriteBatch, ">", Color.WHITE, teamAContainer.getX() - arrowSpacingFromEdge + teamAContainer.getWidth(), controllerSelectorY);
}
text.drawSmallRoboto(spriteBatch, teamAAIText, teamATextColor, teamAContainer.getX() + teamAContainer.getWidth() / 2f, controllerSelectorY);

if(showTeamBAIArrow) {
text.drawNormalRoboto(spriteBatch, "<", Color.WHITE, teamBContainer.getX() + arrowSpacingFromEdge, controllerSelectorY);
text.drawNormalRoboto(spriteBatch, ">", Color.WHITE, teamBContainer.getX() - arrowSpacingFromEdge + teamBContainer.getWidth(), controllerSelectorY);
}
text.drawSmallRoboto(spriteBatch, teamBAIText, teamBTextColor, teamBContainer.getX() + teamBContainer.getWidth() / 2f, controllerSelectorY);

text.drawBigBoldRoboto(spriteBatch, "START BATTLE!", startBattleColor, gameData.getDisplayWidth() / 2f, 50 );

text.drawBigBoldRoboto(spriteBatch, errorText, new Color(1,0,0, errorOpacity), errorPosition.getX(), errorPosition.getY() );
text.setCoordinateMode(TextUtils.CoordinateMode.TOP_LEFT);
spriteBatch.end();
}

private void drawMonsterWithName(IMonster monster, Rectangle rectangle, boolean drawMonsterSwitcher) {
final int monsterSpriteGap = 10;
final float monsterSpriteSize = rectangle.getWidth() - monsterSpriteGap * 2;

if (monster == null) {
text.drawSmallRoboto(spriteBatch, "No monster", Color.LIGHT_GRAY,
rectangle.getX() + rectangle.getWidth() / 2f,
rectangle.getY() + rectangle.getHeight() / 2f);
} else {
//Draw the monster
var sprite = AssetLoader.getInstance().getImageAsset(monster.getFrontSprite(), getClass());
sprite.setPosition(rectangle.getX() + monsterSpriteGap, rectangle.getY() + monsterSpriteGap * 2);
sprite.setSize(monsterSpriteSize, monsterSpriteSize);
sprite.draw(spriteBatch, 1);
text.drawSmallRoboto(spriteBatch, monster.getName(), Color.BLACK,
rectangle.getX() + rectangle.getWidth() / 2f,
rectangle.getY() + monsterSpriteGap);
}

if(drawMonsterSwitcher){
final float arrowSpacingFromEdge = 10;
text.drawNormalRoboto(spriteBatch, "<", Color.BLACK,
rectangle.getX() + arrowSpacingFromEdge,
rectangle.getY() + rectangle.getHeight() / 2f );
text.drawNormalRoboto(spriteBatch, ">", Color.BLACK,
rectangle.getX() - arrowSpacingFromEdge + rectangle.getWidth(),
rectangle.getY() + rectangle.getHeight() / 2f );
}
}

public void setTeamAMonsters(IMonster[] teamAMonsters) {
if (teamAMonsters.length != this.teamAMonsters.length) {
throw new IllegalArgumentException("The monster array must be exactly %d entries long.".formatted(this.teamAMonsters.length));
}
this.teamAMonsters = teamAMonsters;
}

public void setTeamBMonsters(IMonster[] teamBMonsters) {
if (teamBMonsters.length != this.teamBMonsters.length) {
throw new IllegalArgumentException("The monster array must be exactly %d entries long.".formatted(this.teamBMonsters.length));
}
this.teamBMonsters = teamBMonsters;
}

public void setTeamA(boolean teamA) {
isTeamA = teamA;
}

public void setChangingMonsterIndex(int changingMonsterIndex) {
this.changingMonsterIndex = changingMonsterIndex;
}

public void setSelectedMonsterIndex(int selectedMonsterIndex) {
this.selectedMonsterIndex = selectedMonsterIndex;
}

public void setTeamAAIText(String teamAAIText) {
this.teamAAIText = teamAAIText;
}

public void setTeamBAIText(String teamBAIText) {
this.teamBAIText = teamBAIText;
}

public void setTeamATextColor(Color teamATextColor) {
this.teamATextColor = teamATextColor;
}

public void setTeamBTextColor(Color teamBTextColor) {
this.teamBTextColor = teamBTextColor;
}

public void setShowTeamAAIArrow(boolean showTeamAAIArrow) {
this.showTeamAAIArrow = showTeamAAIArrow;
}

public void setShowTeamBAIArrow(boolean showTeamBAIArrow) {
this.showTeamBAIArrow = showTeamBAIArrow;
}

public void setStartBattleColor(Color startBattleColor) {
this.startBattleColor = startBattleColor;
}

public void setErrorPosition(Position errorPosition) {
this.errorPosition = errorPosition;
}

public void setErrorOpacity(float errorOpacity) {
this.errorOpacity = errorOpacity;
}

public void setErrorText(String errorText) {
this.errorText = errorText;
}

public void setErrorSize(float errorSize) {
this.errorSize = errorSize;
}

public int getScreenWidth() {
return screenWidth;
}

public int getScreenHeight() {
return screenHeight;
}

private void calculateBoxSizes(GameData gameData) {
final int monsterContainerBottomOffset = 130;
final int monsterContainerCenterOffset = 20;
final int freeSpaceToLeaveAtTop = 150; // Estimated size of text + mer'
final int containerGap = 20;
final float containerWidth = 300;
final float containerHeight = gameData.getDisplayHeight() - monsterContainerBottomOffset - containerGap - freeSpaceToLeaveAtTop;
teamAContainer.setPosition(new Position((gameData.getDisplayWidth() / 2f) - monsterContainerCenterOffset - containerWidth, monsterContainerBottomOffset));
teamBContainer.setPosition(new Position((gameData.getDisplayWidth() / 2f) + monsterContainerCenterOffset, monsterContainerBottomOffset));
teamAContainer.setWidth(containerWidth);
teamBContainer.setWidth(containerWidth);
teamAContainer.setHeight(containerHeight);
teamBContainer.setHeight(containerHeight);

final float monsterBoxSize = 110;
final float topOfContainers = teamAContainer.getY() + teamAContainer.getHeight();
final float monsterGap = 20;
for (int i = 0; i < teamAMonsterBoxes.length; i++) {
teamAMonsterBoxes[i].setHeight(monsterBoxSize);
teamAMonsterBoxes[i].setWidth(monsterBoxSize);
teamBMonsterBoxes[i].setHeight(monsterBoxSize);
teamBMonsterBoxes[i].setWidth(monsterBoxSize);

final float rowCount = i % 2 == 0 ? i + 1 : i;
float boxY = topOfContainers - monsterBoxSize - monsterGap - (monsterGap / 2f * (rowCount - 1)) - (monsterBoxSize / 2f * (rowCount - 1));
if (i % 2 == 0) {
// Even
teamAMonsterBoxes[i].setPosition(new Position(teamAContainer.getX() + monsterGap, boxY));
teamBMonsterBoxes[i].setPosition(new Position(teamBContainer.getX() + monsterGap, boxY));
} else {
// Odd
teamAMonsterBoxes[i].setPosition(new Position(teamAContainer.getX() + teamAContainer.getWidth() - monsterGap - teamAMonsterBoxes[i].getWidth(), boxY));
teamBMonsterBoxes[i].setPosition(new Position(teamBContainer.getX() + teamBContainer.getWidth() - monsterGap - teamBMonsterBoxes[i].getWidth(), boxY));
}
}
}
}
Loading