Skip to content

Commit

Permalink
final changes
Browse files Browse the repository at this point in the history
  • Loading branch information
asemanehnafe committed Apr 25, 2022
1 parent b3597f9 commit 226fcb6
Show file tree
Hide file tree
Showing 18 changed files with 485 additions and 471 deletions.
12 changes: 12 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 15 additions & 41 deletions controllers/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import enums.Color;
import enums.HexState;
import models.Player;
import models.User;
import models.maprelated.Hex;
import models.maprelated.World;
import models.units.Civilian;
Expand All @@ -16,7 +15,7 @@

public class GameController {

private static ArrayList<User> players = new ArrayList<User>();
private static ArrayList<String> players = new ArrayList<String>();
private static int turn;
private static World world;
private static int[] mapBoundaries;
Expand All @@ -26,7 +25,7 @@ public class GameController {
private static ArrayList<Military> allMilitaries;
private static Unit selectedUnit;

public static ArrayList<User> getPlayers() {
public static ArrayList<String> getPlayers() {
return players;
}

Expand Down Expand Up @@ -182,7 +181,7 @@ private static void initializeString(int[] mapBoundaries, String[][] string) {
}
for (int n = mapBoundaries[0]; n < mapBoundaries[1]; n++) {
for (int m = mapBoundaries[2]; m < mapBoundaries[3]; m++) {
if (hex[n][m].getState() == HexState.Visible) drawHex(hex[n][m]);
if(hex[n][m].getState() == HexState.Visible)drawHex(hex[n][m]);
else drawFogOfWar(hex[n][m]);
}
}
Expand Down Expand Up @@ -286,30 +285,23 @@ public static Civilian getCiviliansByLocation(int x, int y) {
return null;
}

private static int[] getDirectionIndex(int[][] direction, int dx, int dy, Unit unit) {
if (dx != 0) dx = dx / Math.abs(dx);
if (dy != 0) dy = dy / Math.abs(dy);
for (int[] ints : direction) {
if (ints[0] == dx && ints[1] == dy && isPositionValid(unit.getCurrentHex().getX() + ints[0], unit.getCurrentHex().getY() + ints[1]))
return ints;
}
return null;
}

public static Hex getNextHex(int finalX, int finalY) {
Unit unit = selectedUnit;
int[] direction;
int[][] oddDirection = new int[][]{{-1, 0}, {0, -1}, {1, -1}, {1, 0}, {0, 1}, {1, 1}};
int[][] evenDirection = new int[][]{{-1, 0}, {-1, -1}, {0, -1}, {1, 0}, {-1, 1}, {0, 1}};
int deltaX = finalX - unit.getCurrentHex().getX();
int deltaY = finalY - unit.getCurrentHex().getY();

if (unit.getCurrentHex().getY() % 2 == 0) {
direction = getDirectionIndex(evenDirection, deltaX, deltaY, unit);
} else {
direction = getDirectionIndex(oddDirection, deltaX, deltaY, unit);
}
return hex[unit.getCurrentHex().getX() + direction[0]][unit.getCurrentHex().getY() + direction[1]];
if (deltaX < 0 && deltaY < 0 && isPositionValid(unit.getCurrentHex().getX() - 1, unit.getCurrentHex().getY() - 1))
return hex[unit.getCurrentHex().getX() - 1][unit.getCurrentHex().getY() - 1];//bala chap
else if (deltaX < 0 && deltaY > 0 && isPositionValid(unit.getCurrentHex().getX() - 1, unit.getCurrentHex().getY() + 1))
return hex[unit.getCurrentHex().getX() - 1][unit.getCurrentHex().getY() + 1];//bala rast
else if (deltaX < 0 && isPositionValid(unit.getCurrentHex().getX() - 1, unit.getCurrentHex().getY()))
return hex[unit.getCurrentHex().getX() - 1][unit.getCurrentHex().getY()];//bala
else if (deltaY == 0 && isPositionValid(unit.getCurrentHex().getX() + 1, unit.getCurrentHex().getY()))
return hex[unit.getCurrentHex().getX() + 1][unit.getCurrentHex().getY()];//paeen
else if (deltaY > 0 && isPositionValid(unit.getCurrentHex().getX(), unit.getCurrentHex().getY() + 1))
return hex[unit.getCurrentHex().getX()][unit.getCurrentHex().getY() + 1];//paeen rast
else
return hex[unit.getCurrentHex().getX()][unit.getCurrentHex().getY() - 1];//paeen chap
}

public static boolean canMoveThrough(int x, int y) {
Expand All @@ -332,27 +324,9 @@ public static void setSelectedUnit(Unit selectedUnit) {
}

public static void moveUnit(Unit unit, int x, int y) {
changeView(x, y);
unit.changeCurrentHex(hex[x][y]);
unit.decreaseMP(hex[x][y].getTerrain().getMovePoint());
}

private static void changeView(int x, int y) {
int[][] oddDirection = new int[][]{{0, 0}, {-1, 0}, {0, -1}, {1, -1}, {1, 0}, {0, 1}, {1, 1}};
int[][] evenDirection = new int[][]{{0, 0}, {-1, 0}, {-1, -1}, {0, -1}, {1, 0}, {-1, 1}, {0, 1}};
int[][] direction;

if (y % 2 == 0) direction = evenDirection;
else direction = oddDirection;

for (int j = 0; j < 7; j++) {
x = x + direction[j][0];
y = y + direction[j][0];
for (int i = 0; i < 7; i++) {
if (isPositionValid(x + direction[i][0], y + direction[i][1]))
hex[x + direction[i][0]][y + direction[i][1]].setState(HexState.Visible);
}
}
}

}
Loading

0 comments on commit 226fcb6

Please sign in to comment.