toolstroke: move bulldozer tool code to a separate file

git-svn-id: https://micropolis.googlecode.com/svn/trunk/micropolis-java@627 d9718cc8-9f43-0410-858b-315f434eb58c
This commit is contained in:
jason@long.name 2013-05-16 23:41:40 +00:00
parent 6e585924a7
commit 0fe2c2fe1d
3 changed files with 136 additions and 138 deletions

View file

@ -0,0 +1,130 @@
package micropolisj.engine;
import java.awt.Rectangle;
import static micropolisj.engine.TileConstants.*;
class Bulldozer extends ToolStroke
{
Bulldozer(Micropolis city, int xpos, int ypos)
{
super(city, MicropolisTool.BULLDOZER, xpos, ypos);
}
@Override
public ToolResult apply()
{
ToolResult checkResult = check();
if (checkResult != ToolResult.SUCCESS) {
return checkResult;
}
Rectangle b = getBounds();
if (b.width == 1 && b.height == 1 &&
isZoneCenter(city.getTile(b.x, b.y)))
{
return dozeZone(b.x, b.y);
}
int countDozed = 0;
for (int y = b.y; y < b.y+b.height; y++) {
for (int x = b.x; x < b.x+b.width; x++) {
char tile = city.getTile(x, y);
if (isDozeable(tile)) {
countDozed++;
dozeField(x, y);
}
}
}
return ToolResult.SUCCESS;
}
@Override
public ToolResult check()
{
Rectangle b = getBounds();
if (b.width == 1 && b.height == 1 &&
isZoneCenter(city.getTile(b.x, b.y)))
{
int cost = 1;
return city.budget.totalFunds >= cost ?
ToolResult.SUCCESS :
ToolResult.INSUFFICIENT_FUNDS;
}
int countDozed = 0;
for (int y = b.y; y < b.y+b.height; y++) {
for (int x = b.x; x < b.x+b.width; x++) {
char tile = city.getTile(x, y);
if (isDozeable(tile)) {
countDozed++;
}
}
}
int cost = 1 * countDozed;
return city.budget.totalFunds < cost ? ToolResult.INSUFFICIENT_FUNDS :
countDozed != 0 ? ToolResult.SUCCESS :
ToolResult.NONE;
}
ToolResult dozeZone(int xpos, int ypos)
{
assert city.testBounds(xpos, ypos);
char currTile = city.getTile(xpos, ypos);
char tmp = (char)(currTile & LOMASK);
// zone center bit is set
assert isZoneCenter(currTile);
city.spend(1);
switch (checkSize(tmp))
{
case 3:
city.makeSound(xpos, ypos, Sound.EXPLOSION_HIGH);
putRubble(xpos, ypos, 3, 3);
break;
case 4:
city.makeSound(xpos, ypos, Sound.EXPLOSION_LOW);
putRubble(xpos, ypos, 4, 4);
break;
case 6:
city.makeSound(xpos, ypos, Sound.EXPLOSION_BOTH);
putRubble(xpos, ypos, 6, 6);
break;
default:
assert false;
break;
}
return ToolResult.SUCCESS;
}
ToolResult dozeField(int xpos, int ypos)
{
char tile = city.getTile(xpos, ypos);
// check dozeable bit
assert isDozeable(tile);
tile = neutralizeRoad(tile);
if (isOverWater(tile))
{
// dozing over water, replace with water.
city.setTile(xpos, ypos, RIVER);
}
else
{
// dozing on land, replace with land. Simple, eh?
city.setTile(xpos, ypos, DIRT);
}
city.spend(1);
return ToolResult.SUCCESS;
}
}

View file

@ -65,7 +65,12 @@ public enum MicropolisTool
public ToolStroke beginStroke(Micropolis engine, int xpos, int ypos)
{
return new ToolStroke(engine, this, xpos, ypos);
if (this == BULLDOZER) {
return new Bulldozer(engine, xpos, ypos);
}
else {
return new ToolStroke(engine, this, xpos, ypos);
}
}
public ToolResult apply(Micropolis engine, int xpos, int ypos)

View file

