From 4e28801b38534354980f68e040fc8ae480386a52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Tue, 7 Jun 2022 21:35:13 +0200 Subject: [PATCH] done --- resources/map/Maps.tiled-session | 12 +-- resources/map/test_couloir.tmx | 8 +- src/Control/Controller.java | 19 ++++ src/Entity/Character.java | 151 ++++++++++++++++++++++++++++++- src/Entity/Enemy.java | 15 ++- src/Entity/Entity.java | 54 +++++++++-- src/Entity/Player.java | 64 ++++++++++++- src/Entity/Stuff.java | 8 ++ src/Screen/ScreenMap.java | 137 +++++++--------------------- src/testHER.java | 29 +++++- 10 files changed, 361 insertions(+), 136 deletions(-) create mode 100644 src/Control/Controller.java diff --git a/resources/map/Maps.tiled-session b/resources/map/Maps.tiled-session index 69b88ad..16e8d38 100644 --- a/resources/map/Maps.tiled-session +++ b/resources/map/Maps.tiled-session @@ -197,8 +197,8 @@ "scale": 0.75, "selectedLayer": 1, "viewCenter": { - "x": 117.33333333333331, - "y": 452 + "x": 640.6666666666667, + "y": 640.6666666666666 } }, "desert.tmx#Desert": { @@ -212,8 +212,8 @@ "scale": 1.5, "selectedLayer": 1, "viewCenter": { - "x": 119.33333333333331, - "y": 119.33333333333333 + "x": 160.33333333333331, + "y": 160.33333333333331 } }, "test.tmx#test": { @@ -227,8 +227,8 @@ "scale": 1, "selectedLayer": 4, "viewCenter": { - "x": 115, - "y": 74 + "x": 320.5, + "y": 159.5 } }, "test_couloir.tmx#Dungeon_Tileset": { diff --git a/resources/map/test_couloir.tmx b/resources/map/test_couloir.tmx index d86b1d1..0cc80cb 100644 --- a/resources/map/test_couloir.tmx +++ b/resources/map/test_couloir.tmx @@ -9024,28 +9024,28 @@ - + - + - + - + diff --git a/src/Control/Controller.java b/src/Control/Controller.java new file mode 100644 index 0000000..5787cd4 --- /dev/null +++ b/src/Control/Controller.java @@ -0,0 +1,19 @@ +package Control; + +import java.util.Map; +import java.util.TreeMap; + +import com.badlogic.gdx.Input; + +public class Controller { + public Map keyStatus = new TreeMap(); + + public void init(){ + // init keys status + keyStatus.put(Input.Keys.UP, false); + keyStatus.put(Input.Keys.DOWN, false); + keyStatus.put(Input.Keys.LEFT, false); + keyStatus.put(Input.Keys.RIGHT, false); + } + +} diff --git a/src/Entity/Character.java b/src/Entity/Character.java index 2b96daf..8415477 100644 --- a/src/Entity/Character.java +++ b/src/Entity/Character.java @@ -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; diff --git a/src/Entity/Enemy.java b/src/Entity/Enemy.java index a285417..22f2ec4 100644 --- a/src/Entity/Enemy.java +++ b/src/Entity/Enemy.java @@ -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 + + } } diff --git a/src/Entity/Entity.java b/src/Entity/Entity.java index 15354fb..079c676 100644 --- a/src/Entity/Entity.java +++ b/src/Entity/Entity.java @@ -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() { diff --git a/src/Entity/Player.java b/src/Entity/Player.java index 74d1148..21e1e9f 100644 --- a/src/Entity/Player.java +++ b/src/Entity/Player.java @@ -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 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){ } diff --git a/src/Entity/Stuff.java b/src/Entity/Stuff.java index 8b2b878..3afa338 100644 --- a/src/Entity/Stuff.java +++ b/src/Entity/Stuff.java @@ -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 + + } } diff --git a/src/Screen/ScreenMap.java b/src/Screen/ScreenMap.java index ff0469c..9c70f2b 100644 --- a/src/Screen/ScreenMap.java +++ b/src/Screen/ScreenMap.java @@ -1,38 +1,37 @@ package Screen; -import ch.hevs.gdx2d.lib.GdxGraphics; -import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.Input; -import com.badlogic.gdx.graphics.Color; -import com.badlogic.gdx.maps.MapObject; -import com.badlogic.gdx.maps.MapObjects; -import com.badlogic.gdx.maps.MapProperties; -import com.badlogic.gdx.maps.tiled.*; -import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell; -import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; -import com.badlogic.gdx.math.Vector2; import java.util.Map; import java.util.TreeMap; import java.util.Vector; -public class ScreenMap { - // key management - public Map keyStatus = new TreeMap(); +import com.badlogic.gdx.maps.MapObject; +import com.badlogic.gdx.maps.MapObjects; +import com.badlogic.gdx.maps.MapProperties; +import com.badlogic.gdx.maps.tiled.TiledMap; +import com.badlogic.gdx.maps.tiled.TiledMapRenderer; +import com.badlogic.gdx.maps.tiled.TiledMapTile; +import com.badlogic.gdx.maps.tiled.TiledMapTileLayer; +import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell; +import com.badlogic.gdx.maps.tiled.TmxMapLoader; +import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; +import com.badlogic.gdx.math.Vector2; - // character - private Hero hero; +import Entity.Player; +import ch.hevs.gdx2d.lib.GdxGraphics; + +public class ScreenMap { // tiles management private Vector tiledLayer = new Vector<>(); private MapObjects doors; Map tMap = new TreeMap(); Map tMapRenderer = new TreeMap(); - private String map = "desert"; + public String map = "desert"; public float zoom; private int width; - private int tileWidth; + public int tileWidth; private int height; - private int tileHeight; + public int tileHeight; private void createMap(String name){ @@ -42,33 +41,21 @@ public class ScreenMap { } public void init() { - - // Create hero - hero = new Hero(8, 15); - // Set initial zoom zoom = 1; - // init keys status - keyStatus.put(Input.Keys.UP, false); - keyStatus.put(Input.Keys.DOWN, false); - keyStatus.put(Input.Keys.LEFT, false); - keyStatus.put(Input.Keys.RIGHT, false); - // create map createMap("test"); createMap("test_couloir"); createMap("desert"); } - public void graphicRender(GdxGraphics g) { - g.clear(); + public void graphicRender(GdxGraphics g, Player p) { tiledLayer.clear(); for (int i = 0; i < 50; i++) { try { tiledLayer.add((TiledMapTileLayer) tMap.get(map).getLayers().get(i)); } catch (Exception e) { } } - //System.out.println(tiledLayer.size() + " layers imported"); TiledMapTileLayer tl = tiledLayer.get(0); width = tl.getWidth(); tileWidth = (int) tl.getTileWidth(); @@ -79,26 +66,18 @@ public class ScreenMap { doors = tMap.get(map).getLayers().get("door").getObjects(); } catch (Exception e) { doors = null; } - // Hero activity - manageHero(); - System.out.println("Hero: " + (int)hero.getPosition().x/tileWidth + " x " + (int)hero.getPosition().y/tileHeight); - // Camera follows the hero g.zoom(zoom); - g.moveCamera(hero.getPosition().x, hero.getPosition().y, width * tileWidth, height * tileHeight); + g.moveCamera(p.getPosition().x, p.getPosition().y, width * tileWidth, height * tileHeight); // Render the tileMap tMapRenderer.get(map).setView(g.getCamera()); tMapRenderer.get(map).render(); - - // Draw the hero - hero.animate(Gdx.graphics.getDeltaTime()); - hero.draw(g); g.drawFPS(); } - private Vector getTile(Vector2 position, int offsetX, int offsetY) { + public Vector getTile(Vector2 position, int offsetX, int offsetY) { Vector tiles = new Vector<>(); for (TiledMapTileLayer tl : tiledLayer) { int x = (int) (position.x / tileWidth) + offsetX; @@ -113,7 +92,7 @@ public class ScreenMap { return tiles; } - private boolean isWalkable(Vector tile) { + public boolean isWalkable(Vector tile) { if (tile == null) return false; boolean walkable = false; for (TiledMapTile tiledMapTile : tile) { @@ -123,7 +102,7 @@ public class ScreenMap { return walkable; } - private float getSpeed(Vector tile) { + public float getSpeed(Vector tile) { float speed = 0; for (TiledMapTile tiledMapTile : tile) { Object test = tiledMapTile.getProperties().get("speed"); @@ -133,7 +112,7 @@ public class ScreenMap { return speed; } - private boolean isDoor(Vector2 position) { + public boolean isDoor(Vector2 position) { if (doors == null) return false; boolean onDoor = false; Integer x = null; @@ -156,73 +135,21 @@ public class ScreenMap { if ((x != null || y != null) && (x == ox && y == oy)) { onDoor = true; - try { Door.nextMap = mapProperties.get("nextMap").toString(); } catch (Exception e) { System.out.println("shit 1"); } - try { Door.nextX = Integer.parseInt(mapProperties.get("nextX").toString()); } catch (Exception e) { System.out.println("shit 2"); } - try { Door.nextY = Integer.parseInt(mapProperties.get("nextY").toString()); } catch (Exception e) { System.out.println("shit 3"); } + try { Door.nextMap = mapProperties.get("nextMap").toString(); } catch (Exception e) { } + try { Door.nextX = Integer.parseInt(mapProperties.get("nextX").toString()); } catch (Exception e) { } + try { Door.nextY = Integer.parseInt(mapProperties.get("nextY").toString()); } catch (Exception e) { } } } return onDoor; } - private void manageHero() { + public static class Door { + public static String nextMap; + public static Integer nextX; + public static Integer nextY; - // Do nothing if hero is already moving - if (!hero.isMoving()) { - - // Compute direction and next cell - Vector nextCell = new Vector<>(); - Hero.Direction goalDirection = Hero.Direction.NULL; - - if (keyStatus.get(Input.Keys.RIGHT)) { - goalDirection = Hero.Direction.RIGHT; - nextCell = getTile(hero.getPosition(), 1, 0); - } else if (keyStatus.get(Input.Keys.LEFT)) { - goalDirection = Hero.Direction.LEFT; - nextCell = getTile(hero.getPosition(), -1, 0); - } else if (keyStatus.get(Input.Keys.UP)) { - goalDirection = Hero.Direction.UP; - nextCell = getTile(hero.getPosition(), 0, 1); - } else if (keyStatus.get(Input.Keys.DOWN)) { - goalDirection = Hero.Direction.DOWN; - nextCell = getTile(hero.getPosition(), 0, -1); - } - - // Is the move valid ? - if (isWalkable(nextCell)) { - // Go - hero.setSpeed(getSpeed(nextCell)); - hero.go(goalDirection); - } else { - // Face the wall - hero.turn(goalDirection); - } - - - if(isDoor(hero.getPosition())){ - String nMap = null; - Integer x = null; - Integer y = null; - try { - nMap = Door.nextMap; - x = Door.nextX; - y = Door.nextY; - } catch (Exception e) { } - Door.reset(); - if (nMap == null || x == null || y == null) return; - map = nMap; - hero.setPosition(x*tileWidth, y*tileHeight); - System.out.println("Go to: " + map + " in " + x + " x " + y); - } - } - } - - static class Door { - static String nextMap; - static Integer nextX; - static Integer nextY; - - static void reset(){ + public static void reset(){ nextMap = null; nextX = null; nextY = null; diff --git a/src/testHER.java b/src/testHER.java index 5bf95d0..b177a7c 100644 --- a/src/testHER.java +++ b/src/testHER.java @@ -1,34 +1,53 @@ import com.badlogic.gdx.Input; +import Control.Controller; +import Entity.Entity; +import Entity.Player; import Screen.ScreenMap; import ch.hevs.gdx2d.desktop.PortableApplication; import ch.hevs.gdx2d.lib.GdxGraphics; + public class testHER extends PortableApplication{ - private static ScreenMap sm; + private Controller controller; + private ScreenMap sm; + private Player p1; + private static Entity[] entities; + + + public testHER(){ + controller = new Controller(); + p1 = new Player(8, 15); + sm = new ScreenMap(); + } + public static void main(String[] args) { - sm = new ScreenMap(); new testHER(); } @Override public void onInit() { + controller.init(); sm.init(); + p1.init(); } @Override public void onGraphicRender(GdxGraphics g) { - sm.graphicRender(g); + g.clear(); + p1.manageEntity(sm, controller); + sm.graphicRender(g, p1); // load p1 by Entity[] + p1.graphicRender(g); } @Override public void onKeyUp(int keycode) { super.onKeyUp(keycode); - sm.keyStatus.put(keycode, false); + controller.keyStatus.put(keycode, false); } @Override @@ -49,7 +68,7 @@ public class testHER extends PortableApplication{ default: break; } - sm.keyStatus.put(keycode, true); + controller.keyStatus.put(keycode, true); } }