📄 character.java
字号:
if(age % 2 == 0)
setFrame(status * 5 + (getFrame() - status) % 4 + 1);
return;
}
// 静止或行走状态
if (status != Character.BORN && status != Character.DEAD)
{
if (stayTime < 10000)
{
stayTime++;
}
else
{
stayTime = 100;
}
// 转入休息状态
if(stayTime > PopTang.REST_WAITTIME)
{
status = Character.REST;
}
// 休息状态
if (status == Character.REST)
{
if((stayTime/5) % 15 == 0 || (stayTime/5) % 20 == 0)// 眨完眼睛后停一会
setFrame(Character.REST * 5 + (getFrame() + 1) % 5);
}
return;
}
}
else// 死亡状态
{
// 是否超过死亡时间,人物失效
if (deadTime < PopTang.DEAD_WAITTIME)
{
deadTime++;
}
else
{
isEnabled = false;
setVisible(false);
return;
}
// 正在死亡
if (age % 2 == 0)
{
// 修改姿态
int preFrame = getFrame();
int nextFrame = 0;
if(preFrame < Character.DEAD * 5 || preFrame >= (Character.DEAD + 1) * 5)
{
nextFrame = Character.DEAD * 5;
}// 转换姿态
else
{
if(preFrame != ((Character.DEAD + 1) * 5) - 1)
nextFrame = Character.DEAD * 5 + preFrame % 5 + 1;
else
{
nextFrame = preFrame;
if (age % 4 == 0)
setVisible(false);
else
setVisible(true);
}
}// 继续前一姿态
setFrame(nextFrame);
}
return;
}
}
/**
* 朝某方向行走
* @param direction 方向
*/
public void walk(int direction)
{
status = direction;
stayTime = 0;
if(!isAlive)
return;
int preFrame = getFrame();
// 移动位置
try
{
switch(direction)
{
case Character.FACE_NORTH:
if (getRefY(y - speed) >= 0
&& (map.tiles[Map.LAYER_FEATURE][getRow(getRefY(y - speed))][getCol(x)] & Map.CAN_PASS) != 0
&& (map.tiles[Map.LAYER_FEATURE][getRow(getRefY(y - speed))][getCol(getRightX(x))] & Map.CAN_PASS) != 0)
{
y -= speed;
}
else
{
y -= Math.min(getRefY(y) % PopTang.CELL_HEIGHT, speed);;
// 模糊对齐
if (x % PopTang.CELL_WIDTH <= Character.INGORE_ALIGNMENT)
x -= Math.min(speed, x % PopTang.CELL_WIDTH);
else if (PopTang.CELL_WIDTH - x % PopTang.CELL_WIDTH <= Character.INGORE_ALIGNMENT)
x += Math.min(speed, PopTang.CELL_WIDTH - x % PopTang.CELL_WIDTH);
}
break;
case Character.FACE_SOUTH:
if ((getBottomY(y + speed)) < map.tiles[0].length
* PopTang.CELL_HEIGHT
&& (map.tiles[Map.LAYER_FEATURE][getRow(getBottomY(y + speed))][getCol(x)] & Map.CAN_PASS) != 0
&& (map.tiles[Map.LAYER_FEATURE][getRow(getBottomY(y + speed))][getCol(getRightX(x))] & Map.CAN_PASS) != 0)
{
y += speed;
}
else
{
y += Math.min((PopTang.CELL_HEIGHT - (y + getHeight()) % PopTang.CELL_HEIGHT)%PopTang.CELL_HEIGHT, speed);
// 模糊对齐
if (x % PopTang.CELL_WIDTH <= Character.INGORE_ALIGNMENT)
x -= Math.min(speed, x % PopTang.CELL_WIDTH);
else if (PopTang.CELL_WIDTH - x % PopTang.CELL_WIDTH <= Character.INGORE_ALIGNMENT)
x += Math.min(speed, PopTang.CELL_WIDTH - x % PopTang.CELL_WIDTH);
}
break;
case Character.FACE_WEST:
if (x - speed >= 0
&& (map.tiles[Map.LAYER_FEATURE][getRow(getRefY(y))][getCol(x - speed)] & Map.CAN_PASS) != 0
&& (map.tiles[Map.LAYER_FEATURE][getRow(getBottomY(y))][getCol(x - speed)] & Map.CAN_PASS) != 0)
{
x -= speed;
}
else
{
x -= Math.min(x % PopTang.CELL_WIDTH, speed);
// 模糊对齐
if (getRefY(y) % PopTang.CELL_HEIGHT <= Character.INGORE_ALIGNMENT)
y -= Math.min(speed, getRefY(y) % PopTang.CELL_HEIGHT);
else if (PopTang.CELL_HEIGHT - getRefY(y) % PopTang.CELL_HEIGHT <= Character.INGORE_ALIGNMENT)
y += Math.min(speed, PopTang.CELL_HEIGHT - getRefY(y) % PopTang.CELL_HEIGHT);
}
break;
case Character.FACE_EAST:
if (getRightX(x + speed) < PopTang.MAP_COLS * PopTang.CELL_WIDTH
&& (map.tiles[Map.LAYER_FEATURE][getRow(getRefY(y))][getCol(getRightX(x + speed))] & Map.CAN_PASS) != 0
&& (map.tiles[Map.LAYER_FEATURE][getRow(getBottomY(y))][getCol(getRightX(x + speed))] & Map.CAN_PASS) != 0)
{
x += speed;
}
else
{
x += Math.min((PopTang.CELL_WIDTH - x % PopTang.CELL_WIDTH) % PopTang.CELL_WIDTH, speed);
// 模糊对齐
if (getRefY(y) % PopTang.CELL_HEIGHT <= Character.INGORE_ALIGNMENT)
y -= Math.min(speed, getRefY(y) % PopTang.CELL_HEIGHT);
else if (PopTang.CELL_HEIGHT - getRefY(y) % PopTang.CELL_HEIGHT <= Character.INGORE_ALIGNMENT)
y += Math.min(speed, PopTang.CELL_HEIGHT - getRefY(y) % PopTang.CELL_HEIGHT);
}
break;
default:
break;
}
this.setPosition(x, y);
}
catch (Exception e)
{
e.printStackTrace();
}
// 修改姿态
int nextFrame = 0;
if(preFrame < direction * 5 || preFrame >= (direction + 1) * 5)
{
nextFrame = direction * 5 + 1;
}// 转向
else
{
nextFrame = direction * 5 + 1 + (preFrame - direction) % 4;
}// 继续前一姿态
setFrame(nextFrame);
}
/**
* 放置炸弹
*/
public void setBomb()
{
// 检查单元格中是否已放置炸弹
if(!map.hasFeature(getRow(), getCol(), Map.BOMB))
{
// 当角色为生存状态且携弹量大于0时才可放置
if(isAlive && bombsNum > 0)
{
String key = "" + (getCol() * 10 + getRow());
Game.bombs.put(key, new Bomb(this));
bombsNum --;
}
}
}
/*****************
* 人物状态
****************/
/**
* 出生状态
*/
public static final int BORN = 0;
/**
* 休息状态
*/
public static final int REST = 1;
/**
* 死亡状态
*/
public static final int DEAD = 6;
/**
* 朝西
*/
public static final int FACE_WEST = 2;
/**
* 朝东
*/
public static final int FACE_EAST = 3;
/**
* 朝北
*/
public static final int FACE_NORTH = 4;
/**
* 朝南
*/
public static final int FACE_SOUTH = 5;
/**
* 忽略对齐<br>
* 当精灵与地图对齐小于该值时,精灵趋于自动对齐
*/
public static final int INGORE_ALIGNMENT = 5;
/**
* 英雄
*/
public static final int HERO = 1;
/**
* 敌人
*/
public static final int ENEMY = 2;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -