Esempio n. 1
0
    public void SetLocation(GameObject newLoc)
    {
        transform.position = newLoc.transform.TransformPoint(Vector3.zero) + Vector3.forward * gameObject.transform.position.z;
        if (flag)
        {
            flag.transform.position = newLoc.transform.TransformPoint(Vector3.zero) + Vector3.forward * flag.transform.position.z;
            flag.GetComponent <GamePiece>().gridElement = newLoc.GetComponent <GridElement>();
            if (newLoc.GetComponent <GridElement>().goal)
            {
                grid.gameMan.EndLevel();
            }
        }
        // Check if gridElement has been assigned (this is for spawning)
        if (!gridElement)
        {
            if (!FindGridElement())
            {
                Debug.Log(gridElement.piece);
            }
        }
        // Handle Collisions; We're assuming newLoc always has a GridElement
        GridElement otherGE = newLoc.GetComponent <GridElement>();

        if (otherGE && otherGE.piece && otherGE.piece != gameObject)
        {
            //Debug.Log("Collided with: " + otherGE.piece.name);
            // Check to make sure we're working with a unit
            if (otherGE.piece.GetComponent <GamePiece>() is Unit)
            {
                Unit otherUnit = otherGE.piece.GetComponent <Unit>();
                otherUnit.owner.GetComponent <Player>().ReturnUnit(otherGE.piece);
                if (otherUnit.unitType == UnitType.Portalist)
                {
                    otherUnit.GetComponent <PortalPlacer>().PlacePortal(otherGE);
                }
                otherGE.piece = null;
                // check to see if the other piece has the flag
                if (otherUnit.flag)
                {
                    otherGE.piece = otherUnit.flag;
                    flag          = null;
                }
                // make sure you don't have the flag
                if (flag)
                {
                    otherGE.piece = flag;
                    flag.GetComponent <GamePiece>().gridElement = otherGE;
                    flag = null;
                }
                // Don't forget to kill yourself
                owner.GetComponent <Player>().ReturnUnit(gameObject);
                if (unitType == UnitType.Portalist)
                {
                    this.GetComponent <PortalPlacer>().PlacePortal(otherGE);
                }
                gridElement.piece = null;
                return;
            }
            else
            {
                // Check for flag
                if (otherGE.piece.GetComponent <GamePiece>() is Flag)
                {
                    flag           = otherGE.piece;
                    canAct         = false;
                    remainingMoves = 0;
                }
                else if (otherGE.piece.GetComponent <GamePiece>() is Trap)
                {
                    if (unitType == UnitType.Portalist)
                    {
                        this.GetComponent <PortalPlacer>().PlacePortal(otherGE);
                    }
                    if (gridElement.piece == gameObject)
                    {
                        gridElement.piece = null;
                    }
                    if (!grid)
                    {
                        grid = FindObjectOfType <GridMenu>();
                    }
                    owner.GetComponent <Player>().ReturnUnit(gameObject);
                    if (flag)   // flags will destroy traps; currently no piece can destroy traps,
                                // so if a flag lands on one, it must either destroy the trap or the game is unwinnable
                                // it may be better to have traps pull/pushable, while the flag remains aloof. This would
                                // prevent the need to destroy the trap
                    {
                        Destroy(otherGE.piece);
                        otherGE.piece = flag;
                        flag.GetComponent <GamePiece>().gridElement = otherGE;
                        flag = null;
                    }
                    return;
                }
            }
        }
        gridElement.piece = null;
        gridElement       = newLoc.GetComponent <GridElement>();
        gridElement.piece = gameObject;

        if (flag)
        {
            flag.GetComponent <GamePiece>().gridElement = gridElement;
            if (gridElement.goal)
            {
                // Player has moved the flag into the goal!
                // Someone should probably contact the gamemanager
                grid.gameMan.EndLevel();
                grid.activeUIMenu = false;
            }
        }
        if (!grid)
        {
            grid = FindObjectOfType <GridMenu>();
        }
        grid.UpdateDescription();
    }
Esempio n. 2
0
    public void PlaceUnit(GameObject location, UnitType type)
    {
        GameObject unitGO = null;

        switch (type)
        {
        case UnitType.Unit:
            unitGO = unitPool.GetObject();
            break;

        case UnitType.Puller:
            unitGO = pullerPool.GetObject();
            break;

        case UnitType.Pusher:
            unitGO = pusherPool.GetObject();
            break;

        case UnitType.Twister:
            unitGO = twisterPool.GetObject();
            break;

        case UnitType.Portalist:
            unitGO = portalPlacerPool.GetObject();
            break;
        }
        if (unitGO)
        {
            Unit uScript = unitGO.GetComponent <Unit>();
            uScript.owner = gameObject;
            uScript.SetLocation(location);
            uScript.remainingMoves = 2;
            // Set up linked list
            if (head != null)
            {
                DoublyLinkedListNode current = head;
                while (current.forward != head)
                {
                    current = current.forward;
                }
                DoublyLinkedListNode newNode = new DoublyLinkedListNode(unitGO.GetComponent <Unit>(), current, head);
                current.forward = newNode;
                head.prev       = newNode;
            }
            else
            {
                DoublyLinkedListNode newHead = new DoublyLinkedListNode(unitGO.GetComponent <Unit>());
                newHead.forward = newHead;
                newHead.prev    = newHead;
                head            = newHead;
            }
            if (grid == null)
            {
                grid = FindObjectOfType <GridMenu>();
            }
            grid.UpdateDescription();
        }
        else
        {
            Debug.Log("Player::PlaceUnit() - Insufficient " + type + " units");
        }
    }