📄 mygamecanvas.java
字号:
if (var == 0) {
cpc2.initAnim(animHead.copy());
} else if (var == 1) {
cpc2.initAnim(animBody.copy());
} else {
cpc2.initAnim(animEnd.copy());
}
cpc2.anim.initParam(cpc2.bDirection, false, true);
cpc2.anim.changeAction(cpc2.bDirection);
vecticUnits.addElement(cpc2);
}
/**
* 转换状态
*/
final void doStateChange(byte aBtateToLoad) {
// nLoadingTimerC = 0;
sbState = SB_STATE_LOADING;
sbStateToLoad = aBtateToLoad;
loadingGameRes = new LoadingGameRes();
}
/**
* 内部类,多线程资源管理器
*/
class LoadingGameRes implements Runnable {
Thread thread = null;
boolean isWorkComplete = false;
public LoadingGameRes() {
isWorkComplete = false;
thread = new Thread(this);
thread.start();
} // LoadingGameRes
public void run() {
synchronized (this) {
freeAllResource();
initResource();
isWorkComplete = true;
System.gc();
thread = null;
}
} // run
} // inner classs
/**
* 循环控制
*/
public final void tick() {
if (loadingGameRes == null) {
} // if
else {
if (!loadingGameRes.isWorkComplete) {
; // do Nothing when the machine loading resource...
} // if
} // else
synchronized (this) {
doKey();
doLogic();
doPaint();
}
} // void tick()
/**
* 游戏逻辑控制
*/
final void doLogic() {
if (isPaused) {
} else {
switch (sbState) {
case SB_STATE_LOADING: {
// nLoadingTimerC = ++nLoadingTimerC % 4;
}
break;
case SB_STATE_SPLASH: {
nSlpashTimerC += Consts.SN_MILLIS_PER_TICK;
if (nSlpashTimerC >= Consts.SN_SPLASH_TIMER_MAX) {
nSlpashTimerC = 0;
doStateChange(SB_STATE_MENU);
}
}
break;
case SB_STATE_MENU: {
}
break;
case SB_STATE_GAMEING: {
//
if (isStoryModeWorking) {
// nothing
if (nTimerStory < nTimerStoryMax) {
nTimerStory++;
} else {
nTimerStory = 0;
nLine++;
if (nLine > nLineMax) {
isStoryModeWorking = false;
}
}
} else if (isFoorsMenuWorked) {
// 道具栏打开时?
} else {
bGameResult = getGameResult();
//
if (!isMenuInGameWorked) {
if (isScriptWorking) {
//
commandScript();
script();
} else {
}
if (isDialogWorking) {
} else {
if (bGameResult != 0) {
if (nTimerForGameResultShow > 0) {
nTimerForGameResultShow--;
isKeyReponseMenu = false;
} else {
if (isKeyReponseMenu) {
isMenuInGameWorked = true;
}
}
}
doWeather();
mapRefresh();
npcLisa.update();
try {
unitsRefresh();
info();
} catch (Exception ex) {
}
// mapRefresh();
}
}
}
}
break;
}
}
}
//
boolean isCommandWorked = true;
// walk -> w, d -> 对话
String[] strCommand1D = { "w", "w", "w", "d" };
short[][] snCommand2D = { { 7, (short) Canvas.DOWN },
{ 5, (short) Canvas.RIGHT }, { 0, (short) Canvas.UP }, { 0 } };
int nStrCommand1DP = 0;
boolean isScriptDoDiaKeyPressed = false;
public void commandScript() {
if (isCommandWorked) {
if (nStrCommand1DP >= strCommand1D.length) {
isScriptWorking = false;
strCommand1D = null;
snCommand2D = null;
} else {
if (strCommand1D[nStrCommand1DP].equals("w")) {
isScriptWalk = true;
nScriptWalkLength = 0;
nScriptWalkLengthMax = snCommand2D[nStrCommand1DP][0]
* (Consts.SN_TILE_WIDTH / npcLisa.nSpeed);
npcLisa.setDirection((byte) snCommand2D[nStrCommand1DP][1]);
} else if (strCommand1D[nStrCommand1DP].equals("d")) {
isScriptDialog = true;
isScriptDialogWorked = false;
isScriptDoDiaKeyPressed = false;
}
//
nStrCommand1DP++;
isCommandWorked = false;
}
}
}
//
boolean isScriptWalk = false;
int nScriptWalkLength = 0;
int nScriptWalkLengthMax = 10;
//
boolean isScriptDialog = false;
boolean isScriptDialogWorked = false;
public void script() {
if (isScriptWalk) {
isCommandWorked = false;
if (nScriptWalkLength < nScriptWalkLengthMax) {
nScriptWalkLength++;
switch (npcLisa.bDirection) {
case Canvas.UP: {
npcLisa.doKey(UP);
}
break;
case Canvas.DOWN: {
npcLisa.doKey(DOWN);
}
break;
case Canvas.LEFT: {
npcLisa.doKey(LEFT);
}
break;
case Canvas.RIGHT: {
npcLisa.doKey(RIGHT);
}
break;
}
} else {
isScriptWalk = false;
nScriptWalkLength = 0;
isCommandWorked = true;
}
} else if (isScriptDialog) {
// isScriptDialog = true;
// isScriptDialogWorked = false;
if (!isScriptDialogWorked) {
if (!isScriptDoDiaKeyPressed) {
npcLisa.doKey(FIRE);
isScriptDoDiaKeyPressed = true;
} else {
if (!isDialogWorking) {
isScriptDialogWorked = true;
}
}
} else {
isScriptDialog = false;
isCommandWorked = true;
}
}
}
/**
* 控制各种物体在地图上做绝对的运动
*/
public void unitsRefresh() {
// if (vecticUnits != null) {
// for (int i = 0; i < vecticUnits.size(); i++) {
// CPC mf = (CPC) vecticUnits.elementAt(i);
// if (mf.bVar == CPC.B_VAR_BOMBSFIRE) {
// mf.update();
// if (mf.nHP <= 0) {
// vecticUnits.removeElementAt(i);
// i--;
// }
// }
// }
// }
for (int i = 0; i < vecticUnits.size(); i++) {
CPC cpc = ((CPC) vecticUnits.elementAt(i));
if (isScriptWorking) {
if (cpc.bVar == CPC.B_VAR_NPC) {
cpc.update();
}
} else {
cpc.update();
}
if (cpc.nHP <= 0) {
// 掉宝几率控制
switch (cpc.bVar) {
case CPC.B_VAR_RABIT: {
// int id = getRandomInt(0, 5, i);
// num_foos[id]++;
// strInfo = new String("获得" + strrFoos[id]);
// npcLisa.nEX += cpc.nEXMax;
}
break;
case CPC.B_VAR_WORM: {
int id = getRandomInt(0, 5, i);
// num_foos[id]++;
if (id == 2) {
strInfo = new String("获得" + "生命值");
npcLisa.nHP++;
}
}
break;
case CPC.B_VAR_MAGIC_CPC_FIRE_BALL: {
CPC.bulletNum--;
}
break;
}
vecticUnits.removeElementAt(i);
i--;
}
}
}
/**
* 控制绘制范围,不能使绘制范围超过地图的区域
*/
public void mapRefresh() {
if (snWindowX == 0) {
// X方向全屏
if (npcLisa.nX >= (Consts.SN_SCREEN_WIDTH >> 1)
&& npcLisa.nX <= (snMapWidth - (Consts.SN_SCREEN_WIDTH >> 1))) {
snMapX = npcLisa.nX - (Consts.SN_SCREEN_WIDTH >> 1);
} else {
if (npcLisa.nX >= (snMapWidth - (Consts.SN_SCREEN_WIDTH >> 1))) {
snMapX = snMapWidth - Consts.SN_SCREEN_WIDTH;
} else {
snMapX = 0;
}
}
}
if (snWindowY == 0) {
// X方向全屏
if (npcLisa.nY >= (Consts.SN_SCREEN_HEIGHT >> 1)
&& npcLisa.nY <= (snMapHeight - (Consts.SN_SCREEN_HEIGHT >> 1))) {
snMapY = npcLisa.nY - (Consts.SN_SCREEN_HEIGHT >> 1);
} else {
if (npcLisa.nY >= (snMapHeight - (Consts.SN_SCREEN_HEIGHT >> 1))) {
snMapY = snMapHeight - Consts.SN_SCREEN_HEIGHT;
} else {
snMapY = 0;
}
}
}
}
/**
* 游戏键盘控制
*/
boolean isKeyUpWorked = false;
final void doKey() {
if (isPaused) {
if (snKeyCodePressed != 0 || snKeyCodePressed != 0) {
snKeyCodeReleased = 0;
snKeyCodePressed = 0;
isPaused = false;
}
} else {
switch (sbState) {
case SB_STATE_LOADING: {
// NOTHING
}
break;
case SB_STATE_SPLASH: {
// NOTHING
}
break;
case SB_STATE_MENU: {
doKeyStateMenu();
}
break;
case SB_STATE_GAMEING: {
if (isStoryModeWorking) {
// nothing ......
if (snKeyCodePressed != 0) {
if (snKeyCodePressed == KEY_NUM5) {
isStoryModeWorking = false;
}
snKeyCodePressed = 0;
}
} else if (isFoorsMenuWorked) {
int tkey = getKeyPressed(true);
switch (tkey) {
case Consts.SN_KEY_LEFT_SOFT_KEY: {
}
break;
case Consts.SN_KEY_RIGHT_SOFT_KEY: {
// doStateChange(SB_STATE_MENU);
isFoorsMenuWorked = false;
snKeyCodePressed = 0;
}
break;
case UP: {
if (nPStrMenuFoors == 1) {
if (nPStrrFoos > 0) {
nPStrrFoos--;
}
}
}
break;
case DOWN: {
if (nPStrMenuFoors == 1) {
if (nPStrrFoos < strrFoos.length - 1) {
nPStrrFoos++;
}
}
}
break;
case LEFT: {
if (nPStrMenuFoors > 0) {
nPStrMenuFoors--;
}
// Consts.log("heloo");
}
break;
case RIGHT: {
if (nPStrMenuFoors < strMenuFoors.length - 1) {
nPStrMenuFoors++;
}
}
break;
case FIRE: {
// 使用某个道具
//
}
break;
case KEY_STAR: {
if (nStarPointer == nPoundPointer) {
nPoundPointer = -1;
nStarPointer = nPStrrFoos;
} else {
nStarPointer = nPStrrFoos;
}
}
break;
case KEY_POUND: {
if (nStarPointer == nPoundPointer) {
nStarPointer = -1;
nPoundPointer = nPStrrFoos;
} else {
nPoundPointer = nPStrrFoos;
}
}
break;
default:
break;
}
}
else {
if (isMenuInGameWorked) {
int tkey = getKeyPressed(true);
switch (tkey) {
case Consts.SN_KEY_LEFT_SOFT_KEY:
case FIRE: {
// doStateChange(SB_STATE_MENU);
isMenuInGameWorked = false;
if (bGameResult == 0) {
if (nPorintStrrMenuInGame == 0) {
;
} else if (nPorintStrrMenuInGame == 1) {
doStateChange(SB_STATE_MENU);
} else if (nPorintStrrMenuInGame == 2) {
Main.isLooping = false;
}
} else if (bGameResult == 1) {
if (nPorintStrrMenuInGame == 0) {
nMapId += 1;
doStateChange(SB_STATE_GAMEING);
} else if (nPorintStrrMenuInGame == 1) {
doStateChange(SB_STATE_MENU);
} else if (nPorintStrrMenuInGame == 2) {
Main.isLooping = false;
}
} else if (bGameResult == 2) {
if (nPorintStrrMenuInGame == 0) {
doStateChange(SB_STATE_GAMEING);
} else if (nPorintStrrMenuInGame == 1) {
doStateChange(SB_STATE_MENU);
} else if (nPorintStrrMenuInGame == 2) {
Main.isLooping = false;
}
}
}
break;
case UP: {
if (nPorintStrrMenuInGame == 0) {
nPorintStrrMenuInGame = strrMenuInGame.length - 1;
} else {
nPorintStrrMenuInGame--;
}
}
break;
case DOWN: {
if (nPorintStrrMenuInGame == strrMenuInGame.length - 1) {
nPorintStrrMenuInGame = 0;
} else {
nPorintStrrMenuInGame++;
}
}
break;
default:
break;
}
} else {
int tkey = getKeyPressed(false);
if (isDialogWorking) {
switch (tkey) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -