📄 creature.java
字号:
package Creature;
/*
* Creature.java
*
* Created on 2005年11月9日, 下午10:07
*/
import java.lang.Math;
import javax.microedition.lcdui.game.Sprite;
import Snowball.*;
/*
*
* @author 诺飞
*/
public class Creature extends GameObject
{
private final static int a = 1; //重力加速度 1
private int vy; //y方向速率
private int LastEvent; //上一次事件帧数
private int LastY; //上一次的y相对坐标
private int OffsetX; //x相对坐标
private int OffsetY; //y相对坐标
private int StartJumpX; //开始跳跃时x坐标
private int StartJumpY; //开始跳跃时y坐标
private int JumpDir; //跳跃方向 0-向上 1-向下
private int JumpTick; //开始跳跃的记数器
private int WalkState; //玩家移动方向 0-停止 1-向左 2-向右
private int WalkFrame; //走路帧
protected int FeetStart; //脚x1偏移
protected int FeetEnd; //脚x2偏移
protected int JumpState; //玩家跳跃状态 0-停止 1-跳跃
public static GameMap gm; //游戏地图
public int x; //x坐标
public int y; //y坐标
public Sprite ImageL; //向左帧图片
public Sprite ImageR; //向右帧图片
public int Action; //动作
public int WalkDir; //行走方向 0-向左 1-向右
//各种状态
public boolean State; //逻辑状态 true存在,false消失
public boolean DrawState; //绘画状态 true画,false消失
public boolean MoveState; //移动状态 true移动,false消失
//参数
protected int StandFrame; //站立帧
protected int WalkFrameStart; //走路开始帧
protected int WalkFrameRate; //走路总帧数
protected int WalkFrameScale; //帧间隔
protected int JumpUpFrame; //向上跳帧
protected int JumpDownFrame; //向下落帧
protected int WalkInterval; //走路每帧移动距离,单位:象素
protected int JumpInterval; //跳越每帧移动距离,单位:象素
protected int FeetStartL; //向左,脚x1偏移
protected int FeetEndL; //向左,脚x2偏移
protected int FeetStartR; //向右,脚x1偏移
protected int FeetEndR; //向右,脚x2偏移
//添加攻击参数
protected int AttackState;//玩家攻击状态 0-停止 1-攻击
protected int AttackFrame; //攻击帧 0-攻击第一帧 1- 攻击第二帧
protected int AttackStartFrame;//攻击开始帧
protected int AttackEndFrame;//攻击结束帧
//碰撞参数
public int frameWidth;
public int frameHeight;
//雪球参数
protected int SnowVx;//水平速度
//飞行参数
protected int FlyVx; //飞行水平速度
protected int FlyVy; //飞行垂直速度
/** Creates a new instance of Creature */
public Creature(int x, int y, int WalkDir, Sprite ImageL, Sprite ImageR)
{
super();
//设置参数
this.x = x;
this.y = y;
this.WalkDir = WalkDir;
this.ImageL = ImageL;
this.ImageR = ImageR;
//初始化成员变量
WalkFrame = 0;
Action = StandFrame;
//雪球参数水平速度
SnowVx = 16;
}
//处理过程
public void Process(int TickCount)
{
WalkProc();
JumpProc();
}
//跳跃过程
private void JumpProc()
{
int CalcY = 0; //落下停止y坐标
if(JumpState == 1)
{
//处理坐标变换
JumpTick++; //累加跳跃计数器
//x
switch(WalkState)
{
case 0:
break;
case 1:
OffsetX -= JumpInterval;
x = StartJumpX + OffsetX;
break;
case 2:
OffsetX += JumpInterval;
x = StartJumpX + OffsetX;
break;
}
x = gm.CalcX(x, FeetStart, FeetEnd);
//y
OffsetY = vy * JumpTick + a * JumpTick * JumpTick;
y = StartJumpY + OffsetY;
//计算运动方向
if(OffsetY <= LastY)
{
JumpDir = 0; //向上
}
else
{
JumpDir = 1; //向下
//停止跳跃运动
CalcY = gm.CalcY(x, FeetStart, FeetEnd, StartJumpY + LastY, y); //以人物脚为基准取差值
if(CalcY != 0)
{
y = CalcY;
JumpState = 0;
}
}
//进入下一次循环,记住当前y偏移
LastY = OffsetY;
//处理动作
Action = (JumpDir == 0) ? JumpUpFrame : JumpDownFrame;
}
}
//跳跃
public void Jump()
{
if(JumpState == 0)
{
StartJumpX = x;
StartJumpY = y;
OffsetX = 0;
OffsetY = 0;
JumpTick = 0;
JumpState = 1;
WalkFrame = 0;
LastY = 0;
vy = -14;
}
}
//处理走路过程
private void WalkProc()
{
int CalcY = 0; //落下停止y坐标
if(JumpState == 0)
{
//运动
switch(WalkState)
{
case 0:
break;
case 1://向左
x -= WalkInterval;
x = gm.CalcX(x, FeetStart, FeetEnd);
CalcY = gm.CalcY(x, FeetStart, FeetEnd, y, y);
if(CalcY == 0) //落下
{
Drop();
}
break;
case 2://向右
x += WalkInterval;
x = gm.CalcX(x, FeetStart, FeetEnd);
CalcY = gm.CalcY(x, FeetStart, FeetEnd, y, y);
if(CalcY == 0) //落下
{
Drop();
}
break;
}
//处理动作
CartoonProc();
}
}
//走路
public void Walk(int State)
{
WalkState = State;
switch(State)
{
case 0:
break;
case 1://向左
WalkDir = 0;
FeetStart = FeetStartL;
FeetEnd = FeetEndL;
break;
case 2://向右
WalkDir = 1;
FeetStart = FeetStartR;
FeetEnd = FeetEndR;
break;
}
}
//处理动画
private void CartoonProc()
{
if(JumpState == 0)
{
if(WalkState == 0)
{
Action = StandFrame;
WalkFrame = 0;
}
else
{
Action = WalkFrame / WalkFrameScale + WalkFrameStart;
if(WalkFrame < (WalkFrameRate * WalkFrameScale - 1))
{
WalkFrame++;
}
else
{
WalkFrame = 0;
}
}
}
}
//掉落
private void Drop()
{
StartJumpX = x;
StartJumpY = y;
OffsetX = 0;
OffsetY = 0;
JumpTick = 0;
JumpState = 1;
WalkFrame = 0;
LastY = 0;
vy = 0;
}
//碰撞检测
public boolean collidesWith(Creature sprite, boolean pixelLevel)
{
if (pixelLevel)
{
return false;
}
else {
if (Math.abs(sprite.getX()-this.getX())<(sprite.frameWidth+this.frameWidth)/2)
{
if (Math.abs(sprite.getY()-this.getY())<(sprite.frameHeight+this.frameHeight)/2)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
//得到x
public int getX() {
return x;
}
//得到y
public int getY() {
return y;
}
//设置x
public void setX(int X) {
x = X;
}
//设置y
public void setY(int Y) {
y = Y;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -