Package it.timehero.entities

Source Code of it.timehero.entities.Entity

/**
* TimeHero - http://timehero.sf.net
*
* @license See LICENSE
*/
package it.timehero.entities;

import it.timehero.actors.Actor;
import it.timehero.util.Helper;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.geom.Rectangle;


/**
* Un entit� di gioco: parte visuale di un elemento dinamico di gioco
*
* @author AM
* @see Actor
*/
public class Entity  {

  private Animation animAttackDown;
 
    private Animation animAttackLeft;
   
    private Animation animAttackRight;
    private Animation animAttackUp;
   
    private Animation animWalkDown;
   
    //private String ID;
   
    private Animation animWalkLeft;
   
    private Animation animWalkRight;
    /** The animation of entity */
    private Animation animWalkUp;
    private boolean attackDown;
    private boolean attackLeft;
   
    private boolean attackRight;
    protected boolean attackUp;
    /** The size of the sprite - used for finding the centre */
    public final int ENTITY_SPRITE_SIZE = 32;
    private boolean firstMove = true;   

    public String lastDirection = null; //= Helper.DOWN_DIR;
   
    private Rectangle me;
   
    /** The speed of movement */
    //public final float MOVE_SPEED = 0.08f;
    protected boolean moveDown;// = true;
    protected boolean moveLeft;// = false;
    protected boolean moveRight;// = false;
   
    protected boolean moveUp;// = false;
   
    private SpriteSheet sheet;

  private boolean toRemove = false;

  /** The x position in tiles */
    private float x;

  /** The y position in tiles */
    private float y;

   
    /**
     * Crea l'entit� coi parametri richiesti
     *
     * @param x - tileset x iniziale
     * @param y - tileset y iniziale
     * @param percorsoSpriteSheet  - percorso dove trovare la grafica
     * @param dimSHX - dimensione X stilesheet
     * @param dimSHY - dimensione Y stilesheet
     * @param rigaUp - indice nell'animazione per movimento up
     * @param rigaDown - indice nell'animazione per movimento down
     * @param rigaRight - indice nell'animazione per movimento right
     * @param rigaLeft - indice nell'animazione per movimento left
     * @param startNumAnimazioni - colonna da cui si parte per leggere l'animazione     *
     * @param numAnimazioni - numero di animazioni (da 0) da cui leggere l'animazione
     * @throws SlickException - se non viene trovata la grafica dell'animazione
     */
  public Entity(int x,int y,
        String percorsoSpriteSheet,
        int dimSHX, int dimSHY,
        int rigaUp, int rigaDown,
        int rigaRight,  int rigaLeft,
        int startNumAnimazioni, int numAnimazioni,
        int rigaAttackUp, int rigaAttackDown,
        int rigaAttackRight, int rigaAttackLeft,
        int startNumAnimazioniAttack, int numAnimazioniAttack
  ) throws SlickException{
   
    this.x = x;
    this.y = y;
   
      // load the sprites and tiles, note that underneath the texture
        // will be shared between the sprite sheet and tilemap
      // x e y dell'animazione!
        sheet = new SpriteSheet(percorsoSpriteSheet,dimSHX, dimSHY);
       
        // animazione movimento
        setAnimWalkUp(setAnimationFromSheet(sheet,rigaUp,startNumAnimazioni,numAnimazioni));
        setAnimWalkRight(setAnimationFromSheet(sheet,rigaRight,startNumAnimazioni,numAnimazioni));
        setAnimWalkDown(setAnimationFromSheet(sheet,rigaDown,startNumAnimazioni,numAnimazioni));
        setAnimWalkLeft(setAnimationFromSheet(sheet,rigaLeft,startNumAnimazioni,numAnimazioni));
       
        // animazioni attacco
        setAnimAttackUp(setAnimationFromSheet(sheet,rigaAttackUp, startNumAnimazioniAttack,numAnimazioniAttack));
        setAnimAttackRight(setAnimationFromSheet(sheet,rigaAttackRight, startNumAnimazioniAttack,numAnimazioniAttack));
        setAnimAttackDown(setAnimationFromSheet(sheet,rigaAttackDown, startNumAnimazioniAttack,numAnimazioniAttack));       
        setAnimAttackLeft(setAnimationFromSheet(sheet,rigaAttackLeft, startNumAnimazioniAttack,numAnimazioniAttack));
       
        // per gestione collisioni
    me = new Rectangle(x,y,ENTITY_SPRITE_SIZE,ENTITY_SPRITE_SIZE) ;
   
  }

