游戏开发(五):关卡信息的存储以及关卡责任链的开发

本文详细介绍了在游戏开发中如何存储关卡信息,包括使用Tool宏编辑地图,Game宏运行游戏,定义关卡信息类,怪物波次信息类,以及关卡类的设计。同时,探讨了在MapMaker中重写地图加载方法和GridPoint格子状态更新及创建物品的方法,以实现更灵活的游戏逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编辑地图时用Tool宏,游戏运行时用Game宏

关卡信息类:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 关卡信息类
/// </summary>
public class LevelInfo  {

    public int bigLevelID;
    public int levelID;

    public List<GridPoint.GridState> gridStateList;

    public List<GridPoint.GridIndex> monsterPathList;

    public List<Round.RoundInfo> roundInfoList;
}

怪物波次的信息类:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Round
{
    [System.Serializable]
    public struct RoundInfo
    {
        public int[] mMonsterIDList;
    }

    public RoundInfo roundInfo;
    protected Round mNextRound;
    protected int mRoundID;
    protected Level mLevel;

    public Round(int[] monsterIDList, int roundID, Level level)
    {
        roundInfo.mMonsterIDList = monsterIDList;
        mRoundID = roundID;
        mLevel = level;
    }

    public void SetNextHandle(Round nextRound)
    {
        mNextRound = nextRound;
    }

    /// <summary>
    /// 根据波次ID进行产怪
    /// </summary>
    /// <param name="roundID"></param>
    public void Handle(int roundID)
    {
        if (mRoundID < roundID)
        {
            mNextRound.Handle(roundID);
        }
        else
        {
            //产怪
            GameController.Instance.mMonsterIDList = roundInfo.mMonsterIDList;
            GameController.Instance.CreateMonster();
            GameController.Instance.creatingMonster = true;
        }
    }

}

关卡类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Level
{
    public int totalRound;
    public Round[] roundList;
    public int currentRound;

    public Level(int totalRoundNum, List<Round.RoundInfo> roundInfoList)
    {
        totalRound = totalRoundNum;
        roundList = new Round[totalRound];
        //对Round数组赋值
        for (int i = 0; i < totalRound; i++)
        {
            roundList[i] = new Round(roundInfoList[i].mMonsterIDList, i, this);
        }
        //设置责任链
        for (int i = 0; i < totalRound; i++)
        {
            if (i == totalRound - 1)
            {
                break;
            }
            roundList[i].SetNextHandle(roundList[i + 1]);
        }
    }

    public void HandleRound()
    {
        Debug.Log(currentRound+":"+ totalRound);
        if (currentRound >= totalRound)
        {
            //TODO 胜利

        }
        else if (currentRound == totalRound - 1)
        {
            //TODO  最后一波怪的UI显示  与 音乐播放.

            HandleLastRound();
        }
        else
        {
            roundList[currentRound].Handle(currentRound);
        }
    }

    public void HandleLastRound()
    {
        roundList[currentRound].Handle(currentRound);
    }

    public void AddRoundNun()
    {
        currentRound++;
    }
}

MapMaker中重写对地图加载的方法:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;

public class MapMaker : MonoBehaviour
{
#if Tool
    //是否画线
    public bool drawLine;

    //格子预制体
    public GameObject gridGO;

    public static MapMaker _instance;
    public static MapMaker Instance
    {
        get { return _instance; }
    }
#endif

    //当前关卡索引
    public int bigLevelID;
    public int levelID;

    //地图
    public float mapWidth;
    public float mapHeight;

    //格子
    public float gridWidth;
    public float gridHeight;

    //全部的格子对象
    public GridPoint[,] gridPoints;

    //行列
    public int xColumn = 12;
    public int yRow = 8;

    private SpriteRenderer bg
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值