예제 #1
0
        private void DrawPath()
        {
            if (Simulation.Debug)
            {
                if (!path.Any())
                {
                    return;
                }

                foreach (IPathAction <Tile> action in path)
                {
                    PathfindingMovement move  = action as PathfindingMovement;
                    PathfindingDelay    delay = action as PathfindingDelay;

                    if (move != null)
                    {
                        Debug.DrawLine(move.Origin.Position, move.Destination.Position, debugColor);
                    }
                    else
                    {
                        Vector3 topLeft     = delay.Origin.Position + new Vector3(-Tile.TileSize / 4, 0, Tile.TileSize / 4);
                        Vector3 topRight    = delay.Origin.Position + new Vector3(Tile.TileSize / 4, 0, Tile.TileSize / 4);
                        Vector3 bottomLeft  = delay.Origin.Position + new Vector3(-Tile.TileSize / 4, 0, -Tile.TileSize / 4);
                        Vector3 bottomRight = delay.Origin.Position + new Vector3(Tile.TileSize / 4, 0, -Tile.TileSize / 4);

                        Debug.DrawLine(topLeft, bottomRight, debugColor);
                        Debug.DrawLine(topRight, bottomLeft, debugColor);
                    }
                }
            }
        }
예제 #2
0
        private void ClaimPath()
        {
            // The number of frames until we reach a given tile
            int time = 0;

            for (int i = 0; i < path.Count; ++i)
            {
                IPathAction <Tile> current = path.ElementAt(i);

                if (current is PathfindingDelay)
                {
                    PathfindingDelay delay = current as PathfindingDelay;
                    delay.Origin.AddClaim(time, delay.Delay, this, null);
                    time += delay.Delay;
                }
                else
                {
                    PathfindingMovement move = current as PathfindingMovement;
                    float dist = Vector3.Distance(move.Origin.Position, move.Destination.Position);

                    int numframes  = (int)(dist / speedPerFrame);
                    int halfframes = numframes / 2;

                    // Half of the trip will be in each tile
                    move.Origin.AddClaim(time, halfframes, this, move.Destination);
                    move.Destination.AddClaim(time + halfframes, halfframes, this, null);

                    time += numframes;
                }
            }
        }
예제 #3
0
        private void Move()
        {
            PathfindingMovement target = path.FirstOrDefault() as PathfindingMovement;
            PathfindingDelay    delay  = path.FirstOrDefault() as PathfindingDelay;

            if (target != null)
            {
                Tile destination = target.Destination;

                // Moves the agent towards the next tile in it's magical journey.
                agent.transform.position = Vector3.MoveTowards(agent.transform.position, target.Destination.Position, speedPerFrame);

                if (Vector3.Distance(destination.Position, agent.transform.position) < Simulation.Tolerance)
                {
                    path.RemoveAt(0);

                    if (path.Any())
                    {
                        // Rotate towards our new target
                        var nextTarget = path.OfType <PathfindingMovement>().FirstOrDefault();

                        if (nextTarget != null)
                        {
                            agent.transform.LookAt(nextTarget.Destination.Position);
                        }
                    }
                    else
                    {
                        // We've reached our goal! Now we need to figure out what we're supposed to do now.
                        var action = destination.GetTileAction();

                        // TODO: If we add any actions that don't unspawn the agent, we need to give it a new goal.
                        if (action != null)
                        {
                            action(this);
                        }
                    }
                }
            }
            else if (delay != null)
            {
                //Debug.Log ("Delay is occuring!: " + delay.Delay + " speed: " + model.GetComponent<Animator>().GetInteger("Speed".GetHashCode()));
                delay.Delay -= 1;

                // if waiting on a tile, play the idle animation
                if (delay.Delay > 5)
                {
                    //anim.SetTrigger("StopWalking");
                    Debug.Log("To idle state");
                }
                else if (delay.Delay <= 0)
                {
                    path.RemoveAt(0);
                    // now that the delay is over, start walking animations
                    //anim.SetTrigger("StartWalking");
                    Debug.Log("To walking state");
                }
            }
        }