Skip to content

Commit c202a79

Browse files
committed
v1.7.1 Alpha - A* Search
2 parents 921f02e + c910947 commit c202a79

File tree

12 files changed

+261
-52
lines changed

12 files changed

+261
-52
lines changed

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
JavaGame Alpha v1.7
1+
JavaGame Alpha v1.7.1 - 1 Year anniversary
22
=====================
33

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

66
#####What is JavaGame?
7-
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.
7+
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.
88

99
#####Why name it JavaGame?
10-
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
10+
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
11+
12+
#####Play the Game
13+
* For latest version get
14+
* [v1.7.1](https://github.com/redomar/JavaGame/releases/tag/v1.7.1)
15+
* For multiplayer enabled get
16+
* [v1.5.4](https://github.com/redomar/JavaGame/releases/tag/v1.5.4)
1117

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

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

jar/javagame.jar

29.4 KB
Binary file not shown.

src/com/redomar/game/Game.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class Game extends Canvas implements Runnable {
3434

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

5859
private static JFrame frame;
5960

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

89+
/**
90+
* @author Redomar
91+
* @version Alpha 1.7.1
92+
*/
8893
public Game() {
8994
setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
9095
setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
@@ -100,7 +105,7 @@ public Game() {
100105
getFrame().setVisible(true);
101106

102107
setDevMode(false);
103-
setDevTime(false);
108+
setClosing(false);
104109
}
105110

106111
public void init() {
@@ -312,7 +317,7 @@ public void render() {
312317
g.drawString("Press Q to quit", (getWidth()/2)-("Press Q to quit".length()*3), getHeight() -17);
313318
g.setColor(Color.YELLOW);
314319
g.drawString(time.getTime(), (getWidth() - 58), (getHeight() - 3));
315-
status(g, isDevMode());
320+
status(g, isDevMode(), isClosing());
316321
g.setColor(Color.WHITE);
317322
if (noAudioDevice == true) {
318323
g.setColor(Color.RED);
@@ -347,7 +352,7 @@ public void render() {
347352
bs.show();
348353
}
349354

350-
private void status(Graphics g, boolean TerminalMode) {
355+
private void status(Graphics g, boolean TerminalMode, boolean TerminalQuit) {
351356
if (TerminalMode == true){
352357
g.setColor(Color.GREEN);
353358
g.drawString("JavaGame Stats", 0, 10);
@@ -358,6 +363,13 @@ private void status(Graphics g, boolean TerminalMode) {
358363
g.drawString("Foot Steps: " + steps, 0, 40);
359364
g.drawString("NPC: " + WordUtils.capitalize(String.valueOf(isNpc())) , 0, 55);
360365
}
366+
if (TerminalQuit == true){
367+
g.setColor(Color.BLACK);
368+
g.fillRect(0, 0, getWidth(), getHeight());
369+
g.setColor(Color.RED);
370+
g.drawString("Shutting down the Game", (getWidth()/2)-70, (getHeight()/2)-8);
371+
g.dispose();
372+
}
361373
}
362374

363375
public static void main(String[] args) {
@@ -545,19 +557,19 @@ public void setInput(InputHandler input) {
545557
}
546558

547559
public static boolean isDevMode() {
548-
return devMode[0];
560+
return devMode;
549561
}
550562

551563
public static void setDevMode(boolean devMode) {
552-
Game.devMode[0] = devMode;
564+
Game.devMode = devMode;
553565
}
554566

555-
public static boolean isDevTime() {
556-
return devMode[1];
567+
public static boolean isClosing() {
568+
return closingMode;
557569
}
558570

559-
public static void setDevTime(boolean devTime) {
560-
Game.devMode[1] = devTime;
571+
public static void setClosing(boolean closing) {
572+
Game.closingMode = closing;
561573
}
562574

563575
}

src/com/redomar/game/InputHandler.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,21 @@ public void toggleKey(int keyCode, boolean isPressed) {
106106
}
107107
}
108108
if (keyCode == KeyEvent.VK_Q){
109+
Game.setClosing(true);
110+
try {
111+
Thread.sleep(1000);
112+
} catch (InterruptedException e) {
113+
e.printStackTrace();
114+
}
109115
Game.getLevel().removeEntity(Game.getPlayer().getSantizedUsername());
110116
Game.setRunning(false);
111117
Game.getFrame().dispose();
112118
System.exit(1);
113119
}
114120

115121
if (keyCode == KeyEvent.VK_BACK_QUOTE){
116-
if (Game.isDevTime() == false && Game.isDevMode() == false){
122+
if (Game.isClosing() == false && Game.isDevMode() == false){
117123
Game.setDevMode(true);
118-
Game.setDevTime(true);
119124
new Thread(new SleepThread());
120125
}
121126
}

src/com/redomar/game/entities/Dummy.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.redomar.game.gfx.Colours;
88
import com.redomar.game.gfx.Screen;
99
import com.redomar.game.level.LevelHandler;
10+
import com.redomar.game.level.Node;
1011

1112
public class Dummy extends Mob {
1213

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

2125
private Swim swim;
2226

2327
public Dummy(LevelHandler level, String name, int x, int y, int shirtCol,
2428
int faceCol) {
25-
super(level, "h", x, y, speed);
29+
super(level, "h", x, y, speed, collisionBoders);
2630
this.faceCol = faceCol;
2731
this.shirtCol = shirtCol;
2832
this.colour = Colours.get(-1, 111, shirtCol, faceCol);
2933
}
3034

3135
public void tick() {
3236

33-
List<Player> players = level.getPlayers(this, 8);
34-
if (players.size() > 0) {
35-
followMovementAI((int) getX(), (int) getY(), (int) Game.getPlayer().getX(), (int) Game
36-
.getPlayer().getY(), xa, ya, speed, this);
37-
}else{
38-
isMoving = false;
39-
}
37+
//List<Player> players = level.getPlayers(this, 8);
38+
aStarMovementAI((int) getX(), (int) getY(), (int) Game.getPlayer().getX(), (int) Game
39+
.getPlayer().getY(), xa, ya, speed, this, path, time);
40+
4041

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

5152
public void render(Screen screen) {
53+
time++;
5254
int xTile = 8;
5355
int yTile = 28;
5456
int walkingSpeed = 4;

src/com/redomar/game/entities/Mob.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.redomar.game.entities;
22

3+
import java.util.List;
34
import java.util.Random;
45

56
import com.redomar.game.level.LevelHandler;
7+
import com.redomar.game.level.Node;
68
import com.redomar.game.level.tiles.Tile;
9+
import com.redomar.game.lib.utils.Vector2i;
710

811
public abstract class Mob extends Entity {
912

@@ -19,13 +22,21 @@ public abstract class Mob extends Entity {
1922
protected boolean isMuddy = false;
2023
protected boolean changeLevels = false;
2124
protected int ticker;
22-
23-
public Mob(LevelHandler level, String name, int x, int y, double speed) {
25+
/**
26+
* [0] Contains the <strong>xMin</strong><br>
27+
* [1] Contains the <strong>xMax</strong><br>
28+
* [2] Contains the <strong>yMin</strong><br>
29+
* [3] Contains the <strong>yMax
30+
*/
31+
protected int[] collisionBoders = new int[4];
32+
33+
public Mob(LevelHandler level, String name, int x, int y, double speed, int[] collisionBoders) {
2434
super(level);
2535
this.name = name;
2636
this.setX(x);
2737
this.setY(y);
2838
this.speed = speed;
39+
this.collisionBoders = collisionBoders;
2940
}
3041

3142
public void move(double xa, double ya) {
@@ -87,10 +98,10 @@ public void move(double xa, double ya) {
8798
}
8899

89100
public boolean hasCollided(double xa, double ya){
90-
int xMin = 0;
91-
int xMax = 7;
92-
int yMin = 3;
93-
int yMax = 7;
101+
int xMin = collisionBoders[0];
102+
int xMax = collisionBoders[1];
103+
int yMin = collisionBoders[2];
104+
int yMax = collisionBoders[3];
94105

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

156167
return false;
157168
}
169+
170+
protected void aStarMovementAI(int x, int y, int px, int py, double xa,
171+
double ya, double speed, Mob mob, List<Node> path, int time){
172+
xa = 0;
173+
ya = 0;
174+
Vector2i start = new Vector2i(x >> 3, y >> 3);
175+
Vector2i goal = new Vector2i(px >> 3, py >> 3);
176+
path = level.findPath(start, goal);
177+
if(path != null) {
178+
if(path.size() > 0){
179+
Vector2i vector = path.get(path.size() - 1).tile;
180+
if(x < vector.getX() << 3) xa =+ speed;
181+
if(x > vector.getX() << 3) xa =- speed;
182+
if(y < vector.getY() << 3) ya =+ speed;
183+
if(y > vector.getY() << 3) ya =- speed;
184+
moveMob(xa, ya, mob);
185+
}
186+
}
187+
}
158188

159189
protected void followMovementAI(int x, int y, int px, int py, double xa,
160190
double ya, double speed, Mob mob) {

src/com/redomar/game/entities/Player.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ public class Player extends Mob {
2424
private String userName;
2525
private boolean[] swimType;
2626
private int[] swimColour;
27+
private static int[] collisionBoders = {-2, 8, 0, 7};
2728

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

3031
public Player(LevelHandler level, int x, int y, InputHandler input,
3132
String userName, int shirtCol, int faceCol) {
32-
super(level, "Player", x, y, speed);
33+
super(level, "Player", x, y, speed, collisionBoders);
3334
this.input = input;
3435
this.userName = userName;
3536
this.faceCol = faceCol;

src/com/redomar/game/level/LevelHandler.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.io.File;
55
import java.io.IOException;
66
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.Comparator;
79
import java.util.List;
810
import java.util.logging.Level;
911

@@ -15,6 +17,7 @@
1517
import com.redomar.game.entities.PlayerMP;
1618
import com.redomar.game.gfx.Screen;
1719
import com.redomar.game.level.tiles.Tile;
20+
import com.redomar.game.lib.utils.Vector2i;
1821
import com.redomar.game.net.packets.Packet01Disconnect;
1922

2023
public class LevelHandler {
@@ -26,6 +29,16 @@ public class LevelHandler {
2629
private String imagePath;
2730
private BufferedImage image;
2831

32+
private Comparator<Node> nodeSorter = new Comparator<Node>() {
33+
34+
public int compare(Node n0, Node n1) {
35+
if(n1.fCost < n0.fCost) return +1;
36+
if(n1.fCost > n0.fCost) return -1;
37+
return 0;
38+
}
39+
40+
};
41+
2942
public LevelHandler(String imagePath) {
3043

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

210+
public List<Node> findPath(Vector2i start, Vector2i goal){
211+
List<Node> openList = new ArrayList<Node>();
212+
List<Node> closedList = new ArrayList<Node>();
213+
Node current = new Node(start, null, 0, getDistance(start, goal));
214+
openList.add(current);
215+
while(openList.size() > 0){
216+
Collections.sort(openList, nodeSorter);
217+
current = openList.get(0);
218+
if(current.tile.equals(goal)){
219+
List<Node> path = new ArrayList<Node>();
220+
while (current.parent != null) {
221+
path.add(current);
222+
current = current.parent;
223+
}
224+
openList.clear();
225+
closedList.clear();
226+
return path;
227+
}
228+
openList.remove(current);
229+
closedList.add(current);
230+
for(int i = 0; i < 9; i++){
231+
if(i == 4) continue;
232+
int x = current.tile.getX();
233+
int y = current.tile.getY();
234+
int xi = (i % 3) - 1;
235+
int yi = (i / 3) - 1;
236+
Tile at = getTile(x + xi,y + yi);
237+
if(at == null) continue;
238+
if(at.isSolid()) continue;
239+
Vector2i a = new Vector2i(x + xi, y + yi);
240+
double gCost = current.gCost + (getDistance(current.tile, a) == 1 ? 1 : 0.95);
241+
double hCost = getDistance(a, goal);
242+
Node node = new Node(a, current, gCost, hCost);
243+
if(isVectorInList(closedList, a) && gCost >= node.gCost) continue;
244+
if(!isVectorInList(openList, a) || gCost < node.gCost) openList.add(node);
245+
}
246+
}
247+
closedList.clear();
248+
return null;
249+
}
250+
251+
private boolean isVectorInList(List<Node> list, Vector2i vector){
252+
for(Node n : list){
253+
if(n.tile.equals(vector)) return true;
254+
}
255+
return false;
256+
}
257+
258+
private double getDistance(Vector2i tile, Vector2i goal){
259+
double dx = tile.getX() - goal.getX();
260+
double dy = tile.getY() - goal.getY();
261+
return Math.sqrt(dx * dx + dy * dy);
262+
}
263+
197264
public List<Entity> getEntities(Entity e, int radius){
198265
List<Entity> result = new ArrayList<Entity>();
199266
int ex = (int) e.getX();

0 commit comments

Comments
 (0)