public bool TryAssignAttack(bool replace = true) { bool didAssignAttack = TryAssignAttackUnit(replace, Owner, AllUnits, AggroRadius); // we prioritize units over buildings, since units can fight back // At this time, only bad guys can attack buildings. May need to change // this if we decide to add units that are built by the bad guys: if (!didAssignAttack && Owner.UnitData.IsEnemy) { #if DEBUG if (AllBuildings == null) { throw new NullReferenceException($"Need to assign {nameof(AllBuildings)} when instantiating this unit."); } #endif float buildingAggroSquared = (AggroRadius + 24) * (AggroRadius + 24); var foundBuilding = AllBuildings .Where(item => (item.Position - Owner.Position).LengthSquared() < buildingAggroSquared) .OrderBy(item => (item.Position - Owner.Position).LengthSquared()) .FirstOrDefault(); if (foundBuilding != null) { var attackBuildingGoal = Owner.AssignAttackGoal(foundBuilding, replace); // prefer attacking units: attackBuildingGoal.PreferAttackingUnits = true; didAssignAttack = true; } } return(didAssignAttack); }
// Use this for initialization void Start() { systemObj = GameObject.Find("System"); AllBuildings = systemObj.GetComponent <AllBuildings>(); Building_System = systemObj.GetComponent <Building_System>(); GameScript = systemObj.GetComponent <Game_Script>(); }
// Use this for initialization void Start() { System_Script = this.GetComponent <System_Script>(); IconEdit_Script = this.GetComponent <IconEdit_Script>(); AllBuildings_Script = this.GetComponent <AllBuildings>(); for (int iX = 0; iX < Rows; iX++) { for (int iY = 0; iY < Columns; iY++) { BuildingGrid[iX, iY] = new TerainAdditions { B_Types = BuildingTypes.Nothing, Object = null }; } } }
internal void AssignAttackThenRetreat(float worldX, float worldY, bool replace = true) { // we'll just make a circle: var circle = new Circle(); circle.Radius = AttackThenRetreat.BuildingAttackingRadius;; circle.X = worldX; circle.Y = worldY; var buildingsToTarget = AllBuildings .Where(item => item.CollideAgainst(circle)) .Take(3) .ToList(); // if there's no buildings, then just do a regular attack move: if (buildingsToTarget.Count == 0) { AssignMoveAttackGoal(worldX, worldY, replace); } else { var goal = new AttackThenRetreat(); goal.StartX = this.X; goal.StartY = this.Y; goal.TargetX = worldX; goal.TargetY = worldY; goal.AllUnits = AllUnits; goal.BuildingsToFocusOn.AddRange(buildingsToTarget); goal.Owner = this; if (replace) { this.HighLevelGoals.Clear(); } this.HighLevelGoals.Push(goal); this.ImmediateGoal = null; } }
public WalkToHighLevelGoal GetResourceReturnWalkGoal() { WalkToHighLevelGoal walkGoal = null; // Set up to return resource // Find "closest" building by position comparison. // FUTURE: Get building with shorted node path (in case closest is a long winding path). ResourceReturnBuilding = AllBuildings .Where(building => building.BuildingData.Name == BuildingData.TownHall && building.IsConstructionComplete) .OrderBy(building => (building.Position - Owner.Position).Length()) .FirstOrDefault(); if (ResourceReturnBuilding != null) { walkGoal = new WalkToHighLevelGoal(); ResourceReturnBuildingTile = GetSingleTile(ResourceReturnBuilding.Position); Vector3 pointSlightlySkewedTowardOwner = DeterminePositionWithinTileSlightlyCloserToOwner(ResourceReturnBuildingTile.Position, ResourceReturnBuildingTile.Width); walkGoal = new WalkToHighLevelGoal(); walkGoal.Owner = Owner; walkGoal.TargetPosition = pointSlightlySkewedTowardOwner; walkGoal.ForceAttemptToGetToExactTarget = true; } return(walkGoal); }
public void UpdatePlayerContext(PlayerContext context) { World = context.World; Me = context.Wizards.Single(x => x.IsMe); CurrentBuildings = new Dictionary <long, Building>(); CurrentWizards = new Dictionary <long, Wizard>(); CurrentMinions = new Dictionary <long, Minion>(); CurrentProjectiles = new Dictionary <long, Projectile>(); foreach (var unit in context.World.Buildings) { DeadBuildings.Remove(unit.Id); AllBuildings[unit.Id] = unit; CurrentBuildings[unit.Id] = unit; } foreach (var unit in context.World.Wizards) { DeadWizards.Remove(unit.Id); AllWizards[unit.Id] = unit; CurrentWizards[unit.Id] = unit; } foreach (var unit in context.World.Minions) { DeadMinions.Remove(unit.Id); AllMinions[unit.Id] = unit; CurrentMinions[unit.Id] = unit; } foreach (var unit in context.World.Projectiles) { DeadProjectiles.Remove(unit.Id); AllProjectiles[unit.Id] = unit; CurrentProjectiles[unit.Id] = unit; } foreach (var diedUnit in AllBuildings.Except(CurrentBuildings).Where(x => x.Value.IsUnion(Me) || x.Value.Life <= 36)) { DeadBuildings[diedUnit.Key] = new DeadUnitInfo(context.World, diedUnit.Value, Me); } foreach (var diedUnit in AllWizards.Except(CurrentWizards).Where(x => x.Value.IsUnion(Me) || x.Value.Life <= 36)) { DeadWizards[diedUnit.Key] = new DeadUnitInfo(context.World, diedUnit.Value, Me); } foreach (var diedUnit in AllMinions.Except(CurrentMinions).Where(x => x.Value.IsUnion(Me) || x.Value.Life <= 36)) { DeadMinions[diedUnit.Key] = new DeadUnitInfo(context.World, diedUnit.Value, Me); } foreach (var diedUnit in AllProjectiles.Except(CurrentProjectiles)) { bool isTargetTree = context.World.Trees.Any(x => x.GetDistanceTo(diedUnit.Value) < 220); DeadProjectiles[diedUnit.Key] = new DeadUnitInfo(context.World, diedUnit.Value, Me, isTargetTree); } AllBuildings = CurrentBuildings; AllWizards = CurrentWizards; AllMinions = CurrentMinions; AllProjectiles = CurrentProjectiles; CurrentBuildingTargets = new Dictionary <long, List <LivingUnit> >(); foreach (var building in CurrentBuildings) { var buildingUnit = building.Value; var attackRange = buildingUnit.Type == BuildingType.GuardianTower ? Game.GuardianTowerAttackRange : Game.FactionBaseAttackRange; var maxDamage = buildingUnit.Type == BuildingType.GuardianTower ? Game.GuardianTowerDamage : Game.FactionBaseDamage; List <LivingUnit> p1Units = new List <LivingUnit>(); List <LivingUnit> p2Units = new List <LivingUnit>(); foreach (var minion in CurrentMinions) { FindBuildingTarget(minion.Value, buildingUnit, attackRange, maxDamage, p1Units, p2Units); } foreach (var wizard in CurrentWizards) { FindBuildingTarget(wizard.Value, buildingUnit, attackRange, maxDamage, p1Units, p2Units); } var buildingTargets = p1Units.Count > 0 ? p1Units : p2Units; CurrentBuildingTargets[building.Key] = buildingTargets; } DeadProjectiles = DeadProjectiles .Where(x => x.Value.ProjectileType == ProjectileType.MagicMissile && context.World.TickIndex < x.Value.TickIndex + 5 * 4) .ToDictionary(x => x.Key, y => y.Value); WorldUpdated?.Invoke(this, EventArgs.Empty); }