?? gamehandle.java
字號:
package game;
import javax.microedition.lcdui.*;
import java.lang.Math;
import java.util.*;
/*
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class GameHandle extends Canvas {
public int affairsMsg=0;//行為消息
public int behaviorMsg=0;//事件消息
public int nTouchWay = -1;
public boolean bIsPopMove = false;
Canvas canvas;
public boolean bIsOverlap = false;
Bomb.Pop temp = new Bomb.Pop();
public GameHandle() {
}
protected void paint(Graphics g){
}
public void DrawLine(){
for(int i=0;i<11;i++)
for(int j = 0;j<13;j++)
GameManage.g.drawRect(i*16,j*16,16,16) ;
}
public class Posit{
int rIndex,cIndex;
Posit(int c,int r){
cIndex =c;
rIndex =r;
}
}
public Posit[] boobPosit={
new Posit(0,0),
new Posit(-1,0),
new Posit(0,-1),
new Posit(1,0),
new Posit(0,1)
};
public void InitBoobPosit(){
}
/*---------------------------------檢測通用類--------------------------------------------*/
/*---------------------------------------------------------
//重疊檢測通用模塊,如果和其它人物圖片重疊需要重繪其它人物圖片
----------------------------------------------------------*/
public boolean isOverlap(Role role1,Role role2,Map map){
for(int i = 0;i<4;i++){
//role1的其中一個點,是否在role2圖象內(nèi)部
if( role1.imagePoint[i].x > role2.imagePoint[0].x-5 && // 50
role1.imagePoint[i].y > role2.imagePoint[0].y-5 && // 42
role1.imagePoint[i].x < role2.imagePoint[2].x+5 && // 66
role1.imagePoint[i].y < role2.imagePoint[2].y+5){ // 60
return true;
}
}
return false;
}
/*------------------------------------------
//排序算法通用類,簡單地實現(xiàn),以后需要改進
--------------------------------------------*/
public void paintRank(Role role1,Role role2,Map map,boolean flag){
//一定要更新數(shù)據(jù),否則判斷的依據(jù)是舊的坐標數(shù)據(jù)
role1.UpdataAll();
role2.UpdataAll();
if(isOverlap(role1,role2,map)){//有重疊要畫兩個人物
if(role2.y<role1.y){
if( role2.nCurrentMsg >= Role.MSG_DEAD ){
role2.paintImage(GameManage.g,
role2.x,
role2.y,
role2.imgDeath,//繪制死亡
role2.btDeathTotalCol,//role.btDeathTotalRow,
role2.btDeathTotalRow,//role.btDeathTotalCol,
role2.btDeathCurrentIndex);
}else{
role2.paint(GameManage.g);//先畫其它人
}
role1.paint(GameManage.g);//再畫自己
}else{
role1.paint(GameManage.g);//先畫自己
if(role2.nCurrentMsg >= Role.MSG_DEAD ){
role2.paintImage(GameManage.g,
role2.x,
role2.y,
role2.imgDeath,//繪制死亡
role2.btDeathTotalCol,//role.btDeathTotalRow,
role2.btDeathTotalRow,//role.btDeathTotalCol,
role2.btDeathCurrentIndex);
}else{
role2.paint(GameManage.g);//再畫其它人
}
}
}else{//無重疊只畫一個人物
if(flag)
role1.paint(GameManage.g);
}
}
/*-----------------------
//繪背景地圖通用模塊,但以后得改進
------------------------*/
public void drawBackground(Role role,Role role2,Map map){//畫人物當(dāng)前所在的背景地圖
role.UpdataAll();
//左上角一定要繪。
map.paintFloor(GameManage.g,
role.imagePoint[0].row,
role.imagePoint[0].col);
map.paintBuild(GameManage.g,
role.imagePoint[0].row,
role.imagePoint[0].col);
//若人物圖片占了兩行以上,則畫同列下一行的地圖
if(role.imagePoint[0].row != role.imagePoint[3].row){
map.paintFloor(GameManage.g,
role.imagePoint[3].row,
role.imagePoint[3].col);
map.paintBuild(GameManage.g,
role.imagePoint[3].row,
role.imagePoint[3].col);
if( role.imagePoint[3].row - role.imagePoint[0].row == 2 ){//人物圖占了三行
map.paintFloor(GameManage.g,
role.imagePoint[3].row-1,
role.imagePoint[3].col);
map.paintBuild(GameManage.g,
role.imagePoint[3].row-1,
role.imagePoint[3].col);
}
}
//若人物圖片占了兩列以上,則畫同行下一列的地圖
if(role.imagePoint[0].col != role.imagePoint[1].col){
map.paintFloor(GameManage.g,
role.imagePoint[1].row,
role.imagePoint[1].col);
map.paintBuild(GameManage.g,
role.imagePoint[1].row,
role.imagePoint[1].col);
}
//若人物圖片同占了兩行兩列以上,則畫下一列下一行的地圖
if(role.imagePoint[0].row != role.imagePoint[3].row &&
role.imagePoint[0].col != role.imagePoint[1].col){
map.paintFloor(GameManage.g,
role.imagePoint[2].row,
role.imagePoint[2].col);
map.paintBuild(GameManage.g,
role.imagePoint[2].row,
role.imagePoint[2].col);
if( role.imagePoint[3].row - role.imagePoint[0].row == 2 ){//人物圖占了三行
map.paintFloor(GameManage.g,
role.imagePoint[2].row-1,
role.imagePoint[2].col);
map.paintBuild(GameManage.g,
role.imagePoint[2].row-1,
role.imagePoint[2].col);
}
}
}
/*---------------------
//通用函數(shù),移植較高
//0表示左上,1表示右上,2表示右下,3表示左下.
//nTouchWay 表示碰撞方向,可被nearlyMove引用
----------------------*/
public boolean TouchCheck(int msg,Role r,Map m){//碰撞函數(shù)
switch(msg){//GameCheck.behaviorMsg ){
case Role.MSG_UP_MOVE:{
if(m.byBuild[r.touchPoint[0].row][r.touchPoint[0].col]>0||
m.byBuild[r.touchPoint[1].row][r.touchPoint[1].col]>0){//上邊有碰撞
if((m.byBuild[r.touchPoint[1].row][r.touchPoint[1].col] == 9 ||
m.byBuild[r.touchPoint[1].row][r.touchPoint[1].col] == 9 ) &&
bIsPopMove == false){
//if(r.RoleType ==Role.PLAYER){
bIsPopMove = true;
nTouchWay = 0;
//}
}
return true;
}
}break;
case Role.MSG_LEFT_MOVE:{
if(m.byBuild[r.touchPoint[0].row][r.touchPoint[0].col]>0||
m.byBuild[r.touchPoint[3].row][r.touchPoint[3].col]>0){//左邊有碰撞
if((m.byBuild[r.touchPoint[0].row][r.touchPoint[0].col] == 9 ||
m.byBuild[r.touchPoint[3].row][r.touchPoint[3].col] == 9) &&
bIsPopMove == false ){
//if(r.RoleType ==Role.PLAYER){
bIsPopMove = true;
nTouchWay = 1;
//}
}
return true;
}
}break;
case Role.MSG_DOWN_MOVE:{
if(m.byBuild[r.touchPoint[2].row][r.touchPoint[2].col]>0||
m.byBuild[r.touchPoint[3].row][r.touchPoint[3].col]>0){//下邊有碰撞
if((m.byBuild[r.touchPoint[2].row][r.touchPoint[2].col] == 9 ||
m.byBuild[r.touchPoint[3].row][r.touchPoint[3].col] == 9 ) &&
bIsPopMove == false){
//if(r.RoleType ==Role.PLAYER){
bIsPopMove = true;
nTouchWay = 2;
//}
}
return true;
}
}break;
case Role.MSG_RIGHT_MOVE:{
if(m.byBuild[r.touchPoint[1].row][r.touchPoint[1].col]>0||
m.byBuild[r.touchPoint[2].row][r.touchPoint[2].col]>0){//右邊有碰撞
if((m.byBuild[r.touchPoint[1].row][r.touchPoint[1].col] == 9 ||
m.byBuild[r.touchPoint[2].row][r.touchPoint[2].col] == 9 ) &&
bIsPopMove == false){
//if(r.RoleType ==Role.PLAYER){
bIsPopMove = true;
nTouchWay = 3;
//}
}
return true;
}
}break;
}
//nTouchWay = -1;
return false;
}
/*--------------------------------------
//泡泡專用函數(shù),后期未必用得到,
----------------------------------------*/
//得到放泡泡的位置
public int getPutPaopRow(Role role,Map map){
if(role.touchPoint[0].row == role.touchPoint[3].row )
return role.touchPoint[0].row;
int lenth = Math.abs(role.touchPoint[0].y - map.mapPosit[role.touchPoint[0].row][1].iy);
int lenth2 = Math.abs(role.touchPoint[3].y - map.mapPosit[role.touchPoint[0].row][1].iy);
if(lenth >lenth2){ return role.touchPoint[0].row;
}else{
return role.touchPoint[3].row;
}
}
public int getPutPaopCol(Role role,Map map){
if(role.touchPoint[0].col == role.touchPoint[1].col )
return role.touchPoint[0].col;
int lenth = Math.abs(role.touchPoint[0].x - map.mapPosit[1][role.touchPoint[1].col].ix);
int lenth2 = Math.abs(role.touchPoint[1].x - map.mapPosit[1][role.touchPoint[1].col].ix);;
if(lenth >lenth2){
return role.touchPoint[0].col;
}else{
return role.touchPoint[1].col;
}
}
public void RoleHandleProcess(int msg,Role role,Role role2,Role role3,Map map){//人物動作處理類
//消息處理
switch (msg) {
case Role.MSG_UP_MOVE:{//人物上移消息
drawBackground(role,role2, map);//畫背景
role.UpMove();//移動算法
if (TouchCheck(msg,role, map)) {
role.RecoverData();//若有碰撞不能移動,則恢復(fù)數(shù)據(jù)
}
paintRank(role,role2,map,true);//排序繪圖
paintRank(role,role3,map,false);
GameManage.bIsRoleMsg = false;//禁止重畫
}break;
case Role.MSG_LEFT_MOVE:{//人物左移消息
drawBackground(role,role2,map);
role.LeftMove();
if (TouchCheck(msg,role, map)) {
role.RecoverData();
}
paintRank(role,role2,map,true);//排序繪圖
paintRank(role,role3,map,false);
GameManage.bIsRoleMsg = false;//禁止重畫
}break;
case Role.MSG_DOWN_MOVE:{//人物下移消息
drawBackground(role,role2,map);
role.DownMove();
if (TouchCheck(msg,role, map)) {
role.RecoverData();
}
paintRank(role,role2,map,true);//排序繪圖
paintRank(role,role3,map,false);
GameManage.bIsRoleMsg = false;//禁止重畫
}break;
case Role.MSG_RIGHT_MOVE:{//人物右移消息
drawBackground(role,role2,map);
role.RightMove();
if (TouchCheck(msg,role, map)) {
role.RecoverData();
}
paintRank(role,role2,map,true);//排序繪圖
paintRank(role,role3,map,false);
GameManage.bIsRoleMsg = false;//禁止重畫
}break;
case Bomb.MSG_DOING:{//泡泡啟動消息
int row = getPutPaopRow(role, map);//得到人物當(dāng)前行列
int col = getPutPaopCol(role, map);
role.bomb.nBlastNum++;//泡泡爆炸數(shù)加一
Bomb.Pop p = new Bomb.Pop();//新的泡泡
role.bomb.createPopQueue(p);//創(chuàng)建pop隊列
p.amSwell.ix = map.mapPosit[row][col].ix;//建立在人物所在的坐標
p.amSwell.iy = map.mapPosit[row][col].iy;
p.amSwell.btAtCol = (byte)col;//建立在人物所在的行列
p.amSwell.btAtRow = (byte)row;
p.amBlast.ix = map.mapPosit[row][col].ix;//建立在人物所在的坐標
p.amBlast.iy = map.mapPosit[row][col].iy;
p.amBlast.btAtCol = (byte)col;//建立在人物所在的行列
p.amBlast.btAtRow = (byte)row;
p.btCurrentMsg = p.MSG_SWELL;//泡泡狀態(tài)改變
role.bomb.popQueue.addElement(p);//泡泡隊列增加
role.bomb.popQueue.insertElementAt(p,0);//role.bomb.nBlastNum);
role.nCurrentMsg = Role.MSG_NOTHING;
GameManage.bIsRoleMsg = false;//禁止重畫
}break;
case Role.MSG_DEAD:{//死亡
if (++role.nDeathRunTime%15 ==0){
drawBackground(role,role2,map);//畫背景
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -