Android Open Source - Gamework Condition






From Project

Back to project page Gamework.

License

The source code is released under:

Apache License

If you think the Android project Gamework listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package cz.robyer.gamework.scenario.hook;
/*from   ww w.  j a v  a2 s .c o m*/
import cz.robyer.gamework.scenario.BaseObject;
import cz.robyer.gamework.scenario.variable.BooleanVariable;
import cz.robyer.gamework.scenario.variable.DecimalVariable;
import cz.robyer.gamework.scenario.variable.Variable;
import cz.robyer.gamework.utils.Log;

/**
 * Represents condition for {@link Hook}.
 * @author Robert P?sel
 */
public class Condition extends BaseObject {
  public static final String TAG = Condition.class.getSimpleName();
  
  public static enum ConditionType {EQUALS, NOTEQUALS, GREATER, SMALLER, GREATEREQUALS, SMALLEREQUALS};
  
  protected ConditionType type;
  protected String variable;
  protected String value;
  protected Hook parent;
  
  /**
   * Basic constructor.
   * @param type of condition
   * @param id of variable for condition, could be empty
   * @param value to be compared with value of variable
   */
  public Condition(ConditionType type, String variable, String value) {
    super();
    this.type = type;
    this.variable = variable;
    this.value = value;
  }
  
  /**
   * Returns type of this condition.
   * @return condition type
   */
  public ConditionType getType() {
    return type;
  }
  
  /**
   * Sets parent hook on which is this condition attached.
   * @param parent
   */
  public void setParent(Hook parent) {
    this.parent = parent;
  }
  
  /**
   * Returns parent hook of this condition.
   * @return hook or throws exception
   */
  public Hook getParent() {
    if (parent == null) {
      Log.e(TAG, "No parent is attached");
      throw new RuntimeException();
    }

    return parent;
  }
  
  /**
   * Checks if this condition pass.
   * @param variable which was changed (and which called our hook)
   * @return true if this condition is valid, false otherwise
   */
  public boolean isValid(Variable variable) {
    // If this condition has defined own variable, we use that
    if (this.variable != null) {
      Log.d(TAG, String.format("This condition uses own variable '%s'.", this.variable));
      variable = getScenario().getVariable(this.variable);
    }
    
    if (variable == null) {
      Log.e(TAG, "Variable to check is null");
      return false;
    }
    
    boolean valid = false;
    
    if (variable instanceof BooleanVariable) {
      boolean varValue = ((BooleanVariable)variable).getValue();
      boolean condValue = Boolean.parseBoolean(value);
      
      switch (type) {
      case EQUALS:
        valid = (varValue == condValue);
        break;
      case NOTEQUALS:
        valid = (varValue != condValue);
        break;
      default:
        break;
      }
      
      Log.v(TAG, "Checking condition on varValue='" + varValue + ", condValue='" + condValue + "', valid='" + valid + "'");
    } else if (variable instanceof DecimalVariable) {
      int varValue = ((DecimalVariable)variable).getValue();
      int condValue = Integer.parseInt(value);
      
      switch (type) {
      case EQUALS:
        valid = (varValue == condValue);
        break;
      case NOTEQUALS:
        valid = (varValue != condValue);
        break;
      case GREATER:
        valid = (varValue > condValue);
        break;
      case SMALLER:
        valid = (varValue < condValue);
        break;
      case GREATEREQUALS:
        valid = (varValue >= condValue);
        break;
      case SMALLEREQUALS:
        valid = (varValue <= condValue);
        break;
      }
      
      Log.v(TAG, "Checking condition on varValue='" + varValue + ", condValue='" + condValue + "', valid='" + valid + "'");
    }

    return valid;
  }

}




Java Source Code List

cz.robyer.gamework.app.activity.BaseActivity.java
cz.robyer.gamework.app.activity.BaseGameActivity.java
cz.robyer.gamework.app.activity.GameInventoryActivity.java
cz.robyer.gamework.app.activity.GameMapActivity.java
cz.robyer.gamework.app.activity.GameMessagesActivity.java
cz.robyer.gamework.app.activity.GameObjectivesActivity.java
cz.robyer.gamework.app.activity.GameToolsActivity.java
cz.robyer.gamework.app.activity.HelpActivity.java
cz.robyer.gamework.app.activity.MainActivity.java
cz.robyer.gamework.app.activity.MessageActivity.java
cz.robyer.gamework.app.game.GameService.java
cz.robyer.gamework.app.service.JavaScriptHandler.java
cz.robyer.gamework.app.service.MessageAdapter.java
cz.robyer.gamework.constants.Constants.java
cz.robyer.gamework.game.GameEventBroadcaster.java
cz.robyer.gamework.game.GameEventListener.java
cz.robyer.gamework.game.GameEvent.java
cz.robyer.gamework.game.GameHandler.java
cz.robyer.gamework.game.GameService.java
cz.robyer.gamework.game.GameStatus.java
cz.robyer.gamework.scenario.BaseObject.java
cz.robyer.gamework.scenario.HookableObject.java
cz.robyer.gamework.scenario.IdentificableObject.java
cz.robyer.gamework.scenario.ScenarioInfo.java
cz.robyer.gamework.scenario.Scenario.java
cz.robyer.gamework.scenario.area.Area.java
cz.robyer.gamework.scenario.area.MultiPointArea.java
cz.robyer.gamework.scenario.area.PointArea.java
cz.robyer.gamework.scenario.area.SoundArea.java
cz.robyer.gamework.scenario.helper.EventHookable.java
cz.robyer.gamework.scenario.helper.ScannerHookable.java
cz.robyer.gamework.scenario.helper.TimeHookable.java
cz.robyer.gamework.scenario.hook.Condition.java
cz.robyer.gamework.scenario.hook.Hook.java
cz.robyer.gamework.scenario.message.Message.java
cz.robyer.gamework.scenario.parser.XmlScenarioParser.java
cz.robyer.gamework.scenario.reaction.ActivityReaction.java
cz.robyer.gamework.scenario.reaction.EventReaction.java
cz.robyer.gamework.scenario.reaction.MessageReaction.java
cz.robyer.gamework.scenario.reaction.MultiReaction.java
cz.robyer.gamework.scenario.reaction.Reaction.java
cz.robyer.gamework.scenario.reaction.ReferenceReaction.java
cz.robyer.gamework.scenario.reaction.SoundReaction.java
cz.robyer.gamework.scenario.reaction.VariableReaction.java
cz.robyer.gamework.scenario.reaction.VibrateReaction.java
cz.robyer.gamework.scenario.variable.BooleanVariable.java
cz.robyer.gamework.scenario.variable.DecimalVariable.java
cz.robyer.gamework.scenario.variable.Variable.java
cz.robyer.gamework.utils.GPoint.java
cz.robyer.gamework.utils.Log.java
cz.robyer.gamework.utils.Utils.java