Android Open Source - Gamework Hook






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 av a  2s  .co  m
import java.util.ArrayList;
import java.util.List;

import cz.robyer.gamework.scenario.BaseObject;
import cz.robyer.gamework.scenario.HookableObject;
import cz.robyer.gamework.scenario.Scenario;
import cz.robyer.gamework.scenario.reaction.Reaction;
import cz.robyer.gamework.scenario.variable.Variable;
import cz.robyer.gamework.utils.Log;

/**
 * Represents hook which is activated when some game state (variable, time,...) is changed.
 * @author Robert P?sel
 */
public class Hook extends BaseObject {
  private static final String TAG = Hook.class.getSimpleName();

  public static enum HookType {AREA, AREA_ENTER, AREA_LEAVE, VARIABLE, TIME, EVENT, SCANNER};  
  public static enum HookConditions {NONE, ANY, ALL};
  
  public static final int RUN_ALWAYS = -1;
  
  protected HookType type;
  protected HookConditions conditions_type;
  protected int runs;
  protected String value; 
  protected String reaction;
  protected List<Condition> conditions;
  protected HookableObject parent;
  
  protected Reaction react;
  
  /**
   * Class constructor.
   * @param type
   * @param value
   * @param reaction
   * @param conditions_type
   * @param runs
   */
  public Hook(HookType type, String value, String reaction, HookConditions conditions_type, int runs) {
    super();
    this.type = type;
    this.value = value;
    this.reaction = reaction;
    this.conditions_type = conditions_type;
    this.runs = (runs > 0 ? runs : RUN_ALWAYS);
  }

  /**
   * Class constructor.
   * @param type
   * @param value
   * @param reaction
   * @param conditions_type
   */
  public Hook(HookType type, String value, String reaction, HookConditions conditions_type) {
    this(type, value, reaction, conditions_type, RUN_ALWAYS);
  }
  
  /**
   * Class constructor.
   * @param type
   * @param value
   * @param reaction
   */
  public Hook(HookType type, String value, String reaction) {
    this(type, value, reaction, HookConditions.NONE, RUN_ALWAYS);
  }
  
  /* (non-Javadoc)
   * @see cz.robyer.gamework.scenario.BaseObject#setScenario(cz.robyer.gamework.scenario.Scenario)
   */
  @Override
  public void setScenario(Scenario scenario) {
    super.setScenario(scenario);
  
    if (conditions != null)
      for (Condition c : conditions) {
        c.setScenario(scenario);
      }
  }
  
  /* (non-Javadoc)
   * @see cz.robyer.gamework.scenario.BaseObject#onScenarioLoaded()
   */
  @Override
  public boolean onScenarioLoaded() {
    react = scenario.getReaction(reaction);
    if (react == null)
      Log.e(TAG, String.format("Reaction '%s' is null", reaction));
    
    boolean ok = react != null;
    
    if (conditions != null)
      for (Condition c : conditions) {
        if (!c.onScenarioLoaded())
          ok = false;
      }
    
    return ok;
  }
  
  /**
   * Sets hook parent on which is attached.
   * @param parent
   */
  public void setParent(HookableObject parent) {
    this.parent = parent;
  }
  
  /**
   * Returns hook parent.
   */
  public HookableObject getParent() {
    if (parent == null) {
      Log.e(TAG, "No parent is attached");
      throw new RuntimeException();
    }

    return parent;
  }
  
  /**
   * Returns hook type.
   * @return type
   */
  public HookType getType() {
    return type;
  }
  
  /**
   * Returns hook value.
   * @return value
   */
  public String getValue() {
    return value;
  }

  /**
   * Add condition into list.
   * @param condition
   */
  public void addCondition(Condition condition) {
    if (condition == null) {
      Log.w(TAG, "addCondition() with null condition");
      return;
    }
    
    if (conditions == null)
      conditions = new ArrayList<Condition>();
    
    if (isAttached())
      condition.setScenario(scenario);
    
    condition.setParent(this);
    conditions.add(condition);
  }

  /**
   * Run reaction if conditions and other conditions (runs) are valid.
   * @param variable which was changed (and which runned this hook)
   */
  public void call(Variable variable) {
    if (react == null) {
      Log.e(TAG, "Reaction to call is null");
      return;
    }
    
    boolean valid = false;
      
    switch (conditions_type) {
    case NONE:
      valid = true;
      Log.v(TAG, "Checking NO conditions");
      break;
    case ALL:
      valid = true;
      if (conditions != null)
        for (Condition condition : conditions) {
          if (!condition.isValid(variable)) {
            valid = false;
            break;
          }
        }
      Log.v(TAG, "Checking ALL conditions, valid=" + valid);
      break;
    case ANY:
      if (conditions != null)
        for (Condition condition : conditions) {
          if (condition.isValid(variable)) {
            valid = true;
            break;
          }
        }
      Log.v(TAG, "Checking ANY conditions, valid=" + valid);
      break;
    }
    
    if (runs != RUN_ALWAYS) {
      if (runs > 0) {
        runs--;
      } else {
        valid = false;
      }
    }
    
    if (valid)
      react.action();
  }

}




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