IndividualCharacter.java :  » Game » spritebasedrpg » com » rpg » battle » Android Open Source

Android Open Source » Game » spritebasedrpg 
spritebasedrpg » com » rpg » battle » IndividualCharacter.java
/**
 * 
 * This is an abstract class for any one character.
 * 
 */
package com.rpg.battle;

import java.io.InputStream;

import com.rpg.Drawable_I;
import com.rpg.R;

import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.CountDownTimer;
import android.test.suitebuilder.annotation.Suppress;
import android.util.Log;
import android.view.MotionEvent;

/**
 * @author christ66
 *
 */

//TODO: Remove this unused warning in near future. Next step is to implement statuses for every character
@SuppressWarnings("unused") 
public class IndividualCharacter implements Drawable_I {
  public IndividualCharacter(Context context, int id, int hp, int mp) {
    this.context = context;
    imageID = id;
    totalHitPoints = hitPoints = hp;
    manaPower = mp;
    characterState = new CharacterState();
    InputStream is = context.getResources().openRawResource(id);  
    character = Movie.decodeStream(is);
    x = y = 100;
    ATTACKPAINT.setARGB(255, 137, 207, 240);
  }
  
  public void setStartPosition(int x, int y) {
    this.x = startx = x;
    this.y = starty = y;
    
  }
  
  private void moveToAttacked() {
    if (attacked != null) {
      if (x > attacked.x) {
        x-=2;
      }
      else if (x < attacked.x) {
        x+=2;
      }
      
      if (y > attacked.y) {
        y-=2;
      }
      else if (y < attacked.y) {
        y+=2;
      }
      
      if (x == attacked.x && y == attacked.y) {
        x = startx;
        y = starty;
        readyToAttack = false;
        attacking = false;
        Log.d("IndividualCharacter", hitPoints + " / " + totalHitPoints);
        attacked.inflictDamage(attackDamage);
        startTimer();
        attacked = null;
      }
    }
  }
  
  public int getAttackDamage() {
    return attackDamage;
  }
    
  public void onDraw(Canvas canvas) {
     long now = android.os.SystemClock.uptimeMillis();  
    
     int relTime = (int)(now % character.duration()) ;
     
     character.setTime(relTime);
     
     if (readyToAttack && !attacking) {
       canvas.drawOval(new RectF(x, y + (character.height() / 2), x + character.width(), y + character.height()), ATTACKPAINT);
     }
     
     if (attacking) {
       moveToAttacked();
     }     
     
     character.draw(canvas, x, y);
  }
  
  public void inflictDamage(int damage) {
      hitPoints -= damage;
    if (hitPoints <= 0) {
      hitPoints = 0;
      changeSprite(R.drawable.cronodead);
      System.out.println("Character died!");
    }
  }
  
  private void changeSprite(int id) {
    try {
      InputStream is = context.getResources().openRawResource(id);  
      character = Movie.decodeStream(is);
    }
    catch (NotFoundException e) {
      System.out.println("Character" + id + " not found.");
      e.printStackTrace();
    }
  }
  
  /*
   * Start the timer before they are able to attack.
   */
  public void startTimer() {
      new CountDownTimer(5000, 10000) {

        @Override
        public void onFinish() {
          readyToAttack = true;
        }

        @Override
        public void onTick(long millisUntilFinished) {
          // TODO Auto-generated method stub
        }
        
      }.start();
  }
  
  public boolean getReadyToAttack() {
    return readyToAttack;
  }
  
  public void attack(IndividualCharacter attacked) {
    attacking = true;
    this.attacked = attacked;
  }
  
  public void setAttacked(IndividualCharacter attacked) {
    this.attacked = attacked;
  }

  
  
  Context context;
  
  IndividualCharacter attacked;
  Movie character;
  private boolean readyToAttack = false; 
  private boolean attacking = false;
  private int x, y, startx, starty;
  private int attackDamage = 10;
  private int imageID;
  private int totalHitPoints;
  private int manaPower;
  private CharacterState characterState;
  private int time;
  private int hitPoints;
  private final Paint ATTACKPAINT = new Paint();
  public boolean gotTouched(MotionEvent event) {
    if ((int)event.getX() > x - 50 && (int)event.getX() < x + 50 &&
        (int)event.getY() > y - 50 && (int)event.getY() < y + 50 && hitPoints != 0) {
      System.out.println("Someone got touched");
      return true;
    }
    return false;
  }

  public void prepareForAttack() {
    // TODO Auto-generated method stub
    attacking = true;
  }
}
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.