Android Open Source - Gamework Reference Reaction






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.reaction;
// www  . ja v  a  2  s  . c  om
import android.util.Log;

/**
 * Game reaction which only activate another reaction.
 * @author Robert P?sel
 */
public class ReferenceReaction extends Reaction {
  protected String value;
  protected Reaction reaction;
  
  /**
   * Class constructor.
   * @param id Identificator of this reaction.
   * @param value Identificator of reaction which should be activated.
   */
  public ReferenceReaction(String id, String value) {
    super(id);
    this.value = value;
  }
  
  /**
   * Checks if given Reaction can be cross referenced to this. It works also recursively for MultiReactions.
   * @param reaction MultiReaction to check cross reference.
   * @return Reaction which points to this reaction or null if there isn's any.
   */
  private Reaction checkCrossReference(Reaction reaction, int num) {
    if (num > 10) {
      Log.e(TAG, "There are too many nested reactions, most likely due to cross references.");
      return reaction;
    }
    
    if (reaction instanceof ReferenceReaction) {
      String value = ((ReferenceReaction)reaction).value;
      
      if (value.equalsIgnoreCase(id)) // ! this IF must be before the other one
        return reaction; // cross-reference with this reaction
      
      if (value.equalsIgnoreCase(reaction.getId()))
        return null; // self-reference of that reaction

      reaction = getScenario().getReaction(value);
      return checkCrossReference(reaction, num+1);
    } else if (reaction instanceof MultiReaction) {
      for (Reaction r : ((MultiReaction)reaction).getReactions()) {
        Reaction crossref = checkCrossReference(r, num+1);
        if (crossref != null)
          return crossref;
      }
    }
    return null;
  }
  
  /* (non-Javadoc)
   * @see cz.robyer.gamework.scenario.BaseObject#onScenarioLoaded()
   */
  @Override
  public boolean onScenarioLoaded() {
    reaction = getScenario().getReaction(value);
    if (reaction == null)
      Log.e(TAG, String.format("Reaction '%s' is null", value));
    
    // check cross references
    Reaction crossReaction = checkCrossReference(reaction, 0);
    if (crossReaction != null) {
      Log.e(TAG, "This reaction is cross-referenced with reaction id='" + crossReaction.getId() + "'");
      reaction = null;
    }
    
    return reaction != null;
  }

  /* (non-Javadoc)
   * @see cz.robyer.gamework.scenario.reaction.Reaction#action()
   */
  @Override
  public void action() {
    if (reaction == null) {
      Log.e(TAG, String.format("Reaction '%s' is null", value));
      return;
    }
    
    reaction.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