using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Controller : MonoBehaviour {
GameObject main, left, right;
// 对应每张牌上的Button组件
Button[] buttons;
// 每个位置的牌对应哪张图
int[] ids;
// 每个位置的按钮是否被点开
bool[] clicked;
// 行和列的数量
int col= 0, row = 0;
// 第一/二个被点击的牌的位置,游戏对象
int firstClickedIdx = -1, secondClickedIdx = -1;
// 第一/二个被点击的牌是哪张牌
int firstClickedPer = -1, secondClickedPer = -1;
// 第一/二个被点击的牌的游戏对象
GameObject firstClickedGO, secondClickedGO;
// 牌背面的精灵
Sprite pokerBackSprit;
int corrected = 0;
//计时器
public float time = 100;
public Text time_text;
//计分
public int score = 0;
public Text text_score;
//音乐
public AudioClip audioClip;
public AudioSource audioSource;
//结束提示
public GameObject gameover;
public Text end_score;
public Text end_time;
public Text end_text;
public float addtime = 10;
public GameObject star;
float time_ = 0;
void Start () {
Time.timeScale = 0;
// 相关变量的初始化
initVars ();
// 准备开始游戏qw
prepareGame ();
gameover.SetActive(false);
}
void Update(){
//计时
if (time > 0 && corrected < 18)
{
time_ = time_ + Time.deltaTime;
time = time - Time.deltaTime;
time_text.text = "时间 :" + time.ToString("f0") + " S";
audioSource.PlayOneShot(audioClip);
}
else
{
audioSource.Stop();
gameover.SetActive(true);
end_text.text = "游 戏 失 败";
end_score.text = "最终得分:" + score + " 分";
end_time.text = "最终用时:" + (time_- time).ToString("f2") + " S";
}
if(time>0&&corrected==18)
{
gameover.SetActive(true);
end_text.text = "游 戏 胜 利";
end_score.text = "最终得分:" + score + " 分";
end_time.text = "最终用时:" + (time_ - time).ToString("f2") + " S";
}
//计分
text_score.text = "得分 :" + score;
}
public void di()
{
star.SetActive(false);
time = 30;
addtime = 10;
Time.timeScale = 1;
}
public void zhong()
{
star.SetActive(false);
time = 20;
addtime = 8;
Time.timeScale = 1;
}
public void gao()
{
star.SetActive(false);
time = 10;
addtime = 8;
Time.timeScale = 1;
}
public void LoadGame()
{
SceneManager.LoadScene(0);
}
void initVars(){
// 扑克牌的行数和列数
col = 6;
row = 3;
// 定义一个Button数组,表示每一张牌
// Button 表示按钮上的 Button 组件
buttons = new Button[row * col];
// 用于Button的格式字符串
string strFormat = "Canvas/Main/Left/Button{0}{1}";
Debug.Log (string.Format(strFormat,8,1));
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
// idx 表示数组的索引值
// fromRCToIdx 表示 From Row and Col to Index
// 比如 第一行第一个(0,0),对应 0
// 比如 第二行第二个(1,1),对应 7 = (2-1)*6+(2-1)
int idx = fromRCToIdx (new int[2]{ i, j });
// 为数组中的每一个元素赋值,和界面上的牌一一对应
buttons [idx] = GameObject.Find (string.Format (strFormat, i, j)).GetComponent<Button> ();
// 按钮自带字符串,手动删太麻烦了,因此在这里顺手就去掉
Text txt = buttons [idx].transform.GetChild (0).gameObject.GetComponent<Text> ();
txt.text = "";
}
}
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
int idx = fromRCToIdx (new int[2]{ i, j });
Button btn = buttons [idx];
// 给每一个按钮增加点击响应。
btn.onClick.AddListener (delegate() {
pokerClicked (btn.gameObject);
});
}
}
// 指定扑克牌的背景图片
pokerBackSprit = Resources.Load ("Textures/PokerBG1", typeof(Sprite)) as Sprite;
// 找到左边面板
left = GameObject.Find ("Canvas/Main/Left");
}
void prepareGame(){
// 为每一个牌分配一个图片
List<int> t = new List<int> ();
// 因为准备了 9 组图片,所以这里生成 18 个随机数,0-8各出现两次
for (int i = 0; i < 9; ++i) {
t.Add (i);
t.Add (i);
}
// ids 是一个整型数组,表示各按钮对应的图片
// 比如 ids[7] = 8,表示第八张牌对应第九张照片
// 注意,第八张牌其实就是第二排第二张
ids = new int[row*col];
// clicked 记录的是该按钮是否被点击的信息
clicked = new bool[row*col];
for (int i = 0; i < row; ++i)
for (int j = 0; j < col; ++j) {
// 每次生成一个随机数,范围从 0 到 t 的长度
int ii = Random.Range (0, t.Count);
// 根据button 的行号和列号得到其索引值,比如(1,1)->7
int idx = fromRCToIdx (new int[2]{ i, j });
// 表示该牌没有被点击
clicked [idx] = false;
// 赋予该牌一张照片
ids [idx] = t [ii];
// 既然照片已经赋给一张牌了,因此将它从 t 中去掉
t.RemoveAt (ii);
}
}
int[] getRCFromName(string name){
int[] ret = new int[2];
ret[0] = int.Parse (name.Substring(6,1));
ret [1] = int.Parse (name.Substring(7,1));
return ret;
}
int fromRCToIdx(int[] rc){
return rc [0] * col + rc [1];
}
int[] fromIdxToRC(int idx){
int[] ret = new int[2];
ret [0] = idx / col;
ret [1] = idx % col;
return ret;
}
void showImage(GameObject go){
// 获取行列和索引值
int[] rc = getRCFromName (go.name);
int idx = fromRCToIdx (rc);
// 获取当前牌的 Image 组件
Image img = go.GetComponent<Image> ();
// 利用牌的命名规则,从资源中读取图像,并显示
Sprite s = Resources.Load (string.Format ("Textures/{0}A", ids [idx] + 1), typeof(Sprite)) as Sprite;
img.overrideSprite = s;
}
void showImageCorrect(GameObject go){
int[] rc = getRCFromName (go.name);
int idx = fromRCToIdx (rc);
Image img = go.GetComponent<Image> ();
Sprite s = Resources.Load (string.Format ("Textures/{0}B", ids [idx] + 1), typeof(Sprite)) as Sprite;
img.overrideSprite = s;
}
void showBack(GameObject go){
Image img = go.GetComponent<Image> ();
img.overrideSprite = pokerBackSprit;
}
void clearFlag(){
firstClickedIdx = -1;
firstClickedPer = -1;
firstClickedGO = null;
secondClickedIdx = -1;
secondClickedPer = -1;
secondClickedGO = null;
}
void judgeWin(){
if (firstClickedPer == -1 || secondClickedPer == -1)
return;
// 如果点开的两张牌图片相同
if (firstClickedPer == secondClickedPer) {
// 则显示灰色
showImageCorrect (firstClickedGO);
showImageCorrect (secondClickedGO);
// 并记录信息,表示它们已经被点击过了
clicked [firstClickedIdx] = true;
clicked [secondClickedIdx] = true;
// 记录正确的数量
corrected += 2;
score =score+10;
time = time + addtime;
time_ = time_ + addtime;
} else {
// 否则继续显示背面
showImage(firstClickedGO);
showImage(secondClickedGO);
showBack(firstClickedGO);
showBack(secondClickedGO);
}
// 清空信息
clearFlag ();
// 如果 18 张牌都对了,游戏结束
if (corrected == 18) {
Debug.Log ("END");
}
}
// 每个牌被点击后的代码
void pokerClicked(GameObject go){
// 根据名字确定当前牌在第几行第几列
int[] rc = getRCFromName (go.name);
// 进而确定它在 button 数组中的位置
int idx = fromRCToIdx (rc);
// 确定它对应的是那一张照片
int per = ids [idx];
// 如果当前牌已经被点击了(正面向上),则当作什么也没有发生
if (clicked [idx])
return;
// 如果这是两张中翻开的第一张
if (firstClickedIdx == -1) {
// 记录信息
firstClickedIdx = idx;
firstClickedPer = per;
firstClickedGO = go;
// 让它正面向上
showImage (go);
// 如果这是两张中翻开的第二张
} else if (secondClickedIdx == -1) {
// 如果正好点到了第一张,或者早就翻开的牌,则当作什么也没有发生
if (idx == firstClickedIdx || clicked [idx])
return;
// 记录信息
secondClickedIdx = idx;
secondClickedPer = per;
secondClickedGO = go;
// 让它正面向上
showImage (go);
} else {
// 判断是否游戏结束
judgeWin ();
}
}
public void GetExit()//退出运行
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;//用于退出运行
#else
Application.Quit();
#endif
}
}