From b40f969a5051ad700b10d73e5ac0929e9bda054d Mon Sep 17 00:00:00 2001 From: Eugene Kravchenko Date: Sat, 4 Jul 2020 21:15:25 +0300 Subject: [PATCH] fix duplication --- .../ru/itmo/roguelike/characters/Player.java | 9 +--- .../mobs/strategy/BossBehavior.java | 24 +---------- .../manager/actormanager/BossManager.java | 42 +++++++++---------- .../itmo/roguelike/utils/FloatCoordinate.java | 10 ++--- 4 files changed, 29 insertions(+), 56 deletions(-) diff --git a/src/main/java/ru/itmo/roguelike/characters/Player.java b/src/main/java/ru/itmo/roguelike/characters/Player.java index 94429ab..de4599c 100644 --- a/src/main/java/ru/itmo/roguelike/characters/Player.java +++ b/src/main/java/ru/itmo/roguelike/characters/Player.java @@ -7,10 +7,6 @@ import ru.itmo.roguelike.characters.inventory.Droppable; import ru.itmo.roguelike.characters.inventory.Inventory; import ru.itmo.roguelike.characters.inventory.Usable; -import ru.itmo.roguelike.characters.mobs.Enemy; -import ru.itmo.roguelike.characters.mobs.PersonX; -import ru.itmo.roguelike.characters.mobs.strategy.AggressiveBehavior; -import ru.itmo.roguelike.characters.mobs.strategy.MobWithTarget; import ru.itmo.roguelike.characters.movement.Mover; import ru.itmo.roguelike.field.Field; import ru.itmo.roguelike.field.TileType; @@ -53,6 +49,7 @@ public class Player extends Actor { armor.renderInInventory(g, x - 20, y - 20, 40, 40); } }); + private final BossManager bossManager; private IntCoordinate moveDirection = IntCoordinate.getZeroPosition(); private boolean doAttack = false; private int level; @@ -60,8 +57,6 @@ public class Player extends Actor { private long lastInventoryWarning = GameManager.GLOBAL_TIME; private long lastDroppableWarning = GameManager.GLOBAL_TIME; - private final BossManager bossManager; - @Inject public Player(@NotNull EventManager eventManager, @NotNull BossManager bossManager) { this.eventManager = eventManager; @@ -293,7 +288,7 @@ public void addExp(float exp) { if (levelGain > 0) { if (level <= 2 && level + levelGain > 2) { - bossManager.createBoss(); + bossManager.createBoss(); } level += levelGain; diff --git a/src/main/java/ru/itmo/roguelike/characters/mobs/strategy/BossBehavior.java b/src/main/java/ru/itmo/roguelike/characters/mobs/strategy/BossBehavior.java index b157ccd..a6b241e 100644 --- a/src/main/java/ru/itmo/roguelike/characters/mobs/strategy/BossBehavior.java +++ b/src/main/java/ru/itmo/roguelike/characters/mobs/strategy/BossBehavior.java @@ -1,27 +1,5 @@ package ru.itmo.roguelike.characters.mobs.strategy; -import ru.itmo.roguelike.field.Field; -import ru.itmo.roguelike.utils.IntCoordinate; +public class BossBehavior extends AggressiveBehavior { -public class BossBehavior extends MobWithTarget { - private final Field field; - - public BossBehavior(Field field) { - this.field = field; - } - - @Override - public IntCoordinate getPath() { - if (target == null) { - return IntCoordinate.getZeroPosition(); - } - - final IntCoordinate diff = new IntCoordinate(target.getPosition()); - diff.substract(self.getPosition()); - if (diff.lenL2() < self.getRadius()) { - return diff.signum(); - } - - return IntCoordinate.getZeroPosition(); - } } diff --git a/src/main/java/ru/itmo/roguelike/manager/actormanager/BossManager.java b/src/main/java/ru/itmo/roguelike/manager/actormanager/BossManager.java index e0fb2bd..8ad88e6 100644 --- a/src/main/java/ru/itmo/roguelike/manager/actormanager/BossManager.java +++ b/src/main/java/ru/itmo/roguelike/manager/actormanager/BossManager.java @@ -16,6 +16,26 @@ public class BossManager { private GameManager gameManager; + @NotNull + private static IntCoordinate getFreeBossCoordinate(@NotNull Field field, int x, int y) { + final int cellSize = 100; + + for (int k = 2; ; k++) { + for (int j = -k; j < k; j++) { + for (IntCoordinate coordinate : new IntCoordinate[]{ + new IntCoordinate(x + k * cellSize, y + j * cellSize), + new IntCoordinate(x - k * cellSize, y + j * cellSize), + new IntCoordinate(x + j * cellSize, y + k * cellSize), + new IntCoordinate(x + j * cellSize, y - k * cellSize), + }) { + if (!field.getTileType(coordinate).isSolid()) { + return new IntCoordinate(coordinate); + } + } + } + } + } + public void setGameManager(GameManager gameManager) { this.gameManager = gameManager; } @@ -34,29 +54,9 @@ public void createBoss() { Enemy.builder(PersonX::new) .setPosition(bossPosition) - .setBehavior(MobWithTarget.builder(() -> new BossBehavior(field))) + .setBehavior(MobWithTarget.builder(() -> new BossBehavior())) .setRadius(10000000) .setTarget(player) .createAndRegister(); } - - @NotNull - private static IntCoordinate getFreeBossCoordinate(@NotNull Field field, int x, int y) { - final int cellSize = 100; - - for (int k = 2; ; k++) { - for (int j = -k; j < k; j++) { - for (IntCoordinate coordinate : new IntCoordinate[]{ - new IntCoordinate(x + k * cellSize, y + j * cellSize), - new IntCoordinate(x - k * cellSize, y + j * cellSize), - new IntCoordinate(x + j * cellSize, y + k * cellSize), - new IntCoordinate(x + j * cellSize, y - k * cellSize), - }) { - if (!field.getTileType(coordinate).isSolid()) { - return new IntCoordinate(coordinate); - } - } - } - } - } } diff --git a/src/main/java/ru/itmo/roguelike/utils/FloatCoordinate.java b/src/main/java/ru/itmo/roguelike/utils/FloatCoordinate.java index 13d21d8..3a8e9dc 100644 --- a/src/main/java/ru/itmo/roguelike/utils/FloatCoordinate.java +++ b/src/main/java/ru/itmo/roguelike/utils/FloatCoordinate.java @@ -29,6 +29,10 @@ public static FloatCoordinate getZeroPosition() { return new FloatCoordinate(0, 0); } + public static FloatCoordinate fromAngle(float angle) { + return new FloatCoordinate((float) Math.cos(angle), (float) Math.sin(angle)); + } + public float getX() { return posX; } @@ -79,15 +83,11 @@ public float lenL1() { /** * @return angle in degrees (in range [-pi, pi]) between {@code new FloatCoordinate(1, 0)} - * */ + */ public float toAngle() { return (float) Math.atan2(posY, posX); } - public static FloatCoordinate fromAngle(float angle) { - return new FloatCoordinate((float) Math.cos(angle), (float) Math.sin(angle)); - } - @Override public String toString() { return String.format("FCoord[%f, %f]", posX, posY);