Android Open Source - ninja-trials Shuriken Hands






From Project

Back to project page ninja-trials.

License

The source code is released under:

Apache License

If you think the Android project ninja-trials 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 com.madgear.ninjatrials.trials.shuriken;
//from ww  w.  ja v a  2  s.  c  o  m
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.entity.Entity;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;

import android.util.Log;

import com.madgear.ninjatrials.managers.GameManager;
import com.madgear.ninjatrials.managers.ResourceManager;
import com.madgear.ninjatrials.managers.SFXManager;
import com.madgear.ninjatrials.trials.TrialSceneShuriken;

public class ShurikenHands extends Entity{
  private final float SCRNWIDTH = ResourceManager.getInstance().cameraWidth;
  private final float SCRNHEIGHT = ResourceManager.getInstance().cameraHeight;
  private ShurikenCoordinates coordinates;
  private Sprite[] handsSprites = new Sprite[3];
  private boolean ignoreInputBecauseMoving = false;
  private float movementDistanceDelta = SCRNWIDTH/1920*25; // pixels
  private float movementTimeDelta = .020f; // seconds
  private int movementAnimationExtraTimeMargin = 13; // miliseconds
  private IUpdateHandler shurikenUpdateHandler;
  private float shurikenLaunchTime;
  private AnimatedSprite hands;
  private int shurikenAnimationCounter;
  private boolean movingLeft = false;
  private boolean movingRight = false;
  private boolean busyHands = false; // flag to prevent multiple keypress events when holding the button.
  
  public ShurikenHands() {
    float posX = SCRNWIDTH/2;
    float posY = SCRNHEIGHT/1080*160f;
    this.coordinates = new ShurikenCoordinates(posX, posY);
    ITiledTextureRegion handsITTR;
    if (GameManager.getSelectedCharacter() == GameManager.CHAR_SHO) {
      handsITTR = ResourceManager.getInstance().shurikenShoHands;
        }
        else if (GameManager.getSelectedCharacter() == GameManager.CHAR_RYOKO) {
          handsITTR = ResourceManager.getInstance().shurikenRyokoHands;
        }
        else {
          handsITTR = ResourceManager.getInstance().shurikenShoHands;
          Log.d("Bruno", "Warning: selected character unknown, using Sho as default.");
        }
    hands = new AnimatedSprite(posX, posY, handsITTR, ResourceManager.getInstance().engine.getVertexBufferObjectManager());
    hands.setCurrentTileIndex(2);        
    attachChild(hands);
    Timer timer = new Timer();
    timer.schedule(new HandsMovementTrigger(this), 50);
  }
  public void moveLeft() {
    movingLeft = true;    
  }
  public void moveRight() {
    movingRight = true;
  }
  public void stop() {
    movingLeft = false;
    movingRight = false;
  }
  public void hide() {
    // TODO Las oculta lentamente por la parte inferior de la pantalla.
  }
  public ShurikenCoordinates getPosition() {
    return this.coordinates;
  }
  public void launch() {
    /*
     * el lanzamiento es puramente vertical
     * los shurikens se destruyen tras impactar/perderse
     * pueden simultanearse varios lanzamientos (no hay bloqueo)
     * la velocidad vertical es constante (shurikenSpeed)
     */  
    if (!busyHands) {
      busyHands = true;
      hands.animate(200, 0);
      ShurikenShuriken shuriken = new ShurikenShuriken();
      attachChild(shuriken);
      shuriken.launch(this.coordinates.x);
      SFXManager.playSound(ResourceManager.getInstance().trialShurikenThrowing);
      if (generateCustomRandom(.25f)) {
        if (GameManager.getSelectedCharacter() == GameManager.CHAR_SHO) {
          SFXManager.playSound(ResourceManager.getInstance().shoShurikenThrow);
            }
            else if (GameManager.getSelectedCharacter() == GameManager.CHAR_RYOKO) {
              SFXManager.playSound(ResourceManager.getInstance().ryokoShurikenThrow);
            }
            else {
              SFXManager.playSound(ResourceManager.getInstance().shoShurikenThrow);
              Log.d("Bruno", "Warning: selected character unknown, using Sho as default.");
            }
      }
    }    
  }
  public void releaseHands() {
    busyHands = false;
  }
  private boolean generateCustomRandom(float chanceOfTrue) {
    Random randomGenerator = new Random();
    float n = randomGenerator.nextFloat();
    return (n > 1 - chanceOfTrue);
  }
  
  
  /**
   * Aux. class for class Hands.
   * TimerTask that enables input
   */
  private class HandsMovementWaiter extends TimerTask {    
    ShurikenHands hands;
    
    public HandsMovementWaiter(ShurikenHands hands){
      super();
      this.hands = hands;
    }
    
    @Override
    public void run() {
      hands.ignoreInputBecauseMoving = false;      
    }    
  }
  private class HandsMovementTrigger extends TimerTask {    
    ShurikenHands hands;
    
