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

xiaoxiao2021-04-05  470

1.创建地图元素数组并设计元素基类

在GameManager中添加mapArray属性,是用来存储地图的二维数组类型

public BaseElement[,] mapArray; //地图的二维数组类型

并在Awake中进行初始化

mapArray = new BaseElement[w, h];//w是宽度,h是高度

然后将创建的可操作元素放置到地图元素数组中

//创建可操作的元素并放置到地图元素数组中 mapArray[i,j] = Instantiate(baseElement, new Vector3(i, j, 0), Quaternion.identity, elementHolder).GetComponent<BaseElement>();

给BaseElement添加boxcolider 2D,然后添加几个点击方法。考虑到这个是个基类,所有都使用虚方法。

public virtual void OnMouseOver() { } public virtual void OnLeftMouseButton() { } public virtual void OnRightMouseButton() { } public virtual void OnMiddleMouseButton() { }

2.分析每个元素的共有功能并完善元素基类

完善OnMouseOver方法,并添加新的方法PlayerStand()

public virtual void OnMouseOver() { //Tip1:GetMouseButtonUp当鼠标点击后抬起才会触发,符合该游戏规则 //Tip2:使用else if拥有优先级,先判断中键,再判断其他 if (Input.GetMouseButtonUp(2)) { OnMiddleMouseButton(); } else if (Input.GetMouseButtonUp(0)) { OnLeftMouseButton(); } else if (Input.GetMouseButtonUp(1)) { OnRightMouseButton(); } } public virtual void PlayerStand() { }

修改UML图。

3.创建枚举类型来标识元素的状态

创建枚举类Enumeration

public enum ElementState { Covered, Uncovered, Marked } public enum ElementType { SingleCovered, DoubleCovered, CantCovered } public enum ElementContent { Number, Trap, Tool, Gold, Enemy, Door, BigWall, SmallWall, Exit }

在BaseElement中添加三个变量

public ElementState elementState; public ElementType elementType; public ElementContent elementContent;

修改中键按下的判断

if (Input.GetMouseButtonUp(2) && elementState == ElementState.Uncovered)

创建三个按键的类并继承BaseElement

4.设计可以翻开一次的元素的基类

添加随机生成的方法和三个状态的方法。

using System.Collections; using System.Collections.Generic; using UnityEngine; public class SingleCoveredElement : BaseElement { public override void Awake() { base.Awake();//先执行基类的Awake放啊 elementType = ElementType.SingleCovered; LoadSprite(GameManager._instance.coverTileSprites[Random.Range(0, GameManager._instance.coverTileSprites.Length)]); } public void UncoveredElement() { } public void AddFlag() { } public void RemoveFlag() { } }

5.完善可以翻开一次的元素的基类设计

左键点击调用方法PlayStand(),便于后期测试。

public virtual void OnLeftMouseButton() { PlayerStand(); }

更新翻开一次的元素的基类方法

public override void OnRightMouseButton() { switch (elementState) { case ElementState.Covered: AddFlag(); break; case ElementState.Uncovered: return; case ElementState.Marked: RemoveFlag(); break; } } public void UncoveredElement() { if (elementState == ElementState.Uncovered) return; UncoverElementSingle(); OnUnCovered(); } public void AddFlag() { elementState = ElementState.Marked; } public void RemoveFlag() { elementState = ElementState.Uncovered; }

6.准备设计插拔旗功能

重写PlayerStand方法

public override void PlayerStand() { switch (elementState) { case ElementState.Covered: UncoveredElement(); break; case ElementState.Uncovered: return; case ElementState.Marked: RemoveFlag(); break; } }

准备旗子预制体。

7.制作插拔旗的动画

完善插旗动画和拔旗动画。

public void AddFlag() { elementState = ElementState.Marked; GameObject flag = Instantiate(GameManager._instance.flagElement, transform); flag.name = "flagElement"; flag.transform.DOLocalMoveY(0, 0.1f); } public void RemoveFlag() { Transform flag = transform.Find("flagElement"); if (flag != null) { elementState = ElementState.Covered; flag.DOLocalMoveY(0.15f, 0.1f).onComplete += () => { Destroy(flag.gameObject); }; }

转载请注明原文地址: https://www.6miu.com/read-4813451.html

最新回复(0)