mirror of
				https://github.com/Klagarge/PokeHES.git
				synced 2025-11-04 13:19:17 +00:00 
			
		
		
		
	done
This commit is contained in:
		@@ -1,12 +1,155 @@
 | 
			
		||||
package Entity;
 | 
			
		||||
 | 
			
		||||
import com.badlogic.gdx.Gdx;
 | 
			
		||||
import com.badlogic.gdx.math.Interpolation;
 | 
			
		||||
import com.badlogic.gdx.math.Vector2;
 | 
			
		||||
 | 
			
		||||
import ch.hevs.gdx2d.components.bitmaps.Spritesheet;
 | 
			
		||||
import ch.hevs.gdx2d.lib.GdxGraphics;
 | 
			
		||||
 | 
			
		||||
public abstract class Character extends Entity{
 | 
			
		||||
    public Character(String name) {
 | 
			
		||||
        super(name);
 | 
			
		||||
        //TODO Auto-generated constructor stub
 | 
			
		||||
 | 
			
		||||
    public enum Direction{
 | 
			
		||||
        UP,
 | 
			
		||||
        DOWN,
 | 
			
		||||
        RIGHT,
 | 
			
		||||
        LEFT,
 | 
			
		||||
        NULL
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private int pv;
 | 
			
		||||
    /**
 | 
			
		||||
     * The currently selected sprite for animation
 | 
			
		||||
     */
 | 
			
		||||
    int textureX = 0;
 | 
			
		||||
    int textureY = 1;
 | 
			
		||||
    float speed = 1;
 | 
			
		||||
 | 
			
		||||
    float dt = 0;
 | 
			
		||||
    int currentFrame = 0;
 | 
			
		||||
    int nFrames = 4;
 | 
			
		||||
    final float FRAME_TIME = 0.1f; // Duration of each frime
 | 
			
		||||
    private String img;
 | 
			
		||||
 | 
			
		||||
    protected int pv;
 | 
			
		||||
 | 
			
		||||
    public Character(String name, int x, int y, String img){
 | 
			
		||||
        super(name, x, y);
 | 
			
		||||
        this.img = img;
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void init() {
 | 
			
		||||
        super.init();
 | 
			
		||||
        ss = new Spritesheet("./resources/" + img + ".png", SPRITE_WIDTH, SPRITE_HEIGHT);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void graphicRender(GdxGraphics g) {
 | 
			
		||||
        super.graphicRender(g);
 | 
			
		||||
        animate(Gdx.graphics.getDeltaTime());
 | 
			
		||||
		draw(g);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Update the position and the texture of the hero.
 | 
			
		||||
     * @param elapsedTime The time [s] elapsed since the last time which this method was called.
 | 
			
		||||
     */
 | 
			
		||||
    public void animate(double elapsedTime) {
 | 
			
		||||
        float frameTime = FRAME_TIME / speed;
 | 
			
		||||
 | 
			
		||||
        position = new Vector2(lastPosition);
 | 
			
		||||
        if(isMoving()) {
 | 
			
		||||
            dt += elapsedTime;
 | 
			
		||||
            float alpha = (dt+frameTime*currentFrame)/(frameTime*nFrames);
 | 
			
		||||
 | 
			
		||||
            position.interpolate(newPosition, alpha,Interpolation.linear);
 | 
			
		||||
        }else{
 | 
			
		||||
            dt = 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (dt > frameTime) {
 | 
			
		||||
            dt -= frameTime;
 | 
			
		||||
            currentFrame = (currentFrame + 1) % nFrames;
 | 
			
		||||
 | 
			
		||||
            if(currentFrame == 0){
 | 
			
		||||
                move = false;
 | 
			
		||||
                lastPosition = new Vector2(newPosition);
 | 
			
		||||
                position = new Vector2(newPosition);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return True if the entity is actually doing a step.
 | 
			
		||||
     */
 | 
			
		||||
    public boolean isMoving(){
 | 
			
		||||
        return move;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param speed The new speed of the hero.
 | 
			
		||||
     */
 | 
			
		||||
    public void setSpeed(float speed){
 | 
			
		||||
        this.speed = speed;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Do a step on the given direction
 | 
			
		||||
     * @param direction The direction to go.
 | 
			
		||||
     */
 | 
			
		||||
    public void go(Direction direction){
 | 
			
		||||
        move = true;
 | 
			
		||||
        switch(direction){
 | 
			
		||||
            case RIGHT:
 | 
			
		||||
                newPosition.add(SPRITE_WIDTH, 0);
 | 
			
		||||
                break;
 | 
			
		||||
            case LEFT:
 | 
			
		||||
                newPosition.add(-SPRITE_WIDTH, 0);
 | 
			
		||||
                break;
 | 
			
		||||
            case UP:
 | 
			
		||||
                newPosition.add(0, SPRITE_HEIGHT);
 | 
			
		||||
                break;
 | 
			
		||||
            case DOWN:
 | 
			
		||||
                newPosition.add(0, -SPRITE_HEIGHT);
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
                break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        turn(direction);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Turn the hero on the given direction without do any step.
 | 
			
		||||
     * @param direction The direction to turn.
 | 
			
		||||
     */
 | 
			
		||||
    public void turn(Direction direction){
 | 
			
		||||
        switch(direction){
 | 
			
		||||
            case RIGHT:
 | 
			
		||||
                textureY = 2;
 | 
			
		||||
                break;
 | 
			
		||||
            case LEFT:
 | 
			
		||||
                textureY = 1;
 | 
			
		||||
                break;
 | 
			
		||||
            case UP:
 | 
			
		||||
                textureY = 3;
 | 
			
		||||
                break;
 | 
			
		||||
            case DOWN:
 | 
			
		||||
                textureY = 0;
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
                break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Draw the character on the graphic object.
 | 
			
		||||
     * @param g Graphic object.
 | 
			
		||||
     */
 | 
			
		||||
    public void draw(GdxGraphics g) {
 | 
			
		||||
        g.draw(ss.sprites[textureY][currentFrame], position.x, position.y);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getPv() {
 | 
			
		||||
        return pv;
 | 
			
		||||
 
 | 
			
		||||
@@ -2,10 +2,13 @@ package Entity;
 | 
			
		||||
 | 
			
		||||
import com.badlogic.gdx.math.Vector2;
 | 
			
		||||
 | 
			
		||||
import ch.hevs.gdx2d.lib.GdxGraphics;
 | 
			
		||||
 | 
			
		||||
public class Enemy extends Character{
 | 
			
		||||
    
 | 
			
		||||
    public Enemy(String name) {
 | 
			
		||||
        super(name);
 | 
			
		||||
 | 
			
		||||
    public Enemy(String name, int x, int y, String img) {
 | 
			
		||||
        super(name, x, y, img);
 | 
			
		||||
        //TODO Auto-generated constructor stub
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setPosition(int x, int y){
 | 
			
		||||
@@ -21,5 +24,11 @@ public class Enemy extends Character{
 | 
			
		||||
        // TODO Auto-generated method stub
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void draw(GdxGraphics arg0) {
 | 
			
		||||
        // TODO Auto-generated method stub
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,16 +2,58 @@ package Entity;
 | 
			
		||||
 | 
			
		||||
import com.badlogic.gdx.math.Vector2;
 | 
			
		||||
 | 
			
		||||
public abstract class Entity {
 | 
			
		||||
    private Vector2 position;
 | 
			
		||||
    private String name;
 | 
			
		||||
import ch.hevs.gdx2d.components.bitmaps.Spritesheet;
 | 
			
		||||
import ch.hevs.gdx2d.lib.GdxGraphics;
 | 
			
		||||
import ch.hevs.gdx2d.lib.interfaces.DrawableObject;
 | 
			
		||||
 | 
			
		||||
public abstract class Entity implements DrawableObject {
 | 
			
		||||
    protected String name;
 | 
			
		||||
 | 
			
		||||
    Spritesheet ss;
 | 
			
		||||
    
 | 
			
		||||
    protected final static int SPRITE_WIDTH = 32;
 | 
			
		||||
    protected final static int SPRITE_HEIGHT = 32;
 | 
			
		||||
 | 
			
		||||
    Vector2 lastPosition;
 | 
			
		||||
    Vector2 newPosition;
 | 
			
		||||
    Vector2 position;
 | 
			
		||||
 | 
			
		||||
    protected boolean move = false;
 | 
			
		||||
 | 
			
		||||
    public Entity(String name){
 | 
			
		||||
        this.name = name;
 | 
			
		||||
        this(name, new Vector2(0,0));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Vector2 getPosition() {
 | 
			
		||||
        return position;
 | 
			
		||||
    public Entity(String name, int x, int y){
 | 
			
		||||
        this(name, new Vector2(SPRITE_WIDTH * x, SPRITE_HEIGHT * y));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Entity(String name, Vector2 initialPosition){
 | 
			
		||||
        this.name = name;
 | 
			
		||||
        lastPosition = new Vector2(initialPosition);
 | 
			
		||||
        newPosition = new Vector2(initialPosition);
 | 
			
		||||
        position = new Vector2(initialPosition);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void init(){
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void graphicRender(GdxGraphics g){
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return the current position of the entity on the map.
 | 
			
		||||
     */
 | 
			
		||||
    public Vector2 getPosition(){
 | 
			
		||||
        return this.position;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setPosition(int x, int y){
 | 
			
		||||
        lastPosition.set(x, y);
 | 
			
		||||
        newPosition.set(x, y);
 | 
			
		||||
        position.set(x, y);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getName() {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,20 +1,78 @@
 | 
			
		||||
package Entity;
 | 
			
		||||
 | 
			
		||||
import java.util.Vector;
 | 
			
		||||
 | 
			
		||||
import com.badlogic.gdx.Input;
 | 
			
		||||
import com.badlogic.gdx.maps.tiled.TiledMapTile;
 | 
			
		||||
import com.badlogic.gdx.math.Vector2;
 | 
			
		||||
 | 
			
		||||
import Control.Controller;
 | 
			
		||||
import Screen.ScreenMap;
 | 
			
		||||
 | 
			
		||||
public class Player extends Character{
 | 
			
		||||
 | 
			
		||||
    private int xp;
 | 
			
		||||
 | 
			
		||||
    public Player(String name) {
 | 
			
		||||
        super(name);
 | 
			
		||||
        //TODO Auto-generated constructor stub
 | 
			
		||||
    public Player(int x, int y) {
 | 
			
		||||
        super("Player", x, y, "lumberjack_sheet32");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void addXp(int xp){
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void manageEntity(ScreenMap sm, Controller c) {
 | 
			
		||||
 | 
			
		||||
		// Do nothing if hero is already moving
 | 
			
		||||
		if (!isMoving()) {
 | 
			
		||||
 | 
			
		||||
			// Compute direction and next cell
 | 
			
		||||
			Vector<TiledMapTile> nextCell = new Vector<>();
 | 
			
		||||
			Player.Direction goalDirection = Player.Direction.NULL;
 | 
			
		||||
 | 
			
		||||
			if (c.keyStatus.get(Input.Keys.RIGHT)) {
 | 
			
		||||
				goalDirection = Player.Direction.RIGHT;
 | 
			
		||||
				nextCell = sm.getTile(getPosition(), 1, 0);
 | 
			
		||||
			} else if (c.keyStatus.get(Input.Keys.LEFT)) {
 | 
			
		||||
				goalDirection = Player.Direction.LEFT;
 | 
			
		||||
				nextCell = sm.getTile(getPosition(), -1, 0);
 | 
			
		||||
			} else if (c.keyStatus.get(Input.Keys.UP)) {
 | 
			
		||||
				goalDirection = Player.Direction.UP;
 | 
			
		||||
				nextCell = sm.getTile(getPosition(), 0, 1);
 | 
			
		||||
			} else if (c.keyStatus.get(Input.Keys.DOWN)) {
 | 
			
		||||
				goalDirection = Player.Direction.DOWN;
 | 
			
		||||
				nextCell = sm.getTile(getPosition(), 0, -1);
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// Is the move valid ?
 | 
			
		||||
			if (sm.isWalkable(nextCell)) {
 | 
			
		||||
				// Go
 | 
			
		||||
				setSpeed(sm.getSpeed(nextCell));
 | 
			
		||||
				go(goalDirection);
 | 
			
		||||
			} else {
 | 
			
		||||
				// Face the wall
 | 
			
		||||
				turn(goalDirection);
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			
 | 
			
		||||
			if(sm.isDoor(getPosition())){
 | 
			
		||||
				String nMap = null;
 | 
			
		||||
				Integer x = null;
 | 
			
		||||
				Integer y = null;
 | 
			
		||||
				try {
 | 
			
		||||
					nMap = ScreenMap.Door.nextMap;
 | 
			
		||||
					x = ScreenMap.Door.nextX;
 | 
			
		||||
					y = ScreenMap.Door.nextY;
 | 
			
		||||
				} catch (Exception e) { }
 | 
			
		||||
				ScreenMap.Door.reset();
 | 
			
		||||
				if (nMap == null || x == null || y == null) return;
 | 
			
		||||
				sm.map = nMap;
 | 
			
		||||
				setPosition(x*sm.tileWidth, y*sm.tileHeight);
 | 
			
		||||
				System.out.println("Go to: " + sm.map + " in " + x + " x " + y);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    public void move(int x, int y){
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,10 +1,18 @@
 | 
			
		||||
package Entity;
 | 
			
		||||
 | 
			
		||||
import ch.hevs.gdx2d.lib.GdxGraphics;
 | 
			
		||||
 | 
			
		||||
public class Stuff extends Entity{
 | 
			
		||||
 | 
			
		||||
    public Stuff(String name) {
 | 
			
		||||
        super(name);
 | 
			
		||||
        //TODO Auto-generated constructor stub
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void draw(GdxGraphics arg0) {
 | 
			
		||||
        // TODO Auto-generated method stub
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user