📄 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图象内部
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;
}
/*------------------------------------------
//排序算法通用类,简单地实现,以后需要改进
--------------------------------------------*/
public void paintRank(Role role1,Role role2,Map map,boolean flag){
//一定要更新数据,否则判断的依据是旧的坐标数据
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){//画人物当前所在的背景地图
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);
}
}
}
/*---------------------
//通用函数,移植较高
//0表示左上,1表示右上,2表示右下,3表示左下.
//nTouchWay 表示碰撞方向,可被nearlyMove引用
----------------------*/
public boolean TouchCheck(int msg,Role r,Map m){//碰撞函数
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;
}
/*--------------------------------------
//泡泡专用函数,后期未必用得到,
----------------------------------------*/
//得到放泡泡的位置
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();//若有碰撞不能移动,则恢复数据
}
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);//得到人物当前行列
int col = getPutPaopCol(role, map);
role.bomb.nBlastNum++;//泡泡爆炸数加一
Bomb.Pop p = new Bomb.Pop();//新的泡泡
role.bomb.createPopQueue(p);//创建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;//泡泡状态改变
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);//画背景
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -