Skip to content

Commit

Permalink
v1.7.1 Alpha - A* Search
Browse files Browse the repository at this point in the history
  • Loading branch information
redomar committed May 6, 2014
2 parents 921f02e + c910947 commit c202a79
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 52 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
JavaGame Alpha v1.7
JavaGame Alpha v1.7.1 - 1 Year anniversary
=====================

[![Ohloh Stats](https://www.ohloh.net/p/JavaGame/widgets/project_thin_badge.gif)](https://www.ohloh.net/p/JavaGame)

#####What is JavaGame?
JavaGame is a game project that have been working on since May. I have added many features to the game, but now I am running out of ideas.
JavaGame is a game project that have been working on since May 2013. I have added many features to the game over the last year and I plan on adding even more features. This game is purely for my own sake to practice my skills in Java.

#####Why name it JavaGame?
Well i'm still not sure what exactly i'm going to do with it, and I haven't thought of a suitable name either
Well i'm still not sure what exactly i'm going to do with it, and I haven't thought of a suitable name either. I hope to change the name in the near future

#####Play the Game
* For latest version get
* [v1.7.1](https://github.com/redomar/JavaGame/releases/tag/v1.7.1)
* For multiplayer enabled get
* [v1.5.4](https://github.com/redomar/JavaGame/releases/tag/v1.5.4)

#####How to download this repository for eclipse tutorial
Watch this video [here](http://youtu.be/_3nCgac3KKM) or checkout the [GitHub Pages](http://redomar.github.io/JavaGame/)
Expand All @@ -22,9 +28,8 @@ Watch this video [here](http://youtu.be/_3nCgac3KKM) or checkout the [GitHub Pag
* Click Add Class Folder
* Check the /res folder and hit finish
* Make the changes in the /src folder
* Commit your changes (```git commit -am "Change Title"```)
* Commit your changes (```git commit -m "Change Title"```)
* Push to the branch (```git push origin my_branch```)
* Open a [Pull Request](https://github.com/redomar/JavaGame/pull/new/master)

[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/redomar/javagame/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/redomar/javagame/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
Binary file modified jar/javagame.jar
Binary file not shown.
34 changes: 23 additions & 11 deletions src/com/redomar/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class Game extends Canvas implements Runnable {

// Setting the size and name of the frame/canvas
private static final long serialVersionUID = 1L;
private static final String game_Version = "v1.7 Alpha";
private static final String game_Version = "v1.7.1 Alpha";
private static final int WIDTH = 160;
private static final int HEIGHT = (WIDTH / 3 * 2);
private static final int SCALE = 3;
Expand All @@ -53,7 +53,8 @@ public class Game extends Canvas implements Runnable {
private static int fps;
private static int tps;
private static int steps;
private static boolean[] devMode = new boolean[2];
private static boolean devMode;
private static boolean closingMode;

private static JFrame frame;

Expand Down Expand Up @@ -85,6 +86,10 @@ public class Game extends Canvas implements Runnable {
private GameServer socketServer;
private Printing print = new Printing();

/**
* @author Redomar
* @version Alpha 1.7.1
*/
public Game() {
setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
Expand All @@ -100,7 +105,7 @@ public Game() {
getFrame().setVisible(true);

setDevMode(false);
setDevTime(false);
setClosing(false);
}

public void init() {
Expand Down Expand Up @@ -312,7 +317,7 @@ public void render() {
g.drawString("Press Q to quit", (getWidth()/2)-("Press Q to quit".length()*3), getHeight() -17);
g.setColor(Color.YELLOW);
g.drawString(time.getTime(), (getWidth() - 58), (getHeight() - 3));
status(g, isDevMode());
status(g, isDevMode(), isClosing());
g.setColor(Color.WHITE);
if (noAudioDevice == true) {
g.setColor(Color.RED);
Expand Down Expand Up @@ -347,7 +352,7 @@ public void render() {
bs.show();
}

private void status(Graphics g, boolean TerminalMode) {
private void status(Graphics g, boolean TerminalMode, boolean TerminalQuit) {
if (TerminalMode == true){
g.setColor(Color.GREEN);
g.drawString("JavaGame Stats", 0, 10);
Expand All @@ -358,6 +363,13 @@ private void status(Graphics g, boolean TerminalMode) {
g.drawString("Foot Steps: " + steps, 0, 40);
g.drawString("NPC: " + WordUtils.capitalize(String.valueOf(isNpc())) , 0, 55);
}
if (TerminalQuit == true){
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.RED);
g.drawString("Shutting down the Game", (getWidth()/2)-70, (getHeight()/2)-8);
g.dispose();
}
}

public static void main(String[] args) {
Expand Down Expand Up @@ -545,19 +557,19 @@ public void setInput(InputHandler input) {
}

public static boolean isDevMode() {
return devMode[0];
return devMode;
}

public static void setDevMode(boolean devMode) {
Game.devMode[0] = devMode;
Game.devMode = devMode;
}

public static boolean isDevTime() {
return devMode[1];
public static boolean isClosing() {
return closingMode;
}

public static void setDevTime(boolean devTime) {
Game.devMode[1] = devTime;
public static void setClosing(boolean closing) {
Game.closingMode = closing;
}

}
9 changes: 7 additions & 2 deletions src/com/redomar/game/InputHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,21 @@ public void toggleKey(int keyCode, boolean isPressed) {
}
}
if (keyCode == KeyEvent.VK_Q){
Game.setClosing(true);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Game.getLevel().removeEntity(Game.getPlayer().getSantizedUsername());
Game.setRunning(false);
Game.getFrame().dispose();
System.exit(1);
}

if (keyCode == KeyEvent.VK_BACK_QUOTE){
if (Game.isDevTime() == false && Game.isDevMode() == false){
if (Game.isClosing() == false && Game.isDevMode() == false){
Game.setDevMode(true);
Game.setDevTime(true);
new Thread(new SleepThread());
}
}
Expand Down
18 changes: 10 additions & 8 deletions src/com/redomar/game/entities/Dummy.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.redomar.game.gfx.Colours;
import com.redomar.game.gfx.Screen;
import com.redomar.game.level.LevelHandler;
import com.redomar.game.level.Node;

public class Dummy extends Mob {

Expand All @@ -17,26 +18,26 @@ public class Dummy extends Mob {
private boolean[] swimType;
private int[] swimColour;
private static double speed = 0.75;
private List<Node> path = null;
private int time = 0;
private static int[] collisionBoders = {0, 7, 0, 7};

private Swim swim;

public Dummy(LevelHandler level, String name, int x, int y, int shirtCol,
int faceCol) {
super(level, "h", x, y, speed);
super(level, "h", x, y, speed, collisionBoders);
this.faceCol = faceCol;
this.shirtCol = shirtCol;
this.colour = Colours.get(-1, 111, shirtCol, faceCol);
}

public void tick() {

List<Player> players = level.getPlayers(this, 8);
if (players.size() > 0) {
followMovementAI((int) getX(), (int) getY(), (int) Game.getPlayer().getX(), (int) Game
.getPlayer().getY(), xa, ya, speed, this);
}else{
isMoving = false;
}
//List<Player> players = level.getPlayers(this, 8);
aStarMovementAI((int) getX(), (int) getY(), (int) Game.getPlayer().getX(), (int) Game
.getPlayer().getY(), xa, ya, speed, this, path, time);


setSwim(new Swim(level, (int) getX(), (int) getY()));
swimType = getSwim().swimming(isSwimming, isMagma, isMuddy);
Expand All @@ -49,6 +50,7 @@ public void tick() {
}

public void render(Screen screen) {
time++;
int xTile = 8;
int yTile = 28;
int walkingSpeed = 4;
Expand Down
42 changes: 36 additions & 6 deletions src/com/redomar/game/entities/Mob.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.redomar.game.entities;

import java.util.List;
import java.util.Random;

import com.redomar.game.level.LevelHandler;
import com.redomar.game.level.Node;
import com.redomar.game.level.tiles.Tile;
import com.redomar.game.lib.utils.Vector2i;

public abstract class Mob extends Entity {

Expand All @@ -19,13 +22,21 @@ public abstract class Mob extends Entity {
protected boolean isMuddy = false;
protected boolean changeLevels = false;
protected int ticker;

public Mob(LevelHandler level, String name, int x, int y, double speed) {
/**
* [0] Contains the <strong>xMin</strong><br>
* [1] Contains the <strong>xMax</strong><br>
* [2] Contains the <strong>yMin</strong><br>
* [3] Contains the <strong>yMax
*/
protected int[] collisionBoders = new int[4];

public Mob(LevelHandler level, String name, int x, int y, double speed, int[] collisionBoders) {
super(level);
this.name = name;
this.setX(x);
this.setY(y);
this.speed = speed;
this.collisionBoders = collisionBoders;
}

public void move(double xa, double ya) {
Expand Down Expand Up @@ -87,10 +98,10 @@ public void move(double xa, double ya) {
}

public boolean hasCollided(double xa, double ya){
int xMin = 0;
int xMax = 7;
int yMin = 3;
int yMax = 7;
int xMin = collisionBoders[0];
int xMax = collisionBoders[1];
int yMin = collisionBoders[2];
int yMax = collisionBoders[3];

for (int x = xMin; x < xMax; x++) {
if (isSolid((int) xa, (int) ya, x, yMin)) {
Expand Down Expand Up @@ -155,6 +166,25 @@ protected boolean isSolid(int xa, int ya, int x, int y) {

return false;
}

protected void aStarMovementAI(int x, int y, int px, int py, double xa,
double ya, double speed, Mob mob, List<Node> path, int time){
xa = 0;
ya = 0;
Vector2i start = new Vector2i(x >> 3, y >> 3);
Vector2i goal = new Vector2i(px >> 3, py >> 3);
path = level.findPath(start, goal);
if(path != null) {
if(path.size() > 0){
Vector2i vector = path.get(path.size() - 1).tile;
if(x < vector.getX() << 3) xa =+ speed;
if(x > vector.getX() << 3) xa =- speed;
if(y < vector.getY() << 3) ya =+ speed;
if(y > vector.getY() << 3) ya =- speed;
moveMob(xa, ya, mob);
}
}
}

protected void followMovementAI(int x, int y, int px, int py, double xa,
double ya, double speed, Mob mob) {
Expand Down
3 changes: 2 additions & 1 deletion src/com/redomar/game/entities/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ public class Player extends Mob {
private String userName;
private boolean[] swimType;
private int[] swimColour;
private static int[] collisionBoders = {-2, 8, 0, 7};

public static String guestPlayerName = customeName.setName("Player ");

public Player(LevelHandler level, int x, int y, InputHandler input,
String userName, int shirtCol, int faceCol) {
super(level, "Player", x, y, speed);
super(level, "Player", x, y, speed, collisionBoders);
this.input = input;
this.userName = userName;
this.faceCol = faceCol;
Expand Down
67 changes: 67 additions & 0 deletions src/com/redomar/game/level/LevelHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Level;

Expand All @@ -15,6 +17,7 @@
import com.redomar.game.entities.PlayerMP;
import com.redomar.game.gfx.Screen;
import com.redomar.game.level.tiles.Tile;
import com.redomar.game.lib.utils.Vector2i;
import com.redomar.game.net.packets.Packet01Disconnect;

public class LevelHandler {
Expand All @@ -26,6 +29,16 @@ public class LevelHandler {
private String imagePath;
private BufferedImage image;

private Comparator<Node> nodeSorter = new Comparator<Node>() {

public int compare(Node n0, Node n1) {
if(n1.fCost < n0.fCost) return +1;
if(n1.fCost > n0.fCost) return -1;
return 0;
}

};

public LevelHandler(String imagePath) {

if (imagePath != null) {
Expand Down Expand Up @@ -194,6 +207,60 @@ public void movePlayer(String username, int x, int y, int numSteps,
player.setMovingDir(movingDir);
}

public List<Node> findPath(Vector2i start, Vector2i goal){
List<Node> openList = new ArrayList<Node>();
List<Node> closedList = new ArrayList<Node>();
Node current = new Node(start, null, 0, getDistance(start, goal));
openList.add(current);
while(openList.size() > 0){
Collections.sort(openList, nodeSorter);
current = openList.get(0);
if(current.tile.equals(goal)){
List<Node> path = new ArrayList<Node>();
while (current.parent != null) {
path.add(current);
current = current.parent;
}
openList.clear();
closedList.clear();
return path;
}
openList.remove(current);
closedList.add(current);
for(int i = 0; i < 9; i++){
if(i == 4) continue;
int x = current.tile.getX();
int y = current.tile.getY();
int xi = (i % 3) - 1;
int yi = (i / 3) - 1;
Tile at = getTile(x + xi,y + yi);
if(at == null) continue;
if(at.isSolid()) continue;
Vector2i a = new Vector2i(x + xi, y + yi);
double gCost = current.gCost + (getDistance(current.tile, a) == 1 ? 1 : 0.95);
double hCost = getDistance(a, goal);
Node node = new Node(a, current, gCost, hCost);
if(isVectorInList(closedList, a) && gCost >= node.gCost) continue;
if(!isVectorInList(openList, a) || gCost < node.gCost) openList.add(node);
}
}
closedList.clear();
return null;
}

private boolean isVectorInList(List<Node> list, Vector2i vector){
for(Node n : list){
if(n.tile.equals(vector)) return true;
}
return false;
}

private double getDistance(Vector2i tile, Vector2i goal){
double dx = tile.getX() - goal.getX();
double dy = tile.getY() - goal.getY();
return Math.sqrt(dx * dx + dy * dy);
}

public List<Entity> getEntities(Entity e, int radius){
List<Entity> result = new ArrayList<Entity>();
int ex = (int) e.getX();
Expand Down
Loading

0 comments on commit c202a79

Please sign in to comment.