예제 #1
0
        public override bool isLoseCondition(int halfTurnsElapsed)
        {
            if (battlefield.charactersUnits[playerCharacter].Count == 0)
            {
                return(true);
            }
            foreach (Coord coord in this.capturePoints)
            {
                Unit unit = battlefield.units[coord.x, coord.y];
                if (unit != null && unit.getCharacter(battlefield) != playerCharacter)
                {
                    //I know this looks clunky but it's the best I could think of so the timer doesn't tick when you first step on the goal, but does every consecutive turn
                    if (!this.holding)
                    {
                        this.holding         = true;
                        lastHalfTurnsElapsed = halfTurnsElapsed;
                    }
                    else if (lastHalfTurnsElapsed < halfTurnsElapsed)
                    {
                        timeHeld++;
                        lastHalfTurnsElapsed = halfTurnsElapsed;
                    }

                    return(timeHeld >= timeToHold);
                }
            }

            return(false);
        }
예제 #2
0
 public override bool isLoseCondition(int halfTurnsElapsed)
 {
     if (battlefield.charactersUnits[playerCharacter].Count == 0)
     {
         return(true);
     }
     for (int i = 0; i < capturePoints.Count; i++)
     {
         Coord coord = capturePoints[i];
         Unit  unit  = battlefield.units[coord.x, coord.y];
         if (unit != null && unit.getCharacter(battlefield) != playerCharacter)
         {
             if (!holding[i])
             {
                 holding[i] = true;
                 lastHalfTurnsElapsed[i] = halfTurnsElapsed;
             }
             else if (lastHalfTurnsElapsed[i] < halfTurnsElapsed)
             {
                 timeHeld[i]++;
                 lastHalfTurnsElapsed[i] = halfTurnsElapsed;
             }
             return(timeHeld[i] >= timeToHold);
         }
     }
     return(false);
 }
예제 #3
0
        public override bool isWinCondition(int halfTurnsElapsed)
        {
            bool allCaptured = true;

            for (int i = 0; i < capturePoints.Count; i++)
            {
                if (timeHeld[i] < timeToHold)
                {
                    Coord coord = capturePoints[i];
                    Unit  unit  = battlefield.units[coord.x, coord.y];
                    if (unit == null || unit.getCharacter(battlefield) != playerCharacter)
                    {
                        timeHeld[i] = 0;
                        holding[i]  = false;
                        allCaptured = false;
                    }
                    else if (!holding[i])
                    {
                        holding[i] = true;
                        lastHalfTurnsElapsed[i] = halfTurnsElapsed;
                    }
                    else if (lastHalfTurnsElapsed[i] < halfTurnsElapsed)
                    {
                        timeHeld[i]++;
                        lastHalfTurnsElapsed[i] = halfTurnsElapsed;
                    }
                    allCaptured = timeHeld[i] >= timeToHold && allCaptured;
                }
            }
            return(allCaptured);
        }
