This commit is contained in:
Ziver Koc 2007-04-17 15:55:22 +00:00
parent 98cd3b8287
commit 0bfc621ade
6 changed files with 32 additions and 20 deletions

View file

@ -5,7 +5,6 @@ import java.util.List;
import java.util.Random;
import ei.engine.math.Vector2i;
import ei.game.algo.AStarNode2D.Heuristic;
import ei.game.gamestate.InGameState;
public class AStar{
@ -43,13 +42,13 @@ public class AStar{
// Create the map.
map = new AStarNode2D[width][hight];
for(int x = 0, nodeId = 0; width > x; x++) {
for(int y = 0; hight > y; y++, nodeId++) {
map[x][y] = new AStarNode2D(x, y, nodeId);
if(!InGameState.getMap().isPosEmpty(x, y)) {
map[x][y].setBlocked(true);
}
}
}
@ -60,21 +59,21 @@ public class AStar{
List<AStarNeighbour> neighbours = new LinkedList<AStarNeighbour>();
if(y-1 >= 0)
neighbours.add(new AStarNeighbour(map[x][y-1], AStarNeighbour.Location.Adjacent)); // North.
neighbours.add(new AStarNeighbour(map[x][y-1], AStarNeighbour.Location.Diagonal)); // North.
if(x+1 < width && y-1 > 0)
neighbours.add(new AStarNeighbour(map[x+1][y-1], AStarNeighbour.Location.Diagonal)); // North-East.
neighbours.add(new AStarNeighbour(map[x+1][y-1], AStarNeighbour.Location.Adjacent)); // North-East.
if(x+1 < width)
neighbours.add(new AStarNeighbour(map[x+1][y], AStarNeighbour.Location.Adjacent)); // East.
neighbours.add(new AStarNeighbour(map[x+1][y], AStarNeighbour.Location.Diagonal)); // East.
if(x+1 < width && y+1 < hight)
neighbours.add(new AStarNeighbour(map[x+1][y+1], AStarNeighbour.Location.Diagonal)); // South-East.
neighbours.add(new AStarNeighbour(map[x+1][y+1], AStarNeighbour.Location.Adjacent)); // South-East.
if(y+1 < hight)
neighbours.add(new AStarNeighbour(map[x][y+1], AStarNeighbour.Location.Adjacent)); // South.
neighbours.add(new AStarNeighbour(map[x][y+1], AStarNeighbour.Location.Diagonal)); // South.
if(x-1 >= 0 && y+1 < hight)
neighbours.add(new AStarNeighbour(map[x-1][y+1], AStarNeighbour.Location.Diagonal)); // South-West.
neighbours.add(new AStarNeighbour(map[x-1][y+1], AStarNeighbour.Location.Adjacent)); // South-West.
if(x-1 >= 0)
neighbours.add(new AStarNeighbour(map[x-1][y], AStarNeighbour.Location.Adjacent)); // West.
if(x-1 >= 0 && y-1 > 0) neighbours.add(new AStarNeighbour(map[x-1][y-1], AStarNeighbour.Location.Diagonal)); // North-West.
neighbours.add(new AStarNeighbour(map[x-1][y], AStarNeighbour.Location.Diagonal)); // West.
if(x-1 >= 0 && y-1 > 0)
neighbours.add(new AStarNeighbour(map[x-1][y-1], AStarNeighbour.Location.Adjacent)); // North-West.
map[x][y].setNeighbours(neighbours);

View file

@ -9,7 +9,7 @@ import ei.game.algo.AStarNeighbour.Location;
*/
public class AStarNode2D extends AStarNode {
protected final static float adjacentCost = 1;
protected final static float diagonalCost = (float)Math.sqrt(2)*adjacentCost+1;
protected final static float diagonalCost = (float)Math.sqrt(2)*adjacentCost;
protected final static float tieBreaker = adjacentCost/(1024f/1024f);
public enum Heuristic {

View file

@ -23,9 +23,14 @@ public class InGameState extends GameState{
super.getInput().addInput(mouse);
HumanPlayer player = new HumanPlayer();
player.addUnit(new Tank());
player.addUnit(new Tank(1,0));
Tank t1 = new Tank();
t1.setLife(10);
player.addUnit(t1);
Tank t2 = new Tank(1,0);
t2.setLife(30);
player.addUnit(t2);
player.addUnit(new Tank(2,0));
rootNode.add(player.getNode());
PlayerHandler.getInstance().addPlayer(player);
}

View file

@ -31,6 +31,7 @@ public abstract class GameEntity{
if(life > max_life){
max_life = life;
}
if(getSelection() != null)getSelection().setValue(l);
}
/**

View file

@ -64,6 +64,17 @@ public class SelectBox {
* @param v The value of the health bar
*/
public void setValue(int v){
value.setSize(new Vector2f((v/max)*width,height));
value.setSize(new Vector2f(((float)v/max)*width,height));//(v/max)*width
value.setLocation(new Vector2f(-width/2+(((float)v/max)*width)/2,-width/2+height/2));
if(((float)v/max) < 0.15f){
value.getTexture().setColor(new Vector4f(1.0f, 0f, 0f, 0.6f));
}
else if(((float)v/max) < 0.50f){
value.getTexture().setColor(new Vector4f(1.0f, .95f, 0, 0.6f));
}
else{
value.getTexture().setColor(new Vector4f(0.3f, 1.0f, 0.3f, 0.6f));
}
}
}

View file

@ -97,17 +97,13 @@ public abstract class Unit extends GameEntity{
* Updating the unit
*/
public void update() {
getSelection().setValue(getLife());
if(moveTo != null) {
Vector2i moveVect = new Vector2i((int)moveTo.getX(),(int)moveTo.getY());
//Vector2i currentVect = new Vector2i((int)unitNode.getLocation().getX(), (int)unitNode.getLocation().getY());
int moveXminus = moveVect.getX()-oldVect.getX();
int moveYminus = moveVect.getY()-oldVect.getY();
System.out.println("moveVect: "+moveVect);
//System.out.println("currentVect: "+currentVect);
float divideY = (moveVect.getY()+1)/(oldVect.getY()+1);
float divideX = (moveVect.getX()+1)/(oldVect.getX()+1);