Archived
1
0

-Fixed moving, hitting an edge no longer crashes the game

-Other stuff that I forget now...

Signed-off-by: Tony Grosinger <github2@grosinger.net>
This commit is contained in:
Tony Grosinger 2011-04-23 20:03:45 -07:00
parent 70a933ac09
commit f4bae64bbd
17 changed files with 198 additions and 23 deletions

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

View File

@ -2,11 +2,21 @@ package net.grosinger.nomads;
/*
* Anything that is on the theWorld that is not a drone of some sort is a building
*
* //Safe Zones - Measured in radius of a square
* TownHall - 3
* RepairShop - 2
* UpgradeShop - 2
* PoliceStation - 3
* Home - 1
*
*
*
*/
public class Building implements GameObject {
Structure structure;
String name;
// Can't decide if I want to add Drone Houses as another enum item or as a
// separate class that extends Building
public enum Structure {
@ -15,6 +25,24 @@ public class Building implements GameObject {
public Building(Structure thisBuilding) {
structure = thisBuilding;
switch (structure) {
case TOWNHALL: {
name = "TownHall";
break;
}
case REPAIRSHOP: {
name = "RepairShop";
break;
}
case UPGRADESHOP: {
name = "UpgradeShop";
break;
}
case POLICESTATION: {
name = "PoliceStation";
break;
}
}
}
@Override
@ -24,7 +52,7 @@ public class Building implements GameObject {
@Override
public void setName(String newName) {
name = newName;
name = newName;
}
/*

View File

@ -56,6 +56,10 @@ public class DroneListItem {
previous = thePrevious;
current = theCurrent;
visibleDistance = 15;
speed = 1;
// Place itself in the world
Nomads.awesomeWorld.placeNewDrone(this);
// Give the Drone it's tools
yourTools = new DroneTools(current, this);
@ -231,6 +235,26 @@ public class DroneListItem {
visibleDistance = newDistance;
}
/**
* Used when adding the drone to the map
*
* @param newY
* <code>int</code> new X location
*/
public void setX(int newX) {
x = newX;
}
/**
* Used when adding the drone to the map
*
* @param newY
* <code>int</code> new Y location
*/
public void setY(int newY) {
y = newY;
}
/**
* Increases the Visible Distance by specified amount
*
@ -265,20 +289,32 @@ public class DroneListItem {
return true;
}
case North: {
moveDrone(Direction.N);
return true;
if (yourTools.canMoveNorth()) {
moveDrone(Direction.N);
return true;
} else
return false;
}
case South: {
moveDrone(Direction.S);
return true;
if (yourTools.canMoveSouth()) {
moveDrone(Direction.S);
return true;
} else
return false;
}
case East: {
moveDrone(Direction.E);
return true;
if (yourTools.canMoveEast()) {
moveDrone(Direction.E);
return true;
} else
return false;
}
case West: {
moveDrone(Direction.W);
return true;
if (yourTools.canMoveWest()) {
moveDrone(Direction.W);
return true;
} else
return false;
}
case Upgrade: {
// TODO - Implement upgrade
@ -322,6 +358,14 @@ public class DroneListItem {
break;
}
}
// Make the move
Nomads.awesomeWorld.moveObjectAt(getX(), getY(), amountN, amountE);
// Update the saved coordinates
if (amountN != 0)
setY(getY() + amountN);
if (amountE != 0)
setX(getX() + amountE);
}
}

View File

@ -163,7 +163,16 @@ public class DroneTeam {
* <code>DroneListItem</code> that needs to be removed
*/
public void removeDrone(DroneListItem toRemove) {
// TODO - Implement removeDrone
DroneListItem firstDrone = first;
while (firstDrone != null) {
if (firstDrone.getX() == toRemove.getX() && firstDrone.getY() == toRemove.getY()) {
// Make other two drones pass over it
firstDrone.getPrevious().setNext(firstDrone.getNext());
firstDrone.getNext().setPrevious(firstDrone.getPrevious());
break;
} else
firstDrone = firstDrone.getNext();
}
}
/**

View File

@ -8,10 +8,12 @@ public class DroneTools {
private Drone referredDrone;
private DroneListItem listItem;
private int worldSize;
public DroneTools(Drone aDrone, DroneListItem droneParent) {
referredDrone = aDrone;
listItem = droneParent;
worldSize = Nomads.awesomeWorld.getWorldSize();
}
/**
@ -43,19 +45,61 @@ public class DroneTools {
// Movement
/**
* Can drone move 1 space to the North
*
* @return <code>Boolean</code> - can move
*/
public boolean canMoveNorth() {
return Nomads.awesomeWorld.getWorldGrid()[getY() + 1][getX()] == null;
if (getY() < worldSize-1)
return Nomads.awesomeWorld.getWorldGrid()[getX()][getY() + 1] == null;
else
return false;
}
/**
* Can drone move 1 space to the South
*
* @return <code>Boolean</code> - can move
*/
public boolean canMoveSouth() {
return Nomads.awesomeWorld.getWorldGrid()[getY() - 1][getX()] == null;
if (getY() > 0)
return Nomads.awesomeWorld.getWorldGrid()[getX()][getY() - 1] == null;
else
return false;
}
/**
* Can drone move 1 space to the East
*
* @return <code>Boolean</code> - can move
*/
public boolean canMoveEast() {
return Nomads.awesomeWorld.getWorldGrid()[getY()][getX() + 1] == null;
if (getX() < worldSize-1)
return Nomads.awesomeWorld.getWorldGrid()[getX() + 1][getY()] == null;
else
return false;
}
/**
* Can drone move 1 space to the West
*
* @return <code>Boolean</code> - can move
*/
public boolean canMoveWest() {
return Nomads.awesomeWorld.getWorldGrid()[getY()][getX() - 1] == null;
if (getX() > 0)
return Nomads.awesomeWorld.getWorldGrid()[getX() - 1][getY()] == null;
else
return false;
}
/**
* Is the drone in an sort of safe zone where it can not be attacked. This
* also means the drone can not attack other drones.
*
* @return <code>Boolean</code> - is Safe
*/
public boolean inSafeZone() {
return Nomads.awesomeWorld.inSafeZone(getX(), getY());
}
}

View File

@ -34,6 +34,10 @@ public class InitializeGame {
// Obtain a list of the classes that exist in the directory
String[] classesToLoad = generateList();
if(classesToLoad.length == 0){
System.out.println("No Drones to load");
Nomads.running = false;
}
// Loop through the list of filenames
for (int i = 0; i < classesToLoad.length; i++) {

View File

@ -44,10 +44,6 @@ public class Nomads {
// Or catch the error
}
// Generate the locations of the main buildings in the world
// Load the drones in to the world
// Start the game loop
if (DEBUGSTATUS)
System.out.println("Game initialization finished, going to game loop");
@ -79,7 +75,7 @@ public class Nomads {
}
// For testing purposes...
//running = false;
// running = false;
}
if (DEBUGSTATUS)

View File

@ -16,7 +16,12 @@ public class World {
/**
* The 2D array of the entire world
*/
private static GameObject theWorld[][] = new GameObject[WORLDSIZE][WORLDSIZE];
private GameObject theWorld[][];
/**
* All drones will start at Y=20. Will then move East from X=40
*/
private int lastUsedX = 40;
/**
* Class constructor
@ -24,7 +29,8 @@ public class World {
public World() {
if (Nomads.DEBUGSTATUS)
System.out.println("Intializing the world...");
// TODO - Implement World Constructor
theWorld = new GameObject[WORLDSIZE][WORLDSIZE];
if (Nomads.DEBUGSTATUS)
System.out.println("World initialization complete");
@ -86,7 +92,7 @@ public class World {
}
/**
* Used to set a new GameObject at a given location Not used for moving a
* Used to set a new GameObject at a given location. Not used for moving a
* GameObject
*
* @param x
@ -112,4 +118,48 @@ public class World {
int randY = 0 + (int) (Math.random() * ((getWorldSize() - 0) + 1));
setObjectAt(randX, randY, newItem);
}
/**
* Places a drone in the world at it's starting point
*
* @param newDrone
* <code>DroneListItem</code> to be placed.
*/
public void placeNewDrone(DroneListItem newDrone) {
lastUsedX++;
newDrone.setX(lastUsedX);
newDrone.setY(20);
// The actual placement
setObjectAt(lastUsedX, 20, newDrone.getCurrent());
}
public boolean inSafeZone(int x, int y) {
/*
* Safe Zones - Measured in radius of a square TownHall - 3 : RepairShop
* - 2 : UpgradeShop - 2 : PoliceStation - 3 : Home - 1
*/
// Maximum distance away a building that provides a safehouse is 3
for (int i = -3; i <= 3; i++) {
for (int j = -3; j <= 3; j++) {
//Prevent OutofBounds. Indexes = - WORLDSIZE-1
if (x + i >= WORLDSIZE-1 || x + i < 0 || y + j >= WORLDSIZE-1 || y + j < 0) {
} else {
GameObject objectHere = theWorld[x + i][y + j];
if (objectHere != null) { // TODO - ignore 0, 0
String name = objectHere.getName();
if (name.equalsIgnoreCase("TownHall") || name.equalsIgnoreCase("PoliceStation")) {
return true;
} else if ((name.equalsIgnoreCase("RepairShop") || name.equalsIgnoreCase("UpgradeShop")) && i <= 2 && j <= 2) {
return true;
}
// TODO - Include Team Houses in the safe zone test
}
}
}
}
return false;
}
}