chenxin
2020-11-06 893f645dafa46b818a2edb9fa40337af0c3598d6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using TowerDefense.Agents;
using TowerDefense.MeshCreator;
using UnityEngine;
 
namespace TowerDefense.Nodes
{
    /// <summary>
    /// A point along the path which agents will navigate towards before recieving the next instruction from the NodeSelector
    /// Requires a collider to be added manually.
    /// Agent孵化和移动的节点,有相应的功能封装。
    /// </summary>
    [RequireComponent(typeof(Collider))]
    public class Node : MonoBehaviour
    {
        /// <summary>
        /// 是否是对手方刷新点,根据这个值来判断刷新出来的Agent是哪一方
        /// </summary>
        public bool bOpponentSide = false;
 
        /// <summary>
        /// Reference to the MeshObject created by an AreaMeshCreator
        /// </summary>
        [HideInInspector]
        public AreaMeshCreator areaMesh;
 
        /// <summary>
        /// Selection weight of the node
        /// </summary>
        public int weight = 1;
 
        /// <summary>
        /// Gets the next node from the selector
        /// </summary>
        /// <returns>Next node, or null if this is the terminating node</returns>
        public Node GetNextNode()
        {
            var selector = GetComponent<NodeSelector>();
            if (selector != null)
            {
                return selector.GetNextNode();
            }
            return null;
        }
 
        /// <summary>
        /// Gets a random point inside the area defined by a node's meshcreator
        /// </summary>
        /// <returns>A random point within the MeshObject's area</returns>
        public Vector3 GetRandomPointInNodeArea()
        {
            // Fallback to our position if we have no mesh
            return transform.position;
            // 返回随机位置信息.
            //return areaMesh == null ? transform.position : areaMesh.GetRandomPointInside();
        }
 
        /// <summary>
        /// When agent enters the node area, get the next node
        /// </summary>
        public virtual void OnTriggerEnter(Collider other)
        {
            // var agent = other.gameObject.GetComponent<Agent>();
            // if (agent != null)
            // {
            //     agent.MoveToNextNode(this);
            // }
        }
 
#if UNITY_EDITOR
        /// <summary>
        /// Ensure the collider is a trigger
        /// </summary>
        protected void OnValidate()
        {
            var trigger = GetComponent<Collider>();
            if (trigger != null)
            {
                trigger.isTrigger = true;
            }
            
            // Try and find AreaMeshCreator
            if (areaMesh == null)
            {
                areaMesh = GetComponentInChildren<AreaMeshCreator>();
            }
        }
 
        void OnDrawGizmos()
        {
            Gizmos.DrawIcon(transform.position + Vector3.up, "movement_node.png", true);
        }
#endif
    }
}