diff --git a/.vscode/settings.json b/.vscode/settings.json index b2d444a..1ad73b8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "cSpell.words": [ - "pokemudry" + "pokemudry", + "Spritesheet" ] } \ No newline at end of file diff --git a/app/bin/main/PokeMudry.class b/app/bin/main/PokeMudry.class index a53aaff..4e0a6c6 100644 Binary files a/app/bin/main/PokeMudry.class and b/app/bin/main/PokeMudry.class differ diff --git a/app/bin/main/Screen/Hero$Direction.class b/app/bin/main/Screen/Hero$Direction.class new file mode 100644 index 0000000..4bbc909 Binary files /dev/null and b/app/bin/main/Screen/Hero$Direction.class differ diff --git a/app/bin/main/Screen/Hero.class b/app/bin/main/Screen/Hero.class new file mode 100644 index 0000000..7016af7 Binary files /dev/null and b/app/bin/main/Screen/Hero.class differ diff --git a/app/bin/main/Screen/ScreenMap.class b/app/bin/main/Screen/ScreenMap.class index e4d88c6..a1bae30 100644 Binary files a/app/bin/main/Screen/ScreenMap.class and b/app/bin/main/Screen/ScreenMap.class differ diff --git a/app/bin/main/lumberjack_sheet32.png b/app/bin/main/lumberjack_sheet32.png new file mode 100644 index 0000000..84f3de8 Binary files /dev/null and b/app/bin/main/lumberjack_sheet32.png differ diff --git a/app/bin/main/map/test_couloir.tmx b/app/bin/main/map/test_couloir.tmx index 00e67b8..1a13639 100644 --- a/app/bin/main/map/test_couloir.tmx +++ b/app/bin/main/map/test_couloir.tmx @@ -6479,19 +6479,19 @@ - + - + - + @@ -6683,19 +6683,19 @@ - + - + - + @@ -6887,19 +6887,19 @@ - + - + - + @@ -8967,7 +8967,7 @@ - + 21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21, 21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21, @@ -8981,7 +8981,7 @@ 21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21 - + 956,956,956,956,956,956,956,956,956,956,956,956,956,956,956,956,956,956,956,956, 956,956,956,956,956,956,956,956,956,956,956,956,956,956,956,956,956,956,956,956, @@ -8995,7 +8995,7 @@ 921,921,921,921,921,921,921,921,921,921,921,921,921,921,921,921,921,921,921,921 - + @@ -9021,7 +9021,7 @@ - + 21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21, 40,40,40,8,9,40,40,40,40,40,40,40,40,40,40,8,9,40,40,40, diff --git a/app/bin/main/testHER.class b/app/bin/main/testHER.class new file mode 100644 index 0000000..fb1fa77 Binary files /dev/null and b/app/bin/main/testHER.class differ diff --git a/app/src/main/java/PokeMudry.java b/app/src/main/java/PokeMudry.java index 76fd223..0b46f85 100644 --- a/app/src/main/java/PokeMudry.java +++ b/app/src/main/java/PokeMudry.java @@ -6,9 +6,11 @@ public class PokeMudry extends PortableApplication{ public final int PLAYERS = 1; public static final int TIME = 10; // number of minutes for kill all enemy + /* public static void main(String[] args) { } + */ @Override public void onInit() { diff --git a/app/src/main/java/Screen/Hero.java b/app/src/main/java/Screen/Hero.java new file mode 100644 index 0000000..af4d88a --- /dev/null +++ b/app/src/main/java/Screen/Hero.java @@ -0,0 +1,189 @@ +package Screen; + +import ch.hevs.gdx2d.components.bitmaps.Spritesheet; +import ch.hevs.gdx2d.lib.GdxGraphics; +import ch.hevs.gdx2d.lib.interfaces.DrawableObject; +import com.badlogic.gdx.math.Interpolation; +import com.badlogic.gdx.math.Vector2; + +/** + * Character for the demo. + * + * @author Alain Woeffray (woa) + * @author Pierre-André Mudry (mui) + */ +public class Hero implements DrawableObject { + + public enum Direction{ + UP, + DOWN, + RIGHT, + LEFT, + NULL + } + + /** + * The currently selected sprite for animation + */ + int textureX = 0; + int textureY = 1; + float speed = 1; + + float dt = 0; + int currentFrame = 0; + int nFrames = 4; + private final static int SPRITE_WIDTH = 32; + private final static int SPRITE_HEIGHT = 32; + final float FRAME_TIME = 0.1f; // Duration of each frime + Spritesheet ss; + + Vector2 lastPosition; + Vector2 newPosition; + Vector2 position; + + + private boolean move = false; + + /** + * Create the hero at the start position (0,0) + */ + public Hero(){ + this(new Vector2(0,0)); + } + + /** + * Create the hero at the given start tile. + * @param x Column + * @param y Line + */ + public Hero(int x, int y){ + this(new Vector2(SPRITE_WIDTH * x, SPRITE_HEIGHT * y)); + } + + /** + * Create the hero at the start position + * @param initialPosition Start position [px] on the map. + */ + public Hero(Vector2 initialPosition) { + + lastPosition = new Vector2(initialPosition); + newPosition = new Vector2(initialPosition); + position = new Vector2(initialPosition); + + ss = new Spritesheet("app/src/main/resources/lumberjack_sheet32.png", SPRITE_WIDTH, SPRITE_HEIGHT); + } + + /** + * @return the current position of the hero 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); + + } + + /** + * 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 hero 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); + } +} diff --git a/app/src/main/java/Screen/ScreenMap.java b/app/src/main/java/Screen/ScreenMap.java index 367e361..87d120f 100644 --- a/app/src/main/java/Screen/ScreenMap.java +++ b/app/src/main/java/Screen/ScreenMap.java @@ -1,5 +1,232 @@ package Screen; +import ch.hevs.gdx2d.lib.GdxGraphics; +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Input; +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.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(); + + // character + private Hero hero; + + // tiles management + private Vector tiledLayer = new Vector<>(); + private MapObjects doors; + Map tMap = new TreeMap(); + Map tMapRenderer = new TreeMap(); + private String map = "test_couloir"; + public float zoom; + private int width; + private int tileWidth; + private int height; + private int tileHeight; + + // position + Vector2 pannel = new Vector2(30, 30); + + private void createMap(String name){ + TiledMap tm =new TmxMapLoader().load("app/src/main/resources/map/"+ name + ".tmx"); + tMap.put(name,tm); + tMapRenderer.put(name,new OrthogonalTiledMapRenderer(tm)); + } + + public void init() { + + // Create hero + hero = new Hero(9, 4); + + // 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"); + } + + public void graphicRender(GdxGraphics g) { + g.clear(); + + for (int i = 0; i < 100; i++) { + try { tiledLayer.add((TiledMapTileLayer) tMap.get(map).getLayers().get(i)); } catch (Exception e) { } + } + TiledMapTileLayer tl = tiledLayer.get(0); + width = tl.getWidth(); + tileWidth = (int) tl.getTileWidth(); + height = tl.getHeight(); + tileHeight = (int) tl.getTileHeight(); + System.out.println(width + " x " + height + " - " + tileWidth + " x " + tileHeight); + try { + doors = tMap.get(map).getLayers().get("door").getObjects(); + } catch (Exception e) { } + + // Hero activity + manageHero(); + // System.out.println(hero.getPosition().x + " - " + hero.getPosition().y); + + // Camera follows the hero + g.zoom(zoom); + g.moveCamera(hero.getPosition().x, hero.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) { + Vector tiles = new Vector<>(); + for (TiledMapTileLayer tl : tiledLayer) { + int x = (int) (position.x / width) + offsetX; + int y = (int) (position.y / height) + offsetY; + System.out.println("tl: " + tl); + TiledMapTile cell = tl.getCell(x, y).getTile(); + try { + + System.out.println("cell: "+ cell); + tiles.add(cell); + } catch (Exception e) { System.out.println("shit");} + } + + return tiles; + } + + private boolean isWalkable(Vector tile) { + if (tile == null) return false; + boolean walkable = false; + System.out.println("tile: " + tile); + + for (TiledMapTile tiledMapTile : tile) { + System.out.println("tiledMapTile: " + tiledMapTile); + Object test = tiledMapTile.getProperties().get("walkable"); + walkable = Boolean.parseBoolean(test.toString()) ? true:walkable; + } + + //System.out.println(" walkable: " + walkable); + return walkable; + } + + private float getSpeed(Vector tile) { + float speed = 0; + for (TiledMapTile tiledMapTile : tile) { + Object test = tiledMapTile.getProperties().get("speed"); + float newSpeed = Float.parseFloat(test.toString()); + speed = newSpeed > speed ? newSpeed:speed; + } + return speed; + } + + private String getName(TiledMap tile) { + Object test = tile.getProperties().get("name"); + return test.toString(); + } + + private boolean isDoor(Vector2 position) { + boolean onDoor = false; + Integer x = null; + Integer y = null; + int ox = 0; + int oy = 0; + try { + x = (int) (position.x / 32); //tiledLayer.getTileWidth() + y = (int) (position.y / 32); //tiledLayer.getTileHeight() + } catch (Exception e) { } + + for (MapObject object : doors){ + MapProperties mapProperties = null; + try { mapProperties = object.getProperties(); } catch (Exception e) { } + try { ox = (int) ((float) mapProperties.get("x")); } catch (Exception e) { } + try { oy = (int) ((float) mapProperties.get("y")); } catch (Exception e) { } + + oy-=288; + oy/=-1; + + ox /= 32; + oy /= 32; + + String id = null; + try { id = mapProperties.get("id").toString(); } catch (Exception e) { } + //if(x != null && y != null) System.out.println(id + ": " + x + " x " + y + " - " + ox + " x " + oy); + + if (x != null || y != null) onDoor = (x == ox && y == oy) ? true:onDoor; + } + + return onDoor; + } + + private void manageHero() { + + // 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())){ + /* + switch(getName(tMap.get(map))){ + case "test": + if(hero.getPosition().x == 32 && hero.getPosition().y == 288){ + hero.setPosition(576, 256); + map = "desert"; + } + break; + default: + + break; + } + */ + + System.out.println("it's a door"); + } + } + } + } diff --git a/app/src/main/java/testHER.java b/app/src/main/java/testHER.java new file mode 100644 index 0000000..03b5f7f --- /dev/null +++ b/app/src/main/java/testHER.java @@ -0,0 +1,55 @@ +import com.badlogic.gdx.Input; + +import Screen.ScreenMap; +import ch.hevs.gdx2d.desktop.PortableApplication; +import ch.hevs.gdx2d.lib.GdxGraphics; + +public class testHER extends PortableApplication{ + + private static ScreenMap sm; + + + public static void main(String[] args) { + sm = new ScreenMap(); + new testHER(); + } + + @Override + public void onInit() { + sm.init(); + } + + @Override + public void onGraphicRender(GdxGraphics g) { + sm.graphicRender(g); + } + + @Override + public void onKeyUp(int keycode) { + super.onKeyUp(keycode); + + sm.keyStatus.put(keycode, false); + } + + @Override + public void onKeyDown(int keycode) { + super.onKeyDown(keycode); + + switch (keycode) { + case Input.Keys.Z: + if (sm.zoom == 1.0) { + sm.zoom = 2.0f; + } else if (sm.zoom == 2.0) { + sm.zoom = 4; + } else { + sm.zoom = 1; + } + return; + + default: + break; + } + sm.keyStatus.put(keycode, true); + } + +} diff --git a/app/src/main/resources/lumberjack_sheet32.png b/app/src/main/resources/lumberjack_sheet32.png new file mode 100644 index 0000000..84f3de8 Binary files /dev/null and b/app/src/main/resources/lumberjack_sheet32.png differ diff --git a/app/src/main/resources/map/test_couloir.tmx b/app/src/main/resources/map/test_couloir.tmx index 00e67b8..1a13639 100644 --- a/app/src/main/resources/map/test_couloir.tmx +++ b/app/src/main/resources/map/test_couloir.tmx @@ -6479,19 +6479,19 @@ - + - + - + @@ -6683,19 +6683,19 @@ - + - + - + @@ -6887,19 +6887,19 @@ - + - + - + @@ -8967,7 +8967,7 @@ - + 21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21, 21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21, @@ -8981,7 +8981,7 @@ 21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21 - + 956,956,956,956,956,956,956,956,956,956,956,956,956,956,956,956,956,956,956,956, 956,956,956,956,956,956,956,956,956,956,956,956,956,956,956,956,956,956,956,956, @@ -8995,7 +8995,7 @@ 921,921,921,921,921,921,921,921,921,921,921,921,921,921,921,921,921,921,921,921 - + @@ -9021,7 +9021,7 @@ - + 21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21, 40,40,40,8,9,40,40,40,40,40,40,40,40,40,40,8,9,40,40,40,