2D猎宝行动(类扫雷小游戏)DAY 3

1.制作插旗时的扬尘特效

2.部署插旗特效并创建数字元素类和陷阱元素类

建立NumberElement和TrapElement类,分析需要重写的方法。

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

public class NumberElement : SingleCoveredElement {

    public override void Awake()
    {
        base.Awake();
        elementState = ElementState.Covered;
        elementContent = ElementContent.Number;
    }

    public override void OnMiddleMouseButton()
    {
        
    }

    public override void UncoverElementSingle()
    {
        
    }

    public override void OnUnCovered()
    {
        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TrapElement : SingleCoveredElement {

    public override void Awake()
    {
        base.Awake();
        elementState = ElementState.Covered;
        elementContent = ElementContent.Trap;
    }

    public override void UncoverElementSingle()
    {

    }

    public override void OnUnCovered()
    {

    }
}

3.设计数字元素类的功能

首先在基类BaseElement添加ClearShadow方法

    public void ClearShadow()
    {
        Transform shadow = transform.Find("shadow");
        if(shadow != null)
        {
            Destroy(shadow.gameObject);
        }
    }

完善数字元素类功能并分析后面要实现的功能

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

public class NumberElement : SingleCoveredElement {

    public override void Awake()
    {
        base.Awake();
        elementState = ElementState.Covered;
        elementContent = ElementContent.Number;
    }

    public override void OnMiddleMouseButton()
    {
        //TODO 检查八领域并翻开
    }

    public override void UncoverElementSingle()
    {
        if (elementState == ElementState.Uncovered) return;
        RemoveFlag();
        elementState = ElementState.Uncovered;
        ClearShadow();
        //TODO 显示泥土翻开的特效
        //TODO 计算并显示自身数字
    }

    public override void OnUnCovered()
    {
        //TODO 泛洪算法翻开周边的元素
    }
}

4.设计陷阱元素类的功能

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

public class TrapElement : SingleCoveredElement {

    public override void Awake()
    {
        base.Awake();
        elementState = ElementState.Covered;
        elementContent = ElementContent.Trap;
    }

    public override void UncoverElementSingle()
    {
        if (elementState == ElementState.Uncovered) return;
        RemoveFlag();
        elementState = ElementState.Uncovered;
        ClearShadow();
        //TODO 显示泥土翻开的特效
        LoadSprite(GameManager._instance.trapSprites[Random.Range(0, GameManager._instance.trapSprites.Length)]);
    }

    public override void OnUnCovered()
    {
        //TODO 翻开所有雷
    }
}

5.制作并部署泥土翻开特效

根据之前做过的扬尘特效制作泥土特效,然后建一个自动销毁的脚本

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

public class AutoDestroy : MonoBehaviour {

    public float delay;

    private void Start()
    {
        Destroy(gameObject, delay);
    }
}

然后部署泥土的特效

public GameObject uncoveredEffect;

Instantiate(GameManager._instance.uncoveredEffect, transform);

6.设计地图的初始化方案

分析地图初始化方案,创建存储可用索引值的列表

    private void InitMap()
    {
        //可用索引值的列表
        List<int> availabaleIndex = new List<int>();
        for(int i = 0; i < w * h; i++)
        {
            availabaleIndex.Add(i);
        }
    }

7.设计地址转换方法与元素转换方法

设计一维索引值和二维索引值相互转换的方法以及生成陷阱的方法

    /// <summary>
    /// 生成陷阱
    /// </summary>
    /// <param name="availableIndex">尚未初始化的地图元素的索引值</param>
    private void GenerateTrap(List<int> availableIndex)
    {
        float trapProbability = Random.Range(minTrapProbability, maxTrapProbability);
        int trapNum = (int)(availableIndex.Count * trapProbability);
        for(int i = 0; i < trapNum; i++)
        {
            int tempIndex = availableIndex[Random.Range(0, availableIndex.Count)];
            int x, y;
            GetPosition(tempIndex, out x, out y);
            availableIndex.Remove(tempIndex);
        }
    }


    private BaseElement SetElement(int index,ElementContent content)
    {
        return null;
    }
    /// <summary>
    /// 将给定的一维索引值转换为二维索引值
    /// </summary>
    /// <param name="index">给顶的一维索引值</param>
    /// <param name="x">转换后的二维索引值的x</param>
    /// <param name="y">转换后的二维索引值的y</param>
    private void GetPosition(int index,out int x,out int y)
    {
        y = index / w;
        x = index - y * w;
    }

    /// <summary>
    /// 将给定的二维索引值转换为一维索引值
    /// </summary>
    /// <param name="x">二维索引值的x</param>
    /// <param name="y">二维索引值的y</param>
    /// <returns></returns>
    private int GetIndex(int x,int y)
    {
        return w * y + x;
    }

猜你喜欢

转载自blog.csdn.net/kouzhuanjing1849/article/details/83017782