  /**
   * aggiunge il paramentro alla coordinata x
   * @param addX
   */
  public void addHorizontalMovement(float addX){
    x = x+ addX;
  }
 
 
    /**
   * aggiunge il paramentro alla coordinata y
   * @param y
   */
  public void addVerticalMovement(float addY){
    y = y+ addY;
  }

  /**
   * Notification that this entity collided with another.
   *
   * @param other The entity with which this entity collided.
   * @deprecated la rimozione di un'entit� � gestita a livello di attore/scheda
   */
  public void collidedWith(Entity other){
    //toRemove = false;
  }

  /**
   * Check if this entity collised with another.
   *
   * @param other The other entity to check collision against
   * @return True if the entities collide with each other
   */
  public boolean collidesWith(Entity other, float x, float y) {
   
    Rectangle him = other.me;
   
    Rectangle collision = new Rectangle(me.getX()+x,me.getY()+y,ENTITY_SPRITE_SIZE,ENTITY_SPRITE_SIZE);
   
    //me.setBounds( me.getX()+x, me.getY()+y,ENTITY_SPRITE_SIZE/2,ENTITY_SPRITE_SIZE/2);
   
    //him.setBounds( other.getX()*32 , other.getY()*32 ,other.ENTITY_SPRITE_SIZE/2,other.ENTITY_SPRITE_SIZE/2);
    if (collision.intersects(him)){
      this.collidedWith(other);
      return true;
    } else {
      return false;
    }
  }

  public void draw(Graphics g){
        // work out the centre of the entity in rendering coordinates and then
        // spit onto the screen
        float cx =  (x * 32);
        float cy =  (y * 32);

    me.setBounds( cx-4, cy-4,ENTITY_SPRITE_SIZE-8,ENTITY_SPRITE_SIZE-8);
   
    // riattivare per avere bordo di collisione dell'entit�
    //g.draw(me);
       
        // aggiunto per risolvere baco mostrava gigi sempre in basso anche quando camminava
        // bisogna mostrare qualcosa se il giocatore non si muove!
        if (firstMove){
            getPlayerWalkDown().draw(cx-4,cy-16);         
        }
       
        //attacking
        if (attackUp){
          getAnimAttackUp().draw(cx-4,cy-16);
        }
        if (attackDown){
          getAnimAttackDown().draw(cx-4,cy-16);
        }
        if (attackLeft){
          getAnimAttackLeft().draw(cx-4,cy-16);
        }
        if (attackRight){
          getAnimAttackRight().draw(cx-4,cy-16);
        }
       
        // moving
        if (moveRight)
          getPlayerWalkRight().draw(cx-4,cy-16);
        if (moveLeft)
          getPlayerWalkLeft().draw(cx-4,cy-16);
        if (moveUp)
          getPlayerWalkUp().draw(cx-4,cy-16);
        if (moveDown)
          getPlayerWalkDown().draw(cx-4,cy-16);         
  }

  /**
   * @return the animAttackDown
   */
  public Animation getAnimAttackDown() {
    return animAttackDown;
  }
 
  /**
   * @return the animAttackLeft
   */
  public Animation getAnimAttackLeft() {
    return animAttackLeft;
  }
 
 
  /**
   * @return the animAttackRight
   */
  public Animation getAnimAttackRight() {
    return animAttackRight;
  }

  /**
   * @return the animAttackUp
   */
  public Animation getAnimAttackUp() {
    return animAttackUp;
  }

  /**
   * @return the animWalkDown
   */
  public Animation getAnimWalkDown() {
    return animWalkDown;
  }

  /**
   * @return the animWalkLeft
   */
  public Animation getAnimWalkLeft() {
    return animWalkLeft;
  }

  /**
   * @return the animWalkRight
   */
  public Animation getAnimWalkRight() {
    return animWalkRight;
  }

  /**
   * @return the animWalkUp
   */
  public Animation getAnimWalkUp() {
    return animWalkUp;
  }

  /**
   * @return the lastDirection
   */
  public String getLastDirection() {
    return lastDirection;
  }

  public Animation getPlayerWalkDown() {
    return animWalkDown;
  }

  public Animation getPlayerWalkLeft() {
    return animWalkLeft;
  }

  public Animation getPlayerWalkRight() {
    return animWalkRight;
  }
 
  public Animation getPlayerWalkUp() {
    return animWalkUp;
  }

  public float getX() {
    return x;
  }

  public float getY() {
    return y;
  }
 
  public boolean isToRemove() {
    return toRemove;
  }

