1. 程式人生 > >2D獵寶行動(類掃雷小遊戲)DAY 8

2D獵寶行動(類掃雷小遊戲)DAY 8

1.完成AStar尋路的整合

完善AStarPathfinding中的方法,然後在GameManager中進行呼叫。

    /// <summary>
    /// 尋路方法
    /// </summary>
    /// <param name="e">尋路終點</param>
    public void FindPath(Point e)
    {

    }

然後在baseElement中進行呼叫

    /// <summary>
    /// 當玩家左鍵點選當前元素時候執行的操作
    /// </summary>
    public virtual void OnLeftMouseButton()
    {
        GameManager._instance.FindPath(new Point(x, y));
    }

2.編寫擴充套件方法轉換路徑陣列並尋路

新建ExtensionClass類,寫List轉換的擴充套件方法

using System.Collections.Generic;
using UnityEngine;
public static class ExtraClass
{
    public static Vector3[] ToVector3Array(this List<Point> list)
    {
        Vector3[] v3s = new Vector3[list.Count];
        for(int i = 0; i < list.Count; i++)
        {
            v3s[i] = new Vector3(list[i].x, list[i].y, 0);
        }
        return v3s;
    }
}

在GameManager中完善尋路方法

    /// <summary>
    /// 尋路方法
    /// </summary>
    /// <param name="e">尋路終點</param>
    public void FindPath(Point e)
    {
        Point s = new Point((int)player.transform.position.x, (int)player.transform.position.y);
        List<Point> pathList = new List<Point>();
        if (AStarPathfinding.FindPath(s, e, pathList) == false) return;
        player.transform.DOPath(pathList.ToVector3Array(), pathList.Count * 0.1f);
    }

3.完善尋路方法的細節

    /// <summary>
    /// 尋路方法
    /// </summary>
    /// <param name="e">尋路終點</param>
    public void FindPath(Point e)
    {
        if(pathFinding == true)  pathTweener.Kill();
        Point s = new Point((int)player.transform.position.x, (int)player.transform.position.y);
        List<Point> pathList = new List<Point>();
        if (AStarPathfinding.FindPath(s, e, pathList) == false) return;
        ResetTarget();
        pathFinding = true;
        pathTweener = player.transform.DOPath(pathList.ToVector3Array(), pathList.Count * 0.1f);
        pathTweener.SetEase(Ease.Linear);
        pathTweener.onComplete += () => {
            pathFinding = false;
        };
        pathTweener.onKill += () => {
            pathFinding = false;
        };
    }

4.使角色可以翻開走過的元素

完善站立區域方法

    /// <summary>
    /// 生成站立區域
    /// </summary>
    /// <param name="y">站立區域中心y座標</param>
    private void GenerateStandArea(int y)
    {
        for(int i = 0; i < standAreaW; i++)
        {
            for(int j = y - 1; j <= y + 1; j++)
            {
                ((SingleCoveredElement)mapArray[i, j]).UncoverElementSingle();
            }
        }
        player.transform.position = new Vector3(1, y, 0);
        prePos = nowPos = player.transform.position.ToVector3Int();
        mapArray[1, y].OnPlayerStand();
    }

在ExtensionClass中新增新的擴充套件方法,轉換Int型

    public static Vector3 ToVector3Int(this Vector3 v)
    {
        int x = v.x - Mathf.FloorToInt(v.x) > 0.5f ? Mathf.CeilToInt(v.x) : Mathf.FloorToInt(v.x);
        int y = v.y - Mathf.FloorToInt(v.y) > 0.5f ? Mathf.CeilToInt(v.y) : Mathf.FloorToInt(v.y);
        return new Vector3(x, y, 0);
    }

在GameManager的Update裡面新增翻開走過

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            ResetTarget();
        }
        nowPos = player.transform.position.ToVector3Int();
        if (prePos != nowPos)
        {
            mapArray[(int)nowPos.x,(int)nowPos.y].OnPlayerStand();
            mapArray[(int)nowPos.x, (int)nowPos.y].OnPlayerStand();
            if(mapArray[(int)nowPos.x, (int)nowPos.y].elementContent == ElementContent.Trap)
            {
                pathTweener.Kill();
                nowPos = prePos;
                player.transform.position = nowPos;
            }
            else
            {
                prePos = nowPos;
            }
        }

5.將移動與動畫狀態機關聯起來