toolstroke: fix how tool results are reported
also, fix query tool functionality git-svn-id: https://micropolis.googlecode.com/svn/trunk/micropolis-java@643 d9718cc8-9f43-0410-858b-315f434eb58c
This commit is contained in:
parent
1b19af8057
commit
082e05f63c
4 changed files with 65 additions and 74 deletions
|
@ -60,9 +60,9 @@ class ToolEffect implements ToolEffectIfc
|
|||
ToolResult apply()
|
||||
{
|
||||
if (originX - preview.offsetX < 0 ||
|
||||
originX - preview.offsetX >= city.getWidth() ||
|
||||
originX - preview.offsetX + preview.getWidth() >= city.getWidth() ||
|
||||
originY - preview.offsetY < 0 ||
|
||||
originY - preview.offsetY >= city.getHeight())
|
||||
originY - preview.offsetY + preview.getHeight() >= city.getHeight())
|
||||
{
|
||||
return ToolResult.UH_OH;
|
||||
}
|
||||
|
@ -87,7 +87,7 @@ class ToolEffect implements ToolEffectIfc
|
|||
return ToolResult.SUCCESS;
|
||||
}
|
||||
else {
|
||||
return ToolResult.NONE;
|
||||
return preview.toolResult;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@ public class ToolPreview implements ToolEffectIfc
|
|||
this.tiles = new short[1][1];
|
||||
this.tiles[0][0] = CLEAR;
|
||||
this.sounds = new ArrayList<SoundInfo>();
|
||||
this.toolResult = ToolResult.NONE;
|
||||
}
|
||||
|
||||
//implements ToolEffectIfc
|
||||
|
@ -44,6 +45,16 @@ public class ToolPreview implements ToolEffectIfc
|
|||
}
|
||||
}
|
||||
|
||||
int getWidth()
|
||||
{
|
||||
return tiles[0].length;
|
||||
}
|
||||
|
||||
int getHeight()
|
||||
{
|
||||
return tiles.length;
|
||||
}
|
||||
|
||||
boolean inRange(int dx, int dy)
|
||||
{
|
||||
return offsetY+dy >= 0 &&
|
||||
|
|
|
@ -32,90 +32,59 @@ public class ToolStroke
|
|||
|
||||
public ToolResult apply()
|
||||
{
|
||||
ToolResult tr = ToolResult.NONE;
|
||||
|
||||
Rectangle r = getBounds();
|
||||
ToolEffect eff = new ToolEffect(city, r.x, r.y);
|
||||
|
||||
for (int i = 0; i < r.height; i += tool.getHeight()) {
|
||||
for (int j = 0; j < r.width; j += tool.getWidth()) {
|
||||
ToolResult tmp = apply1(
|
||||
new TranslatedToolEffect(eff, j, i)
|
||||
);
|
||||
if (tmp == ToolResult.SUCCESS) {
|
||||
tr = tmp;
|
||||
}
|
||||
else if (tr == ToolResult.NONE) {
|
||||
tr = tmp;
|
||||
}
|
||||
apply1(new TranslatedToolEffect(eff, j, i));
|
||||
}
|
||||
}
|
||||
|
||||
ToolResult tmp = eff.apply();
|
||||
if (tmp != ToolResult.NONE) {
|
||||
return tmp;
|
||||
}
|
||||
else {
|
||||
return tr;
|
||||
}
|
||||
return eff.apply();
|
||||
}
|
||||
|
||||
ToolResult apply1(ToolEffectIfc eff)
|
||||
boolean apply1(ToolEffectIfc eff)
|
||||
{
|
||||
ToolResult tr = ToolResult.SUCCESS;
|
||||
|
||||
switch (tool)
|
||||
{
|
||||
case PARK:
|
||||
tr = applyParkTool(eff);
|
||||
break;
|
||||
return applyParkTool(eff);
|
||||
|
||||
case RESIDENTIAL:
|
||||
tr = applyZone(eff, 3, 3, RESBASE);
|
||||
break;
|
||||
return applyZone(eff, 3, 3, RESBASE);
|
||||
|
||||
case COMMERCIAL:
|
||||
tr = applyZone(eff, 3, 3, COMBASE);
|
||||
break;
|
||||
return applyZone(eff, 3, 3, COMBASE);
|
||||
|
||||
case INDUSTRIAL:
|
||||
tr = applyZone(eff, 3, 3, INDBASE);
|
||||
break;
|
||||
return applyZone(eff, 3, 3, INDBASE);
|
||||
|
||||
case FIRE:
|
||||
tr = applyZone(eff, 3, 3, FIRESTBASE);
|
||||
break;
|
||||
return applyZone(eff, 3, 3, FIRESTBASE);
|
||||
|
||||
case POLICE:
|
||||
tr = applyZone(eff, 3, 3, POLICESTBASE);
|
||||
break;
|
||||
return applyZone(eff, 3, 3, POLICESTBASE);
|
||||
|
||||
case POWERPLANT:
|
||||
tr = applyZone(eff, 4, 4, COALBASE);
|
||||
break;
|
||||
return applyZone(eff, 4, 4, COALBASE);
|
||||
|
||||
case STADIUM:
|
||||
tr = applyZone(eff, 4, 4, STADIUMBASE);
|
||||
break;
|
||||
return applyZone(eff, 4, 4, STADIUMBASE);
|
||||
|
||||
case SEAPORT:
|
||||
tr = applyZone(eff, 4, 4, PORTBASE);
|
||||
break;
|
||||
return applyZone(eff, 4, 4, PORTBASE);
|
||||
|
||||
case NUCLEAR:
|
||||
tr = applyZone(eff, 4, 4, NUCLEARBASE);
|
||||
break;
|
||||
return applyZone(eff, 4, 4, NUCLEARBASE);
|
||||
|
||||
case AIRPORT:
|
||||
tr = applyZone(eff, 6, 6, AIRPORTBASE);
|
||||
break;
|
||||
return applyZone(eff, 6, 6, AIRPORTBASE);
|
||||
|
||||
default:
|
||||
// not expected
|
||||
throw new Error("unexpected tool: "+tool);
|
||||
}
|
||||
|
||||
return tr;
|
||||
}
|
||||
|
||||
public void dragTo(int xdest, int ydest)
|
||||
|
@ -155,7 +124,12 @@ public class ToolStroke
|
|||
return r;
|
||||
}
|
||||
|
||||
ToolResult applyZone(ToolEffectIfc eff, int width, int height, char tileBase)
|
||||
public CityLocation getLocation()
|
||||
{
|
||||
return new CityLocation(xpos, ypos);
|
||||
}
|
||||
|
||||
boolean applyZone(ToolEffectIfc eff, int width, int height, char tileBase)
|
||||
{
|
||||
int cost = tool.getToolCost();
|
||||
boolean canBuild = true;
|
||||
|
@ -176,7 +150,8 @@ public class ToolStroke
|
|||
}
|
||||
}
|
||||
if (!canBuild) {
|
||||
return ToolResult.UH_OH;
|
||||
eff.toolResult(ToolResult.UH_OH);
|
||||
return false;
|
||||
}
|
||||
|
||||
eff.spend(cost);
|
||||
|
@ -198,7 +173,7 @@ public class ToolStroke
|
|||
}
|
||||
|
||||
fixBorder(eff, width, height);
|
||||
return ToolResult.SUCCESS;
|
||||
return true;
|
||||
}
|
||||
|
||||
//compatible function
|
||||
|
@ -223,14 +198,15 @@ public class ToolStroke
|
|||
}
|
||||
}
|
||||
|
||||
ToolResult applyParkTool(ToolEffectIfc eff)
|
||||
boolean applyParkTool(ToolEffectIfc eff)
|
||||
{
|
||||
int cost = tool.getToolCost();
|
||||
|
||||
if (eff.getTile(0, 0) != DIRT) {
|
||||
// some sort of bulldozing is necessary
|
||||
if (!city.autoBulldoze) {
|
||||
return ToolResult.UH_OH;
|
||||
eff.toolResult(ToolResult.UH_OH);
|
||||
return false;
|
||||
}
|
||||
|
||||
//FIXME- use a canAutoBulldoze-style function here
|
||||
|
@ -240,7 +216,8 @@ public class ToolStroke
|
|||
}
|
||||
else {
|
||||
// cannot be auto-bulldozed
|
||||
return ToolResult.UH_OH;
|
||||
eff.toolResult(ToolResult.UH_OH);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -255,7 +232,7 @@ public class ToolStroke
|
|||
eff.spend(cost);
|
||||
eff.setTile(0, 0, tile);
|
||||
|
||||
return ToolResult.SUCCESS;
|
||||
return true;
|
||||
}
|
||||
|
||||
// checks whether the tile value represents road with traffic
|
||||
|
|
|
@ -929,7 +929,14 @@ public class MainWindow extends JFrame
|
|||
int x = ev.getX() / MicropolisDrawingArea.TILE_WIDTH;
|
||||
int y = ev.getY() / MicropolisDrawingArea.TILE_HEIGHT;
|
||||
|
||||
this.toolStroke = currentTool.beginStroke(engine, x, y);
|
||||
if (currentTool == MicropolisTool.QUERY) {
|
||||
doQueryTool(x, y);
|
||||
this.toolStroke = null;
|
||||
}
|
||||
else {
|
||||
this.toolStroke = currentTool.beginStroke(engine, x, y);
|
||||
}
|
||||
|
||||
this.lastX = x;
|
||||
this.lastY = y;
|
||||
}
|
||||
|
@ -937,7 +944,9 @@ public class MainWindow extends JFrame
|
|||
private void onToolUp(MouseEvent ev)
|
||||
{
|
||||
if (toolStroke != null) {
|
||||
toolStroke.apply();
|
||||
CityLocation loc = toolStroke.getLocation();
|
||||
ToolResult tr = toolStroke.apply();
|
||||
showToolResult(loc, tr);
|
||||
toolStroke = null;
|
||||
}
|
||||
}
|
||||
|
@ -961,6 +970,9 @@ public class MainWindow extends JFrame
|
|||
parseColor(strings.getString("tool."+currentTool.name()+".border"))
|
||||
);
|
||||
}
|
||||
else if (currentTool == MicropolisTool.QUERY) {
|
||||
doQueryTool(x, y);
|
||||
}
|
||||
|
||||
lastX = x;
|
||||
lastY = y;
|
||||
|
@ -995,34 +1007,25 @@ public class MainWindow extends JFrame
|
|||
drawingArea.setToolPreview(null);
|
||||
}
|
||||
|
||||
private void applyCurrentTool(int x, int y, boolean drag)
|
||||
private void showToolResult(CityLocation loc, ToolResult result)
|
||||
{
|
||||
if (currentTool == MicropolisTool.QUERY) {
|
||||
doQueryTool(x, y);
|
||||
return;
|
||||
}
|
||||
|
||||
ToolResult result = currentTool.apply(engine, x, y);
|
||||
|
||||
switch (result) {
|
||||
case SUCCESS:
|
||||
citySound(currentTool == MicropolisTool.BULLDOZER ? Sound.BULLDOZE : Sound.BUILD, new CityLocation(x, y));
|
||||
citySound(currentTool == MicropolisTool.BULLDOZER ? Sound.BULLDOZE : Sound.BUILD, loc);
|
||||
dirty1 = true;
|
||||
break;
|
||||
|
||||
case NONE: break;
|
||||
case UH_OH:
|
||||
if (!drag) {
|
||||
messagesPane.appendCityMessage(MicropolisMessage.BULLDOZE_FIRST);
|
||||
citySound(Sound.UHUH, new CityLocation(x, y));
|
||||
}
|
||||
messagesPane.appendCityMessage(MicropolisMessage.BULLDOZE_FIRST);
|
||||
citySound(Sound.UHUH, loc);
|
||||
break;
|
||||
|
||||
case INSUFFICIENT_FUNDS:
|
||||
if (!drag) {
|
||||
messagesPane.appendCityMessage(MicropolisMessage.INSUFFICIENT_FUNDS);
|
||||
citySound(Sound.SORRY, new CityLocation(x, y));
|
||||
}
|
||||
messagesPane.appendCityMessage(MicropolisMessage.INSUFFICIENT_FUNDS);
|
||||
citySound(Sound.SORRY, loc);
|
||||
break;
|
||||
|
||||
default:
|
||||
assert false;
|
||||
}
|
||||
|
|
Reference in a new issue