예제 #4
0
        // Update is called once per frame
        async void Update()
        {
            switch (battleStage)
            {
            case BattleLoopStage.Initial:
                if (!cutscene.inProgress)
                {
                    advanceBattleStage();
                }
                break;

            case BattleLoopStage.Pick:
                //TODO This is temp just for testing until pick phase gets built.
                addUnit(UnitType.Knight, level.characters[0], 0, 0);
                addUnit(UnitType.Knight, level.characters[0], 1, 0);
                addUnit(UnitType.Knight, level.characters[0], 0, 1);
                addUnit(UnitType.Knight, level.characters[1], 3, 7);
                addUnit(UnitType.Knight, level.characters[1], 4, 7);
                foreach (Unit unit in battlefield.charactersUnits[level.characters[1]])
                {
                    Renderer rend = unit.gameObject.GetComponent <Renderer>();
                    rend.material.shader = Shader.Find("_Color");
                    rend.material.SetColor("_Color", Color.green);
                    rend.material.shader = Shader.Find("Specular");
                    rend.material.SetColor("_SpecColor", Color.green);
                }

                advanceBattleStage();
                break;

            case BattleLoopStage.BattleLoopStart:
                advanceBattleStage();
                break;

            case BattleLoopStage.TurnChange:
                //There's probably a less fragile way of doing this. It's just to make sure this call only happens once per turn loop.
                if (!turnPlayerText.enabled)
                {
                    currentCharacter             = (currentCharacter + 1) % level.characters.Length;
                    turnPlayerText.text          = level.characters[currentCharacter].name + "'s turn";
                    turnPlayerText.enabled       = true;
                    turnChangeBackground.enabled = true;
                    Util.setTimeout(advanceBattleStage, 1000);
                }
                break;

            case BattleLoopStage.TurnChangeEnd:
                turnPlayerText.enabled       = false;
                turnChangeBackground.enabled = false;
                advanceBattleStage();
                break;

            case BattleLoopStage.UnitSelection:
                //Player input. LMB
                if (Input.GetButtonDown("Select"))
                {
                    RaycastHit hit;
                    Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                    if (Physics.Raycast(ray, out hit, 1000.0f))
                    {
                        Vector3Int       tileCoords   = Util.WorldToGrid(hit.transform.position);
                        IBattlefieldItem selectedItem = battlefield.battlefieldItemAt(tileCoords.x, tileCoords.y, tileCoords.z);
                        if (selectedItem is Tile)
                        {
                            //Selected a tile, show info abt that tile
                            //TODO: Show info about tile if a tile is clicked
                            Tile selectedTile = selectedItem as Tile;
                            highlightSingleObject(selectedTile.gameObject);
                        }
                        else if (selectedItem is Unit)
                        {
                            Unit selectedUnit = selectedItem as Unit;

                            if (selectedUnit.getCharacter(battlefield) == level.characters[currentCharacter] && !selectedUnit.hasMovedThisTurn)
                            {
                                //Selected friendly unit. show move options.
                                highlightSingleObject(selectedUnit.gameObject, 1);
                                this.highlightedFriendlyUnit = selectedUnit;

                                moveOptions = selectedUnit.getValidMoves(tileCoords.x, tileCoords.y, battlefield);
                                foreach (Coord moveOption in moveOptions)
                                {
                                    highlightMultipleObjects(battlefield.map[moveOption.x, moveOption.y].Peek().gameObject);
                                }

                                this.highlightedEnemyUnits = selectedUnit.getTargets(tileCoords.x, tileCoords.y, battlefield, level.characters[currentCharacter]);
                                foreach (Unit targetableUnit in highlightedEnemyUnits)
                                {
                                    highlightMultipleObjects(targetableUnit.gameObject, 2);
                                }

                                advanceBattleStage();
                            }
                            else
                            {
                                //Selected enemy unit. Show unit and its move options.
                                //TODO: highlight enemy's valid move tiles.
                            }
                        }
                        else if (selectedItem == null)
                        {
                            //Clicked on empty space! Nbd, don't do anything.
                            Debug.Log("Clicked on empty space");
                        }
                        else
                        {
                            Debug.LogWarning("Item of unrecognized type clicked on.");
                        }
                    }
                }


                //If player has selected a move:
                //TODO play the animation
                break;

            case BattleLoopStage.ActionSelection:
                if (Input.GetButtonDown("Select"))
                {
                    RaycastHit hit;
                    Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                    if (Physics.Raycast(ray, out hit, 1000.0f))
                    {
                        Vector3Int       tileCoords   = Util.WorldToGrid(hit.transform.position);
                        IBattlefieldItem selectedItem = battlefield.battlefieldItemAt(tileCoords.x, tileCoords.y, tileCoords.z);
                        if (selectedItem is Tile)
                        {
                            //We selected a tile! lets move to it
                            if (moveOptions.Any(move => (move.x == tileCoords.x && move.y == tileCoords.y)))
                            {
                                moveUnit(highlightedFriendlyUnit, tileCoords);
                                deselectMoveOptions();

                                highlightedFriendlyUnit.hasMovedThisTurn = true;

                                if (battlefield.charactersUnits[level.characters[currentCharacter]].All(unit => unit.hasMovedThisTurn))
                                {
                                    advanceBattleStage();
                                }
                                else
                                {
                                    this.battleStage = BattleLoopStage.UnitSelection;
                                }
                            }
                        }
                        else if (selectedItem == null)
                        {
                            //Clicked on empty space, deselect
                            deselectMoveOptions();
                            battleStage = BattleLoopStage.UnitSelection;
                        }
                        else if (selectedItem is Unit)
                        {
                            Unit selectedUnit = selectedItem as Unit;

                            if (highlightedFriendlyUnit == selectedUnit)
                            {
                                //clicked on the same unit, deselect
                                deselectMoveOptions();
                                battleStage = BattleLoopStage.UnitSelection;
                            }
                            else if (selectedUnit.getCharacter(battlefield) == level.characters[currentCharacter])
                            {
                                //Clicked on a friendly unit. Deselect the current one.
                                deselectMoveOptions();
                                battleStage = BattleLoopStage.UnitSelection;
                            }
                            else
                            {
                                //Clicked on a hostile unit! fight!
                                if (highlightedEnemyUnits.Contains(selectedUnit))
                                {
                                    bool defenderDefeated = highlightedFriendlyUnit.doBattleWith(
                                        selectedUnit,
                                        battlefield.map[tileCoords.x, tileCoords.y].Peek(),
                                        battlefield);

                                    await Task.Delay(TimeSpan.FromMilliseconds(250));

                                    if (defenderDefeated)
                                    {
                                        highlightedEnemyUnits.RemoveAll(units => units == null);
                                    }
                                    else
                                    {
                                        //Counterattack
                                        Coord unitCoords = battlefield.getUnitCoords(highlightedFriendlyUnit);
                                        selectedUnit.doBattleWith(
                                            highlightedFriendlyUnit,
                                            battlefield.map[unitCoords.x, unitCoords.y].Peek(),
                                            battlefield);
                                    }
                                    checkWinAndLose();

                                    highlightedFriendlyUnit.hasMovedThisTurn = true;
                                    await Task.Delay(TimeSpan.FromMilliseconds(250));

                                    deselectMoveOptions();

                                    //If all of our units have moved advance. Otherwise, go back to unit selection.
                                    if (battlefield.charactersUnits[level.characters[currentCharacter]].All(unit => unit.hasMovedThisTurn))
                                    {
                                        advanceBattleStage();
                                    }
                                    else
                                    {
                                        this.battleStage = BattleLoopStage.UnitSelection;
                                    }
                                }
                            }
                        }
                        else
                        {
                            Debug.LogWarning("Item of unrecognized type clicked on.");
                        }
                    }
                }

                break;

            case BattleLoopStage.EndTurn:
                foreach (Unit unit in battlefield.charactersUnits[level.characters[currentCharacter]])
                {
                    unit.hasMovedThisTurn = false;
                }

                checkWinAndLose();
                advanceBattleStage();
                break;
            }
        }