  /**
     * Ritorna animazione
     * @param sheet - sheet da cui prendere l'animazione
     * @param rigaSheet - riga da considerare per lo sheet
     * @param startNumFramAnimazione - colonna da cui si parte per leggere l'animazione (da 0)
     * @param numFramAnimazione - numero di colonna da cui leggere l'animazione
     * @return
     */
    private Animation setAnimationFromSheet(SpriteSheet sheet , int rigaSheet, int startNumFramAnimazione, int numFramAnimazione){
      Animation animation = new Animation();
        for (int frame=startNumFramAnimazione;frame<numFramAnimazione;frame++) {
          animation.addFrame(sheet.getSprite(frame,rigaSheet), 150);
        }
        animation.setAutoUpdate(false);
        return animation;
    }

  private void setAnimAttackDown(Animation setAnimationFromSheet) {
      this.animAttackDown = setAnimationFromSheet;   
  }

 
    private void setAnimAttackLeft(Animation setAnimationFromSheet) {
      this.animAttackLeft = setAnimationFromSheet;
  }

    private void setAnimAttackRight(Animation setAnimationFromSheet) {
      this.animAttackRight = setAnimationFromSheet;   
  }
   
    public void setAnimAttackUp(Animation setAnimationFromSheet) {
      this.animAttackUp = setAnimationFromSheet;
  }

    /**
   * @param animWalkDown the animWalkDown to set
   */
  public void setAnimWalkDown(Animation animWalkDown) {
    this.animWalkDown = animWalkDown;
  }

  /**
   * @param animWalkLeft the animWalkLeft to set
   */
  public void setAnimWalkLeft(Animation animWalkLeft) {
    this.animWalkLeft = animWalkLeft;
  }

  /**
   * @param animWalkRight the animWalkRight to set
   */
  public void setAnimWalkRight(Animation animWalkRight) {
    this.animWalkRight = animWalkRight;
  }

  /**
   * @param animWalkUp the animWalkUp to set
   */
  public void setAnimWalkUp(Animation animWalkUp) {
    this.animWalkUp = animWalkUp;
  }

  public void setAttackDown(){
    attackUp = false;
    attackDown = true;
    attackLeft = false;
    attackRight = false;
   
    moveRight = false;
    moveLeft = false;
    moveUp = false;
    moveDown = false;
  }

  public void setAttackLeft(){
    attackUp = false;
    attackDown = false;
    attackLeft = true;
    attackRight = false;
   
    moveRight = false;
    moveLeft = false;
    moveUp = false;
    moveDown = false;
  }

  public void setAttackRight(){
    attackUp = false;
    attackDown = false;
    attackLeft = false;
    attackRight = true;
   
    moveRight = false;
    moveLeft = false;
    moveUp = false;
    moveDown = false;
  }

  public void setAttackUp(){
    attackUp = true;
    attackDown = false;
    attackLeft = false;
    attackRight = false;
   
    moveRight = false;
    moveLeft = false;
    moveUp = false;
    moveDown = false;
  }

  public void setMovementDown(){
      if (firstMove) firstMove = false;
    attackUp = false;  
    attackDown = false;
    attackLeft = false;
    attackRight = false;

    moveRight = false;
    moveLeft = false;
    moveUp = false;
    moveDown = true;
    lastDirection = Helper.DOWN_DIR;
    }

  public void setMovementLeft(){
      if (firstMove) firstMove = false;
    attackUp = false;
    attackDown = false;
    attackLeft = false;
    attackRight = false;

    moveRight = false;
    moveLeft = true;
    moveUp = false;
    moveDown = false;
    lastDirection = Helper.LEFT_DIR;
    }

  public void setMovementRight(){
      if (firstMove) firstMove = false;
    attackUp = false;
    attackDown = false;
    attackLeft = false;
    attackRight = false;
   
    moveRight = true;
    moveLeft = false;
    moveUp = false;
    moveDown = false;
    lastDirection = Helper.RIGHT_DIR;
    }

  public void setMovementUp(){
      if (firstMove) firstMove = false;
    attackUp = false;
    attackDown = false;
    attackLeft = false;
    attackRight = false;

    moveRight = false;
    moveLeft = false;
    moveUp = true;
    moveDown = false;
    lastDirection = Helper.UP_DIR;
    }

  /**
   * Setta l'entit� corrente come da rimuovere (la uccide)
   */
  public void setToRemove(){
    toRemove = true;
  }


}
TOP

Related Classes of it.timehero.entities.Entity

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.