📄 bomberplayer.java
字号:
/** if down key is released */
if (evt.getKeyCode() == keys[DOWN]) {
/** remove key from the all keys down buffer */
dirKeysDown ^= BDOWN;
/** reset current key down */
currentDirKeyDown ^= BDOWN;
/** remove it from the key queue */
keyQueue.removeItems(BDOWN);
}
/** if up key is released */
else if (evt.getKeyCode() == keys[UP]) {
/** remove key from the all keys down buffer */
dirKeysDown ^= BUP;
/** reset current key down */
currentDirKeyDown ^= BUP;
/** remove it from the key queue */
keyQueue.removeItems(BUP);
}
/** if left key is released */
else if (evt.getKeyCode() == keys[LEFT]) {
/** remove key from the all keys down buffer */
dirKeysDown ^= BLEFT;
/** reset current key down */
currentDirKeyDown ^= BLEFT;
/** remove it from the key queue */
keyQueue.removeItems(BLEFT);
}
/** if right key is released */
else if (evt.getKeyCode() == keys[RIGHT]) {
/** remove key from the all keys down buffer */
dirKeysDown ^= BRIGHT;
/** reset current key down */
currentDirKeyDown ^= BRIGHT;
/** remove it from the key queue */
keyQueue.removeItems(BRIGHT);
}
/** if no key is currently down */
if (currentDirKeyDown == 0)
{
/** see if last key pressed is still pressed or not */
boolean keyFound = false;
/** search for last key pressed */
while (!keyFound && keyQueue.size() > 0) {
/** if key is found then exit the loop */
if ((keyQueue.getLastItem() & dirKeysDown) > 0) {
currentDirKeyDown = keyQueue.getLastItem();
keyFound = true;
}
/** if key is not found then pop the current key */
/** and on to the next one */
else keyQueue.pop();
}
/** if no key found */
if (!keyFound)
{
/** remove all keys from queue if not already removed */
keyQueue.removeAll();
/** reset key buffers */
currentDirKeyDown = 0x00;
dirKeysDown = 0x00;
keyPressed = false;
interrupt();
}
}
}
/** if the bomb key is released */
if (!isExploding && !isDead && evt.getKeyCode() == keys[BOMB])
{
bombKeyDown = false;
interrupt();
}
}
/**
* Deactivates the player so it can't be controlled.
*/
public void deactivate()
{
isActive = false;
}
/**
* Kills the player
*/
public void kill()
{
/** is player isn't dead or isn't dieing already */
if (!isDead && !isExploding)
{
/** lower players left */
BomberGame.playersLeft -= 1;
/** reset frame counter */
frame = 0;
/** set exploding mode */
state = EXPLODING;
/** make it animate */
moving = true;
/** prepare to explode! */
isExploding = true;
/** release keys */
keyPressed = false;
BomberMain.sndEffectPlayer.playSound("Die");
/** wake up and die */
interrupt();
}
}
/**
* @return x co-ordinate
*/
public int getX() { return x; }
/**
* @return y co-ordinate
*/
public int getY() { return y; }
/**
* @return whether player is (dead or dieing) or not
*/
public boolean isDead() { return (isDead | isExploding); }
/**
* Main loop
*/
public void run()
{
/** can move flat */
boolean canMove;
/** keeps track of last key state */
boolean lastState = false;
/** shift count */
int shiftCount = BomberMain.shiftCount;
/** offset size */
int offset = 1 << (BomberMain.shiftCount / 2);
/** block size */
int size = BomberMain.size;
/** half the block size */
int halfSize = BomberMain.size / 2;
/** temporary variables */
int bx = 0, by = 0;
/** unconditional loop */
while (true) {
/** if bomb key is down */
if (!isExploding && !isDead && bombKeyDown && isActive) {
/** if bombs are available */
if ((totalBombs - usedBombs) > 0 &&
/** and a bomb isn't placed there already */
map.grid[x >> shiftCount][y >> shiftCount]
!= BomberMap.BOMB && !bombGrid[(x + halfSize) >>
BomberMain.shiftCount][(y + halfSize) >>
BomberMain.shiftCount]) {
usedBombs += 1;
bombGrid[(x + halfSize) >> BomberMain.shiftCount]
[(y + halfSize) >> BomberMain.shiftCount] = true;
/** create bomb */
map.createBomb(x + halfSize, y + halfSize, playerNo);
}
}
/** if other keys are down */
if (!isExploding && !isDead && keyPressed) {
/** store last state */
lastState = keyPressed;
/** increase frame */
frame = (frame + 1) % 5;
/** set moving to true */
moving = true;
/** assume can't move */
canMove = false;
/** make sure a key is down */
if (dirKeysDown > 0) {
/** if left key is down */
if ((currentDirKeyDown & BLEFT) > 0) {
state = LEFT;
/** if west slot is empty then it can move */
canMove = (x % size != 0 || (y % size == 0 &&
(map.grid[(x >> shiftCount) - 1][y >> shiftCount]
<= BomberMap.NOTHING)));
/** if it can't move */
if (!canMove) {
int oy = 0;
/** if it's a little bit north */
for (oy = -offset; oy < 0; oy += (size / 4)) {
/** and west slot is empty */
if ((y + oy) % size == 0 &&
map.grid[(x >> shiftCount) - 1]
[(y + oy) >> shiftCount] <= BomberMap.NOTHING) {
/** then move anyway */
canMove = true; break;
}
}
/** if it still can't move */
if (!canMove) {
/** if it's a little bit south */
for (oy = (size / 4); oy <= offset;
oy += (size / 4)) {
/** and west slot is empty */
if ((y + oy) % size == 0 &&
map.grid[(x >> shiftCount) - 1]
[(y + oy) >> shiftCount]
<= BomberMap.NOTHING) {
/** move anyway */
canMove = true; break;
}
}
}
/** if it can move now */
if (canMove) {
/** clear original spot */
clear = true; game.paintImmediately(x,
y - halfSize, width, height);
/** move up or down */
y += oy;
/** redraw the sprite */
clear = false; game.paintImmediately(x,
y - halfSize, width, height);
}
}
/** if it can move */
if (canMove) {
/** clear original spot */
clear = true; game.paintImmediately(x,
y - halfSize, width, height);
/** move left */
x -= (size / 4);
/** redraw the sprite */
clear = false; game.paintImmediately(x,
y - halfSize, width, height);
}
/** if it can't move */
else {
/** refresh the sprite */
moving = false; game.paintImmediately(x,
y - halfSize, width, height);
}
}
/** if the right key is down */
else if ((currentDirKeyDown & BRIGHT) > 0) {
state = RIGHT;
canMove = false;
/** if east slot is empty */
canMove = (x % size != 0 || (y % size == 0 &&
(map.grid[(x >> shiftCount) + 1][y >> shiftCount]
<= BomberMap.NOTHING)));
/** if it can't move */
if (!canMove) {
int oy = 0;
/** see if it's a bit south */
for (oy = -offset; oy < 0; oy += (size / 4)) {
/** and the east slot is empty */
if ((y + oy) % size == 0 &&
map.grid[(x >> shiftCount) + 1]
[(y + oy) >> shiftCount] <= BomberMap.NOTHING) {
/** move it */
canMove = true; break;
}
}
/** if it still can't move */
if (!canMove) {
/** see if it's a bit north */
for (oy = (size / 4); oy <= offset;
oy += (size / 4)) {
/** and the east slot if empty */
if ((y + oy) % size == 0 &&
map.grid[(x >> shiftCount) + 1]
[(y + oy) >> shiftCount]
<= BomberMap.NOTHING) {
/** move it */
canMove = true; break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -