using UnityEngine;
using System.Collections;
public enum MoleStates
{
NormalState,// 初始状态
UpState,// 钻出来状态
DownState,//钻进去状态
HitState//打击状态
}
public struct Data
{
public MoleStates currentState; // 地鼠动画状态
public int currentIndex;// 地鼠动画帧下标
}
public class GameScript : MonoBehaviour {
// 这些public 的变量需要在unity中设置
public Texture2D backGround;
public Texture2D moleNormalState; // 地鼠状态
public Texture2D mouseNormalState;// 鼠标状态
public int moleWidth;//
public int moleHeight;
public Texture2D mouseHitState;// 鼠标打击状态
public Texture2D [] vertigoFrame; // 眩晕动画帧数
public Texture2D [] moleAnimationFrame;//地鼠动画帧数
public float animationSpeed;// 地鼠动画播放速度
public int gameTime;//游戏倒计时
public float hitAnimationSpeed;//打击动画播放速度
public float moleSpeed;// 地鼠随机出来的速度
private int row;//地图行,列
private int col;
Texture2D mouseImage;// 中间变量,存储锤子图片
int curVetigoFrame;//
float tempTime; // 累加每次deltaTime
float randomTime; // 地鼠没有出来前,时间累加 用于地鼠图片的计时器
float hitTime; // 眩晕效果计时器
float secondTime;// 游戏没有结束前,时间累加 游戏总时间计时器
Data [,] moleDatas;//结构体数据 读取对应的图片下标和地鼠状态
//bool hasMole;
int score;
float hitX;//眩晕效果 左上角坐标
float hitY;
int hitIndex;//打击眩晕效果帧动画下标
bool isOver;
bool isHitAnimation;//是否显示打击眩晕效果
void OnGUI()
{
GUI.DrawTexture( new Rect(0f, 0f, Screen.width, Screen.height),backGround );
for(int i = 0; i < row; ++i)
{
for(int j = 0; j < col ; ++j)
{
GUI.Label( new Rect( j * moleWidth + (( Screen.width - moleWidth * col ) >> 1), i * moleHeight + (( Screen.height - moleHeight * row ) >> 1),moleWidth, moleHeight ), moleAnimationFrame[ moleDatas[i,j].currentIndex ]);
}
}
// 绘制眩晕效果
GUI.Label (new Rect(hitX,hitY,100f, 100f),vertigoFrame[hitIndex] );
float mouseX = Input.mousePosition.x;
float mouseY = Input.mousePosition.y;
// 绘制锤子动画
GUI.Label( new Rect( mouseX - 26, Screen.height - mouseY - 40, 100f, 100f ), mouseImage );
// 设置文本颜色,设置字体颜色
GUI.skin.label.normal.textColor = Color.blue;
GUI.skin.label.fontSize = 40;
//绘制得分
GUI.Label( new Rect(10f,10f,200f,90f), string.Format("Score:{0}", score) );
//绘制倒计时
GUI.Label( new Rect(10f,100f,200f,90f), string.Format("Time:{0}", gameTime) );
// 游戏结束后 绘制lable
if(isOver)
{
GUI.skin.label.alignment = TextAnchor.MiddleCenter;
GUI.skin.label.fontSize = 68;
GUI.Label( new Rect(0f, 0f, Screen.width, Screen.height),string.Format( "Game Over!" ) );
}
}
void Awake() //初始化
{
row = 4;
col = 4;
tempTime = 0f;
randomTime = 0f;
hitTime = 0f;
secondTime = 0f;
//hasMole = false;
score = 0;
hitIndex = 0;
isOver = false;
isHitAnimation = false;
mouseImage = mouseNormalState;
moleDatas = new Data[row, col];
// 初始化地鼠数据
for(int i = 0; i < row; ++i)
{
for(int j = 0; j < col ; ++j)
{
moleDatas[i,j].currentIndex = 0;
moleDatas[i,j].currentState = MoleStates.NormalState;
}
}
}
void Update()
{
if (!isOver)
{
// 鼠标事件监听
MouseListener();
// 控制current动画帧
ControlCurrentAnimFrame();
// 产生随机地鼠
RandomMole();
// 打击眩晕效果动画
HitAinmation();
// 时间大于一秒后,让游戏时间减一
if (secondTime > 1f)
{
// 时间归0
secondTime = 0f;
gameTime--;
// 当游戏时间小于1时游戏结束
if (gameTime < 1)
{
isOver = true;
}
}
else
{
// 否则继续运行
secondTime += Time.deltaTime;
}
}
}
// 打击眩晕效果动画
void HitAinmation()
{
// 如果打中则播放动画
if (isHitAnimation)
{
// 打击时间 > 动画播放速度
if (hitTime > hitAnimationSpeed)
{
hitTime = 0f;
// 一帧一帧的播放
hitIndex++;
// 播到最后一帧 置0动画停止
if (hitIndex > vertigoFrame.Length - 1)
{
hitIndex = 0;
isHitAnimation = false;
}
}
else
{
hitTime += Time.deltaTime;
}
}
}
// 鼠标事件监听
void MouseListener()
{
if ( Input.GetMouseButtonDown(0) )
{
// 将锤子变为打击状态
mouseImage = mouseHitState;
float mouseX = Input.mousePosition.x;
float mouseY = Screen.height - Input.mousePosition.y;// 屏幕左上角y坐标为屏幕的高,所以叫减去当前鼠标点击的y坐标
// 获取打中的是哪个地鼠 行和列
int r = (int) ((mouseY - ( Screen.height - moleHeight * row ) / 2 ) / moleHeight );
int c = (int) ((mouseX - ( Screen.width - moleHeight * col ) / 2 ) / moleWidth );
// 根据状态打地鼠
if(moleDatas[r,c].currentState == MoleStates.DownState || moleDatas[r,c].currentState == MoleStates.UpState )
{
moleDatas[r,c].currentState = MoleStates.HitState;
}
}
if( Input.GetMouseButtonUp(0) )
{
mouseImage = mouseNormalState;
}
}
// 产生随机地鼠
void RandomMole()
{
// 随机时间 > 一只地鼠动画所需播放速度
if ( randomTime > moleSpeed )
{
randomTime = 0f;
int i = Random.Range(0 , row);
int j = Random.Range(0 , col);
if( moleDatas[i,j].currentState == MoleStates.NormalState )
{
moleDatas[i,j].currentState = MoleStates.UpState;
}
}
else
{
randomTime += Time.deltaTime;
}
}
// 控制current动画帧
void ControlCurrentAnimFrame()
{
for(int i = 0; i < row; ++i)
{
for(int j = 0; j < col ; ++j)
{
// 控制动画播放时间
if( tempTime > animationSpeed )
{
tempTime = 0f;
switch( moleDatas[i,j].currentState )
{
case MoleStates.NormalState:
{
moleDatas[i,j].currentIndex = 0;
}
break;
case MoleStates.UpState:
{
//hasMole = true;
moleDatas[i,j].currentIndex ++;
if( moleDatas[i,j].currentIndex > moleAnimationFrame.Length - 1 )
{
moleDatas[i,j].currentIndex = moleAnimationFrame.Length - 1;
moleDatas[i,j].currentState = MoleStates.DownState;
}
}
break;
case MoleStates.DownState:
{
moleDatas[i,j].currentIndex --;
if ( moleDatas[i,j].currentIndex < 1 )
{
moleDatas[i,j].currentState = MoleStates.NormalState;
//hasMole = false;
}
}
break;
case MoleStates.HitState:
{
curVetigoFrame ++;
score += 5;
Debug.Log( string.Format("score = {0}",score) );
hitY = i * moleHeight + (( Screen.height - moleHeight * row ) >> 1);
hitX = j * moleWidth + (( Screen.width - moleWidth * col ) >> 1);
isHitAnimation = true;
moleDatas [i, j].currentState = MoleStates.NormalState;
//hasMole = false;
}
break;
default:
break;
}
}
else
{
tempTime += Time.deltaTime;
}
}
}
}
}