Example #1
0
        public void Cofanie()
        {
            var        actorPosition      = new Vector2Int(10, 10);
            var        toEnemy            = new Vector2Int(-1, -1);
            Vector2Int directionFromEnemy = Vector2IntUtilities.Normalized(toEnemy) * -1;
            IEnumerable <Vector2Int> positionsToStepBack = Vector2IntUtilities.GetCone(directionFromEnemy)
                                                           .Select(coneVector => actorPosition + coneVector - directionFromEnemy);

            foreach (var vector2Int in positionsToStepBack)
            {
                Console.WriteLine(vector2Int);
            }
        }
Example #2
0
        private IGameAction ResolveActionForAggresion(ActorData actorData)
        {
            if (_rng.Check(0.04f))
            {
                if (_gameContext.PlayerActor.ActorData.Health <= 0)
                {
                    if (_rng.Check(0.07f))
                    {
                        string text = _rng.Choice(new[] { "Ha, ha!", "I got him!", "I know my strength!", "Got what he deserved!",
                                                          "Guess what we'll cook for dinner..." });
                        _textEffectPresenter.ShowTextEffect(actorData.LogicalPosition, text);
                    }
                }
                else if (actorData.ActorType == ActorType.Dog && actorData.Team != Team.Beasts)
                {
                    string text = _rng.Choice(new[] { "Woof", "Wrrrr!" });
                    _textEffectPresenter.ShowTextEffect(actorData.LogicalPosition, text);
                }
                else if (actorData.ActorType == ActorType.LastMonster)
                {
                    var potential = new[] { "Whshsh!", "Rrrruv!" }.ToList();
                    if (!actorData.Entity.IsVisible)
                    {
                        potential.AddRange(new[] { "[THUD!]", "[THUD!]", "[THUD!]" });
                    }
                    string text = _rng.Choice(potential);
                    _textEffectPresenter.ShowTextEffect(actorData.LogicalPosition, text, text == "[THUD!]" ? Color.white
                                                : Color.magenta, true);
                }
                else if (actorData.ActorType == ActorType.Friend || actorData.ActorType == ActorType.Buddy)
                {
                    string text = _rng.Choice(new[] { "Ma-uluh, ruv!", "Suku bgeve lir...", "Alir tak rettenekopast!" });
                    _textEffectPresenter.ShowTextEffect(actorData.LogicalPosition, text, new Color(0.7f, 0.8f, 1f));
                }
                else if (actorData.ActorType != ActorType.Basher && actorData.ActorType != ActorType.LastMonster)
                {
                    string text = _rng.Choice(new[] { "Back to your ward!", "Squeak!", "You're mine!", "Comrades, help me!", "Aah!" });
                    _textEffectPresenter.ShowTextEffect(actorData.LogicalPosition, text);
                }
            }

            List <ActorData> enemiesClose = _entityDetector.DetectActors(actorData.LogicalPosition, actorData.VisionRayLength)
                                            .Where(a => a.Team != actorData.Team && a.Entity.IsVisible)
                                            .ToList();

            enemiesClose.Sort((first, second) =>
                              Vector2IntUtilities.WalkDistance(first.LogicalPosition, actorData.LogicalPosition)
                              .CompareTo(Vector2IntUtilities.WalkDistance(second.LogicalPosition, actorData.LogicalPosition))
                              );
            ActorData closestEnemy = enemiesClose.FirstOrDefault();

            if (closestEnemy != null)
            {
                Vector2Int toEnemy = closestEnemy.LogicalPosition - actorData.LogicalPosition;

                // mam nadzieje, ze zadziala
                if (Vector2IntUtilities.WalkDistance(closestEnemy.LogicalPosition, actorData.LogicalPosition) == 2)
                {                 // move towards player if possible and desired
                    bool possible = false;

                    Vector2Int?legalMove        = null;
                    Vector2Int directionToEnemy = Vector2IntUtilities.Normalized(toEnemy);
                    IEnumerable <Vector2Int> candidateMovesToEnemy = Vector2IntUtilities.GetCone(directionToEnemy)
                                                                     .Select(coneVector => coneVector - directionToEnemy)
                                                                     .Where(fixedConeVector => fixedConeVector != actorData.LogicalPosition);

                    IList <Vector2Int> candidateMovesShuffled = _rng.Shuffle(candidateMovesToEnemy);
                    foreach (var cand in candidateMovesShuffled)
                    {
                        if (!_entityDetector.DetectActors(actorData.LogicalPosition + cand).Any() &&
                            _gridInfoProvider.IsWalkable(actorData.LogicalPosition + cand))
                        {
                            legalMove = cand;
                            break;
                        }
                    }
                    if (legalMove.HasValue)
                    {
                        int closeCombatAdvantage = actorData.WeaponWeld.WeaponDefinition.CloseCombatModifier -
                                                   closestEnemy.WeaponWeld.WeaponDefinition.CloseCombatModifier;
                        if (closeCombatAdvantage < 0)
                        {
                            closeCombatAdvantage = 0;
                        }
                        float chanceToMove = .1f + 0.2f * closeCombatAdvantage;
                        if (_rng.Check(chanceToMove))
                        {
                            return(_actionFactory.CreateMoveAction(actorData, legalMove.Value));
                        }
                    }
                }

                if (Vector2IntUtilities.IsOneStep(toEnemy) ||
                    (actorData.WeaponWeld.WeaponDefinition.AllowsFarCombat && Vector2IntUtilities.IsOneOrTwoSteps(toEnemy) &&
                     _clearWayBetweenTwoPointsDetector.ClearWayExists(actorData.LogicalPosition, closestEnemy.LogicalPosition)))
                {
                    IGameAction actionToPerform;

                    bool pushingIsPossible = actorData.AiTraits.Contains(AiTrait.Pusher) && Vector2IntUtilities.IsOneStep(toEnemy) &&
                                             _gridInfoProvider.IsWalkable(closestEnemy.LogicalPosition + toEnemy);
                    bool pushingIsDesired = false;
                    if (pushingIsPossible)
                    {
                        float pushingChanceScore = 0.08f;
                        if (actorData.WeaponWeld.WeaponDefinition.CloseCombatModifier < closestEnemy.WeaponWeld.WeaponDefinition.CloseCombatModifier)
                        {
                            pushingChanceScore += .25f;
                        }
                        if (!_gridInfoProvider.IsWalkable(closestEnemy.LogicalPosition + toEnemy + toEnemy))
                        {
                            pushingChanceScore += .2f;
                        }

                        if (_rng.Check(pushingChanceScore))
                        {
                            pushingIsDesired = true;
                        }
                    }

                    if (pushingIsPossible && pushingIsDesired)
                    {
                        actionToPerform = _actionFactory.CreatePushAction(actorData, closestEnemy);
                    }
                    else
                    {
                        bool isInGoodPosition = Vector2IntUtilities.IsOneStep(toEnemy) && actorData.WeaponWeld.WeaponDefinition.CloseCombatModifier
                                                > closestEnemy.WeaponWeld.WeaponDefinition.CloseCombatModifier;
                        if (Vector2IntUtilities.IsOneOrTwoSteps(toEnemy) && !isInGoodPosition)                        // && actorData.AiTraits.Contains(AiTrait.Careful)))
                        {
                            float chanceToStepBack = 0f;
                            float healthFactor     = (1 - actorData.HealthProgress) * .15f;
                            float swordsFactor     = (closestEnemy.Swords - actorData.Swords) * .15f;
                            chanceToStepBack = healthFactor + swordsFactor;
                            if (_rng.Check(chanceToStepBack))
                            {
                                Vector2Int directionFromEnemy = Vector2IntUtilities.Normalized(toEnemy) * -1;
                                IEnumerable <Vector2Int> positionsToStepBack = Vector2IntUtilities.GetCone(directionFromEnemy)
                                                                               .Select(coneVector => actorData.LogicalPosition + coneVector - directionFromEnemy)
                                                                               .Where(position => position != actorData.LogicalPosition);
                                foreach (var conePosition in positionsToStepBack)
                                {
                                    if (!_gridInfoProvider.IsWalkable(conePosition) ||
                                        _entityDetector.DetectEntities(conePosition).Any())
                                    {
                                        continue;
                                    }
                                    Vector2Int stepBackMoveVector = conePosition - actorData.LogicalPosition;
                                    return(_actionFactory.CreateMoveAction(actorData, stepBackMoveVector));
                                }
                            }
                        }

                        bool isDaringBlow = false;
                        if (actorData.Traits.Contains(Trait.DaringBlow) && actorData.Swords >= 2)
                        {
                            float daringBlowChance = actorData.AiTraits.Contains(AiTrait.Aggressive) ? .5f : .2f;
                            if (actorData.ActorType == ActorType.Basher)
                            {
                                daringBlowChance += .2f;
                            }
                            if (_rng.Check(daringBlowChance))
                            {
                                isDaringBlow = true;
                            }
                        }
                        actionToPerform = _actionFactory.CreateAttackAction(actorData, closestEnemy, isDaringBlow);
                    }
                    if (DateTime.UtcNow < closestEnemy.BlockedUntil)
                    {
                        actorData.StoredAction = actionToPerform;
                        actorData.BlockedUntil = closestEnemy.BlockedUntil;
                        return(null);
                    }
                    return(actionToPerform);
                }
                int        moveX      = toEnemy.x.CompareTo(0);
                int        moveY      = toEnemy.y.CompareTo(0);
                Vector2Int moveVector = new Vector2Int(moveX, moveY);

                Func <Vector2Int, bool> isWalkableAndFree = position =>
                                                            _gridInfoProvider.IsWalkable(position) &&
                                                            !_entityDetector.DetectActors(position).Any();

                if (!isWalkableAndFree(actorData.LogicalPosition + moveVector))
                {
                    Vector2Int?finalMoveVector = null;
                    Vector2Int alternativeMoveVector1;
                    Vector2Int alternativeMoveVector2;

                    // trying to find best alternative vectors to move
                    if (moveVector.x == 0)
                    {
                        alternativeMoveVector1 = new Vector2Int(+1, moveVector.y);
                        alternativeMoveVector2 = new Vector2Int(-1, moveVector.y);
                    }
                    else if (moveVector.y == 0)
                    {
                        alternativeMoveVector1 = new Vector2Int(moveVector.x, -1);
                        alternativeMoveVector2 = new Vector2Int(moveVector.x, +1);
                    }
                    else
                    {
                        alternativeMoveVector1 = new Vector2Int(moveVector.x, 0);
                        alternativeMoveVector2 = new Vector2Int(0, moveVector.y);
                    }

                    if (isWalkableAndFree(actorData.LogicalPosition + alternativeMoveVector1))
                    {
                        finalMoveVector = alternativeMoveVector1;
                    }
                    else if (isWalkableAndFree(actorData.LogicalPosition + alternativeMoveVector2))
                    {
                        finalMoveVector = alternativeMoveVector2;
                    }
                    if (finalMoveVector.HasValue)
                    {
                        return(_actionFactory.CreateMoveAction(actorData, finalMoveVector.Value));
                    }
                    return(_actionFactory.CreatePassAction(actorData));
                }
                return(_actionFactory.CreateMoveAction(actorData, moveVector));
            }
            else if (actorData.Team == Team.Beasts)
            {
                ActorData playerClose = _entityDetector.DetectActors(actorData.LogicalPosition, actorData.VisionRayLength).FirstOrDefault(
                    f => f != actorData && f.ActorType == ActorType.Player);
                if (playerClose != null)
                {
                    Vector2Int toFriend          = playerClose.LogicalPosition - actorData.LogicalPosition;
                    Vector2Int directionToFriend = Vector2IntUtilities.Normalized(toFriend);
                    Vector2Int legalMove         = new Vector2Int();

                    IEnumerable <Vector2Int> candidateMovesToFriend = Vector2IntUtilities.GetCone(directionToFriend)
                                                                      .Select(coneVector => coneVector - directionToFriend)
                                                                      .Where(fixedConeVector => fixedConeVector != actorData.LogicalPosition);

                    IList <Vector2Int> candidateMovesShuffled = _rng.Shuffle(candidateMovesToFriend);
                    foreach (var cand in candidateMovesShuffled)
                    {
                        if (!_entityDetector.DetectActors(actorData.LogicalPosition + cand).Any() &&
                            _gridInfoProvider.IsWalkable(actorData.LogicalPosition + cand))
                        {
                            legalMove = cand;
                            break;
                        }
                    }

                    Vector2Int moveVector = legalMove;
                    if (!_entityDetector.DetectActors(actorData.LogicalPosition + moveVector).Any())
                    {
                        return(_actionFactory.CreateMoveAction(actorData, moveVector));
                    }
                }
                return(_actionFactory.CreatePassAction(actorData));
            }
            if (Vector2IntUtilities.WalkDistance(actorData.LogicalPosition, _gameContext.PlayerActor.ActorData.LogicalPosition) < 15 &&
                _gameContext.PlayerActor.ActorData.Health > 0 && actorData.ActorType != ActorType.Basher)
            {
                Vector2Int?farReachablePoint = GetFarReachablePoint(actorData);
                if (farReachablePoint.HasValue)
                {
                    Vector2Int moveVector = Vector2IntUtilities.Normalized(farReachablePoint.Value - actorData.LogicalPosition);
                    return(_actionFactory.CreateMoveAction(actorData, moveVector));
                }
            }
            return(_actionFactory.CreatePassAction(actorData));
        }
