Package be.demmel.jgws.modules

Source Code of be.demmel.jgws.modules.Movement

package be.demmel.jgws.modules;

import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;

import java.util.Date;

import org.jboss.logging.MDC;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import be.demmel.jgws.ActionQueue;
import be.demmel.jgws.CharacterData;
import be.demmel.jgws.MovementState;
import be.demmel.jgws.GameServerSession;
import be.demmel.jgws.SessionKey;
import be.demmel.jgws.actions.MovePlayerAction;
import be.demmel.jgws.actions.RotatePlayerAction;

public class Movement extends AffectingAllPlayers {

    private static final Logger LOGGER = LoggerFactory.getLogger(Movement.class);
    private final ActionQueue actionQueue;

    public Movement(ActionQueue actionQueue, ChannelGroup channels) {
        super(channels, 10);
        this.actionQueue = actionQueue;
    }

    @Override
    protected void handle(ChannelGroup channels) throws IllegalStateException, Exception {
     
      for(Channel channel : channels) {
        MDC.put("channel", String.format("[id: 0x%08x]",channel.hashCode()));
        GameServerSession playerData = channel.attr(SessionKey.SESSION_KEY).get();
       
        // Retrieve the Character the player is currently playing
            CharacterData character = playerData.getCurrentCharacter();
           
            if(character == null) {// character not loaded yet
              continue;
            }

            MovementState movementState = character.getMoveState();

            switch (movementState) {
                case MOVE_CHANGE_DIR:
                    MovePlayerAction movePlayerActionChangeDir = new MovePlayerAction(channel, channels);
                    this.actionQueue.addAction(movePlayerActionChangeDir);

                    character.setMoveState(MovementState.MOVE_KEEP_DIR);
                    break;
                case MOVE_KEEP_DIR:
                    // Only move the player each 250ms (if the direction is kept)
                    if ((new Date().getTime() - character.getLastHeartBeat().getTime()) > 250) {
                        MovePlayerAction movePlayerActionKeepDir = new MovePlayerAction(channel, channels);
                        this.actionQueue.addAction(movePlayerActionKeepDir);
                    }
                    break;
                case NOT_MOVING_UNHANDLED:
                    MovePlayerAction movePlayerActionUnhandled = new MovePlayerAction(channel, channels);
                    this.actionQueue.addAction(movePlayerActionUnhandled);
                   
                    // Calculate the rotation
                    character.setRotation((float)Math.atan2(character.getDirection().getY(), character.getDirection().getX()));
                    RotatePlayerAction rotatePlayerAction = new RotatePlayerAction(channel, channels);
                    this.actionQueue.addAction(rotatePlayerAction);
                   
                    character.setMoveState(MovementState.NOT_MOVING);
                    break;
            }
      }
    }
}
TOP

Related Classes of be.demmel.jgws.modules.Movement

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.