public void AddPower(float power) { if (owner && (owner.GetPower() + power) <= 5000) { owner.AddPower(power); } else if (owner.GetPower() <= 5000) { owner.AddPower(5000 - owner.GetPower()); } }
public static void BuyItem(ShellCore core, int index, IVendor vendor) { if (core.unitsCommanding.Count >= core.GetTotalCommandLimit()) { return; } GameObject creation = new GameObject(); creation.transform.position = vendor.GetPosition(); var blueprint = vendor.GetVendingBlueprint(); switch (blueprint.items[index].entityBlueprint.intendedType) { case EntityBlueprint.IntendedType.Turret: Turret tur = creation.AddComponent <Turret>(); tur.blueprint = blueprint.items[index].entityBlueprint; core.SetTractorTarget(creation.GetComponent <Draggable>()); tur.SetOwner(core); break; case EntityBlueprint.IntendedType.Tank: Tank tank = creation.AddComponent <Tank>(); tank.blueprint = blueprint.items[index].entityBlueprint; tank.SetOwner(core); break; default: break; } creation.GetComponent <Entity>().spawnPoint = vendor.GetPosition(); creation.GetComponent <Entity>().faction = core.faction; creation.name = blueprint.items[index].entityBlueprint.name; core.sectorMngr.InsertPersistentObject(blueprint.items[index].entityBlueprint.name, creation); core.AddPower(-blueprint.items[index].cost); }
public void AddPower(float power) { if (owner) { owner.AddPower(power); } }
public static Entity BuyItem(ShellCore core, int index, IVendor vendor) { if (core.unitsCommanding.Count >= core.GetTotalCommandLimit()) { return(null); } GameObject creation = new GameObject(); creation.transform.position = vendor.GetPosition(); var blueprint = vendor.GetVendingBlueprint(); switch (blueprint.items[index].entityBlueprint.intendedType) { case EntityBlueprint.IntendedType.Turret: Turret tur = creation.AddComponent <Turret>(); tur.blueprint = blueprint.items[index].entityBlueprint; core.SetTractorTarget(creation.GetComponent <Draggable>()); tur.SetOwner(core); if (SectorManager.instance && SectorManager.instance.GetComponentInChildren <BattleZoneManager>()) { var stats = SectorManager.instance.GetComponentInChildren <BattleZoneManager>().stats.Find(s => s.faction == core.faction); if (stats == null) { stats = new BattleZoneManager.Stats(core.faction); SectorManager.instance.GetComponentInChildren <BattleZoneManager>().stats.Add(stats); } stats.turretSpawns++; } break; case EntityBlueprint.IntendedType.Tank: Tank tank = creation.AddComponent <Tank>(); tank.blueprint = blueprint.items[index].entityBlueprint; tank.SetOwner(core); break; default: break; } creation.GetComponent <Entity>().spawnPoint = vendor.GetPosition(); creation.GetComponent <Entity>().faction = core.faction; creation.name = blueprint.items[index].entityBlueprint.name; core.sectorMngr.InsertPersistentObject(blueprint.items[index].entityBlueprint.name, creation); core.AddPower(-blueprint.items[index].cost); return(creation.GetComponent <Entity>()); }
public override void ActionTick() { if (waitingDraggable != null && waitingDraggable) { ai.movement.SetMoveTarget(waitingDraggable.transform.position, 100f); Vector2 delta = waitingDraggable.transform.position - craft.transform.position; if (ai.movement.targetIsInRange() && shellcore.GetTractorTarget() != null && shellcore.GetTractorTarget().gameObject.GetComponent <EnergySphereScript>() == null) { shellcore.SetTractorTarget(waitingDraggable); } } switch (state) { case BattleState.Attack: if (shellcore.GetTractorTarget() == null) { if (attackTurret == null) { Turret t = null; float minDistance = float.MaxValue; for (int i = 0; i < AIData.entities.Count; i++) { if (AIData.entities[i] != null && AIData.entities[i] && AIData.entities[i] is Turret && AIData.entities[i].faction == craft.faction && AIData.entities[i].GetComponentInChildren <WeaponAbility>() != null && AIData.entities[i].GetComponentInChildren <WeaponAbility>().GetID() != 16) { float d = (AIData.entities[i].transform.position - craft.transform.position).sqrMagnitude; if (d < minDistance) { t = AIData.entities[i] as Turret; minDistance = d; } } } attackTurret = t; } else { float d = (attackTurret.transform.position - shellcore.transform.position).sqrMagnitude; if (d < 150) { shellcore.SetTractorTarget(attackTurret.GetComponent <Draggable>()); } } } // go to nearest enemy construct, attack units / turrets if in visual range if ((primaryTarget == null && nextSearchTime < Time.time) || nextSearchTime < Time.time - 3f) { // get nearest construct primaryTarget = AirCraftAI.getNearestEntity <AirConstruct>(craft.transform.position, craft.faction, true); //TODO: Exclude turrets? nextSearchTime = Time.time + 1f; //if(primaryTarget) // Debug.Log("AggroTarget: " + primaryTarget.name + " Factions: " + primaryTarget.faction + " - " + craft.faction); } if (primaryTarget != null) { ai.movement.SetMoveTarget(primaryTarget.transform.position); //craft.MoveCraft((primaryTarget.transform.position - craft.transform.position).normalized); } //TODO: AI Attack: // action sequences // Use existing turrets: // -Drag torpedo turrets to enemy bunkers // -Drag siege turrets to outposts // ground attack location = own bunker location // drag tanks from one platform to another "LandPlatformGenerator.getEnemiesOnPlatform(Vector2 platformLocation, int faction)"? // how to react to different pressures on land and air? break; case BattleState.Defend: // destroy enemy units around base, ignore everything outside siege range if (primaryTarget && !primaryTarget.GetIsDead()) { ai.movement.SetMoveTarget(primaryTarget.transform.position); } // buy a turret matching the biggest threat's element, if possible break; case BattleState.Collect: // go from outpost to outpost, (also less fortified enemy outposts [count enemy units nearby {TODO}]) and collect energy if (findNewTarget || collectTarget == null) { // Find new target float minD = float.MaxValue; EnergyRock targetRock = null; int maxEnergy = -1; for (int i = 0; i < AIData.energyRocks.Count; i++) { if (AirCraftAI.getEnemyCountInRange(AIData.energyRocks[i].transform.position, 10f, craft.faction) > 2) { continue; } int energy = 0; for (int j = 0; j < AIData.energySpheres.Count; j++) { if ((AIData.energySpheres[j].transform.position - AIData.energyRocks[i].transform.position).sqrMagnitude < 16) { energy++; } } float d = (craft.transform.position - AIData.energyRocks[i].transform.position).sqrMagnitude; if ((maxEnergy < energy || d * 1.5f < minD || (maxEnergy == energy && d < minD)) && AIData.energyRocks[i] != collectTarget) { minD = d; maxEnergy = energy; targetRock = AIData.energyRocks[i]; } } collectTarget = targetRock; if (collectTarget != null) { findNewTarget = false; } //Debug.LogFormat("Faction {0} collect target: {1}", craft.faction, collectTarget); } if (collectTarget != null) { ai.movement.SetMoveTarget(collectTarget.transform.position); if (ai.movement.targetIsInRange()) { findNewTarget = true; } } break; case BattleState.Fortify: // TODO: place turrets // set primary target to an outpost with least defending turrets if (fortificationTarget == null || !fortificationTarget) { UpdateTargetInfluences(); float closestToZero = float.MaxValue; for (int i = 0; i < AITargets.Count; i++) { if (Mathf.Abs(AITargets[i].influence) < closestToZero && AITargets[i].entity.faction == shellcore.faction) { fortificationTarget = AITargets[i].entity; } } } else if (attackTurret == null || !attackTurret) { UpdateTargetInfluences(); float minDistance = float.MaxValue; for (int i = 0; i < AIData.entities.Count; i++) { if (AIData.entities[i] is Turret) { float d = (craft.transform.position - AIData.entities[i].transform.position).sqrMagnitude; float d2 = (fortificationTarget.transform.position - AIData.entities[i].transform.position).sqrMagnitude; if (d < minDistance && d2 > 150f) { minDistance = d; attackTurret = AIData.entities[i] as Turret; } } } if (attackTurret == null) { state = BattleState.Attack; ActionTick(); nextStateCheckTime += 1f; return; } } else if (shellcore.GetTractorTarget() != attackTurret) { ai.movement.SetMoveTarget(attackTurret.transform.position, 100f); if (ai.movement.targetIsInRange()) { var target = shellcore.GetTractorTarget(); if (target != null && target) { if (target.gameObject.GetComponent <EnergySphereScript>() == null) { shellcore.SetTractorTarget(attackTurret.GetComponent <Draggable>()); } } } } else { Vector2 turretDelta = fortificationTarget.transform.position - attackTurret.transform.position; Vector2 targetPosition = (Vector2)fortificationTarget.transform.position + turretDelta.normalized * 16f; Vector2 delta = targetPosition - (Vector2)craft.transform.position; if (turretDelta.sqrMagnitude < 16f) { shellcore.SetTractorTarget(null); } } break; default: break; } int energyCount = 0; // always collect energy if (shellcore.GetTractorTarget() != null && shellcore.GetTractorTarget().gameObject.GetComponent <EnergySphereScript>() == null) { for (int i = 0; i < AIData.energySpheres.Count; i++) { if ((AIData.energySpheres[i].transform.position - shellcore.transform.position).sqrMagnitude < 150) { energyCount++; if (shellcore.GetTractorTarget() != null) { waitingDraggable = shellcore.GetTractorTarget(); shellcore.SetTractorTarget(null); } } } } else if (shellcore.GetTractorTarget() == null && waitingDraggable != null) { for (int i = 0; i < AIData.energySpheres.Count; i++) { if ((AIData.energySpheres[i].transform.position - shellcore.transform.position).sqrMagnitude < 150) { energyCount++; } } if (energyCount == 0) { shellcore.SetTractorTarget(waitingDraggable); if (shellcore.GetTractorTarget() == waitingDraggable) { waitingDraggable = null; } } } // always buy more turrets/tanks if (shellcore.unitsCommanding.Count < shellcore.GetTotalCommandLimit() && energyCount == 0) { for (int i = 0; i < AIData.vendors.Count; i++) { if ((AIData.vendors[i].transform.position - craft.transform.position).sqrMagnitude < 380f && AIData.vendors[i].faction == craft.faction) { IVendor vendor = AIData.vendors[i] as IVendor; if (vendor.GetVendingBlueprint() == null) { continue; } int itemIndex = -1; if (state == BattleState.Attack) { bool ownGroundExists = false; for (int j = 0; j < AIData.entities.Count; j++) { if (AIData.entities[j].faction == craft.faction && AIData.entities[j].Terrain == Entity.TerrainType.Ground) { ownGroundExists = true; break; } } if (!ownGroundExists && enemyGroundTargets(true) && shellcore.GetPower() >= 150) { // Attack & enemy holds all ground itemIndex = vendor.GetVendingBlueprint().getItemIndex("Torpedo Turret"); } else { if (shellcore.GetPower() >= 200) { itemIndex = vendor.GetVendingBlueprint().getItemIndex("Missile Turret"); } else if (shellcore.GetPower() >= 100) { itemIndex = vendor.GetVendingBlueprint().getItemIndex("Defense Turret"); } } } if (itemIndex == -1) { for (int j = 0; j < vendor.GetVendingBlueprint().items.Count; j++) { if (vendor.GetVendingBlueprint().items[j].cost <= shellcore.GetPower() && shellcore.unitsCommanding.Count < shellcore.GetTotalCommandLimit()) { if (itemIndex != -1 && vendor.GetVendingBlueprint().items[j].cost <= vendor.GetVendingBlueprint().items[itemIndex].cost) // more expensive => better (TODO: choose based on the situation) { continue; } if (vendor.GetVendingBlueprint().items[j].entityBlueprint.intendedType == EntityBlueprint.IntendedType.Tank && !enemyGroundTargets(true)) //TODO: get turret / tank attack category from somewhere else { continue; } itemIndex = j; } } } if (itemIndex != -1) { GameObject creation = new GameObject(); creation.transform.position = AIData.vendors[i].transform.position; switch (vendor.GetVendingBlueprint().items[itemIndex].entityBlueprint.intendedType) { case EntityBlueprint.IntendedType.Turret: creation.name = "Turret"; Turret tur = creation.AddComponent <Turret>(); tur.blueprint = vendor.GetVendingBlueprint().items[itemIndex].entityBlueprint; tur.SetOwner(shellcore); tur.faction = craft.faction; shellcore.SetTractorTarget(creation.GetComponent <Draggable>()); break; case EntityBlueprint.IntendedType.Tank: creation.name = "Tank"; Tank tank = creation.AddComponent <Tank>(); tank.blueprint = vendor.GetVendingBlueprint().items[itemIndex].entityBlueprint; tank.enginePower = 250; tank.SetOwner(shellcore); tank.faction = craft.faction; break; default: break; } shellcore.sectorMngr.InsertPersistentObject(vendor.GetVendingBlueprint().items[itemIndex].entityBlueprint.name, creation); creation.GetComponent <Entity>().spawnPoint = AIData.vendors[i].transform.position; shellcore.AddPower(-vendor.GetVendingBlueprint().items[itemIndex].cost); break; } } } } }