Arrow.java :  » Game » zeldroid » com » zeldroid » Android Open Source

Android Open Source » Game » zeldroid 
zeldroid » com » zeldroid » Arrow.java
/* Author: Larry Spraggins
 * Date: 10/16/2010
 * 
 * Updated by: David Gibson
 * Date: 11/29/2010
 */

package com.zeldroid;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.RotateDrawable;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.media.MediaPlayer;

public class Arrow extends Item{

  private Drawable upAnimation;
  private Drawable downAnimation;
  private Drawable rightAnimation;
  private Drawable leftAnimation;
  private Drawable currentAnimation;
  
  public Arrow() {
    super();
    setX(32);
    setY(32);
    setDx(10);
    setDy(0);
    
    upAnimation = GameView.resources.getDrawable(R.drawable.arrowup);
    downAnimation = GameView.resources.getDrawable(R.drawable.arrowdown);
    rightAnimation = GameView.resources.getDrawable(R.drawable.arrowright);
    leftAnimation = GameView.resources.getDrawable(R.drawable.arrowleft);

    currentAnimation = rightAnimation;
  }
  
  public Arrow(int x, int y, int dx, int dy) {
    setX(x);
    setY(y);
    setDx(dx);
    setDy(dy);
    
    upAnimation = GameView.resources.getDrawable(R.drawable.linku1);
    downAnimation = GameView.resources.getDrawable(R.drawable.linkd1);
    rightAnimation = GameView.resources.getDrawable(R.drawable.linkr1);
    leftAnimation = GameView.resources.getDrawable(R.drawable.linkl1);

    if(getDx() > 0 && getDy() == 0){
      currentAnimation = rightAnimation;
    }
    else if(getDx() < 0 && getDy() == 0) {
      currentAnimation = leftAnimation;
    }
    else if(getDx() == 0 && getDy() > 0) {
      currentAnimation = downAnimation;
      
    }
    else {
      currentAnimation = upAnimation;
    }
  }
  
  public void update()
  {  
    if(getDx() > 0 && getDy() == 0)
    {
      currentAnimation = rightAnimation;
      setX(getX()+getDx());
    }
    else if(getDx() < 0 && getDy() == 0)
    {
      currentAnimation = leftAnimation;
      setX(getX()+getDx());
    }
    else if(getDx() == 0 && getDy() > 0)
    {
      currentAnimation = downAnimation;
      setY(getY()+getDy());
    }
    else if(getDx() == 0 && getDy() < 0)
    {
      currentAnimation = upAnimation;
      setY(getY()+getDy());
    }
  }
  
  public Rect getBounds()
  {
    return new Rect(this.getX(), this.getY(), this.getX() + 30, this.getY() + 30);
  }
  
  @Override
  public Drawable getSprite() {
    currentAnimation.setBounds(this.getBounds());
    return currentAnimation;
  }
}

/*
 * NOTES: 11/29/10 I have updated this class to animate the arrow when it is moving.
 * Arrows should not be spawned with the default Constructor, as it will always spawn an arrow
 * that fires to the right.
 * 
 * Use Arrow(int dx, int dy) and give the appropriate dx or dy to get the desired direction.
 * on Arrow.update() the arrow will move itself.  Collision detection must be done in GameView.
 */
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.