コード例 #1
0
    // Advances our pathfinding progress by one tile.
    void AdvancePathing()
    {
        if (remainingMovement <= 0)
        {
            return;
        }

        if (currentPath == null || currentPath.Count <= 1)
        {
            return;
        }

        map.audioSource.PlayOneShot(sndSlide);

        // Teleport us to our correct "current" position, in case we
        // haven't finished the animation yet.
        transform.position = map.TileCoordToWorldCoord(pos, Y_POS);

        //Debug.Log(remainingMovement + " on tile " + currentPath[0].pos + ". Moving to " + currentPath[1].pos);

        // Move us to the next tile in the sequence
        pos.x = currentPath[1].pos.x;
        pos.y = currentPath[1].pos.y;

        // Add our remaining movement to current tile
        ClickableTile currentTile = map.tiles[pos.x, pos.y];

        currentTile.AddToValue(moveSpeed - remainingMovement + 1);
        currentTile.DrawHighlight(false, .01f);
        if (currentTile.CheckForCapture(faction))
        {
            // This is the last tile, so set out movespeed to one
            if (currentTile.pos == currentPath[currentPath.Count - 1].pos)
            {
                SetMoveSpeed(1);
            }
        }

        // Subtract cost from current tile to next tile
        remainingMovement -= 1;

        // Remove the old "current" tile from the pathfinding list
        currentPath.RemoveAt(0);

        if (currentPath.Count == 1)
        {
            // We only have one tile left in the path, and that tile MUST be our ultimate
            // destination -- and we are standing on it!

            // Unit arrived at destination. Sending out event message!
            if (pathCompleteEvent != null)
            {
                pathCompleteEvent();
            }

            // We are at our destinationa and we are out of moves, so end our turn
            if (remainingMovement <= 0)
            {
                //Debug.Log("Unit out of path and moves! Ending turn.");
                FinishTurn();
            }
        }
    }
コード例 #2
0
    void DrawPath()
    {
        if (Input.GetButtonUp("Fire1"))
        {
            //We have let go, return to idle state
            state = ControllerState.idle;

            //All moves used?
            if (drawPathStack.Count - 1 == selectedUnit.GetMoveSpeed())
            {
                //Is this a valid path that leads to a destination tile?
                Node finalNode = drawPathStack.Peek();
                if (map.tiles[finalNode.pos.x, finalNode.pos.y].IsAvailable())
                {
                    //Feed our unit this path
                    Debug.Log("sending drawn path to unit");
                    List <Node> path = new List <Node>(drawPathStack);
                    path.Reverse();
                    selectedUnit.SetPath(path);
                }
            }
            else
            {
                map.ResetTileDrawPath();
            }
            ClearDrawPath();
            return;
        }

        Node       currentNode = drawPathStack.Peek();
        RaycastHit hit;

        //Check if we are hovering over tile
        if (MouseCast(out hit))
        {
            if (hit.transform.tag == "Tile")
            {
                ClickableTile tempTile = hit.transform.GetComponent <ClickableTile>();
                Node          tempNode = map.nodes[tempTile.pos.x, tempTile.pos.y];

                //Is the tile a walkable neighbor tile?
                if (NodeIsNeighbour(currentNode, tempNode) && map.UnitCanEnterTile(tempNode.pos, selectedUnit))
                {
                    //Is it the previous path node?
                    if (drawPathStack.Contains(tempNode))
                    {
                        //pop current position
                        Node          popped = drawPathStack.Pop();
                        ClickableTile tile   = map.tiles[popped.pos.x, popped.pos.y].GetComponent <ClickableTile>();
                        Debug.Log(tile.GetValue());
                        tile.DrawHighlight(false, .01f);
                        tile.SetValueText(tile.GetValue());

                        audioSource.volume = .4f;
                        audioSource.pitch  = 1.9f;
                        audioSource.clip   = sndClickUp;
                        audioSource.Play();

                        //adjust available tiles
                        var coordOptions = selectedUnit.GetAvailableTileOptions(tempNode, selectedUnit.GetMoveSpeed() - drawPathStack.Count + 1);
                        map.ResetTileAvailability();
                        List <Map.Coord> coordList = new List <Map.Coord>();
                        for (int i = 0; i < selectedUnit.coordOptions.Length; i++)
                        {
                            if (coordOptions.Contains(selectedUnit.coordOptions[i]))
                            {
                                coordList.Add(selectedUnit.coordOptions[i]);
                            }
                        }
                        map.MakeTilesAvailable(coordList.ToArray());
                    }
                    //If not, we can push this neighbor is we have remianing moves, one step closer to destination
                    else if (drawPathStack.Count - 1 < selectedUnit.GetMoveSpeed())
                    {
                        {
                            //adjust available tiles
                            var coordOptions = selectedUnit.GetAvailableTileOptions(tempNode, selectedUnit.GetMoveSpeed() - drawPathStack.Count);
                            map.ResetTileAvailability();
                            List <Map.Coord> coordList = new List <Map.Coord>();
                            for (int i = 0; i < selectedUnit.coordOptions.Length; i++)
                            {
                                if (coordOptions.Contains(selectedUnit.coordOptions[i]))
                                {
                                    coordList.Add(selectedUnit.coordOptions[i]);
                                }
                            }
                            map.MakeTilesAvailable(coordList.ToArray());

                            drawPathStack.Push(tempNode);
                            //highlight tempTile? show its part of path
                            tempTile.DrawHighlight(true, .02f);
                            tempTile.SetValueText(tempTile.GetValue() + drawPathStack.Count - 1);

                            audioSource.volume = .95f;
                            audioSource.pitch  = (((float)drawPathStack.Count - 1f) / (float)selectedUnit.GetMoveSpeed()) * .1f + .9f;
                            audioSource.clip   = sndClickDown;
                            audioSource.Play();
                        }
                    }
                }
            } //end MouseCast
        }
    }