admin管理员组文章数量:1621658
package com.hundsun.zuma;
import java.util.Random;
import android.app.Activity;
import android.graphics.PointF;
import android.util.Log;
import com.hundsun.mode.Ball;
import com.hundsun.mode.Gun;
import com.hundsun.mode.List;
import com.hundsun.mode.Map;
import com.hundsun.mode.MusicTool;
public class GameEngine
{
public static int mScreenw;
public static int mScreenh;
private Random rm=new Random(System.currentTimeMillis());
private int mBallkind=6;
public Map mMap=new Map();
public List mMoveList=new List();
public List mBulletList=new List();
public List ExplodeList=new List();
public Gun mGun=new Gun();
public int mInitBalls=12; //锟秸匡拷始锟接猴拷锟斤拷锟斤拷拥锟斤拷锟斤拷锟�
public Ball[] mAcBall;
private int mMaxLength=50; //锟斤拷戏锟斤拷锟斤拷要锟斤拷锟斤拷母锟斤拷锟�?private int mMovingSpeed=1; //锟斤拷锟狡讹拷锟劫讹�?
private int RollInsertPos=-1; //锟接碉拷锟斤拷锟斤拷t锟斤拷锟叫碉拷锟斤拷位锟斤拷
private int AheadIndex=0;//锟斤拷前锟斤拷锟斤拷锟侥碉拷一锟斤拷锟斤拷锟絠ndex
private int BackIndex=-1;//锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟絠ndex
public int GameStatus=4;
public static final int MSTATUS_SETTING=1;
public static final int MSTATUS_GAMING=2;
public static final int MSTATUS_OVER=3;
public static final int MSTATUS_Loding=4;
public ZumaActivity mMainAc;
public boolean mbShowResult=false;
public MusicTool mMusicTool;
public MusicTool mMusicFire;
public int mBombIndex=0;
public GameEngine(Activity ac)
{
mMainAc=(ZumaActivity)ac;
InitData();
}
private void InitData()
{
mIsRuning=true;
mAcBall=new Ball[mMaxLength];
for(int i=0;i
{
mAcBall[i]=new Ball(getrandomColor(),-1);
}
mMusicTool=new MusicTool(mMainAc);
mMusicFire=new MusicTool(mMainAc);
mMusicTool.SetRes(R.raw.welcom_background);
mMusicFire.SetRes(R.raw.r_285);
AddBalltoList();
//AddBalltoList();
}
public PointF mRestartPt=new PointF(160,183);
public PointF mResultPt=new PointF(154,100);
public PointF mExitPt=new PointF(160,293);
// public boolean mbSelectBtnStart=true;//锟叫断碉拷前锟斤拷选锟脚帮拷钮锟斤拷锟侥革�?
public boolean mbWinFlag=false;
public float mPressx=-1;public float mPressy=-1;
public long Presstime;
public void DealPress()
{
if(mPressx==-1)
return;
float x=mPressx;
float y=mPressy;
Long ct=System.currentTimeMillis();
if(ct-Presstime<700)
return;
Presstime=System.currentTimeMillis();
if(mbShowResult)
{
if(x>=mRestartPt.x-60&&x=mRestartPt.y-24
&&y<=(mRestartPt.y+24))
{
RestartGame();
//mbSelectBtnStart=true;
}/*else if(x>=mExitPt.x-60&&x=mExitPt.y-24
&&y<=(mExitPt.y+24))
{
ExitGame();
//mbSelectBtnStart=false;
}*/
mPressx=-1;
mPressy=-1;
return;
}
if(mBulletList.getCount()>0)
return;
float basex=mGun.mPosx;
float basey=mGun.mPosy;
Double dis=Math.sqrt((x-basex)*(x-basex)+(y-basey)*(y-basey));
double xdis=x-basex;
double ydis=y-basey;
double s=32/dis*xdis;
double t=32/dis*ydis;
mGun.mAimx=mGun.mPosx+(float)s;
mGun.mAimy=mGun.mPosy+(float)t;
mGun.SetOff((float)s,(float)t);
Fire((float)s,(float)t);
mMusicFire.playSound(1,0);
mPressx=-1;
mPressy=-1;
}
public void PointerPress(float x,float y)
{
mPressx=x;
mPressy=y;
}
public boolean mIsRuning;
private void RestartGame()
{
mInitBalls=12;
AddBalltoList();
mbShowResult=false;
}
private void recRes()
{
mMoveList.ClearList();
// mBulletList.ClearList();
// ExplodeList.ClearList();
mGun.ClearBullet();
for(int i=0;i
{
mAcBall[i].mMoveAnimationType=-1;
mAcBall[i].mMoveIndex=0;
}
}
public void ExitGame()
{
try{
// mMusicTool.pauseSound(1);
//mMusicFire.pauseSound(1);
mbShowResult=false;
mMusicTool.stopSound(1);
mMusicFire.stopSound(1);
// mMainAc.finish();
android.os.Process.killProcess(android.os.Process.myPid());
}catch(Exception e)
{
e.getMessage();
}
// System.exit(0);
}
private void Fire(float x,float y)
{
int free=this.GetFreeBall();
mBulletList.Push(free);
mAcBall[free].bulletindex=0;
mAcBall[free].mPosx=mGun.mAimx;
mAcBall[free].mPosy=mGun.mAimy;
mAcBall[free].mMoveIndex=0;
mAcBall[free].mBallColor=mGun.mBullet[1];
mGun.mBullet[1]=-1;
mAcBall[free].mMoveAnimationType=6;
}
public float BulletDistance(float srx,float sry,float dx,float dy)
{
float bullx=srx;
float bully=sry;
float rollx=dx;//mMap.getX(b.mMoveIndex);
float rolly=dy;//mMap.getY(b.mMoveIndex);
double value=Math.sqrt((bullx-rollx)*(bullx-rollx)+(bully-rolly)*(bully-rolly));
return (float)value;
}
public void CulStricke()
{
int buletcount=mBulletList.getCount();
if(buletcount<=0)
return;
int rollcount=mMoveList.getCount();
for(int i=0;i
{
Ball bullet=mAcBall[mBulletList.Get(i)];
float srx=bullet.mPosx;
float sry=bullet.mPosy;
for(int j=0;j
{
Ball roll=mAcBall[mMoveList.Get(j)];
float desx=mMap.getX(roll.mMoveIndex);
float desy=mMap.getY(roll.mMoveIndex);
float dis= BulletDistance(srx,sry,desx,desy);
if(dis<=25)//锟斤拷}锟斤拷锟斤拷锟斤拷撞--32锟斤拷}锟斤拷锟斤拷之锟斤拷木锟斤拷锟�
{
//Log.d("CulStricke", "====================");
//InsertBullet(mBulletList.Get(i),j,true);
//mBulletList.MoveEByIndex(i);
float firstx=mMap.getX(roll.mMoveIndex+16);//锟斤拷图锟斤拷每}锟斤拷锟斤拷锟节碉拷锟斤拷锟斤拷锟斤�?锟斤�?
//锟斤�?6锟斤拷示前一锟斤拷锟斤拷锟斤拷位锟斤�?
//锟斤�?6锟斤拷示锟斤拷一锟斤拷锟斤拷锟轿伙拷锟�
float firsty=mMap.getY(roll.mMoveIndex+16);
float lastx=mMap.getX(roll.mMoveIndex-16);
float lasty=mMap.getY(roll.mMoveIndex-16);
float firstdis=BulletDistance(srx,sry,firstx,firsty);
float lastdis=BulletDistance(srx,sry,lastx,lasty);
if(firstdis
{
InsertBullet(mBulletList.Get(i),j,true);
}else
{
InsertBullet(mBulletList.Get(i),j,false);
}
mBulletList.MoveEByIndex(i);
return;
}
}
}
}
private void setResult(boolean bwin)
{
mbShowResult=true;
mbWinFlag=bwin;
recRes();
}
private void Checkwin()
{
if(mInitBalls<=0&&mMoveList.getCount()<=0)
{
setResult(true);
}
}
private void InsertBullet(int nums,int index,boolean firstpos)
{
//Log.d("InsertBullet", "num"+nums+"index"+index+""+firstpos);
try{
Ball roll=mAcBall[nums];
Ball indexBall=mAcBall[mMoveList.Get(index)];
int moveindex=indexBall.mMoveIndex;
roll.mMoveAnimationType=indexBall.mMoveAnimationType;
if(firstpos)
{
RollInsertPos=index;
int aheadnums=mMoveList.getAhead(index);
if(index==0)
{
roll.mMoveIndex=mAcBall[mMoveList.Get(0)].mMoveIndex+16;
mMoveList.Insert(index, nums,firstpos);
return;
}
Ball firstball=mAcBall[aheadnums];
if(firstball.mMoveIndex>moveindex+17)
{
roll.mMoveIndex=moveindex+16;;
}else
{
roll.mMoveIndex=moveindex+16;;
for(int i=index-1;i>=0;i--)
{
Ball temball=mAcBall[mMoveList.Get(i)];
int temindex=temball.mMoveIndex;
//if(!(temball.mMoveIndex>((index-i)*32+29)))
if(temindex-moveindex<=(index-i)*16)
temball.mMoveIndex+=16;
else
break;
}
}
mMoveList.Insert(index, nums,firstpos);
}else
{
RollInsertPos=index+1;
int behindnums=mMoveList.getBehind(index);
if(behindnums==-1)
{
roll.mMoveIndex=mAcBall[mMoveList.getLast()].mMoveIndex-16;
mMoveList.Insert(index, nums,firstpos);
return;
}
Ball behindball=mAcBall[behindnums];
if(behindball.mMoveIndex
{
roll.mMoveIndex=moveindex-16;;
}else
{
roll.mMoveIndex=moveindex;;
for(int i=index;i>=0;i--)
{
Ball temball=mAcBall[mMoveList.Get(i)];
int temindex=temball.mMoveIndex;
if(temindex<=moveindex+(1+index-i)*16)
temball.mMoveIndex+=16;
else
break;
}
}
mMoveList.Insert(index, nums,firstpos);
//AdjustIndex();
}
}catch(Exception e)
{
String mess=e.getMessage();
e.printStackTrace();
}
}
public void AdjustIndex()
{
int Firstmove=this.getFirstMove();
int laststatic =this.getLastStatic(Firstmove);
int firstvalue=mMoveList.Get(Firstmove);
int lastvalue=mMoveList.Get(laststatic);
Ball firstball=mAcBall[firstvalue];
Ball lastball=mAcBall[lastvalue];
int firstindex=firstball.mMoveIndex;
int lastindex=lastball.mMoveIndex;
int nums=lastindex-firstindex;
if(nums<16)
{
for(int i=0;i<=laststatic;i++)
{
mAcBall[mMoveList.Get(i)].mMoveIndex+=(16-nums);
}
}
}
public void checkBomb(int insert)
{
List TemList=new List();
Ball SertBall=mAcBall[mMoveList.Get(insert)];
int searchc=SertBall.mBallColor;
int searchType=SertBall.mMoveAnimationType;
int searchIndex=SertBall.mMoveIndex;
TemList.Push(mMoveList.Get(insert));
//ExplodeList
for(int i=insert+1;i
{
int checkcolor=mAcBall[mMoveList.Get(i)].mBallColor;
int type=mAcBall[mMoveList.Get(i)].mMoveAnimationType;
int moveindex=mAcBall[mMoveList.Get(i)].mMoveIndex;
if(searchIndex-moveindex<=(i-insert)*16&&searchc==checkcolor&&type==searchType)
{
//if(searchc==checkcolor&&type==searchType)
{
TemList.Push(mMoveList.Get(i));
}
}else
{
break;
}
}
for(int i=insert-1;i>=0;i--)
{
int checkcolor=mAcBall[mMoveList.Get(i)].mBallColor;
int type=mAcBall[mMoveList.Get(i)].mMoveAnimationType;
int moveindex=mAcBall[mMoveList.Get(i)].mMoveIndex;
if(moveindex-searchIndex<=(insert-i)*16&&searchc==checkcolor&&searchType==type)
{
TemList.Push(mMoveList.Get(i));
}
else
{
break;
}
}
if(TemList.getCount()<3)
{
TemList.ClearList();
}else
{
mBombIndex=0;
for(int i=0;i
{
int num=TemList.Get(i);
ExplodeList.Push(num);
mAcBall[num].mMoveAnimationType=3; //锟斤拷锟剿讹拷锟斤拷锟斤拷锟斤拷锟斤拷为锟斤拷炸锟斤�?
}
for(int i=0;i
{
Ball tb=mAcBall[mMoveList.Get(i)];
if(tb.mMoveAnimationType!=3)
{
tb.mMoveAnimationType=0;
}else
break;
}
}
}
public void UpdateBomb()
{
if(ExplodeList.getCount()>0)
{
mBombIndex+=1;
for(int i=0;i
{
int num=ExplodeList.Get(i);
mAcBall[num].mBombIndex=mBombIndex;
/*if(mAcBall[num].mBombIndex>=5)
{
RollInsertPos=-1;
for(int j=0;j
{
int value=ExplodeList.Get(j);
mMoveList.MoveE(value);
mAcBall[value].mMoveAnimationType=-1;
mAcBall[value].mBombIndex=0;
}
ExplodeList.ClearList();
reCheckBullet();
return;
}*/
}
if(mBombIndex>=5)
{
//RollInsertPos=-1;
for(int j=0;j
{
int value=ExplodeList.Get(j);
mMoveList.MoveE(value);
mAcBall[value].mMoveAnimationType=-1;
mAcBall[value].mBombIndex=0;
}
ExplodeList.ClearList();
reCheckBullet();
Checkwin();
mBombIndex=-1;
}
}
}
public void UpdateGun()
{
if(mGun.mBullet[1]<0)
{
PushGun();
}
}
public void reCheckBullet()
{
int aim=mGun.mBullet[1];
int ready=mGun.mBullet[0];
int color[]=new int[6];
for(int i=0;i
{
color[mAcBall[mMoveList.Get(i)].mBallColor]+=1; //锟斤拷锟姐当前锟斤拷锟斤拷锟斤拷色锟斤拷锟斤拷锟�?
}
if(color[aim]<=0)
{
for(int i=0;i
{
if(color[i]>0)
{
mGun.mBullet[1]=i;
break;
}
}
}
if(color[ready]<=0)
{
for(int i=0;i
{
if(color[i]>0)
{
mGun.mBullet[0]=i;
break;
}
}
}
}
private void PushGun()
{
int color=getRandRollColor();
mGun.BulletLoading(color);
}
private int getRandRollColor()
{
if(mMoveList.getCount()<=0)
return -1;
int color[]=new int[6];
for(int i=0;i
{
color[mAcBall[mMoveList.Get(i)].mBallColor]+=1; //锟斤拷锟姐当前锟斤拷锟斤拷锟斤拷色锟斤拷锟斤拷锟�?
}
while(true)
{
int rcolor=getrandomColor(); //锟斤拷锟斤拷取一锟斤拷锟节癸拷锟斤拷锟斤拷锟斤拷�?
Log.d("log:color", ""+rcolor);
if(color[rcolor]>0)
return rcolor;
}
}
private int GetFreeBall()
{
for(int i=0;i
{
if(mAcBall[i].mMoveAnimationType==-1)
{
return i;
}
}
return -1;
}
public void updateBullet()
{
int bulletnums=mBulletList.getCount();
if(bulletnums>0)
{
for(int i=0;i
{
int num=mBulletList.Get(i);
mAcBall[num].bulletindex+=1;
mAcBall[num].mPosx+=mGun.mXoff;
mAcBall[num].mPosy+=mGun.mYoff;
if(mAcBall[num].mPosx<0||mAcBall[num].mPosx>320
|| mAcBall[num].mPosy<0||mAcBall[num].mPosy>480 )
{
mBulletList.MoveEByIndex(i);
mAcBall[num].mMoveAnimationType=-1;
}
}
}
}
public void CheckBulletIncreate(float desx,float desy)
{
}
public void checkLink()
{
int firstindex=getFirstMove();
if(firstindex==-1)//全锟斤拷锟斤拷锟角撅拷止锟斤�?
{
for(int i=0;i
{
int num=mMoveList.Get(i);
mAcBall[num].mMoveAnimationType=1;
}
return;
}
int laststatic=getLastStatic(firstindex);
if(firstindex>0&&laststatic>=0)
{
int num1=mMoveList.Get(firstindex);
int num2=mMoveList.Get(laststatic);
int index1=mAcBall[num1].mMoveIndex;
int index2=mAcBall[num2].mMoveIndex;
if(index1+16>=index2)
{
//mAcBall[num2].mMoveAnimationType=1;
for(int i=laststatic;i>=0;i--)
{
int wide=laststatic-i;
int tem=mMoveList.Get(i);
if(mAcBall[tem].mMoveIndex<=index1+16*(wide+1))
mAcBall[tem].mMoveAnimationType=1;
else
break;
}
}
}
}
public int getFirstMove()
{
for(int i=0;i
{
int index=mMoveList.Get(i);
if(index>=0&&mAcBall[index].mMoveAnimationType==1)
{
return i;
}
}
return -1;
}
public int getLastStatic(int start)
{
for(int i=start-1;i>=0;i--)
{
int index=mMoveList.Get(i);
if(mAcBall[index].mMoveAnimationType==0)
{
return i;
}
}
return -1;
}
public void PushMoveList(int index)
{
mAcBall[index].mAheadBall=-1;
mAcBall[index].mBehindBall=-1;
mAcBall[index].mPosx=268;
mAcBall[index].mPosy=0;
mAcBall[index].mMoveIndex=0;
mAcBall[index].InitAnimation(1);
int last=mMoveList.getLast();
mAcBall[index].mAheadBall=last;
if(last>=0)
mAcBall[last].mBehindBall=index;
mMoveList.Push(index);
}
public void GameLoop()
{
try{
switch(GameStatus)
{
case MSTATUS_Loding:
break;
case MSTATUS_GAMING:
if(RollInsertPos>=0)
{
checkBomb(RollInsertPos);
RollInsertPos=-1;
}
DealPress();
CulStricke();
UpdateBomb();
UpdatePreMoveList();
UpdateGun();
updateBullet();
checkLink();
break;
case MSTATUS_SETTING:
case MSTATUS_OVER:
break;
}
}catch(Exception e)
{
String mess=e.toString();
e.printStackTrace();
}
}
private int mLoopVar=0;
private int mloopCount=2;
public void UpdatePreMoveList()
{
mLoopVar+=1;
if(mInitBalls<6&&mLoopVar<=mloopCount)
{
return;
}
if(mbShowResult)
return;
mLoopVar=0;
for(int i=0;i
{
int index=mMoveList.Get(i);
mAcBall[index].UpdateTile();
int animationtype= mAcBall[index].mMoveAnimationType;
switch(animationtype)
{
case 1:
mAcBall[index].mMoveIndex+=mMovingSpeed;
if(mAcBall[index].mMoveIndex>=mMap.getCount())
{
mAcBall[index].mMoveAnimationType=-1;
mAcBall[index].mMoveIndex=0;
//mMoveList.MoveEByIndex(i);
setResult(false);
}
break;
}
}
int last=mMoveList.getLast();
if(last>=0&&mAcBall[last].mMoveIndex>=16||(mMoveList.getCount()<=0&&mInitBalls>0))
{
AddBalltoList();
}
int first=mMoveList.getFirst();
if(first>=0&&mAcBall[first].mMoveIndex>Map.mMaxPath)
{
RemoveBallFromList(0);
}
}
/*public void setGameStatus(int status)
{
GameStatus=status;
switch(GameStatus)
{
case MSTATUS_OVER:
break;
case MSTATUS_GAMING:
break;
}
}*/
public void AddBalltoList()
{
if(mInitBalls>0)
{
int free=GetFreeBall();
if(free>=0)
{
mAcBall[free].mBallColor=getrandomColor();
PushMoveList(free);
}
mInitBalls-=1;
}
}
public void RemoveBallFromList(int index)
{
int nums=mMoveList.Get(index);
mAcBall[nums].mMoveAnimationType=-1;
mMoveList.MoveEByIndex(index);
}
private int getrandomColor()
{
return Math.abs(rm.nextInt())%mBallkind;
}
}
更多源码 | 好库简介 | 网站地图 | 帮助中心 | 版权说明
Copyright© 2009-2012 OKBASE.NET All Rights Reserved 好库网 版权所有
版权声明:本文标题:祖玛java代码_简单祖玛游戏源码 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1728850215a1176581.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论