Package de.nameless.gameEngine.system

Source Code of de.nameless.gameEngine.system.GameInputAdapter

package de.nameless.gameEngine.system;

import static de.nameless.gameEngine.util.Logger.debug;
import de.nameless.gameEngine.core.NEClient;
import de.nameless.gameEngine.gameObjects.NEField;
import de.nameless.gameEngine.gameObjects.NEUnit;
import de.nameless.gameEngine.system.lan.messages.NETransaktionMessage;
import de.nameless.gameEngine.system.transactions.NEAttackUnitTransaktion;
import de.nameless.gameEngine.system.transactions.NEGameTransaction;
import de.nameless.gameEngine.system.transactions.NEMoveUnitTransaktion;
import de.nameless.gameEngine.util.StateMachine.Action;
import de.nameless.gameEngine.util.StateMachine.State;
import de.nameless.gameEngine.util.StateMachine.StateMachine;

public class GameInputAdapter {
 
  private NEClient client;
 
  private NEUnit selectedUnit;    
 
  StateMachine MoveAttackSM;
 
  State   startState;
  Action   selectUnitAction;
  State   unitSelected;
  Action   deSelectUnitAction;
  Action   moveUnitAction;
  State   unitMovedState;
  Action  attackUnitAction;
  Action   reverseMoveAction;
 
  public GameInputAdapter(NEClient client) {
    this.client = client;
    this.setupStatMachines();
  }
 
  private void setupStatMachines(){
   
    MoveAttackSM = new StateMachine();
   
    selectUnitAction = new Action("selectUnitAction");   
    deSelectUnitAction = new Action("deSelectUnitAction");   
    moveUnitAction = new Action("moveUnitAction");   
    attackUnitAction = new Action("attackUnitAction");   
   
    startState = new State("Start")
    unitSelected = new State("unitSelected");
    unitMovedState = new State("unitMoved");   
   
    startState.addTransition(selectUnitAction, unitSelected);
    unitSelected.addTransition(deSelectUnitAction, startState);
    unitSelected.addTransition(moveUnitAction, unitMovedState);
    unitMovedState.addTransition(moveUnitAction, startState);
    unitMovedState.addTransition(deSelectUnitAction, startState);
    unitMovedState.addTransition(reverseMoveAction, unitSelected);
   
    MoveAttackSM.add(startState)
    MoveAttackSM.add(unitSelected)
    MoveAttackSM.add(unitMovedState)
 
    MoveAttackSM.setCurrentState(startState);
  }

  public void fieldHit(NEField f){   
   
    debug(MoveAttackSM.toString(),false);
   
    if(client.myTurn){ // ist der Spieler an der Reihe     
      debug(MoveAttackSM.toString(),false);     
      if(MoveAttackSM.getCurrentState()==unitSelected){       
        if(f.containtLUnit == null && selectedUnit.reachable(f)){// einheit bewegen
          this.moveSelectedUnit(f)
          MoveAttackSM.applyAction(moveUnitAction);     
          selectedUnit.markAttackableEnemys();
        } else if (f.containtLUnit.getOwnerID()!=selectedUnit.getOwnerID()){
          this.letSelectedUnitAttack(f);
          MoveAttackSM.applyAction(deSelectUnitAction);
        }
       
      } else if (MoveAttackSM.getCurrentState()==startState){// neue selektiert
       
        if(f.containtLUnit != null && !f.containtLUnit.isMovedLableSet() && client.getID() == f.containtLUnit.getOwnerID()) {
          if(MoveAttackSM.getCurrentState()==unitSelected) this.deSelectUnit();
          this.selectUnit(f.containtLUnit);
          MoveAttackSM.applyAction(selectUnitAction);       
        }
       
      } else if (MoveAttackSM.getCurrentState()==unitMovedState){//Nach der bewegung
       
        if(f.containsUnit()&& f.containtLUnit.isAttackableLableSet() &&client.getID() != f.containtLUnit.getOwnerID()){//Attack
          this.letSelectedUnitAttack(f);
          MoveAttackSM.applyAction(deSelectUnitAction);         
        } else if(f.containtLUnit==selectedUnit){//appy the move
          selectedUnit.endMovements();
          client.endTransaction();
          deSelectUnit();
          MoveAttackSM.applyAction(deSelectUnitAction);
        } else {//undo the move         
          this.undoMove()
          selectedUnit.markAttackableEnemys();
          MoveAttackSM.applyAction(reverseMoveAction);
        }
      } 
     
    } // ist der Spieler an der Reihe
 
 
  public void selectUnit(NEUnit u){   
    selectedUnit = u;
    u.select();
  }
 
  public void deSelectUnit(){
    if(selectedUnit!=null){
      selectedUnit.deSelect();
      selectedUnit = null;
    }
  }
 
  public void moveSelectedUnit(NEField target){   
    NEGameTransaction moveTX = new NEMoveUnitTransaktion(selectedUnit.getUnitId(),target.BestWay);
    client.DoTransaction(moveTX);   
    client.addToTurn(moveTX);     
  }
 
  public void letSelectedUnitAttack(NEField target){
    client.addToTurn(new NEAttackUnitTransaktion(this.selectedUnit.getUnitId(), target.containtLUnit.getUnitId()));
    client.endTransaction();         
    this.deSelectUnit();
  }
 
  public void undoMove(){
    if(MoveAttackSM.getCurrentState()==unitMovedState){
      client.removeLastFormTurn();
      selectedUnit.unMarkReachableEnemys();
      selectedUnit.markReachableFields();
    }
  }
 
  public void endTurn(){   
    client.endTurn();
    if (selectedUnit!= null)
      { 
        selectedUnit.deSelect();
        selectedUnit = null;
        MoveAttackSM.setCurrentState(startState);
      }
  }
 
 
 
}
TOP

Related Classes of de.nameless.gameEngine.system.GameInputAdapter

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.