private void GenerateActorsAndItemsOutside() { var bound = new BoundsInt(new Vector3Int(-2, -53, 0), new Vector3Int(19, 18, 1)); for (int i = 0; i < 10; i++) { Vector2Int randomPosition = _rng.NextPosition(bound); if (_gridInfoProvider.IsWalkable(randomPosition)) { var actor = _rng.Choice(new[] { ActorType.Rogue, ActorType.BruisedRat, ActorType.BruisedRat, ActorType.Rat, ActorType.Rat, ActorType.RatChiefBetter, ActorType.RatChief, ActorType.Dog, ActorType.RatVeteran, }); _entitySpawner.SpawnActor(actor, randomPosition); } } ItemDefinition recoverTailDefinition = Resources.Load <ItemDefinition>("PotionOfRecoverTail"); _entitySpawner.SpawnItem(recoverTailDefinition, new Vector2Int(-3, -83)); _entitySpawner.SpawnActor(ActorType.LastMonster, new Vector2Int(-2, -83)); _entitySpawner.SpawnActor(ActorType.Basher, new Vector2Int(11, -64)); _entitySpawner.SpawnActor(ActorType.RatChief, new Vector2Int(3, -87)); }
/// <summary> /// It's assuming that from @from to to there are exactly two steps! /// </summary> public bool ClearWayExists(Vector2Int @from, Vector2Int to) { Func <float, bool> isIntegral = val => Math.Abs(val % 1) < .001f; float middleX = ((float)@from.x + to.x) / 2; float middleY = ((float)@from.y + to.y) / 2; int[] xsToCheck = isIntegral(middleX) ? new[] { (int)middleX } : new[] { Mathf.CeilToInt(middleX), Mathf.FloorToInt(middleX) }; int[] ysToCheck = isIntegral(middleY) ? new[] { (int)middleY } : new[] { Mathf.CeilToInt(middleY), Mathf.FloorToInt(middleY) }; foreach (int x in xsToCheck) { foreach (int y in ysToCheck) { bool actorIsBlockingWay = _entityDetector.DetectActors(new Vector2Int(x, y)).Any(); bool wayIsWalkable = _gridInfoProvider.IsWalkable(new Vector2Int(x, y)); if (wayIsWalkable && !actorIsBlockingWay) { return(true); } } } return(false); }
private bool[][] CreateWalkabilityMatrix() { var walkabilityMatrix = new bool[_gridInfoProvider.XSize][]; var nodePool = new NodePool(); for (int currentX = 0; currentX < _gridInfoProvider.XSize; currentX++) { walkabilityMatrix[currentX] = new bool[_gridInfoProvider.YSize]; for (int currentY = 0; currentY < _gridInfoProvider.YSize; currentY++) { Vector2Int positionToCheckForWall = NonZeroBasedPosition(new Vector2Int(currentX, currentY)); bool isWalkable = _gridInfoProvider.IsWalkable(positionToCheckForWall); walkabilityMatrix[currentX][currentY] = isWalkable; nodePool.SetNode(currentX, currentY, isWalkable); } } return(walkabilityMatrix); }
private Vector2Int GetPositionToPlaceItem(Vector2Int position) { if (_entityDetector.DetectItems(position).Any()) { List <Vector2Int> candidates = Vector2IntUtilities.Neighbours8(position); var candidatesFurther = Vector2IntUtilities.Neighbours8(Vector2Int.zero) .Select(v => new Vector2Int(v.x * 2, v.y * 2)) .Select(v => position + v); candidates.AddRange(candidatesFurther); foreach (Vector2Int neighbour in candidates) { if (_gridInfoProvider.IsWalkable(neighbour) && !_entityDetector.DetectItems(neighbour).Any()) { position = neighbour; break; } } } return(position); }
private int GetEmptyPositionsAround(ActorData actorData) { int emptyPositions = 8; var positionsAround = Vector2IntUtilities.Neighbours8(actorData.LogicalPosition); foreach (Vector2Int position in positionsAround) { if (!_gridInfoProvider.IsWalkable(position) || _entityDetector.DetectActors(position).Any()) { --emptyPositions; } } return(emptyPositions); }
public override IEnumerable <IActionEffect> Execute() { Vector2Int previousPosition = ActorData.LogicalPosition; Vector2Int newPosition = previousPosition + Direction; if (_gridInfoProvider.IsWalkable(newPosition)) { if (ActorData.ActorType == ActorType.Player && _gameContext.EnvironmentTilemap.HasTile(newPosition.ToVector3Int())) { TileBase stairsDownTile = Resources.Load <TileBase>("Tiles/Environment/Stairs_down"); if (_gameContext.EnvironmentTilemap.GetTile(newPosition.ToVector3Int()) == stairsDownTile) { _textEffectPresenter.ShowTextEffect(ActorData.LogicalPosition, "Back down? Never!", Color.yellow); } } if (_gameContext.BasherSteps == 0 && ActorData.ActorType == ActorType.Basher && Vector2IntUtilities.WalkDistance(ActorData.LogicalPosition, _gameContext.PlayerActor.ActorData.LogicalPosition) <= 5) { _gameContext.BasherSteps = 1; _uiConfig.BasherMessage.gameObject.SetActive(true); _uiConfig.BasherMessage.SetMessage("Ha! So you didn't disappoint me! Finally I see you again, my rattish friend! As you might " + "have guessed, that was me that sent you the key. I'll be happy to take you away from this scary place. But first " + "we have to deal with one thing. You saved my life by attributing to yourself my deeds against the revolutionists. " + "But this also made me feel like a lousy coward. My honour has been terribly undermined and I need to clean it. " + "I challenge you to a duel! No magic, no eating, just me, you and steel. Prepare!"); IEnumerable <ActorData> friendsAndBuddies = _entityDetector.DetectActors(ActorData.LogicalPosition, 15) .Where(a => a.ActorType == ActorType.Friend || a.ActorType == ActorType.Buddy); foreach (var friendOrBuddy in friendsAndBuddies) { _deathHandler.HandleDeath(friendOrBuddy); } } IActionEffect effect = ActionEffectFactory.CreateMoveEffect(ActorData, previousPosition); ActorData.LogicalPosition = newPosition; if (ActorData.CaughtActor != null) { ActorData.CaughtActor.LogicalPosition = newPosition; } yield return(effect); } else { IActionEffect effect = ActionEffectFactory.CreateBumpEffect(ActorData, newPosition); yield return(effect); } }
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)); }