diff --git a/src/de/dungeonrunner/Animation.java b/src/de/dungeonrunner/Animation.java index 9ef0cf4..4757982 100644 --- a/src/de/dungeonrunner/Animation.java +++ b/src/de/dungeonrunner/Animation.java @@ -1,16 +1,20 @@ package de.dungeonrunner; import org.jsfml.graphics.Drawable; +import org.jsfml.graphics.FloatRect; import org.jsfml.graphics.IntRect; import org.jsfml.graphics.RenderStates; import org.jsfml.graphics.RenderTarget; import org.jsfml.graphics.Sprite; +import org.jsfml.graphics.Transform; +import org.jsfml.graphics.Transformable; import org.jsfml.system.Time; +import org.jsfml.system.Vector2f; import org.jsfml.system.Vector2i; import de.dungeonrunner.TextureHolder.TextureID; -public class Animation implements Drawable { +public class Animation implements Drawable, Transformable { private Sprite mSprite; private Vector2i mFrameSize; @@ -37,28 +41,33 @@ public void update(Time dt) { while (mElapsedTime.asMilliseconds() >= timePerFrame.asMilliseconds() && (mCurrentFrame <= mNumFrames || mRepeat)) { int textureLeft = textureRect.left + textureRect.width; - - if(textureLeft + textureRect.width > textureBounds.x){ + + if (textureLeft + textureRect.width > textureBounds.x) { textureRect = new IntRect(0, 0, mFrameSize.x, mFrameSize.y); } else { textureRect = new IntRect(textureLeft, textureRect.top, textureRect.width, textureRect.height); } - + mElapsedTime = Time.sub(mElapsedTime, timePerFrame); if (mRepeat) { mCurrentFrame++; - if(mCurrentFrame > mNumFrames){ + if (mCurrentFrame > mNumFrames) { mCurrentFrame = 0; textureRect = new IntRect(0, 0, mFrameSize.x, mFrameSize.y); } - } + } } mSprite.setTextureRect(textureRect); } + + public FloatRect getGlobalBounds(){ + return mSprite.getGlobalBounds(); + } @Override public void draw(RenderTarget target, RenderStates states) { + states = new RenderStates(states, Transform.combine(states.transform, getTransform())); target.draw(mSprite); } @@ -94,4 +103,105 @@ public void setRepeat(boolean repeat) { mRepeat = repeat; } + @Override + public Transform getInverseTransform() { + return mSprite.getInverseTransform(); + } + + @Override + public Vector2f getOrigin() { + return mSprite.getOrigin(); + } + + @Override + public Vector2f getPosition() { + return mSprite.getPosition(); + } + + @Override + public float getRotation() { + return mSprite.getRotation(); + } + + @Override + public Vector2f getScale() { + return mSprite.getScale(); + } + + @Override + public Transform getTransform() { + return mSprite.getTransform(); + } + + @Override + public void move(Vector2f v) { + mSprite.move(v); + + } + + @Override + public void move(float x, float y) { + mSprite.move(x, y); + + } + + @Override + public void rotate(float angle) { + mSprite.rotate(angle); + + } + + @Override + public void scale(Vector2f factors) { + mSprite.scale(factors); + + } + + @Override + public void scale(float x, float y) { + mSprite.scale(x, y); + + } + + @Override + public void setOrigin(Vector2f v) { + mSprite.setOrigin(v); + } + + @Override + public void setOrigin(float x, float y) { + mSprite.setOrigin(x, y); + + } + + @Override + public void setPosition(Vector2f v) { + mSprite.setPosition(v); + + } + + @Override + public void setPosition(float x, float y) { + mSprite.setPosition(x, y); + + } + + @Override + public void setRotation(float angle) { + mSprite.setRotation(angle); + + } + + @Override + public void setScale(Vector2f factors) { + mSprite.setScale(factors); + + } + + @Override + public void setScale(float x, float y) { + mSprite.setScale(x, y); + + } + } diff --git a/src/de/dungeonrunner/GameEntity.java b/src/de/dungeonrunner/GameEntity.java index 3278375..8486737 100644 --- a/src/de/dungeonrunner/GameEntity.java +++ b/src/de/dungeonrunner/GameEntity.java @@ -1,14 +1,18 @@ package de.dungeonrunner; +import org.jsfml.graphics.IntRect; import org.jsfml.system.Time; import org.jsfml.system.Vector2f; public abstract class GameEntity extends SceneNode { private Vector2f mVelocity; + private Vector2f mAfterCollVelocity; + private IntRect mCollisionRectangle; public GameEntity() { mVelocity = Vector2f.ZERO; + mAfterCollVelocity = mVelocity; } public void setVelocity(Vector2f velocity) { diff --git a/src/de/dungeonrunner/GameWorld.java b/src/de/dungeonrunner/GameWorld.java index 70b4918..9e85bcb 100644 --- a/src/de/dungeonrunner/GameWorld.java +++ b/src/de/dungeonrunner/GameWorld.java @@ -1,15 +1,19 @@ package de.dungeonrunner; +import java.awt.Rectangle; import java.util.HashMap; import org.jsfml.graphics.FloatRect; import org.jsfml.graphics.RenderWindow; +import org.jsfml.graphics.Sprite; import org.jsfml.graphics.View; import org.jsfml.system.Time; import org.jsfml.system.Vector2f; import de.dungeonrunner.TextureHolder.TextureID; import tiled.core.Map; +import tiled.core.Tile; +import tiled.core.TileLayer; public class GameWorld { @@ -46,7 +50,7 @@ public GameWorld(RenderWindow window) { private void loadTextures() { TextureHolder holder = TextureHolder.getInstance(); holder.loadTexture(TextureID.ANIM_IDLE, Constants.ANIM_DIR + "hero_idle_anim.png"); - + if (mMap != null) { TextureHolder.getInstance().loadTiledTextures(mMap); } @@ -65,22 +69,56 @@ private void loadMap() { private void buildScene() { mSceneGraph = new SceneNode(); mRenderLayers = new HashMap<>(); - + for (RenderLayers layer : RenderLayers.values()) { SceneNode node = new SceneNode(); mRenderLayers.put(layer, node); mSceneGraph.attachChild(node); } - + if (mMap != null) { - mMapRenderer = new SFMLTileRenderer(mMap); - mRenderLayers.get(RenderLayers.Background).attachChild(mMapRenderer); + for (int i = 0; i < mMap.getLayerCount(); i++) { + if (mMap.getLayer(i) instanceof TileLayer) { + TileLayer tileLayer = (TileLayer) mMap.getLayer(i); + final int tileWidth = mMap.getTileWidth(); + final int tileHeight = mMap.getTileHeight(); + final Rectangle boundsInTiles = tileLayer.getBounds(); + + TextureHolder textureHolder = TextureHolder.getInstance(); + + for (int x = 0; x < boundsInTiles.getWidth(); x++) { + for (int y = 0; y < boundsInTiles.getHeight(); y++) { + Tile tile = tileLayer.getTileAt(x, y); + if (tile == null) { + continue; + } else { + Sprite cachedTile = new Sprite(); + cachedTile.setTexture(textureHolder.getTileTexture(tile.getId())); + cachedTile.setPosition(x * tileWidth, y * tileHeight); + SpriteNode node = new SpriteNode(cachedTile); + node.setProperties(tile.getProperties()); + + switch (i) { + case 0: + mRenderLayers.get(RenderLayers.Background).attachChild(node); + break; + case 1: + mRenderLayers.get(RenderLayers.Middleground).attachChild(node); + break; + default: + mRenderLayers.get(RenderLayers.Foreground).attachChild(node); + } + } + } + } + } + } } - + mPlayer = new PlayerEntity(TextureID.ANIM_IDLE); mRenderLayers.get(RenderLayers.Middleground).attachChild(mPlayer); } - + public void draw() { mRenderWindow.setView(new View(mWorldView.getCenter(), mWorldView.getSize())); mRenderWindow.draw(mSceneGraph); diff --git a/src/de/dungeonrunner/PlayerEntity.java b/src/de/dungeonrunner/PlayerEntity.java index f371a05..500ea8d 100644 --- a/src/de/dungeonrunner/PlayerEntity.java +++ b/src/de/dungeonrunner/PlayerEntity.java @@ -1,5 +1,10 @@ package de.dungeonrunner; +import java.util.List; +import java.util.Properties; + +import org.jsfml.graphics.FloatRect; +import org.jsfml.graphics.IntRect; import org.jsfml.graphics.RenderStates; import org.jsfml.graphics.RenderTarget; import org.jsfml.system.Time; @@ -7,6 +12,8 @@ import org.jsfml.system.Vector2i; import de.dungeonrunner.TextureHolder.TextureID; +import tiled.core.Map; +import tiled.core.Tile; public class PlayerEntity extends GameEntity { @@ -18,7 +25,11 @@ public PlayerEntity(TextureID textureID) { mIdleAnimation.setRepeat(true); mIdleAnimation.setNumFrames(6); mIdleAnimation.setFrameSize(new Vector2i(128, 128)); - setVelocity(new Vector2f(1.5f, 0)); + setVelocity(new Vector2f(100f, 0)); + + Map map = TmxMapLoader.getMap(); + System.out.println(map.getBounds()); +// checkCollision(); } @Override @@ -29,7 +40,27 @@ protected void drawCurrent(RenderTarget target, RenderStates states) { @Override protected void updateCurrent(Time dt) { super.updateCurrent(dt); + mIdleAnimation.setPosition(getPosition()); mIdleAnimation.update(dt); + System.out.println(getBoundingRect()); } - + +// protected void checkCollision(){ +// Properties propsLeftTop = TmxMapLoader.getTileInstancePropertiesAt((int) (getPosition().x + get, (int) getPosition().y); +// getTransform(). +// Properties propsLeftBottom = TmxMapLoader.getTileInstancePropertiesAt((int) getPosition().x, (int) getPosition().y); +// Properties propsRightTop = TmxMapLoader.getTileInstancePropertiesAt((int) getPosition().x, (int) getPosition().y); +// Properties propsRightBottom = TmxMapLoader.getTileInstancePropertiesAt((int) getPosition().x, (int) getPosition().y); +// +// boolean isBlocking = Boolean.valueOf(props.getProperty("BlockVolume", "false")); +// if(isBlocking) +// } +// + public FloatRect getBoundingRect(){ + return getWorldTransform().transformRect(mIdleAnimation.getGlobalBounds()); + } +// +// private void setCollisionRectangle(int left, int top, int width, int height){ +// mCollisionRectangle = new IntRect(left, top, width, height); +// } } diff --git a/src/de/dungeonrunner/SceneNode.java b/src/de/dungeonrunner/SceneNode.java index 4b407be..c885c73 100644 --- a/src/de/dungeonrunner/SceneNode.java +++ b/src/de/dungeonrunner/SceneNode.java @@ -1,5 +1,6 @@ package de.dungeonrunner; +import java.util.Properties; import java.util.Vector; import org.jsfml.graphics.BasicTransformable; @@ -14,14 +15,16 @@ public class SceneNode extends BasicTransformable implements Drawable { private SceneNode mParentNode; private Vector mChildren; + private Properties mProperties; public SceneNode() { mChildren = new Vector<>(); + mProperties = new Properties(); } @Override public void draw(RenderTarget target, RenderStates states) { - states = new RenderStates(states, getTransform()); + states = new RenderStates(states, Transform.combine(states.transform, getTransform())); drawCurrent(target, states); drawChildren(target, states); } @@ -70,7 +73,19 @@ public SceneNode detachChild(SceneNode node) { public void setParentNode(SceneNode node) { mParentNode = node; } - + + public void setProperties(Properties props){ + mProperties = props; + } + + public void addProperty(String key, String value){ + mProperties.setProperty(key, value); + } + + public String removeProperty(String key){ + return (String) mProperties.remove(key); + } + public SceneNode getParentNode() { return mParentNode; } diff --git a/src/de/dungeonrunner/SpriteNode.java b/src/de/dungeonrunner/SpriteNode.java new file mode 100644 index 0000000..b81b2cd --- /dev/null +++ b/src/de/dungeonrunner/SpriteNode.java @@ -0,0 +1,22 @@ +package de.dungeonrunner; + +import org.jsfml.graphics.RenderStates; +import org.jsfml.graphics.RenderTarget; +import org.jsfml.graphics.Sprite; + +import de.dungeonrunner.TextureHolder.TextureID; + +public class SpriteNode extends SceneNode { + + private Sprite mSprite; + + public SpriteNode(Sprite sprite){ + mSprite = sprite; + } + + @Override + protected void drawCurrent(RenderTarget target, RenderStates states) { + super.drawCurrent(target, states); + target.draw(mSprite); + } +} diff --git a/src/de/dungeonrunner/TmxMapLoader.java b/src/de/dungeonrunner/TmxMapLoader.java index fd0e660..6756637 100644 --- a/src/de/dungeonrunner/TmxMapLoader.java +++ b/src/de/dungeonrunner/TmxMapLoader.java @@ -2,14 +2,20 @@ import java.util.ArrayList; import java.util.List; +import java.util.Properties; import tiled.core.Map; +import tiled.core.MapLayer; import tiled.core.Tile; +import tiled.core.TileLayer; import tiled.core.TileSet; import tiled.io.TMXMapReader; public class TmxMapLoader { + private static Map mMap; + private static List mBlockingTiles; + public static Map loadMap(String filepath) { String fileToOpen = filepath; @@ -27,23 +33,45 @@ public static Map loadMap(String filepath) { } System.out.println(map.toString() + " loaded"); + mMap = map; return map; } - - public static List getBlockingTiles(Map map){ + + public static List getBlockingTiles() { List blockingTiles = new ArrayList<>(); - - for (int i = 0; i < map.getTileSets().size(); i++) { - TileSet tileSet = map.getTileSets().get(i); - final int numberTiles = tileSet.size(); - for (int o = 0; o < numberTiles; o++) { - Tile tile = tileSet.getTile(o); - boolean isBlockVolume = Boolean.parseBoolean(tile.getProperties().getProperty("BlockVolume", "false")); - if(isBlockVolume){ - blockingTiles.add(tile); + Map map = mMap; + if (map != null) { + for (int i = 0; i < map.getTileSets().size(); i++) { + TileSet tileSet = map.getTileSets().get(i); + final int numberTiles = tileSet.size(); + for (int o = 0; o < numberTiles; o++) { + Tile tile = tileSet.getTile(o); + boolean isBlockVolume = Boolean + .parseBoolean(tile.getProperties().getProperty("BlockVolume", "false")); + if (isBlockVolume) { + blockingTiles.add(tile); + } } } + } else { + System.err.println("No map loaded, no blocking volumes"); } return blockingTiles; } + + public static Properties getTileInstancePropertiesAt(int x, int y) { + if (mMap != null) { + for (MapLayer layer : mMap) { + if (layer instanceof TileLayer) { + TileLayer tilelayer = (TileLayer) layer; + return tilelayer.getTileInstancePropertiesAt(x, y); + } + } + } + return null; + } + + public static Map getMap() { + return mMap; + } }