Skip to content

Commit

Permalink
fix duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhekehZ committed Jul 4, 2020
1 parent 2f456b2 commit b40f969
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 56 deletions.
9 changes: 2 additions & 7 deletions src/main/java/ru/itmo/roguelike/characters/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,15 +49,14 @@ 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;
private float exp;
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;
Expand Down Expand Up @@ -293,7 +288,7 @@ public void addExp(float exp) {

if (levelGain > 0) {
if (level <= 2 && level + levelGain > 2) {
bossManager.createBoss();
bossManager.createBoss();
}

level += levelGain;
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
}
}
}
}
}
}
10 changes: 5 additions & 5 deletions src/main/java/ru/itmo/roguelike/utils/FloatCoordinate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b40f969

Please sign in to comment.