【Unityの进化四十六亿重奏】第五章 简单棋盘格判断功能实现


前言

备赛的时候需要实现的各种功能,这里记录一下

实现棋盘格触发检测的效果,玩家需要走正确的路线才能通关,否则会回到初始点
效果视频


需要实现如下功能

一、创建棋盘格区域

作为演示 我创建了一个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;
        }
    }


}

总结

基本的逻辑就三种 棋盘初始化 正确路线初始化 格子被触发的逻辑判断
有兴趣的可以在此基础上迭代一下,这里只是作者的一些拙见,有建议可以写到评论区中。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值