FileDocCategorySizeDatePackage
WormLink.javaAPI DocJ2ME MIDP 2.03634Thu Nov 07 12:02:18 GMT 2002example.wormgame

WormLink

public class WormLink extends Object
WormLink represents one sub-section of a worm. Because the worm will usually contain a few straight segments, this is a relatively cost effective way to store the entire worm. The [X,Y] coordinates are the "tail" of the worm. The link is drawn starting at the tail and proceeding "len" spaces in direction "dir".

Fields Summary
private int
x
private int
y
private int
len
private byte
dir
Constructors Summary
private WormLink()

    
public WormLink(int startX, int startY, int length, byte direction)

	x = startX;
	y = startY;
	dir = direction;
	len = length-1;
    
public WormLink(int startX, int startY, byte direction)
Create a worm link with a length of 1. This contstructor is used when the worm changes direction.

	this(startX, startY, 1, direction);
    
Methods Summary
public booleancontains(int x, int y)
Returns true if the worm segment is at the given cell

	switch (dir) {
	case Worm.LEFT:
	    return ((y == this.y) && ((x <= this.x) && (x >= getEndX())));
	case Worm.RIGHT:
	    return ((y == this.y) && ((x >= this.x) && (x <= getEndX())));
	case Worm.UP:
	    return ((x == this.x) && ((y <= this.y) && (y >= getEndY())));
	case Worm.DOWN:
	    return ((x == this.x) && ((y >= this.y) && (y <= getEndY())));
	}
	return false;
    
public voiddecreaseLength()
Remove one cell length from the tail of this segment.

	len--;
	switch (dir) {
	case Worm.LEFT:
	    x--;
	    break;
	case Worm.RIGHT:
	    x++;
	    break;
	case Worm.UP:
	    y--;
	    break;
	case Worm.DOWN:
	    y++;
	    break;
	}
    
public bytegetDirection()
Get the direction this worm segment is pointing.

	return dir;
    
public intgetEndX()
Get the X coordinate of the cell that contains the tail of this worm segment.

	if (dir == Worm.LEFT) 
	    return x-len;
	if (dir == Worm.RIGHT)
	    return x+len;
	return x;
    
public intgetEndY()
Get the Y coordinate of the cell that contains the tail of this worm segment.

	if (dir == Worm.DOWN)
	    return y+len;
	if (dir == Worm.UP)  
	    return y-len;
	return y;
    
public intgetLength()
Get the length, in cells, of this segment.

	return len+1;
    
public intgetX()
Get the X coordinate of the cell that contains the head of this worm segment.

	return x;
    
public intgetY()
Get the Y coordinate of the cell that contains the head of this worm segment.

	return y;
    
public voidincreaseLength()
Add one cell length to the head of this segment.

	len++;
    
public java.lang.StringtoString()
Debug method.

	String dirString;
	switch (dir) {
	case Worm.LEFT:
	    dirString = "Left";
	    break;
	case Worm.RIGHT:
	    dirString = "Right";
	    break;
	case Worm.UP:
	    dirString = "Up";
	    break;
	case Worm.DOWN:
	    dirString = "Down";
	    break;
	default:
	    dirString = "UNKNOWN -- " + dir;
	}
	
	return " pos == [" + x + "," + y + "]" +
	    " - [" + getEndX() + "," + getEndY() + "]" +
	    "   len == " + getLength() + 
	    "   dir == " + dirString;