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  {

  /** The size of the sprite - used for finding the centre */
    public final int ENTITY_SPRITE_SIZE = 32;
 
    /** The speed of movement */
    public final float MOVE_SPEED = 0.08f;
   
    /** The x position in tiles */
    private float x;
    /** The y position in tiles */
    private float y;
   
    private SpriteSheet sheet;
   
    //private String ID;
   
    /** The animation of entity */
    private Animation animWalkUp;
    private Animation animWalkDown;
    private Animation animWalkLeft;
    private Animation animWalkRight;

    private Rectangle me;
   
    private boolean toRemove = false;
   
    protected boolean moveUp;// = false;
    protected boolean moveDown = true;
    protected boolean moveLeft;// = false;
    protected boolean moveRight;// = false;
   
    public String lastDirection = Helper.DOWN_DIR;

   
    /**
     * 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 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) 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
        setAnimWalkUp(setAnimationFromSheet(sheet,rigaUp,startNumAnimazioni,numAnimazioni));
        setAnimWalkRight(setAnimationFromSheet(sheet,rigaRight,startNumAnimazioni,numAnimazioni));
        setAnimWalkDown(setAnimationFromSheet(sheet,rigaDown,startNumAnimazioni,numAnimazioni));
        setAnimWalkLeft(setAnimationFromSheet(sheet,rigaLeft,startNumAnimazioni,numAnimazioni));

        // per gestione collisioni
    me = new Rectangle(x,y,ENTITY_SPRITE_SIZE,ENTITY_SPRITE_SIZE) ;
   
  }

    /**
     * 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;
    }
 
 
  /**
   * 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;
  }

  public float getX() {
    return x;
  }

  public float getY() {
    return y;
  }

  public Animation getPlayerWalkDown() {
    return animWalkDown;
  }

  public Animation getPlayerWalkLeft() {
    return animWalkLeft;
  }

  public Animation getPlayerWalkRight() {
    return animWalkRight;
  }

  public Animation getPlayerWalkUp() {
    return animWalkUp;
  }

  public void draw(Graphics g){
        // work out the centre of the entity in rendering coordinates and then
        // spit onto the screen
        int cx = (int) (x * 32);
        int cy = (int) (y * 32);
        //g.rotate(cx,cy,rot);
       
        getPlayerWalkDown().draw(cx-16,cy-16);       
       
        if (moveRight)
          getPlayerWalkRight().draw(cx-16,cy-16);
        if (moveLeft)
          getPlayerWalkLeft().draw(cx-16,cy-16);
        if (moveUp)
          getPlayerWalkUp().draw(cx-16,cy-16);
        if (moveDown)
          getPlayerWalkDown().draw(cx-16,cy-16);         
  }

  /**
   * 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) {
   
    Rectangle him = other.me;
   
    me.setBounds((int) x*32,(int) y*32,ENTITY_SPRITE_SIZE/2,ENTITY_SPRITE_SIZE/2);
   
    him.setBounds((int) other.getX()*32,(int) other.getY()*32,other.ENTITY_SPRITE_SIZE/2,other.ENTITY_SPRITE_SIZE/2);
   
    return me.intersects(him);
  }
 
  /**
   * Notification that this entity collided with another.
   *
   * @param other The entity with which this entity collided.
   */
  public void collidedWith(Entity other){
    toRemove = true;
  }

  public boolean isToRemove() {
    return toRemove;
  }

    public void setMovementRight(){
    moveRight = true;
    moveLeft = false;
    moveUp = false;
    moveDown = false;
    lastDirection = Helper.RIGHT_DIR;
    //notifyMovement();
    }

    public void setMovementLeft(){
    moveRight = false;
    moveLeft = true;
    moveUp = false;
    moveDown = false;
    lastDirection = Helper.LEFT_DIR;
    //notifyMovement();
    }
   
    public void setMovementUp(){
    moveRight = false;
    moveLeft = false;
    moveUp = true;
    moveDown = false;
    lastDirection = Helper.UP_DIR;
    //notifyMovement();
    }

    public void setMovementDown(){
    moveRight = false;
    moveLeft = false;
    moveUp = false;
    moveDown = true;
    lastDirection = Helper.DOWN_DIR;
    //notifyMovement();
    }

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

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

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

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

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

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

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

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

}
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.