    public HandsMovementTrigger(ShurikenHands hands){
      super();
      this.hands = hands;
    }
    
    @Override
    public void run() {
      if (hands.movingLeft) {
        TrialSceneShuriken.moveSprite(hands.hands, coordinates.x, coordinates.y, coordinates.x - movementDistanceDelta, coordinates.y, movementTimeDelta);
        coordinates.x = coordinates.x - movementDistanceDelta;
      }
      else if (hands.movingRight) {
        TrialSceneShuriken.moveSprite(hands.hands, coordinates.x, coordinates.y, coordinates.x + movementDistanceDelta, coordinates.y, movementTimeDelta);
        coordinates.x = coordinates.x + movementDistanceDelta;
      }
      Timer timer = new Timer();
      timer.schedule(new HandsMovementTrigger(hands), 25);
    }    
  }
}




Java Source Code List

com.madgear.ninjatrials.AchievementsScene.java
com.madgear.ninjatrials.CharacterIntroScene.java
com.madgear.ninjatrials.ControllerOptionsScene.java
com.madgear.ninjatrials.DummyMenu.java
com.madgear.ninjatrials.GameScene.java
com.madgear.ninjatrials.IUserInput.java
com.madgear.ninjatrials.MainMenuScene.java
com.madgear.ninjatrials.MainOptionsScene.java
com.madgear.ninjatrials.ManagedLayer.java
com.madgear.ninjatrials.ManagedScene.java
com.madgear.ninjatrials.MapScene.java
com.madgear.ninjatrials.NinjaTrials.java
com.madgear.ninjatrials.PlayerSelectionScene.java
com.madgear.ninjatrials.RecordsScene.java
com.madgear.ninjatrials.ResultLoseScene.java
com.madgear.ninjatrials.ResultTrial.java
com.madgear.ninjatrials.ResultWinScene.java
com.madgear.ninjatrials.achievements.AchievementSetNinjaTrial.java
com.madgear.ninjatrials.achievements.AchievementSet.java
com.madgear.ninjatrials.achievements.Achievement.java
com.madgear.ninjatrials.hud.AchievementNotify.java
com.madgear.ninjatrials.hud.Chronometer.java
com.madgear.ninjatrials.hud.GameHUD.java
com.madgear.ninjatrials.hud.HeadCharacter.java
com.madgear.ninjatrials.hud.PowerBar.java
com.madgear.ninjatrials.hud.PrecisionAngleBar.java
com.madgear.ninjatrials.hud.PrecisionBar.java
com.madgear.ninjatrials.hud.SelectionStripe.java
com.madgear.ninjatrials.hud.ShurikenEnemyCounter.java
com.madgear.ninjatrials.hud.VolumeBar.java
com.madgear.ninjatrials.layers.GameOverLayer.java
com.madgear.ninjatrials.managers.GameManager.java
com.madgear.ninjatrials.managers.ResourceManager.java
com.madgear.ninjatrials.managers.SFXManager.java
com.madgear.ninjatrials.managers.SceneManager.java
com.madgear.ninjatrials.managers.UserData.java
com.madgear.ninjatrials.records.Record.java
com.madgear.ninjatrials.records.RecordsTableSet.java
com.madgear.ninjatrials.records.RecordsTable.java
com.madgear.ninjatrials.sequences.CreditsScene.java
com.madgear.ninjatrials.sequences.EndingScene.java
com.madgear.ninjatrials.sequences.EndingSequenceRyokoEasy.java
com.madgear.ninjatrials.sequences.EndingSequenceShoEasy.java
com.madgear.ninjatrials.sequences.Intro1Scene.java
com.madgear.ninjatrials.sequences.Intro2Scene.java
com.madgear.ninjatrials.sequences.Intro2SequenceRyokoEasy.java
com.madgear.ninjatrials.sequences.Intro2SequenceShoEasy.java
com.madgear.ninjatrials.sequences.Sequence.java
com.madgear.ninjatrials.sequences.SplashIntroScene.java
com.madgear.ninjatrials.trials.TrialSceneCut.java
com.madgear.ninjatrials.trials.TrialSceneJump.java
com.madgear.ninjatrials.trials.TrialSceneRun.java
com.madgear.ninjatrials.trials.TrialSceneShuriken.java
com.madgear.ninjatrials.trials.run.RunBg.java
com.madgear.ninjatrials.trials.run.RunCharacter.java
com.madgear.ninjatrials.trials.shuriken.ShurikenCoordinates.java
com.madgear.ninjatrials.trials.shuriken.ShurikenEnemy.java
com.madgear.ninjatrials.trials.shuriken.ShurikenHands.java
com.madgear.ninjatrials.trials.shuriken.ShurikenShuriken.java
com.madgear.ninjatrials.utils.AutoDiagonalParallaxBackground.java
com.madgear.ninjatrials.utils.AutoHorizontalParallaxBackground.java
com.madgear.ninjatrials.utils.AutoVerticalParallaxBackground.java
com.madgear.ninjatrials.utils.ParallaxBackground2d.java