void UseRecycledEntity(entity factory) { entity temp = RecycledEntities[0]; RecycledEntities.Remove(temp); temp.Position = factory.Position; Units.Add(temp); }
internal Player(Vector3 baseLocation) { Base = new entity(baseLocation); }
void NewEntity(entity factory) { Units.Add(new entity(factory.Position, 0)); }
void UpdateUnits() { for (int i = 0; i < Units.Count; ++i) { bool destroyed = false; entity currentUnit = Units[i]; if (enemy == null) { Console.WriteLine("Enemy is null!"); } if (enemy.Base == null) { Console.WriteLine("Enemy base is null!"); } entity currentEnemyEntity = enemy.Base; entity target = currentEnemyEntity; float DistanceToTarget = Vector2.Distance(currentUnit.Position, enemy.Base.Position); //Holds data we dont want to recreate on each loop float CurrentDistance = 0; int DistanceForDestroying = 25; if (DistanceToTarget < DistanceForDestroying) { Units.Remove(currentUnit); RecycledEntities.Add(currentUnit); target.Data -= 1; if (target.Data == 0) { Game1.instance.Explosion.Play(); //PLAYER WINS //TODO EndGame(); } else { Game1.instance.Crash.Play(); } continue; } //Check enemy units //for (int e = 0; e < enemy.Units.Count; ++e) //{ // currentEnemyEntity = enemy.Units[e]; // CurrentDistance = Vector2.Distance(currentUnit.Position, currentEnemyEntity.Position); // if (CurrentDistance < DistanceForDestroying) // { // Units.Remove(currentUnit); // enemy.Units.Remove(currentEnemyEntity); // RecycledEntities.Add(currentUnit); // RecycledEntities.Add(currentEnemyEntity); // //Game1.instance.Crash.Play(); // destroyed = true; // break; // } // if (CurrentDistance < DistanceToTarget) // { // target = currentEnemyEntity; // DistanceToTarget = CurrentDistance; // } //} if (enemy.Units.Count > 0) { currentEnemyEntity = enemy.Units[0]; CurrentDistance = Vector2.Distance(currentUnit.Position, currentEnemyEntity.Position); if (CurrentDistance < DistanceForDestroying) { Units.Remove(currentUnit); enemy.Units.Remove(currentEnemyEntity); RecycledEntities.Add(currentUnit); RecycledEntities.Add(currentEnemyEntity); Game1.instance.Crash.Play(); continue; } if (CurrentDistance < DistanceToTarget) { target = currentEnemyEntity; DistanceToTarget = CurrentDistance; } } else { //Check enemy walls for (int e = 0; e < enemy.Walls.Count; ++e) { currentEnemyEntity = enemy.Walls[e]; CurrentDistance = Vector2.Distance(currentUnit.Position, currentEnemyEntity.Position); if (CurrentDistance < DistanceForDestroying) { Units.Remove(currentUnit); RecycledEntities.Add(currentUnit); currentEnemyEntity.Data -= 1; if (currentEnemyEntity.Data == 0) { enemy.Walls.Remove(currentEnemyEntity); RecycledEntities.Add(currentEnemyEntity); Game1.instance.Explosion.Play(); } else { Game1.instance.Crash.Play(); } destroyed = true; break; } if (CurrentDistance < DistanceToTarget) { target = currentEnemyEntity; DistanceToTarget = CurrentDistance; } } if (destroyed) { continue; } //Check enemy factories for (int e = 0; e < enemy.Factories.Count; ++e) { currentEnemyEntity = enemy.Factories[e]; CurrentDistance = Vector2.Distance(currentUnit.Position, currentEnemyEntity.Position); if (CurrentDistance < DistanceForDestroying) { Units.Remove(currentUnit); RecycledEntities.Add(currentUnit); currentEnemyEntity.Data -= 1; if (currentEnemyEntity.Data == 0) { enemy.Factories.Remove(currentEnemyEntity); RecycledEntities.Add(currentEnemyEntity); Game1.instance.Explosion.Play(); } else { Game1.instance.Crash.Play(); } destroyed = true; break; } if (CurrentDistance < DistanceToTarget) { target = currentEnemyEntity; DistanceToTarget = CurrentDistance; } } if (destroyed) { continue; } //Check enemy Resource buildings for (int e = 0; e < enemy.ResourceBuildings.Count; ++e) { currentEnemyEntity = enemy.ResourceBuildings[e]; CurrentDistance = Vector2.Distance(currentUnit.Position, currentEnemyEntity.Position); if (CurrentDistance < DistanceForDestroying) { Units.Remove(currentUnit); RecycledEntities.Add(currentUnit); currentEnemyEntity.Data -= 1; if (currentEnemyEntity.Data == 0) { enemy.ResourceBuildings.Remove(currentEnemyEntity); RecycledEntities.Add(currentEnemyEntity); Game1.instance.Explosion.Play(); } else { Game1.instance.Crash.Play(); } destroyed = true; break; } if (CurrentDistance < DistanceToTarget) { target = currentEnemyEntity; DistanceToTarget = CurrentDistance; } } if (destroyed) { continue; } } Vector2 DistanceVector = target.Position - currentUnit.Position; Units[i].Data = (float)Math.Atan2((DistanceVector.Y), (DistanceVector.X)) + MathHelper.ToRadians(90); DistanceVector.X = Math.Abs(DistanceVector.X); DistanceVector.Y = Math.Abs(DistanceVector.Y); float total = DistanceVector.X + DistanceVector.Y; float ChangeX = 1; float ChangeY = 1; if (Math.Abs(DistanceVector.X) > Math.Abs(DistanceVector.Y)) { ChangeY = 1 * (DistanceVector.Y / total); } else { ChangeX = 1 * (DistanceVector.X / total); } //Console.WriteLine(ChangeX + " " + ChangeY); if (DistanceVector.X > 0) { if (currentUnit.Position.X > target.Position.X) { currentUnit.Position.X -= ChangeX; } else { currentUnit.Position.X += ChangeX; } } if (DistanceVector.Y > 0) { if (currentUnit.Position.Y > target.Position.Y) { currentUnit.Position.Y -= ChangeY; } else { currentUnit.Position.Y += ChangeY; } } } }