Skip to content
This repository was archived by the owner on Jan 21, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package com.davidalexanderkelly.game.Entities.Behaviors;

import com.badlogic.gdx.math.Vector2;

public class Node {

private Vector2 worldPosition;

public float gCost;
public float hCost;
public Node parent;

public Node(Vector2 worldPosition) {
this.worldPosition = worldPosition;
}



public Vector2 getWorldPosition() {
return worldPosition;
}

public float fCost(){
return gCost + hCost;
}

}
package com.davidalexanderkelly.game.Entities.Behaviors;
import com.badlogic.gdx.math.Vector2;
public class Node {
private Vector2 worldPosition;
public float gCost;
public float hCost;
public Node parent;
public Node(Vector2 worldPosition) {
this.worldPosition = worldPosition;
}
public Vector2 getWorldPosition() {
return worldPosition;
}
public float fCost(){
return gCost + hCost;
}
}
Original file line number Diff line number Diff line change
@@ -1,104 +1,104 @@
package com.davidalexanderkelly.game.Entities.Behaviors;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import com.badlogic.gdx.math.Vector2;
import com.davidalexanderkelly.game.Screens.PlayScreen;
import com.davidalexanderkelly.game.Tools.PathfindingWorldCreator;

public class Pathfinding {

private float depth;
PathfindingWorldCreator pathfinder;

public static float round(float d, int decimalPlace) {
BigDecimal bd = new BigDecimal(Float.toString(d));
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
return bd.floatValue();
}

public ArrayList<Node> findPath(Node startPosition,Node goalPosition, PathfindingWorldCreator pathfinder) {
this.pathfinder = pathfinder;
Node startNode = startPosition;
Node endNode = goalPosition;
depth = 0f;

ArrayList<Node> openSet = new ArrayList<Node>();
HashSet<Node> closedSet = new HashSet<Node>();
openSet.add(startNode);
while(openSet.size() > 0) {
Node currentNode = openSet.get(0);
for(int i = 1; i < openSet.size(); i++) {
if((openSet.get(i).fCost() < currentNode.fCost() || (openSet.get(i).fCost() == currentNode.fCost() && openSet.get(i).hCost < currentNode.hCost)) && depth > currentNode.fCost()) {
currentNode = openSet.get(i);
depth = currentNode.fCost();



}
}
openSet.remove(currentNode);
closedSet.add(currentNode);
if(round(currentNode.getWorldPosition().x,2) == round(endNode.getWorldPosition().x,2) && round(currentNode.getWorldPosition().y,2) == round(endNode.getWorldPosition().y,2)) {
ArrayList<Node> returnPath = new ArrayList<Node>();
returnPath = retracePath(startNode,currentNode);

return returnPath;

}

for(Node neighbour : pathfinder.getNeighbours(currentNode)){
if(closedSet.contains(neighbour)) {

continue;

}

float newMovementCostToNeighbour = currentNode.gCost + getDistance(currentNode,neighbour);
if(newMovementCostToNeighbour < neighbour.gCost || !openSet.contains(neighbour)) {
neighbour.gCost = newMovementCostToNeighbour;
neighbour.hCost = getDistance(neighbour,endNode);
neighbour.parent = currentNode;

if(!openSet.contains(neighbour)) {
openSet.add(neighbour);


}
}

}

}

return openSet;

}

public ArrayList<Node> retracePath(Node startNode, Node endNode) {

ArrayList<Node> path = new ArrayList<Node>();
Node currentNode = endNode;
while(currentNode != startNode) {
path.add(currentNode);
currentNode = currentNode.parent;
}

Collections.reverse(path);
return path;
}

public float getDistance(Node nodeA, Node nodeB) {

float distanceX = round(nodeA.getWorldPosition().x,2) - round(nodeB.getWorldPosition().x,2);
float distanceY = round(nodeA.getWorldPosition().y,2) - round(nodeB.getWorldPosition().y,2);

if(distanceX > distanceY)
return 14*distanceY + 10*(distanceX-distanceY);
return 14*distanceX + 10*(distanceY-distanceX);
}
}
package com.davidalexanderkelly.game.Entities.Behaviors;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import com.badlogic.gdx.math.Vector2;
import com.davidalexanderkelly.game.Screens.PlayScreen;
import com.davidalexanderkelly.game.Tools.PathfindingWorldCreator;
public class Pathfinding {
private float depth;
PathfindingWorldCreator pathfinder;
public static float round(float d, int decimalPlace) {
BigDecimal bd = new BigDecimal(Float.toString(d));
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
return bd.floatValue();
}
public ArrayList<Node> findPath(Node startPosition,Node goalPosition, PathfindingWorldCreator pathfinder) {
this.pathfinder = pathfinder;
Node startNode = startPosition;
Node endNode = goalPosition;
depth = 0f;
ArrayList<Node> openSet = new ArrayList<Node>();
HashSet<Node> closedSet = new HashSet<Node>();
openSet.add(startNode);
while(openSet.size() > 0) {
Node currentNode = openSet.get(0);
for(int i = 1; i < openSet.size(); i++) {
if((openSet.get(i).fCost() < currentNode.fCost() || (openSet.get(i).fCost() == currentNode.fCost() && openSet.get(i).hCost < currentNode.hCost)) && depth > currentNode.fCost()) {
currentNode = openSet.get(i);
depth = currentNode.fCost();
}
}
openSet.remove(currentNode);
closedSet.add(currentNode);
if(round(currentNode.getWorldPosition().x,2) == round(endNode.getWorldPosition().x,2) && round(currentNode.getWorldPosition().y,2) == round(endNode.getWorldPosition().y,2)) {
ArrayList<Node> returnPath = new ArrayList<Node>();
returnPath = retracePath(startNode,currentNode);
return returnPath;
}
for(Node neighbour : pathfinder.getNeighbours(currentNode)){
if(closedSet.contains(neighbour)) {
continue;
}
float newMovementCostToNeighbour = currentNode.gCost + getDistance(currentNode,neighbour);
if(newMovementCostToNeighbour < neighbour.gCost || !openSet.contains(neighbour)) {
neighbour.gCost = newMovementCostToNeighbour;
neighbour.hCost = getDistance(neighbour,endNode);
neighbour.parent = currentNode;
if(!openSet.contains(neighbour)) {
openSet.add(neighbour);
}
}
}
}
return openSet;
}
public ArrayList<Node> retracePath(Node startNode, Node endNode) {
ArrayList<Node> path = new ArrayList<Node>();
Node currentNode = endNode;
while(currentNode != startNode) {
path.add(currentNode);
currentNode = currentNode.parent;
}
Collections.reverse(path);
return path;
}
public float getDistance(Node nodeA, Node nodeB) {
float distanceX = round(nodeA.getWorldPosition().x,2) - round(nodeB.getWorldPosition().x,2);
float distanceY = round(nodeA.getWorldPosition().y,2) - round(nodeB.getWorldPosition().y,2);
if(distanceX > distanceY)
return 14*distanceY + 10*(distanceX-distanceY);
return 14*distanceX + 10*(distanceY-distanceX);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
import com.davidalexanderkelly.game.Entities.Behaviors.Node;
import com.davidalexanderkelly.game.Entities.Behaviors.Pathfinding;
import com.davidalexanderkelly.game.Screens.PlayScreen;
import com.davidalexanderkelly.game.Tools.InteractableWorldCreator;
import com.davidalexanderkelly.game.Tools.PathfindingWorldCreator;
import com.davidalexanderkelly.game.Tools.TaskWorldCreator;

public class Enemy extends Sprite{
public enum State{IDLE,RUNNING};
public State currentState;
public State previousState;


private InteractableWorldCreator interactables;
private TaskWorldCreator tasks;
private Animation<TextureRegion> playerIdle;
private Animation<TextureRegion> playerRun;
private float stateTimer;
Expand Down Expand Up @@ -165,14 +165,14 @@ public State getState() {
public void setPath(int start, int end) {
moving = false;
pathfinding = new Pathfinding();
List<Node> interactables = screen.interactables.getLocations();
List<Node> interactables = screen.tasks.getLocations();
path = pathfinding.findPath(interactables.get(start),interactables.get(end),screen.pathfinder);
}


public void defineEnemy() {
BodyDef bodyDefinition = new BodyDef();
bodyDefinition.position.set(screen.interactables.getLocations().get(0).getWorldPosition().x,screen.interactables.getLocations().get(0).getWorldPosition().y);
bodyDefinition.position.set(screen.tasks.getLocations().get(0).getWorldPosition().x,screen.tasks.getLocations().get(0).getWorldPosition().y);
bodyDefinition.type = BodyDef.BodyType.DynamicBody;
box2dBody = world.createBody(bodyDefinition);
FixtureDef fixtureDefinition = new FixtureDef();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,18 @@ public void definePlayer() {
FixtureDef fixtureDefinition = new FixtureDef();
CircleShape shape = new CircleShape();
shape.setRadius(5 / SpaceGamePrototype.PixelsPerMetre);

fixtureDefinition.filter.categoryBits = SpaceGamePrototype.PLAYER_BIT;
fixtureDefinition.filter.maskBits = SpaceGamePrototype.DEFAULT_BIT | SpaceGamePrototype.TELEPORTER_BIT;
fixtureDefinition.shape = shape;

box2dBody.createFixture(fixtureDefinition);

CircleShape sensor = new CircleShape();
sensor.setRadius(6 / SpaceGamePrototype.PixelsPerMetre);
fixtureDefinition.shape = sensor;
fixtureDefinition.isSensor = true;
box2dBody.createFixture(fixtureDefinition).setUserData("player");

}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.davidalexanderkelly.game.Entities;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Filter;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.davidalexanderkelly.game.SpaceGamePrototype;

public class Teleporter {

private float x;
private float y;
private Body box2dBody;

public Teleporter(float x, float y) {
this.x = x;
this.y = y;


}

public void collide() {
System.out.println("Teleport");
}





}
Loading