上篇博客介绍了简单的行走以及相互吃的效果
上篇博客的地址
这篇博客实现一下 关于红蓝色的分开的操作 以及胜利的触发
胜利的触发是一方玩家棋子都被吃光而另一方还有棋子
关于玩家的分化 我是记录每次操作 通过计数取余 来实现玩家的分化
然后上一篇博客的代码还有存在很多小问题的
这篇博客的代码也是更新之后的版本
之后给出我的代码
棋子代码(挂在到每个棋子上)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum PlayerType
{
red,
blue
};
/// <summary>
/// 每个动物身上挂在的脚本(通过这个脚本来判断吃与被吃的关系)
/// </summary>
public class Animal : MonoBehaviour
{
public int Hp;//hp越大 优先级越高(越厉害)
public PlayerType type;//判断是什么玩家
public SpriteRenderer Black;
private BoxCollider collider;
public BoxCollider blackcollider;
float timer = 0;
private void Start()
{
collider = GetComponent<BoxCollider>();
collider.enabled = false;
}
private void Update()
{
if (Black.enabled==false)
{
collider.enabled = true;
blackcollider.enabled = false;
}
}
public void Eated()
{
if (type == PlayerType. red)
{
Arrange.Instance.rednum--;
}
if (type == PlayerType.blue)
{
Arrange.Instance.bluenum--;
}
//被吃掉的方法
gameObject.SetActive(false);
}
/// <summary>
/// 移动到目标位置去
/// </summary>
public void MoveToTargetPosi(Vector3 Targetposi)
{
timer += Time.deltaTime;
if (timer > 1)
{
timer = 0;
}
transform.position = Targetposi;
}
}
排列组合脚本(挂载到任一物体上 需要手动赋值)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Arrange : MonoBehaviour
{
List<Vector3> posis = new List<Vector3>();
public List<GameObject> Animals;//动物的物体
//检测胜利
public Text redVectory;
public Text blueVectory;
//提示语句
public Text PleaseRed;
public Text Pleaseblue;
public int rednum = 8;
public int bluenum = 8;
public static Arrange Instance;
private void Awake()
{
Instance = this;
}
private void Start()
{
for(int i=0;i<4; i++)
{
for(int j = 0; j < 4; j++)
{
posis.Add(new Vector3(i*2, j*2, 0));
}
}
for(int i = 0; i < Animals.Count; i++)
{
Vector3 temp= posis[Random.Range(0, posis.Count)];
Animals[i].transform.position = temp;
posis.Remove(temp);
}
redVectory.enabled = false;
blueVectory.enabled = false;
Pleaseblue.enabled = false;
PleaseRed.enabled = true;
}
private void Update()
{
if (rednum == 0 && bluenum != 0)
{
blueVectory.enabled = true;
AudioManager.Instance.StopMute("BGM");
AudioManager.Instance.PlayAudio("失败", false);
}
if (rednum != 0 && bluenum == 0)
{
redVectory.enabled = true;
AudioManager.Instance.StopMute("BGM");
AudioManager.Instance.PlayAudio("失败", false);
}
if (DisappearLay.Instance.Updateplayer == PlayerType.red)
{
PleaseRed.enabled = true;
Pleaseblue.enabled = false ;
}
else
{
Pleaseblue.enabled = true;
PleaseRed.enabled = false ;
}
if (Input.GetKey(KeyCode.Space))
{
AudioManager.Instance.StopMute("BGM");
AudioManager.Instance.audioDic["失败"].Play();
}
}
}
摄像机脚本(挂在摄像机上)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DisappearLay : MonoBehaviour
{
public Animal a, b;
public float dic;
private Vector3 targetposi;
public int TotalIndex = 0;//总次数 控制玩家的先后顺序
private int selectindex = 0;//点击动物的次数 控制玩家的队伍
/// <summary>
/// 判断先手玩家是红色方还是蓝色方
/// </summary>
///
public PlayerType firstplayer;//开始判断玩家类型
public PlayerType Updateplayer;//实时跟新玩家类型
public static DisappearLay Instance;
private void Awake()
{
Instance = this;
}
private void Update()
{
DisappearBlack();
ClickAnimals();
JudgePlayer();
}
/// <summary>
/// 消除黑色阴影
/// </summary>
public void DisappearBlack()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
if (hit.collider.tag == "Layout")
{
hit.collider.gameObject.GetComponent<SpriteRenderer>().enabled = false;
TotalIndex++;
}
}
}
}
/// <summary>
/// 点击场景中物体的事件
/// </summary>
public void ClickAnimals()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (a == null && b == null)
{
if (Physics.Raycast(ray, out hit, 1000))
{
if (hit.collider.tag == "Animal")
{
a = hit.collider.gameObject.GetComponent<Animal>();
}
if (a.type == Updateplayer)
{
a = hit.collider.gameObject.GetComponent<Animal>();
selectindex++;
}
else
{
a = null;
}
}
}
if (a != null && b != null)
{
if (Physics.Raycast(ray, out hit, 1000))
{
if (hit.collider.tag == "Animal")
{
a = hit.collider.gameObject.GetComponent<Animal>();
}
}
}
if (a != null && b == null)
{
if (Physics.Raycast(ray, out hit, 1000))
{
if (hit.collider.tag == "Animal")
{
b = hit.collider.gameObject.GetComponent<Animal>();
}
if (hit.collider.tag == "NullPosition" && a != null)
{
a.MoveToTargetPosi(hit.collider.gameObject.transform.position-new Vector3(0,0,0.02f));
targetposi = hit.collider.gameObject.transform.position;
a = null;
TotalIndex++;
}
}
}
}
if (a != null && b != null)
{
dic = Vector3.Distance(a.transform.position, b.transform.position);
}
if (a != null && b == null)
{
//dic = Vector3.Distance(a.transform.position, b.transform.position);
}
if (a == b)
{
b = null;
}
if (dic <= 2.1f)
{
//先手大于后首选择的动物
if (a != null && b != null && a.Hp > b.Hp && a.type != b.type && a.Hp - b.Hp != 7)
{
b.Eated();
a.MoveToTargetPosi(b.transform.position);
a = null;
b = null;
TotalIndex++;
}
if (a != null && b != null && a.Hp == b.Hp && a.type != b.type)
{
a.Eated();
b.Eated();
a = null;
b = null;
TotalIndex++;
}
if (a != null && b != null && b.Hp - a.Hp == 7 && a.type != b.type)//大象和老鼠的处理
{
a.MoveToTargetPosi(b.transform.position);
b.Eated();
a = null;
b = null;
TotalIndex++;
}
if (a != null && b != null && a.type == b.type)
{
a = null;
b = null;
}
}
else
{
a = null;
b = null;
}
}
public void JudgePlayer()
{
if (selectindex == 1 && a.type == PlayerType.red)
{
firstplayer = PlayerType.red;
}
if (selectindex == 1 && a.type == PlayerType.blue)
{
firstplayer = PlayerType.blue;
}
if (TotalIndex % 2 == 0 && firstplayer== PlayerType.red)
{
Updateplayer = PlayerType.red;
}
else if(TotalIndex % 2 != 0 && firstplayer == PlayerType.red)
{
Updateplayer = PlayerType.blue;
}
if (TotalIndex % 2 == 0 && firstplayer == PlayerType.blue)
{
Updateplayer = PlayerType.blue;
}
else if (TotalIndex % 2 != 0 && firstplayer == PlayerType.blue)
{
Updateplayer = PlayerType.red;
}
}
}
之后如果大家有什么问题 可以联系我
主页有我的联系方式 希望这篇博文对大家有帮助