package be.demmel.jgws.actions;
import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import be.demmel.jgws.CharacterData;
import be.demmel.jgws.MovementState;
import be.demmel.jgws.MovementType;
import be.demmel.jgws.SessionKey;
import be.demmel.jgws.QueueAction;
import be.demmel.jgws.packets.Vector2;
import be.demmel.jgws.packets.gameserver.outbound.P026_MovementDirection;
import be.demmel.jgws.packets.gameserver.outbound.P030_MovementAim;
import be.demmel.jgws.packets.gameserver.outbound.P032_MovementSpeedModifier;
public class MovePlayerAction implements QueueAction {
private static final Logger LOGGER = LoggerFactory.getLogger(MovePlayerAction.class);
private Channel movingPlayer;
private final ChannelGroup channels;
// Position must be passed so that it can't be changed before the queue processes this action?
public MovePlayerAction(Channel movingPlayer, ChannelGroup channels) {
this.movingPlayer = movingPlayer;
this.channels = channels;
}
@Override
public void execute() {
try {
// Let each player on the map know about this player's movement
for (Channel player : channels) {
movePlayer(movingPlayer, player);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void movePlayer(Channel movingPlayer, Channel recipient) throws Exception {
CharacterData character = movingPlayer.attr(SessionKey.SESSION_KEY).get().getCurrentCharacter();
MovementType moveType = character.getMoveType();
if (moveType == MovementType.FORWARD || moveType == MovementType.DIAG_FW_LEFT || moveType == MovementType.DIAG_FW_RIGHT || moveType == MovementType.STOP) {
character.setSpeedModifier(1f);
} else if (moveType == MovementType.BACKWARD || moveType == MovementType.DIAG_BW_LEFT || moveType == MovementType.DIAG_BW_RIGHT) {
character.setSpeedModifier(.066f);
} else if (moveType == MovementType.SIDE_LEFT || moveType == MovementType.SIDE_RIGHT) {
character.setSpeedModifier(0.75f);
} else if (moveType == MovementType.COLLISION) {
// dont change speed modifier, as it is dynamically set.
// reset movetype, cause 10 is non existant for the client
moveType = MovementType.FORWARD;
}
if (moveType != MovementType.STOP && character.getMoveState() == MovementState.MOVE_CHANGE_DIR) {
P026_MovementDirection movementDirection = new P026_MovementDirection();
movementDirection.setAgentId(character.getAgentID());
Vector2 direction = new Vector2(character.getDirection().getX(), character.getDirection().getY());
movementDirection.setDirection(direction);
movementDirection.setMoveType((short) moveType.getValue());
recipient.write(movementDirection);
// reset the moving state here
character.setMoveState(MovementState.MOVE_KEEP_DIR);
}
P032_MovementSpeedModifier movementSpeedModifier = new P032_MovementSpeedModifier();
movementSpeedModifier.setAgentId(character.getAgentID());
movementSpeedModifier.setSpeed(character.getSpeedModifier());
movementSpeedModifier.setMoveType((short) moveType.getValue());
recipient.write(movementSpeedModifier);
P030_MovementAim movementAim = new P030_MovementAim();
movementAim.setAgentId(character.getAgentID());
Vector2 position = new Vector2(character.getPosition().getX(), character.getPosition().getY());
movementAim.setPosition(position);
movementAim.setPlane(character.getPosition().getZ());
recipient.write(movementAim);
}
}