📄 field.java
字号:
// Copyright 2003 Nokia Corporation.
//
// THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER,
// EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS
// FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE
// OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE
// ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO
// OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR
// SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE
// RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT
// OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED
// BY THIRD PARTIES
//
// Furthermore, information provided in this source code is preliminary,
// and may be changed substantially prior to final release. Nokia Corporation
// retains the right to make changes to this source code at
// any time, without notice. This source code is provided for informational
// purposes only.
//
// Nokia and Nokia Connecting People are registered trademarks of Nokia
// Corporation.
// Java and all Java-based marks are trademarks or registered trademarks of
// Sun Microsystems, Inc.
// Other product and company names mentioned herein may be trademarks or
// trade names of their respective owners.
//
// A non-exclusive, non-transferable, worldwide, limited license is hereby
// granted to the Licensee to download, print, reproduce and modify the
// source code. The licensee has the right to market, sell, distribute and
// make available the source code in original or modified form only when
// incorporated into the programs developed by the Licensee. No other
// license, express or implied, by estoppel or otherwise, to any other
// intellectual property rights is granted herein.
// unnamed package
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
class Field
extends TiledLayer
{
private static final int WIDTH_IN_TILES = 12;
private static final int HEIGHT_IN_TILES = 12;
private static final int TILE_WIDTH = 16;
private static final int TILE_HEIGHT = 16;
private static int[][] cellTiles =
{{-3, -2, -3, -1, -2, -1, -3, -1, -2, -3, -1, -2},
{-2, 3, 4, 3, 1, 2, 3, 2, 1, 5, 2, -3},
{-1, 2, 1, 2, 3, 4, 5, 3, 2, 4, 3, -1},
{-2, 1, 4, 9, 9, 9, 9, 4, 5, 2, 1, -2},
{-3, 3, 5, 9, 10, 10, 10, 2, 1, 3, 5, -1},
{-2, 2, 3, 9, 10, 10, 10, 5, 4, 2, 1, -3},
{-1, 4, 2, 9, 9, 9, 9, 3, 1, 3, 2, -2},
{-3, 2, 5, 1, 3, 1, 4, 2, 5, 4, 3, -3},
{-2, 1, 4, 2, 5, 2, 3, 4, 2, 1, 2, -1},
{-1, 5, 1, 4, 3, 4, 1, 2, 3, 4, 1, -2},
{-3, 2, 4, 5, 2, 3, 2, 4, 1, 2, 3, -3},
{-2, -3, -2, -1, -2, -1, -3, -2, -1, -3, -1, -2}};
private static int FOLD_TILE = 10;
private static int FENCE_TILE = 9;
private static int[][] waterFrames = {{6, 7, 8}, {7, 8, 6}, {8, 6, 7}};
private int tickCount = 0;
Field()
{
super(WIDTH_IN_TILES,
HEIGHT_IN_TILES,
SheepdogMIDlet.createImage("/field.png"),
TILE_WIDTH,
TILE_HEIGHT);
createAnimatedTile(waterFrames[0][0]); // tile -1
createAnimatedTile(waterFrames[1][0]); // tile -2
createAnimatedTile(waterFrames[2][0]); // tile -3
for (int row = 0; row < HEIGHT_IN_TILES; ++row)
{
for (int column = 0; column < WIDTH_IN_TILES; ++column)
{
setCell(column, row, cellTiles[row][column]);
}
}
}
int getSheepdogStartX()
{
return getWidth() - 50;
}
int getSheepdogStartY()
{
return getHeight() - 50;
}
void tick()
{
int tickState = (tickCount++ >> 3); // slow down x8
int tile = tickState % 3;
setAnimatedTile(-1 - tile, waterFrames[tile][(tickState % 9) / 3]);
}
// return true if any part of the rectangle overlaps a water tile
// or the fence
boolean containsImpassableArea(int x, int y, int width, int height)
{
int rowMin = y / TILE_HEIGHT;
int rowMax = (y + height - 1) / TILE_HEIGHT;
int columnMin = x / TILE_WIDTH;
int columnMax = (x + width - 1) / TILE_WIDTH;
for (int row = rowMin; row <= rowMax; ++row)
{
for (int column = columnMin; column <= columnMax; ++column)
{
int cell = getCell(column, row);
if ((cell < 0) || (cell == FENCE_TILE))
{
return true;
}
}
}
return false;
}
// returns true if every pixel of the sprite is in the fold
boolean inFold(Sprite s)
{
// we can assume that the sprite's reference pixel is unchanged
int rowMin = s.getY() / TILE_HEIGHT;
int rowMax = (s.getY() + s.getHeight() - 1) / TILE_HEIGHT;
int columnMin = s.getX() / TILE_WIDTH;
int columnMax = (s.getX() + s.getWidth() - 1) / TILE_WIDTH;
for (int row = rowMin; row <= rowMax; ++row)
{
for (int column = columnMin; column <= columnMax; ++column)
{
if (getCell(column, row) != FOLD_TILE)
{
return false;
}
}
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -