BackgroundMirror.java :  » Game » gaisma » com » game » Android Open Source

Android Open Source » Game » gaisma 
gaisma » com » game » BackgroundMirror.java
package com.game;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.widget.TextView;

public class BackgroundMirror {
  private Bitmap backgroundImg, cracked1, cracked2, cracked3, currentBG;
  private int thresholdC1,thresholdC2,thresholdC3, thresholdDead;
  private int borderSize;
  private int gameFPS;
  TextView fpsDisplay;
  
  private boolean isShaking;
  private int shakeSteps;
  private int shakeTime;
  
  private int bgX, bgY;

  private int mirrorLife;
  
  public BackgroundMirror(Context context, int imgRef, int cr1, int cr2, int cr3, int life)
  {
    //  Load the image
    backgroundImg = BitmapFactory.decodeResource(context.getResources(), imgRef);
    cracked1 = BitmapFactory.decodeResource(context.getResources(), cr1);
    cracked2 = BitmapFactory.decodeResource(context.getResources(), cr2);
    cracked3 = BitmapFactory.decodeResource(context.getResources(), cr3);
    currentBG = backgroundImg;
    setBorderSize(10);
    setGameFPS(0);
    
    
    shakeSteps = 0;
    shakeTime = 10;
    bgX = 0;
    bgY = 0;
    
    Reset(life);
    
    
  }
  
  public void Update()
  {
    if(isShaking)
    {
      int dir = shakeSteps %2;
      
      bgX = dir*2; 
      bgY = dir*2;
      
      shakeSteps++;
    }
    if(shakeSteps >= shakeTime)
    {
      setShaking(false);
      shakeSteps = 0;
      bgX = 0;
      bgY = 0;
    }
    
  }
  
  public void onDraw(Canvas canvas)
  {
    canvas.drawBitmap(currentBG, bgX, bgY, null);
  }
  
  public void HitMirror()
  {
    
    setShaking(true);
    
    mirrorLife--;
    // Select the apropiate img to draw
    ChooseMirrorImage();
  }
  private void ChooseMirrorImage()
  {
    if(mirrorLife >= thresholdC1) { currentBG = backgroundImg;}
    else if(mirrorLife < thresholdC1 && mirrorLife >= thresholdC2){currentBG = cracked1;}
    else if(mirrorLife < thresholdC2 && mirrorLife >= thresholdC3){currentBG = cracked2;}
    else {currentBG = cracked3;}
  }

  public void setBackgroundImg(Bitmap backgroundImg) {
    this.backgroundImg = backgroundImg;
  }

  public Bitmap getBackgroundImg() {
    return backgroundImg;
  }

  public void setBorderSize(int borderSize) {
    this.borderSize = borderSize;
  }

  public int getBorderSize() {
    return borderSize;
  }
  public int getLeftLimit() { return borderSize;}
  public int getRightLimit() { return backgroundImg.getWidth()- borderSize;}
  public int getTopLimit() { return borderSize;}
  public int getBottomLimit() { return backgroundImg.getHeight()- borderSize;}

  public void setGameFPS(int gameFPS) {
    this.gameFPS = gameFPS;
    //fpsDisplay.setText(gameFPS);
  }

  public int getGameFPS() {
    return gameFPS;
  }

  public void setShaking(boolean isShaking) {
    this.isShaking = isShaking;
  }

  public boolean isShaking() {
    return isShaking;
  }

  public void setMirrorLife(int mirrorLife) {
    this.mirrorLife = mirrorLife;
  }

  public int getMirrorLife() {
    return mirrorLife;
  }

  public void Reset(int life)
  {
    setShaking(false);
    setMirrorLife(life);
    // Calculate life thresholds
    thresholdC1 = (int) (mirrorLife/1.3);
    thresholdC2 = mirrorLife/2;
    thresholdC3 = mirrorLife/4;
    thresholdDead=0;
    
    ChooseMirrorImage();
  }
  
  public boolean Destroyed() { return (boolean) (mirrorLife <= thresholdDead); }
  
}
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.