2D獵寶行動(類掃雷小遊戲)DAY 3
阿新 • • 發佈:2018-11-06
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;
}