@ -84,8 +84,6 @@ public class ToolStroke
{
switch (tool)
{
case BULLDOZER:
return checkBulldozer();
// case RAIL:
// case ROADS:
// case WIRE:
@ -102,9 +100,6 @@ public class ToolStroke
{
switch (tool)
{
case BULLDOZER:
return applyBulldozer(xpos, ypos);
case RAIL:
return applyRailTool(xpos, ypos);
@ -455,111 +450,6 @@ public class ToolStroke
}
}
ToolResult checkBulldozer()
{
Rectangle b = getBounds();
if (b.width == 1 && b.height == 1 &&
isZoneCenter(city.getTile(b.x, b.y)))
{
int cost = 1;
return city.budget.totalFunds >= cost ?
ToolResult.SUCCESS :
ToolResult.INSUFFICIENT_FUNDS;
}
int countDozed = 0;
for (int y = b.y; y < b.y+b.height; y++) {
for (int x = b.x; x < b.x+b.width; x++) {
char tile = city.getTile(x, y);
if (isDozeable(tile)) {
countDozed++;
}
}
}
int cost = 1 * countDozed;
return city.budget.totalFunds < cost ? ToolResult.INSUFFICIENT_FUNDS :
countDozed != 0 ? ToolResult.SUCCESS :
ToolResult.NONE;
}
ToolResult applyBulldozer(int xpos, int ypos)
{
if (!city.testBounds(xpos, ypos))
return ToolResult.UH_OH;
char currTile = city.getTile(xpos, ypos);
char tmp = (char)(currTile & LOMASK);
if ((currTile & ZONEBIT) != 0)
{
// zone center bit is set
if (city.budget.totalFunds >= 1)
{
city.spend(1);
switch (checkSize(tmp))
{
case 3:
city.makeSound(xpos, ypos, Sound.EXPLOSION_HIGH);
putRubble(xpos, ypos, 3, 3);
break;
case 4:
city.makeSound(xpos, ypos, Sound.EXPLOSION_LOW);
putRubble(xpos, ypos, 4, 4);
break;
case 6:
city.makeSound(xpos, ypos, Sound.EXPLOSION_BOTH);
putRubble(xpos, ypos, 6, 6);
break;
default:
assert false;
break;
}
return ToolResult.SUCCESS;
}
else
{
return ToolResult.INSUFFICIENT_FUNDS;
}
}
else if (false && isBigZone(tmp))
{
// The GPL Micropolis will uses a bunch of code to find
// the center of this zone, and then converts it to rubble
// the same as clicking the center of the zone.
// I prefer to make the user click the critical spot of
// the zone to destroy it.
return ToolResult.UH_OH;
}
else if (tmp == RIVER ||
tmp == REDGE ||
tmp == CHANNEL)
{
if (city.budget.totalFunds >= 6)
{
ToolResult result = layDoze(xpos, ypos);
if (tmp != (city.getTile(xpos, ypos) & LOMASK))
{
// tile changed
city.spend(5);
fixZone(xpos, ypos);
}
return result;
}
else {
return ToolResult.INSUFFICIENT_FUNDS;
}
}
else
{
ToolResult result = layDoze(xpos, ypos);
fixZone(xpos, ypos);
return result;
}
}
ToolResult applyRailTool(int xpos, int ypos)
{
if (!city.testBounds(xpos, ypos))
@ -638,33 +528,6 @@ public class ToolStroke
return tile;
}
private ToolResult layDoze(int xpos, int ypos)
{
if (city.budget.totalFunds <= 0)
return ToolResult.INSUFFICIENT_FUNDS;
char tile = city.getTile(xpos, ypos);
// check dozeable bit
if ((tile & BULLBIT) == 0)
return ToolResult.NONE;
tile = neutralizeRoad(tile);
if (isOverWater(tile))
{
// dozing over water, replace with water.
city.setTile(xpos, ypos, RIVER);
}
else
{
// dozing on land, replace with land. Simple, eh?
city.setTile(xpos, ypos, DIRT);
}
city.spend(1);
return ToolResult.SUCCESS;
}
private ToolResult layRail(int xpos, int ypos)
{
final int RAIL_COST = 20;