Skip to content

Commit

Permalink
Worked on collision functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Wolfinger committed Oct 10, 2015
1 parent 253a10b commit 97440ba
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 28 deletions.
122 changes: 116 additions & 6 deletions src/de/dungeonrunner/Animation.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);

}

}
4 changes: 4 additions & 0 deletions src/de/dungeonrunner/GameEntity.java
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
52 changes: 45 additions & 7 deletions src/de/dungeonrunner/GameWorld.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
35 changes: 33 additions & 2 deletions src/de/dungeonrunner/PlayerEntity.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
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;
import org.jsfml.system.Vector2f;
import org.jsfml.system.Vector2i;

import de.dungeonrunner.TextureHolder.TextureID;
import tiled.core.Map;
import tiled.core.Tile;

public class PlayerEntity extends GameEntity {

Expand All @@ -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
Expand All @@ -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);
// }
}
19 changes: 17 additions & 2 deletions src/de/dungeonrunner/SceneNode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.dungeonrunner;

import java.util.Properties;
import java.util.Vector;

import org.jsfml.graphics.BasicTransformable;
Expand All @@ -14,14 +15,16 @@ public class SceneNode extends BasicTransformable implements Drawable {

private SceneNode mParentNode;
private Vector<SceneNode> 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);
}
Expand Down Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit 97440ba

Please sign in to comment.