Skip to content
Open
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
48 changes: 48 additions & 0 deletions src/net/fe/network/command/SwapCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package net.fe.network.command;

import java.util.ArrayList;
import java.util.Arrays;

import net.fe.fightStage.AttackRecord;
import net.fe.overworldStage.ClientOverworldStage;
import net.fe.overworldStage.Node;
import net.fe.overworldStage.OverworldStage;
import net.fe.overworldStage.context.FormationContext;
import net.fe.unit.Unit;

public final class SwapCommand extends Command {


private static final long serialVersionUID = -3945531642092346657L;

private Node[] swaps;
private byte id;

public SwapCommand(ArrayList<Node> swaps, byte id) {
this.swaps = swaps.toArray(new Node[0]);
this.id = id;
}

@Override
public ArrayList<AttackRecord> applyServer(OverworldStage stage, Unit unit) {
for(int i = 0; i < swaps.length; i += 2)
stage.grid.swap(swaps[i], swaps[i + 1]);
return null;
}

@Override
public Runnable applyClient(ClientOverworldStage stage, Unit unit, ArrayList<AttackRecord> attackRecords, Runnable callback) {
return () -> {
for(int i = 0; i < swaps.length; i += 2)
stage.grid.swap(swaps[i], swaps[i + 1]);
if(stage.getContext() instanceof FormationContext)
((FormationContext) stage.getContext()).removePlayer(id);
callback.run();
};
}

@Override
public String toString() {
return "Swap" + Arrays.toString(swaps);
}
}
21 changes: 8 additions & 13 deletions src/net/fe/overworldStage/ClientOverworldStage.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.fe.network.message.CommandMessage;
import net.fe.network.message.EndTurn;
import net.fe.overworldStage.Zone.Fog;
import net.fe.overworldStage.context.RepositionContext;
import net.fe.overworldStage.context.Idle;
import net.fe.overworldStage.context.WaitForMessages;
import net.fe.transition.OverworldEndTransition;
Expand Down Expand Up @@ -149,18 +150,8 @@ public ClientOverworldStage(Session s) {
addEntity(new OverworldChat(this.session.getChatlog()));
addEntity(new ObjectiveInfo());
setControl(true);
if(getCurrentPlayer().equals(FEMultiplayer.getLocalPlayer())) {
context = new Idle(this, getCurrentPlayer());
addEntity(new TurnDisplay(true, Party.TEAM_BLUE));
SoundTrack.loop("overworld");
} else {
context = new WaitForMessages(this);
if(FEMultiplayer.getLocalPlayer().isSpectator())
addEntity(new TurnDisplay(false, getCurrentPlayer().getParty().getColor()));
else
addEntity(new TurnDisplay(false, Party.TEAM_RED));
SoundTrack.loop("enemy");
}
context = new RepositionContext(this, null);

repeatTimers = new float[4];
currentCmdString = new ArrayList<Command>();
pendingMessages = new java.util.LinkedList<Message>();
Expand Down Expand Up @@ -547,8 +538,8 @@ public void loadLevel(String levelName) {
InputStream in = ResourceLoader.getResourceAsStream("levels/"+levelName+".lvl");
ObjectInputStream ois = new ObjectInputStream(in);
Level level = (Level) ois.readObject();
grid = new Grid(level.width, level.height, Terrain.NONE);
Set<SpawnPoint> spawns = new HashSet<>(level.spawns);
grid = new Grid(level.width, level.height, Terrain.NONE, new HashSet<>(spawns));
for(int i=0; i<level.tiles.length; i++) {
for(int j=0; j<level.tiles[0].length; j++) {
Tile tile = new Tile(j, i, level.tiles[i][j]);
Expand Down Expand Up @@ -683,4 +674,8 @@ public String toString() {
return representation;
}
}

public OverworldContext getContext() {
return context;
}
}
41 changes: 40 additions & 1 deletion src/net/fe/overworldStage/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import net.fe.FEMultiplayer;
import net.fe.Party;
import net.fe.editor.SpawnPoint;
import net.fe.unit.Unit;
import net.fe.unit.UnitIdentifier;

Expand All @@ -31,6 +32,8 @@ public class Grid{
/** The red throne y. */
private int redThroneX, redThroneY;

private Set<SpawnPoint> spawns;

/** The height. */
public final int width, height;

Expand All @@ -41,7 +44,7 @@ public class Grid{
* @param height the height
* @param defaultTerrain the default terrain
*/
public Grid(int width, int height, Terrain defaultTerrain) {
public Grid(int width, int height, Terrain defaultTerrain, Set<SpawnPoint> spawns) {
grid = new Unit[height][width];
blueThroneX = -1;
blueThroneY = -1;
Expand All @@ -55,6 +58,7 @@ public Grid(int width, int height, Terrain defaultTerrain) {
}
this.width = width;
this.height = height;
this.spawns = spawns;
}

/**
Expand Down Expand Up @@ -124,6 +128,33 @@ public void move(Unit u, int x, int y, boolean animated){
u.gridSetYCoord(y);
}
}

public void swap(Node n1, Node n2) {
if(getUnit(n1) != null && getUnit(n2) != null)
swap(getUnit(n1), getUnit(n2));
else if(getUnit(n2) != null)
swap(n2, n1);
else
move(getUnit(n1), n2.x, n2.y, false);
}

public void swap(Unit u1, Unit u2) {
grid[u1.getYCoord()][u1.getXCoord()] = u2;
grid[u2.getYCoord()][u2.getXCoord()] = u1;

int xTemp = u2.getXCoord();
int yTemp = u2.getYCoord();

u2.gridSetXCoord(u1.getXCoord());
u2.gridSetYCoord(u1.getYCoord());
u2.setOrigX(u1.getXCoord());
u2.setOrigY(u1.getYCoord());

u1.gridSetXCoord(xTemp);
u1.gridSetYCoord(yTemp);
u1.setOrigX(xTemp);
u1.setOrigY(yTemp);
}

/**
* Gets the terrain.
Expand Down Expand Up @@ -158,6 +189,10 @@ public Unit getUnit(int x, int y) {
return grid[y][x];
}

public Unit getUnit(Node n) {
return getUnit(n.x, n.y);
}

/**
* Returns the unit at the given position. If the unit is not visible, returns null instead (as if there was nothing there).
* @param x The x coordinate of the position.
Expand All @@ -179,6 +214,10 @@ public boolean contains(int x, int y) {
return x >= 0 && y >= 0 && x < this.width && y < this.height;
}

public Set<SpawnPoint> getSpawns() {
return spawns;
}

/**
* Sets the throne pos.
*
Expand Down
2 changes: 1 addition & 1 deletion src/net/fe/overworldStage/OverworldStage.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public void loadLevel(String levelName) {
ObjectInputStream ois = new ObjectInputStream(in);
Level level = (Level) ois.readObject();
Set<SpawnPoint> spawns = new HashSet<>(level.spawns);
grid = new Grid(level.width, level.height, Terrain.NONE);
grid = new Grid(level.width, level.height, Terrain.NONE, new HashSet<>(spawns));
for(int i=0; i<level.tiles.length; i++) {
for(int j=0; j<level.tiles[0].length; j++) {
grid.setTerrain(j, i, Tile.getTerrainFromID(level.tiles[i][j]));
Expand Down
4 changes: 4 additions & 0 deletions src/net/fe/overworldStage/Zone.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ public Zone add(Zone b) {
return new Zone(nodes, type);
}

public void add(Node node) {
zone.add(node);
}

public static Zone all(Grid grid, ZoneType fogLight) {
Set<Node> nodes = new HashSet<Node>();
for(int i = 0; i < grid.width; i++)
Expand Down
11 changes: 11 additions & 0 deletions src/net/fe/overworldStage/context/FormationContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.fe.overworldStage.context;

import net.fe.Player;

public interface FormationContext {
/**
* Called when a player is done with formations.
* @param player The player.
*/
public void removePlayer(byte id);
}
55 changes: 55 additions & 0 deletions src/net/fe/overworldStage/context/OverworldInspectionContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package net.fe.overworldStage.context;

import chu.engine.anim.AudioPlayer;
import net.fe.overworldStage.ClientOverworldStage;
import net.fe.overworldStage.CursorContext;
import net.fe.overworldStage.OverworldContext;
import net.fe.overworldStage.Zone;
import net.fe.overworldStage.Zone.ZoneType;
import net.fe.unit.Unit;

public abstract class OverworldInspectionContext extends CursorContext {

private Zone move, attack, heal;

public OverworldInspectionContext(ClientOverworldStage s, OverworldContext prevContext) {
super(s, prevContext);
}

@Override
public void cursorChanged() {
Unit u = getHoveredUnit();

stage.setUnitInfoUnit(u);
if(u != null && !u.hasMoved()) {
addZones(u);
if(u.getParty() == stage.getCurrentPlayer().getParty())
u.sprite.setAnimation("SELECTED");
}
}

@Override
public void cursorWillChange() {
removeZones();
Unit u = getHoveredUnit();
if(u != null && !u.hasMoved())
u.sprite.setAnimation("IDLE");
AudioPlayer.playAudio("cursor");
}

private void addZones(Unit u) {
this.move = new Zone(stage.grid.getPossibleMoves(u), ZoneType.MOVE_LIGHT);
this.attack = new Zone(stage.grid.getAttackRange(u), ZoneType.ATTACK_LIGHT).minus(move);
this.heal = new Zone(stage.grid.getHealRange(u), ZoneType.HEAL_LIGHT).minus(move).minus(attack);
stage.addEntity(move);
stage.addEntity(attack);
stage.addEntity(heal);
}

private void removeZones() {
stage.removeEntity(move);
stage.removeEntity(attack);
stage.removeEntity(heal);
}

}
Loading