Skip to content

Commit

Permalink
Working on collision
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Wolfinger committed Oct 12, 2015
1 parent 97440ba commit 954e79b
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 95 deletions.
2 changes: 1 addition & 1 deletion resources/maps/CollisionTest.tmx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</layer>
<layer name="Kachelebene 2" width="10" height="10">
<data encoding="base64" compression="zlib">
eJxjYCAOKBKhRnWEq1PBwQap0wRiZQJYF4gB6dsD5w==
eJxjYCAOKBOpZiSrU8HBViYBAwDrAwPX
</data>
</layer>
</map>
4 changes: 2 additions & 2 deletions src/de/dungeonrunner/Animation.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public void update(Time dt) {

mSprite.setTextureRect(textureRect);
}
public FloatRect getGlobalBounds(){

public FloatRect getGlobalBounds() {
return mSprite.getGlobalBounds();
}

Expand Down
37 changes: 1 addition & 36 deletions src/de/dungeonrunner/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public Application() {
mRenderWindow = new RenderWindow();
mRenderWindow.create(new VideoMode(800, 480, 32), "DungeonRunner");
mRenderWindow.setFramerateLimit(30);

mGameWorld = new GameWorld(mRenderWindow);

mClock = new Clock();
Expand Down Expand Up @@ -60,40 +60,5 @@ public static void main(String[] args) {

Application application = new Application();
application.run();
// GameWorld world = new GameWorld(window);

// while (window.isOpen()) {
// window.clear();
// // world.draw();
// window.display();
// }

// while (window.isOpen()) {
// for (Event event : window.pollEvents()) {
//
// switch (event.type) {
// case CLOSED:
// window.close();
// break;
// case RESIZED:
// view = new View(new Vector2f(window.getSize().x / 2,
// window.getSize().y / 2),
// new Vector2f(window.getSize()));
// window.setView(view);
// break;
// case KEY_PRESSED:
// view.move(new Vector2f(10, 0));
// window.setView(view);
// default:
// break;
// }
// }
//
// window.clear();
//
// window.draw(playerEntity);
// window.display();
// }

}
}
4 changes: 0 additions & 4 deletions src/de/dungeonrunner/GameEntity.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
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
6 changes: 5 additions & 1 deletion src/de/dungeonrunner/GameWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.awt.Rectangle;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import org.jsfml.graphics.FloatRect;
import org.jsfml.graphics.RenderWindow;
Expand All @@ -10,6 +12,7 @@
import org.jsfml.system.Time;
import org.jsfml.system.Vector2f;

import de.dungeonrunner.SceneNode.CollisionPair;
import de.dungeonrunner.TextureHolder.TextureID;
import tiled.core.Map;
import tiled.core.Tile;
Expand Down Expand Up @@ -44,7 +47,6 @@ public GameWorld(RenderWindow window) {
loadMap();
loadTextures();
buildScene();

}

private void loadTextures() {
Expand Down Expand Up @@ -126,5 +128,7 @@ public void draw() {

public void update(Time dt) {
mSceneGraph.update(dt);
Set<CollisionPair> collisionPairs = new HashSet<>();
mSceneGraph.checkSceneCollision(mSceneGraph, collisionPairs);
}
}
66 changes: 35 additions & 31 deletions src/de/dungeonrunner/PlayerEntity.java
Original file line number Diff line number Diff line change
@@ -1,66 +1,70 @@
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 {

private Animation mIdleAnimation;

public PlayerEntity(TextureID textureID) {
mProperties.setProperty("BlockVolume", "true");
mIdleAnimation = new Animation(textureID);
mIdleAnimation.setDuration(Time.getMilliseconds(900));
mIdleAnimation.setRepeat(true);
mIdleAnimation.setNumFrames(6);
mIdleAnimation.setFrameSize(new Vector2i(128, 128));
setVelocity(new Vector2f(100f, 0));

Map map = TmxMapLoader.getMap();
System.out.println(map.getBounds());
// checkCollision();
setPosition(20f, 0f);
mIdleAnimation.setPosition(getPosition());
setVelocity(new Vector2f(10f, 0f));
}

@Override
protected void drawCurrent(RenderTarget target, RenderStates states) {
mIdleAnimation.draw(target, states);
super.drawCurrent(target, states);
}

@Override
protected void updateCurrent(Time dt) {
super.updateCurrent(dt);
mIdleAnimation.setPosition(getPosition());
mIdleAnimation.move(Vector2f.mul(getVelocity(), dt.asSeconds()));
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(){

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);
// }

@Override
protected void onCollision(SceneNode node) {
FloatRect boundsNode = node.getBoundingRect();
FloatRect playerRect = getBoundingRect();

float left, top;
left = playerRect.left;
top = playerRect.top;

if(playerRect.left + playerRect.width > boundsNode.left){
left = boundsNode.left - boundsNode.width;

} else if(playerRect.left < boundsNode.left + boundsNode.width){
left = boundsNode.left + boundsNode.width;
}

if(playerRect.top < boundsNode.top + boundsNode.height){
top = boundsNode.top + boundsNode.height;
} else if(playerRect.top + playerRect.height > boundsNode.top){
top = boundsNode.top - playerRect.height;
}
mIdleAnimation.setPosition(left, top);

//Check movement collision

}
}
Loading

0 comments on commit 954e79b

Please sign in to comment.