Home | History | Annotate | Download | only in combomoves
      1 /*
      2  * Copyright (c) 2009-2010 jMonkeyEngine
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  * * Redistributions of source code must retain the above copyright
     10  *   notice, this list of conditions and the following disclaimer.
     11  *
     12  * * Redistributions in binary form must reproduce the above copyright
     13  *   notice, this list of conditions and the following disclaimer in the
     14  *   documentation and/or other materials provided with the distribution.
     15  *
     16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
     17  *   may be used to endorse or promote products derived from this software
     18  *   without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 package jme3test.input.combomoves;
     34 
     35 import java.util.Arrays;
     36 import java.util.HashSet;
     37 import jme3test.input.combomoves.ComboMove.ComboMoveState;
     38 
     39 public class ComboMoveExecution {
     40 
     41     private static final float TIME_LIMIT = 0.3f;
     42 
     43     private ComboMove moveDef;
     44     private int state;
     45     private float moveTime;
     46     private boolean finalState = false;
     47 
     48     private String debugString = ""; // for debug only
     49 
     50     public ComboMoveExecution(ComboMove move){
     51         moveDef = move;
     52     }
     53 
     54     private boolean isStateSatisfied(HashSet<String> pressedMappings, float time,
     55                                      ComboMoveState state){
     56 
     57         if (state.getTimeElapsed() != -1f){
     58             // check if an appropriate amount of time has passed
     59             // if the state requires it
     60             if (moveTime + state.getTimeElapsed() >= time){
     61                 return false;
     62             }
     63         }
     64         for (String mapping : state.getPressedMappings()){
     65             if (!pressedMappings.contains(mapping))
     66                 return false;
     67         }
     68         for (String mapping : state.getUnpressedMappings()){
     69             if (pressedMappings.contains(mapping))
     70                 return false;
     71         }
     72         return true;
     73     }
     74 
     75     public String getDebugString(){
     76         return debugString;
     77     }
     78 
     79     public void updateExpiration(float time){
     80         if (!finalState && moveTime > 0 && moveTime + TIME_LIMIT < time){
     81             state    = 0;
     82             moveTime = 0;
     83             finalState = false;
     84 
     85             // reset debug string.
     86             debugString = "";
     87         }
     88     }
     89 
     90     /**
     91      * Check if move needs to be executed.
     92      * @param pressedMappings Which mappings are currently pressed
     93      * @param time Current time since start of app
     94      * @return True if move needs to be executed.
     95      */
     96     public boolean updateState(HashSet<String> pressedMappings, float time){
     97         ComboMoveState currentState = moveDef.getState(state);
     98         if (isStateSatisfied(pressedMappings, time, currentState)){
     99             state ++;
    100             moveTime = time;
    101 
    102             if (state >= moveDef.getNumStates()){
    103                 finalState = false;
    104                 state = 0;
    105 
    106                 moveTime = time+0.5f; // this is for the reset of the debug string only.
    107                 debugString += ", -CASTING " + moveDef.getMoveName().toUpperCase() + "-";
    108                 return true;
    109             }
    110 
    111             // the following for debug only.
    112             if (currentState.getPressedMappings().length > 0){
    113                 if (!debugString.equals(""))
    114                     debugString += ", ";
    115 
    116                 debugString += Arrays.toString(currentState.getPressedMappings()).replace(", ", "+");
    117             }
    118 
    119             if (moveDef.useFinalState() && state == moveDef.getNumStates() - 1){
    120                 finalState = true;
    121             }
    122         }
    123         return false;
    124     }
    125 
    126 }
    127