前言
备赛的时候需要实现的各种功能,这里记录一下
实现棋盘格触发检测的效果,玩家需要走正确的路线才能通关,否则会回到初始点
效果视频
需要实现如下功能
一、创建棋盘格区域
作为演示 我创建了一个4*4的棋盘格区域
每一个格子仅是一个面片,材质为红色透明。
二、初始化路线图
程序利用棋盘格的格子随机形成一条正确的路线,该路线需以棋盘格区域的右下角格子为起点,左上角格子为终点,且是一条连续的完整的可通过的路线。当角色离开棋盘格区域时,会随机生成另一条正确路线。
- Barrier Manager.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//**************************
//制作人:Tenkinoko
//脚本功能:表格管理
//**************************
public class BarrierManager : MonoBehaviour
{
public struct mapDefine
{
public bool[,] visit;
public int Count;
}
public static BarrierManager instance;
[SerializeField]
[Header("控制路线图面板开关")]
public bool isShow = false;
[SerializeField]
[Header("九宫格视图")]
private Image mapImg;
[SerializeField]
[Header("九宫格游戏物体")]
private GameObject mapPlane;
[SerializeField]
[Header("障碍物攻略图")]
private Sprite[] pics;
public mapDefine[] mapInfo;
[SerializeField]
[Header("当前攻略图序")]
public int index;
private void Awake()
{
//光标锁定Game窗口
Cursor.lockState = CursorLockMode.Confined;
instance = this;
//初始化序图
index = UnityEngine.Random.Range(0, 3);
mapInfoInit();
}
//判断玩家踩的格子坐标是否为正确格子
public bool OnTouchMap(int x,int y)
{
return mapInfo[index].visit[x, y];
}
//表格信息初始化
private void mapInfoInit()
{
mapInfo = new mapDefine[3];
//将每个表格数组初始化为False
for (int i = 0; i < mapInfo.Length; i++)
{
mapInfo[i].visit = new bool[4, 4];
for (int x = 0; x < mapInfo[i].visit.GetLength(0); x++)
{
for (int y = 0; y < mapInfo[i].visit.GetLength(0); y++)
{
mapInfo[i].visit[x, y] = false;
}
}
}
//将正确的格子坐标设置为true
mapInfoDabiao();
}
//这里可以用配置表 我这边演示就打表了
//根据你绘制的路线图的各个格子坐标赋值即可
private void mapInfoDabiao()
{
mapInfo[0].visit[0, 0] = true;
mapInfo[0].visit[0, 1] = true;
mapInfo[0].visit[1, 1] = true;
mapInfo[0].visit[1, 2] = true;
mapInfo[0].visit[2, 2] = true;
mapInfo[0].visit[2, 3] = true;
mapInfo[0].visit[3, 3] = true;
mapInfo[0].Count = 7;
mapInfo[1].visit[0, 0] = true;
mapInfo[1].visit[1, 0] = true;
mapInfo[1].visit[1, 1] = true;
mapInfo[1].visit[1, 2] = true;
mapInfo[1].visit[2, 2] = true;
mapInfo[1].visit[3, 2] = true;
mapInfo[1].visit[3, 3] = true;
mapInfo[1].Count = 7;
mapInfo[2].visit[0, 0] = true;
mapInfo[2].visit[1, 0] = true;
mapInfo[2].visit[1, 1] = true;
mapInfo[2].visit[2, 1] = true;
mapInfo[2].visit[2, 2] = true;
mapInfo[2].visit[2, 3] = true;
mapInfo[2].visit[3, 3] = true;
mapInfo[2].Count = 7;
}
/// <summary>
/// 关闭显示表格攻略视图
/// </summary>
public void OnclickMap()
{
isShow = !isShow;
mapPlane.SetActive(isShow);
}
/// <summary>
/// 加载表格攻略图
/// </summary>
public void LoadImage()
{
while (true) {
var temp = UnityEngine.Random.Range(0, 3);
if (index != temp)
{
index = temp;
break;
}
}
mapImg.sprite = pics[index];
}
}
三、格子逻辑处理
当角色进入棋盘格区域移动时,需按照从起点到终点的顺序行走,脚下的格子如果是路线中正确顺序的格子,则颜色会变成绿色,错误的则强制黑幕过渡并回到起点,此时不会重置路线。
格子初始化和基本逻辑编写
- Barrier Trigger Controller (格子触发逻辑)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//**************************
//制作人:Tenkinoko
//脚本功能:格子控制脚本
//**************************
public class BarrierTriggerController : MonoBehaviour
{
[SerializeField]
[Header("当前区块的x坐标")]
private int x;
[SerializeField]
[Header("当前区块的y坐标")]
private int y;
[SerializeField]
[Header("黑幕")]
private GameObject DrakPanle;
[SerializeField]
[Header("是否踩过")]
public bool visit=false;
private void OnTriggerEnter(Collider other)
{
if (other.name == "Play"&&!visit)
{
if (BarrierObjController.instance.goIndex < BarrierManager.instance.mapInfo[BarrierManager.instance.index].Count)
{
if (BarrierManager.instance.OnTouchMap(x, y))
{
this.GetComponent<MeshRenderer>().material = Resources.Load<Material>("Yellow");
BarrierObjController.instance.goIndex++;
visit = true;
}
else
{
StartCoroutine(DrakShow());
StartCoroutine(BarrierObjController.instance.ResetPlayPoint());
visit = true;
}
}
}
}
/// <summary>
/// 黑幕面板的显隐
/// </summary>
/// <returns></returns>
IEnumerator DrakShow()
{
DrakPanle.SetActive(true);
yield return new WaitForSeconds(1f);
DrakPanle.SetActive(false);
}
}
- BarrierObjController.cs (格子物体逻辑控制)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//**************************
//制作人:Tenkinoko
//脚本功能:
//**************************
public class BarrierObjController : MonoBehaviour
{
public static BarrierObjController instance;
public int goIndex = 0;
public Vector3 initPorint;
public GameObject playTsf;
public GameObject[] zaMaterial;
private void Awake()
{
instance = this;
}
/// <summary>
/// 踩错重置信息
/// </summary>
/// <returns></returns>
public IEnumerator ResetPlayPoint()
{
ZhanAiInit();
//已踩格子数初始化
goIndex = 0;
playTsf.GetComponent<PlayController>().enabled = false;
//重置玩家位置
playTsf.transform.position = initPorint;
yield return new WaitForSeconds(0.5f);
playTsf.GetComponent<PlayController>().enabled = true;
}
//将所有格子初始化
public void ZhanAiInit()
{
foreach (var item in zaMaterial)
{
item.GetComponent<MeshRenderer>().material = Resources.Load<Material>("Normal");
item.GetComponent<BarrierTriggerController>().visit = false;
}
}
}
总结
基本的逻辑就三种 棋盘初始化 正确路线初始化 格子被触发的逻辑判断
有兴趣的可以在此基础上迭代一下,这里只是作者的一些拙见,有建议可以写到评论区中。