Example #3
0
        public IGameAction GetAction(ActorData actorData)
        {
            if (_inputHolder.PlayerInput == PlayerInput.None)
            {
                return(null);
            }

            if (_inputHolder.PlayerInput != PlayerInput.UseCurrentItem && _inputHolder.PlayerInput != PlayerInput.DropCurrentItem)
            {
                _uiConfig.ItemHolder.DeselectItem();
                _uiConfig.TooltipPresenter.Panel.gameObject.SetActive(false);
                _uiConfig.TooltipCurrentWeaponPresenter.gameObject.SetActive(false);
                _uiConfig.TooltipCurrentWeaponPresenter.GetComponent <CurrentWeaponTooltip>().LabelWearingUpper.gameObject.SetActive(false);
                var quitButton = GameObject.FindGameObjectWithTag("button_quit");
                if (quitButton != null)
                {
                    quitButton.SetActive(false);
                }
            }
            if (_uiConfig.WalkAbilityButton.GetComponentInChildren <Text>() != null)
            {
                _uiConfig.WalkAbilityButton.GetComponentInChildren <Text>().gameObject.SetActive(false);
            }
            if (_uiConfig.DaringBlowAbilityButton.GetComponentInChildren <Text>() != null)
            {
                _uiConfig.DaringBlowAbilityButton.GetComponentInChildren <Text>().gameObject.SetActive(false);
            }
            if (_uiConfig.PushAbilityButton.GetComponentInChildren <Text>() != null)
            {
                _uiConfig.PushAbilityButton.GetComponentInChildren <Text>().gameObject.SetActive(false);
            }

            IGameAction gameActionToReturn;

            if (_inputHolder.PlayerInput == PlayerInput.PickUp)
            {
                IList <ItemData> items            = _entityDetector.DetectItems(actorData.LogicalPosition).ToList();
                bool             allSlotsOccupied = _uiConfig.ItemHolder.Items.All(i => i != null);
                if (!items.Any() || allSlotsOccupied)
                {
                    return(null);
                }
                ItemData itemToPickUp = items.First();
                _inputHolder.PlayerInput = PlayerInput.None;
                return(_actionFactory.CreatePickUpAction(actorData, itemToPickUp));
            }

            if (_inputHolder.PlayerInput == PlayerInput.Drop)
            {
                ItemData firstItem = actorData.Items.FirstOrDefault();
                if (firstItem == null)
                {
                    return(null);
                }
                _inputHolder.PlayerInput = PlayerInput.None;
                return(_actionFactory.CreateDropAction(actorData, firstItem));
            }
            if (_inputHolder.PlayerInput == PlayerInput.Catch)
            {
                ActorData caughtActor = _entityDetector.DetectActors(actorData.LogicalPosition, 1).FirstOrDefault();
                if (caughtActor == null)
                {
                    return(null);
                }
                _inputHolder.PlayerInput = PlayerInput.None;
                return(_actionFactory.CreateCatchAction(actorData, caughtActor));
            }
            if (_inputHolder.PlayerInput == PlayerInput.Release)
            {
                if (actorData.CaughtActor == null)
                {
                    return(null);
                }
                _inputHolder.PlayerInput = PlayerInput.None;
                return(_actionFactory.CreateReleaseAction(actorData));
            }
            if (_inputHolder.PlayerInput == PlayerInput.Pass)
            {
                _inputHolder.PlayerInput = PlayerInput.None;
                return(_actionFactory.CreatePassAction(actorData));
            }
            if (_inputHolder.PlayerInput == PlayerInput.Eat)
            {
                _inputHolder.PlayerInput = PlayerInput.None;
                ItemData foodAtFeet = _entityDetector.DetectItems(actorData.LogicalPosition).FirstOrDefault(i => i.ItemType == ItemType.DeadBody);
                if (foodAtFeet == null)
                {
                    return(null);
                }
                return(_actionFactory.CreateEatAction(actorData, foodAtFeet));
            }
            if (_inputHolder.PlayerInput == PlayerInput.UseCurrentItem)
            {
                _inputHolder.PlayerInput = PlayerInput.None;
                ItemDefinition item;
                item = _uiConfig.ItemHolder.CurrentItem();
                if (item == null)
                {
                    ItemData itemAtFeet = _entityDetector.DetectItems(actorData.LogicalPosition).FirstOrDefault();
                    if (itemAtFeet != null)
                    {
                        item = itemAtFeet.ItemDefinition;
                        return(_actionFactory.CreateUseItemAction(actorData, item, false));
                    }
                    return(null);
                }
                return(_actionFactory.CreateUseItemAction(actorData, item, true));
            }
            if (_inputHolder.PlayerInput == PlayerInput.DropCurrentItem)
            {
                _inputHolder.PlayerInput = PlayerInput.None;
                ItemDefinition item = _uiConfig.ItemHolder.CurrentItem();
                if (item == null)
                {
                    return(null);
                }

                return(_actionFactory.CreateDropItemAction(actorData, item));
            }
            if (_inputHolder.PlayerInput == PlayerInput.Ascend)
            {
                TileBase stairsDown = Resources.Load <TileBase>("Tiles/Environment/Stairs_down");

                _inputHolder.PlayerInput = PlayerInput.None;
                Vector2Int playerPosition     = _gameContext.PlayerActor.ActorData.LogicalPosition;
                TileBase   envTileBelowPlayer = _gameContext.EnvironmentTilemap.GetTile(playerPosition.ToVector3Int());
                if (envTileBelowPlayer != null && envTileBelowPlayer != stairsDown)
                {
                    return(_actionFactory.CreateAscendAction(actorData));
                }
                return(null);
            }
            Vector2Int actionVector   = GetActionVector(_inputHolder.PlayerInput);
            Vector2Int targetPosition = actionVector + actorData.LogicalPosition;

            if (_inputHolder.PlayerInputModifier == PlayerInputModifier.Move)
            {
                _inputHolder.PlayerInputModifier = PlayerInputModifier.None;
                _inputHolder.PlayerInput         = PlayerInput.None;
                _arrowsVisibilityManager.Hide();

                IEnumerable <ActorData> actorsAtTarget = _entityDetector.DetectActors(targetPosition);
                if (actorsAtTarget.Any())
                {
                    return(null);
                }
                gameActionToReturn = _actionFactory.CreateMoveAction(actorData, actionVector);
                return(gameActionToReturn);
            }
            bool isAggressiveAttack = false;

            if (_inputHolder.PlayerInputModifier == PlayerInputModifier.DaringBlow)
            {
                isAggressiveAttack = true;
                _inputHolder.PlayerInputModifier = PlayerInputModifier.None;
                _inputHolder.PlayerInput         = PlayerInput.None;
                _weaponColorizer.Decolorize((actorData.Entity as ActorBehaviour).WeaponAnimator);
            }

            IList <Vector2Int> targetPositionsCone = Vector2IntUtilities.GetCone(actionVector)
                                                     .Select(zeroBasedPosition => actorData.LogicalPosition + zeroBasedPosition)
                                                     .ToList();
            IList <ActorData> actorsCloseToCone = _entityDetector.DetectActors(targetPosition, 2).ToList();

            ActorData targetActor;
            ActorData actorAtTargetPosition = actorsCloseToCone.FirstOrDefault(a => a.LogicalPosition == targetPosition);

            if (actorAtTargetPosition != null && actorAtTargetPosition.Team == actorData.Team)
            {
                _inputHolder.PlayerInput = PlayerInput.None;
                gameActionToReturn       = _actionFactory.CreateDisplaceAction(actorData, actorAtTargetPosition);
                return(gameActionToReturn);
            }

            bool isPushing = _inputHolder.PlayerInputModifier == PlayerInputModifier.Push;

            _arrowsVisibilityManager.Hide();
            _inputHolder.PlayerInputModifier = PlayerInputModifier.None;
            if ((actorAtTargetPosition != null || isPushing) || actorData.WeaponWeld.WeaponDefinition.AllowsFarCombat == false)
            {
                targetActor = actorAtTargetPosition;
            }
            else
            {
                targetActor = actorsCloseToCone
                              .FirstOrDefault(potentialTarget => potentialTarget.Team != actorData.Team &&
                                              targetPositionsCone.Contains(potentialTarget.LogicalPosition) &&
                                              _clearWayBetweenTwoPointsDetector.ClearWayExists(actorData.LogicalPosition, potentialTarget.LogicalPosition));
            }

            if (targetActor != null)            // hit!
            {
                gameActionToReturn = isPushing
                                        ? _actionFactory.CreatePushAction(actorData, targetActor)
                                        : _actionFactory.CreateAttackAction(actorData, targetActor, isAggressiveAttack);
            }
            else
            {
                TileBase wallTileAtTarget = _gameContext.WallsTilemap.GetTile(targetPosition.ToVector3Int());
                if (wallTileAtTarget != null)
                {
                    _heavyDoorsHClosedTile = Resources.Load <Tile>("Tiles/Environment/doors_HEAVY_0");
                    _heavyDoorsVClosedTile = Resources.Load <Tile>("Tiles/Environment/doors_HEAVY_2");
                    if ((wallTileAtTarget == _heavyDoorsHClosedTile || wallTileAtTarget == _heavyDoorsVClosedTile) &&
                        _uiConfig.ItemHolder.Items.Where(i => i != null).All(i => i.Name != "Key") &&
                        _gameContext.PlayerActor.ActorData.WeaponWeld.Name != "Key")
                    {
                        // bump
                        gameActionToReturn = _actionFactory.CreateMoveAction(actorData, actionVector);
                        _textEffectPresenter.ShowTextEffect(actorData.LogicalPosition, "Umph! Locked!", Color.yellow);
                    }
                    else
                    {
                        var doorsHClosedTile = Resources.Load <Tile>("Tiles/Environment/doors_H_closed");
                        var doorsVClosedTile = Resources.Load <Tile>("Tiles/Environment/doors_V_closed");

                        if (wallTileAtTarget == doorsHClosedTile || wallTileAtTarget == doorsVClosedTile)
                        {
                            bool isHorizontal = wallTileAtTarget == doorsHClosedTile;
                            gameActionToReturn = _actionFactory.CreateOpenDoorAction(actorData, targetPosition, isHorizontal);
                        }
                        else if (wallTileAtTarget == _heavyDoorsHClosedTile || wallTileAtTarget == _heavyDoorsVClosedTile)
                        {
                            bool isHorizontal = wallTileAtTarget == _heavyDoorsHClosedTile;
                            gameActionToReturn = _actionFactory.CreateOpenDoorAction(actorData, targetPosition, isHorizontal, true);
                        }
                        else
                        {
                            gameActionToReturn = _actionFactory.CreateMoveAction(actorData, actionVector);
                        }
                    }
                }
                else
                {
                    gameActionToReturn = _actionFactory.CreateMoveAction(actorData, actionVector);
                }
            }
            _inputHolder.PlayerInput = PlayerInput.None;
            return(gameActionToReturn);
        }