package s3mstest.wormgame; import javax.microedition.lcdui.Graphics; public class WormFood { private int cellX; private int cellY; private int x; private int y; private long arenaSize; /** * Constructor for worm food object. * @param pit pit to which the food is associated. (unused currently) */ public WormFood(WormPit pit) { arenaSize = WormPit.CellWidth * WormPit.CellHeight; regenerate(); } public boolean isAt(int cx, int cy) { return ((this.cellX == cx) && (this.cellY == cy)); } public int getX() { return cellX; } public int getY() { return cellY; } public void regenerate() { int loc = (int)(System.currentTimeMillis() % arenaSize); cellY = (int)(loc / WormPit.CellWidth); cellX = (int)(loc % WormPit.CellHeight); y = cellY * WormPit.CELL_SIZE; // Cache to save time during paint x = cellX * WormPit.CELL_SIZE; // Cache to save time during paint // If food was generated outside of the board, try a new location if (!WormPit.isInBounds(cellX, cellY)) { regenerate(); } } /** * Paint the piece of food. * @param g graphics object to receive rendering of food object */ public void paint(Graphics g) { g.setColor(WormPit.FOOD_COLOUR); g.fillRect(x + 1, y + 1, WormPit.CELL_SIZE - 2, WormPit.CELL_SIZE - 2); g.setColor(WormPit.DRAW_COLOUR); g.drawRect(x, y, WormPit.CELL_SIZE - 1, WormPit.CELL_SIZE - 1); } }