Skip to content

Commit

Permalink
Pathfinding pretty now
Browse files Browse the repository at this point in the history
  • Loading branch information
1000monkeys committed Jun 22, 2020
1 parent 3dd4ac4 commit 68a5478
Show file tree
Hide file tree
Showing 21 changed files with 287 additions and 128 deletions.
Binary file modified .gradle/5.4.1/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/5.4.1/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/5.4.1/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/5.4.1/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/5.4.1/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified .gradle/5.4.1/javaCompile/classAnalysis.bin
Binary file not shown.
Binary file modified .gradle/5.4.1/javaCompile/jarAnalysis.bin
Binary file not shown.
Binary file modified .gradle/5.4.1/javaCompile/javaCompile.lock
Binary file not shown.
Binary file modified .gradle/5.4.1/javaCompile/taskHistory.bin
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
81 changes: 49 additions & 32 deletions .idea/workspace.xml

Large diffs are not rendered by default.

97 changes: 70 additions & 27 deletions core/assets/testmap.tmx

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions core/src/com/kjellvos/aletho/zombieshooter/gdx/Constants.java
Expand Up @@ -52,7 +52,7 @@ public class Constants {
/**
* The width/height in tiles, To get the real width/height multiply with PPT.
*/
public static final int viewWidthInTiles = 20, viewHeightInTiles = 16;
public static final int viewWidthInTiles = 20, viewHeightInTiles = 16; // 20, 16

/**
* The distance light travels.
Expand All @@ -61,7 +61,7 @@ public class Constants {
/**
* The amount of rays used per light, More is smoother shadows but slower performance.
*/
public static final int LIGHT_NUM_RAYS = 64;
public static final int LIGHT_NUM_RAYS = 640;
/**
* The distance from which lights go from lighting to darkness.
*/
Expand Down
Expand Up @@ -11,14 +11,14 @@
import com.kjellvos.aletho.zombieshooter.gdx.b2d.Box2dLocation;

public class SteeringComponent implements Steerable<Vector2>, Component, Pool.Poolable {

public int homeX, homeY;
public static enum SteeringState {WANDER,SEEK,FLEE,ARRIVE,NONE} // a list of possible behaviours
public SteeringState currentMode = SteeringState.WANDER; // stores which state the entity is currently in
public Body body; // stores a reference to our Box2D body

// Steering data
float maxLinearSpeed = 64f; // stores the max speed the entity can go
float maxLinearAcceleration = 640f; // stores the max acceleration
float maxLinearSpeed = 45f; // stores the max speed the entity can go
float maxLinearAcceleration = 300f; // stores the max acceleration
float maxAngularSpeed = 45f; // the max turning speed
float maxAngularAcceleration = 45f;// the max turning acceleration
float zeroThreshold = 0.1f; // how accurate should checks be (0.0000001f will mean the entity must get within 0.0000001f of
Expand All @@ -31,6 +31,13 @@ public static enum SteeringState {WANDER,SEEK,FLEE,ARRIVE,NONE} // a list of po

public SteeringComponent(Body body){
this.body = body;
homeX = Math.round(body.getPosition().x);
homeY = Math.round(body.getPosition().y);
}

public void setHome(int homeX, int homeY) {
this.homeX = homeX;
this.homeY = homeY;
}

@Override
Expand Down
Expand Up @@ -33,7 +33,7 @@ public PlayerEntity(ZombieShooterGame parent) {

Body body = parent.getGameScreen().getWorld().createBody(bodyDef);
PolygonShape shape = new PolygonShape();
shape.setAsBox(16 / 2F, 16 / 2F);
shape.setAsBox(16 / 4F, 16 / 4F);
fixtureDef.shape = shape;
fixtureDef.filter.categoryBits = Constants.CATEGORY_PLAYER;
fixtureDef.filter.maskBits = Constants.MASK_PLAYER;
Expand Down
Expand Up @@ -58,6 +58,6 @@ public void update(float deltaTime) {
playerSteerComp.getPosition().x = bodyComp.body.getPosition().x;
playerSteerComp.getPosition().y = bodyComp.body.getPosition().y;

System.out.println("PLAYER POSITION:" + playerSteerComp.getPosition().x + ":X_Y:" + playerSteerComp.getPosition().y);
//System.out.println("PLAYER POSITION:" + playerSteerComp.getPosition().x + ":X_Y:" + playerSteerComp.getPosition().y);
}
}
Expand Up @@ -46,45 +46,74 @@ protected void processEntity(Entity entity, float deltaTime) {
Body monsterBody = Mapper.bodyCom.get(entity).body;

PlayerEntity playerEntity = parent.getGameScreen().getPlayer();
Body playerBody = Mapper.bodyCom.get(entity).body;
Body playerBody = Mapper.bodyCom.get(playerEntity).body;


if (route == null) {
if (route == null &&
monsterBody.getPosition().x + 80 > playerBody.getPosition().x && playerBody.getPosition().x > monsterBody.getPosition().x - 80 &&
monsterBody.getPosition().y + 80 > playerBody.getPosition().y && playerBody.getPosition().y > monsterBody.getPosition().y - 80
) {
//System.out.println(monsterBody.getPosition().x + ":X_MONSTERBODY_Y:" + monsterBody.getPosition().y);
Tile startNode = parent.getGameScreen().getTile(floor(monsterBody.getPosition().x / 16), floor(monsterBody.getPosition().y / 16));
Tile endNode = parent.getGameScreen().getTile(3, 3);
Tile startNode = parent.getGameScreen().getTile(Math.round(monsterBody.getPosition().x / 16), Math.round(monsterBody.getPosition().y / 16));
Tile endNode = parent.getGameScreen().getTile(Math.round(playerBody.getPosition().x / 16), Math.round(playerBody.getPosition().y / 16));

TilePath path = new TilePath();
parent.getGameScreen().getPathFinder().searchConnectionPath(startNode, endNode, new MandattanDistance(), path);
System.out.println(path.getCount());
if (parent.getGameScreen().getPathFinder().searchConnectionPath(startNode, endNode, new MandattanDistance(), path)) {
System.out.println(path.getCount());

route = new Array<>(path.getCount());
for (int i = 0; i < path.getCount(); i++) {
route.add(new Vector2(path.get(i).getToNode().getX() * 16 + 8, path.get(i).getToNode().getY() * 16 + 8));
System.out.println("Added to path: " + i + " X:" + (path.get(i).getToNode().getX() * 16 + 8) + " Y:" + (path.get(i).getToNode().getY() * 16 + 8));
}
if (path.getCount() > 1) {
route = new Array<>(path.getCount());
for (int i = 0; i < path.getCount(); i++) {
route.add(new Vector2(path.get(i).getToNode().getX() * 16 + 8, path.get(i).getToNode().getY() * 16 + 8));
System.out.println("Added to path: " + i + " X:" + (path.get(i).getToNode().getX() * 16 - 12) + " Y:" + (path.get(i).getToNode().getY() * 16 - 12));
}

if (path.getCount() > 1) {
LinePath<Vector2> linePath = new LinePath<Vector2>(route);
//steer.steeringBehavior = SteeringPresets.getFollowPath(steer, linePath);
steer.currentMode = SteeringComponent.SteeringState.ARRIVE;
steer.steeringBehavior = SteeringPresets.getSeek(steer, new SeekablePoint(route.get(count).x, route.get(count).y));
System.out.println("APPLIED STEERING");
LinePath<Vector2> linePath = new LinePath<Vector2>(route);
//steer.steeringBehavior = SteeringPresets.getFollowPath(steer, linePath);
steer.currentMode = SteeringComponent.SteeringState.SEEK;
steer.steeringBehavior = SteeringPresets.getSeek(steer, new SeekablePoint(route.get(count).x, route.get(count).y));
System.out.println("APPLIED STEERING");
} else {
route = null;
count = 0;
steer.homeX = Math.round(monsterBody.getPosition().x);
steer.homeY = Math.round(monsterBody.getPosition().y);
}
}
}else{
}else if(route != null){
if ( route.size - 1 > count &&
route.get(count).x + 8 > monsterBody.getPosition().x - 4 && monsterBody.getPosition().x + 4 > route.get(count).x - 8 &&
route.get(count).y + 8 > monsterBody.getPosition().y - 4 && monsterBody.getPosition().y + 4 > route.get(count).y - 8
){
count++;
steer.body.setLinearVelocity(new Vector2(0, 0));
steer.body.setAngularVelocity(0F);
steer.currentMode = SteeringComponent.SteeringState.ARRIVE;
//steer.body.setLinearVelocity(new Vector2(0, 0));
//steer.body.setAngularVelocity(0F);
steer.currentMode = SteeringComponent.SteeringState.SEEK;
steer.steeringBehavior = SteeringPresets.getSeek(steer, new SeekablePoint(route.get(count).x, route.get(count).y));
}
if (count == route.size - 1) {
SteeringComponent playerSteer = Mapper.steerCom.get(playerEntity);

steer.currentMode = SteeringComponent.SteeringState.SEEK;
steer.steeringBehavior = SteeringPresets.getSeek(steer, playerSteer);
route = null;
count = 0;
steer.homeX = Math.round(monsterBody.getPosition().x);
steer.homeY = Math.round(monsterBody.getPosition().y);
}
}else {
if (
!(steer.getPosition().x < steer.homeX + 40 && steer.getPosition().x > steer.homeX - 40 &&
steer.getPosition().y < steer.homeY + 40 && steer.getPosition().y > steer.homeY - 40)
) {
steer.currentMode = SteeringComponent.SteeringState.SEEK;
steer.steeringBehavior = SteeringPresets.getSeek(steer, new SeekablePoint(steer.homeX, steer.homeY));
}else if (steer.currentMode != SteeringComponent.SteeringState.WANDER) {
steer.currentMode = SteeringComponent.SteeringState.WANDER;
steer.steeringBehavior = SteeringPresets.getWander(steer);
}
}

System.out.println(monsterBody.getPosition().x + ":X_MONSTERBODY_Y:" + monsterBody.getPosition().y);
//System.out.println(monsterBody.getPosition().x + ":X_MONSTERBODY_Y:" + monsterBody.getPosition().y);
steer.update(deltaTime);
}
}
Expand Up @@ -104,11 +104,11 @@ public static void buildLight(MapObject object, ReadJsonGameFiles readJsonGameFi
bodyDef.type = BodyDef.BodyType.StaticBody;
float x = Float.parseFloat(object.getProperties().get("x").toString());
float y = Float.parseFloat(object.getProperties().get("y").toString());
bodyDef.position.set(x, y);
bodyDef.position.set(x + 8, y + 8);

Body body = world.createBody(bodyDef);
PolygonShape shape = new PolygonShape();
shape.setAsBox( textureRegions[0].getRegionWidth() / 2F, textureRegions[0].getRegionHeight() / 2F);
shape.setAsBox( textureRegions[0].getRegionWidth() / 4F, textureRegions[0].getRegionHeight() / 4F);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.filter.categoryBits = Constants.CATEGORY_BUILDING;
Expand Down
Expand Up @@ -11,11 +11,10 @@ public class SteeringPresets {

public static Wander<Vector2> getWander(SteeringComponent scom){
Wander<Vector2> wander = new Wander<Vector2>(scom)
.setFaceEnabled(false) // let wander behaviour manage facing
.setWanderOffset(20f) // distance away from entity to set target
.setWanderOrientation(0f) // the initial orientation
.setWanderRadius(10f) // size of target
.setWanderRate(MathUtils.PI2 * 2); // higher values = more spinning
.setWanderRate(1000F)
.setWanderOffset(-1.6F)
.setWanderRadius(3.2F);

return wander;
}

Expand Down
Expand Up @@ -108,12 +108,33 @@ public void show() {

for (int x = 0; x < mapWidth; x++) {
for (int y = 0; y < mapHeight; y++) {
Array<Connection<Tile>> connections = new Array<Connection<Tile>>(4);
Array<Connection<Tile>> connections = new Array<Connection<Tile>>(8);

if (x > 0 && tiles.get((x - 1) * mapWidth + y).isWalkable()) connections.add(new TileConnection(tiles.get(x * mapWidth + y), tiles.get((x - 1) * mapWidth + y))); //-1, 0
if (y > 0 && tiles.get(x * mapWidth + (y - 1)).isWalkable()) connections.add(new TileConnection(tiles.get(x * mapWidth + y), tiles.get(x * mapWidth + (y - 1)))); //0, -1
if (x < mapWidth - 1 && tiles.get((x + 1) * mapWidth + y).isWalkable()) connections.add(new TileConnection(tiles.get(x * mapWidth + y), tiles.get((x + 1) * mapWidth + y))); //1, 0
if (y < mapHeight - 1 && tiles.get(x * mapWidth + (y + 1)).isWalkable()) connections.add(new TileConnection(tiles.get(x * mapWidth + y), tiles.get(x * mapWidth + (y + 1)))); //0, 1
if (x > 0 && tiles.get((x - 1) * mapWidth + y).isWalkable()){
connections.add(new TileConnection(tiles.get(x * mapWidth + y), tiles.get((x - 1) * mapWidth + y))); //-1, 0
}
if (y > 0 && tiles.get(x * mapWidth + (y - 1)).isWalkable()){
connections.add(new TileConnection(tiles.get(x * mapWidth + y), tiles.get(x * mapWidth + (y - 1)))); //0, -1
}
if (x < mapWidth - 1 && tiles.get((x + 1) * mapWidth + y).isWalkable()){
connections.add(new TileConnection(tiles.get(x * mapWidth + y), tiles.get((x + 1) * mapWidth + y))); //1, 0
}
if (y < mapHeight - 1 && tiles.get(x * mapWidth + (y + 1)).isWalkable()){
connections.add(new TileConnection(tiles.get(x * mapWidth + y), tiles.get(x * mapWidth + (y + 1)))); //0, 1
}

if ((x + 1) < mapHeight && (y + 1) < mapWidth && tiles.get((x + 1) * mapWidth + y + 1).isWalkable()){
connections.add(new TileConnection(tiles.get(x * mapWidth + y), tiles.get((x + 1) * mapWidth + y + 1))); //1, 1
}
if ((x + 1) < mapHeight && (y - 1) > 0 && tiles.get((x + 1) * mapWidth + y - 1).isWalkable()){
connections.add(new TileConnection(tiles.get(x * mapWidth + y), tiles.get((x + 1) * mapWidth + y - 1))); //1, -1
}
if ((x - 1) > 0 && (y - 1) > 0 && tiles.get((x - 1) * mapWidth + y - 1).isWalkable()){
connections.add(new TileConnection(tiles.get(x * mapWidth + y), tiles.get((x - 1) * mapWidth + y - 1))); //-1, -1
}
if ((x - 1) > 0 && y + 1 < mapWidth && tiles.get((x - 1) * mapWidth + y + 1).isWalkable()){
connections.add(new TileConnection(tiles.get(x * mapWidth + y), tiles.get((x - 1) * mapWidth + y + 1))); //-1, -1
}

tiles.get(x * mapWidth + y).setConnections(connections);
}
Expand Down

0 comments on commit 68a5478

Please sign in to comment.