Exemplo n.º 1
0
 public MapPathNode(Node node)
 {
     this.node = node;
     score     = 0.0f;
     fScore    = 0.0f;
     cameFrom  = null;
 }
    private static void LoadNeighDistFromFile(BinaryReader reader, ref MapPathNode node)
    {
        var nodeId = reader.ReadInt32();

        if (nodeId != node.id)
        {
            throw new InvalidOperationException("Node distance data is corrupted (wrong node id).");
        }

        var neighbourCount = reader.ReadInt32();

        if (neighbourCount != node.neighbours.Length)
        {
            throw new InvalidOperationException("Node distance data is corrupted (wrong neighbour count).");
        }

        if (node.neighbours.Length == 0)
        {
            return;
        }

        var distancesRaw = MemoryMarshal.Cast <float, byte>(node.neighDistances.AsSpan());

        if (reader.Read(distancesRaw) != distancesRaw.Length)
        {
            throw new InvalidOperationException("Node distance data is corrupted (not enough distance data).");
        }

        node.flags |= PathNodeFlag.NEIGHBOUR_DISTANCES_SET;
    }
    private static void LoadNodeFromFile(BinaryReader reader, out MapPathNode node)
    {
        node         = new MapPathNode();
        node.id      = reader.ReadInt32();
        node.nodeLoc = reader.ReadLocationAndOffsets();
        var neighCnt = reader.ReadInt32();

        if (neighCnt == 0)
        {
            // why are we holding nodes without neighbours? they should probably be culled...
            return;
        }

        if (neighCnt > MaxNeighbours)
        {
            Logger.Info("Too many neighbours for node {0}", node.id);
            Trace.Assert(neighCnt <= MaxNeighbours);
        }

        var neighbours    = new int[neighCnt];
        var neighboursRaw = MemoryMarshal.Cast <int, byte>(neighbours);

        if (reader.Read(neighboursRaw) != neighboursRaw.Length)
        {
            throw new InvalidOperationException("Failed to read node neighbours");
        }

        node.neighbours = neighbours;

        node.neighDistances = new float[neighCnt];
    }
Exemplo n.º 4
0
        public override void Import(XmlObject pData, bool pBuild)
        {
            MapPathNode data = pData as MapPathNode;

            NodeTime = data.NodeTime;
            Position = data.Position;
            this.Build();
            this.SetName();
        }
Exemplo n.º 5
0
        public override XmlObject Export()
        {
            MapPathNode data = new MapPathNode
            {
                NodeTime = NodeTime,
                Position = Position
            };

            return(data);
        }
Exemplo n.º 6
0
        public override void Import(DCFG cfg)
        {
            MapPathNode data = cfg as MapPathNode;

            this.Time        = data.Time;
            this.Pos         = data.Pos;
            this.EulerAngles = data.EulerAngles;
            this.Init();
            this.Tangent.position = data.Pos;
        }
Exemplo n.º 7
0
        public override DCFG Export()
        {
            MapPathNode data = new MapPathNode();

            data.Time        = Time;
            data.Pos         = Pos;
            data.EulerAngles = EulerAngles;
            data.TangentPos  = Tangent.position;
            return(data);
        }
Exemplo n.º 8
0
        public override DCFG Export()
        {
            MapPath data = new MapPath();

            data.ID           = this.ID;
            data.Type         = (int)this.Type;
            data.PositionVary = this.PositionVary;
            data.RotationVary = this.RotationVary;
            for (int i = 0; i < PathNodes.Count; i++)
            {
                MapPathNode d = (MapPathNode)PathNodes[i].Export();
                data.PathNodes.Add(d);
            }
            return(data);
        }
Exemplo n.º 9
0
        public override void Import(DCFG cfg)
        {
            MapPath data = cfg as MapPath;

            this.ID           = data.ID;
            this.Type         = (EPathNodeType)data.Type;
            this.PositionVary = data.PositionVary;
            this.RotationVary = data.RotationVary;
            for (int i = 0; i < data.PathNodes.Count; i++)
            {
                MapPathNode d = data.PathNodes[i];
                FTPathNode  p = new GameObject().AddComponent <FTPathNode>();
                p.transform.parent = transform;
                p.Import(d);
            }
        }
Exemplo n.º 10
0
    private void OnDrawGizmos()
    {
        if (lastPath != null)
        {
            Gizmos.color = renderer.color;
            Vector2     lastLocation = lastPath.node.Value.location;
            MapPathNode current      = lastPath;

            while (current != null)
            {
                Gizmos.DrawLine(lastLocation, current.node.Value.location);

                lastLocation = current.node.Value.location;
                current      = current.cameFrom;
            }

            Gizmos.DrawLine(lastLocation, transform.position);
        }
    }
Exemplo n.º 11
0
    protected Vector2 FindNextPathLocation(GhostMovementPattern pattern)
    {
        Vector2 targetLocation = pattern.CalculateTarget(transform, environment, player.gameObject);

        //TODO: optimize to reduce garbage colleciton
        MapPathNode start  = new MapPathNode(environment.GetClosetNode(transform.position));
        MapPathNode target = new MapPathNode(environment.GetClosetNode(targetLocation));

        start.score = 0;
        SortedList <Vector2, MapPathNode> closedSet = new SortedList <Vector2, MapPathNode>(new Vector2Comparer());
        SortedList <float, MapPathNode>   openSet   = new SortedList <float, MapPathNode>(new MapPathNodeComparer());

        openSet.Add(start.fScore, start);

        while (openSet.Count > 0)
        {
            MapPathNode current = openSet.Values[0];

            if (current == target)
            {
                lastPath = current;
                MapPathNode check      = current;
                MapPathNode lastBefore = current;

                while (check.cameFrom != null)
                {
                    lastBefore = check;
                    check      = lastBefore.cameFrom;
                }

                Vector2 result = lastBefore.node.Value.location;

                if (result.x <= environment.min.x || result.x >= environment.max.x || result.y <= environment.min.y || result.y >= environment.max.y)
                {
                    result = (Vector2)transform.position + (lastBefore.direction * speed);
                }

                return(result);
            }

            openSet.RemoveAt(0);
            closedSet.Add(current.node.Value.location, current);

            for (int i = 0; i < 4; ++i)
            {
                Node?child = current.node.Value.connections[i];

                if (child == null || closedSet.ContainsKey(child.Value.location))
                {
                    continue;
                }

                MapPathNode addChild      = new MapPathNode(child.Value);
                float       tenitiveScore = 1.0f + current.score;

                int index = openSet.IndexOfValue(addChild);
                if (index >= 0)
                {
                    if (tenitiveScore >= openSet.Values[index].score)
                    {
                        continue;
                    }
                    else
                    {
                        addChild = openSet.Values[index];
                        openSet.RemoveAt(index);
                    }
                }

                addChild.score     = tenitiveScore;
                addChild.cameFrom  = current;
                addChild.fScore    = pattern.CalculateFScore(targetLocation, addChild.node.Value.location, addChild.score);
                addChild.direction = movementDirections[i];
                openSet.Add(addChild.fScore, addChild);
            }
        }

        return(start.node.Value.location);
    }