public override void Update(GameTime gameTime) { base.Update(gameTime); if (!isActive) { goldText.Text = ""; return; } goldText.Text = goldCount.ToString(); if (buildings.NearestGoldMine.GoldRemaining == 0) { distanceToNearestGoldMine = 99999; foreach (GoldMine mine in goldMines) { if (Vector2.Distance(mine.Position, buildings.GetBase.Position) < distanceToNearestGoldMine && mine.GoldRemaining != 0) { distanceToNearestGoldMine = Vector2.Distance(mine.Position, buildings.GetBase.Position); buildings.NearestGoldMine = mine; } } } if (buildings.GetBase.Health <= 0) { buildings.DisableAll(); units.DisableAll(); isActive = false; Console.WriteLine(renderColor); } if (units.Peons.Count < 8 && goldCount >= 50 && !buildings.GetBase.IsWorking) { buildings.GetBase.CreatePeon(); goldCount -= 50; } else if (units.Soldiers.Count < 10 && goldCount >= 50 && buildings.IsBarracksAvailable()) { Barracks b = buildings.GetAvailableBarracks(); if (b != null) { b.CreateSoldier(); goldCount -= 50; } } foreach (Peon peon in units.Peons) { if (peon.peonObjective == Peon.PeonObjective.Build || peon.peonObjective == Peon.PeonObjective.GoToBuild) { //Do nothing because they are preoccupied } else if (peon.peonObjective == Peon.PeonObjective.Idle && peon.peonState == Peon.PeonState.Idle && peon.GoldCount < peon.GoldCountMax) { if (goldCount >= 400 && !buildings.IsBuildingInProgress(Building.BuildingType.GoldRefinery) && buildings.Refinery == null && !isAgentGoingToBuildSomething) { if (buildings.GetBase.Position.Y + (Game1.graphSize * 3) < Game1.WINDOW_HEIGHT) { buildingLocation = new Vector2(buildings.GetBase.Position.X, buildings.GetBase.Position.Y + (Game1.graphSize * 2)); } else { buildingLocation = new Vector2(buildings.GetBase.Position.X, buildings.GetBase.Position.Y - (Game1.graphSize * 4)); } peon.BuildBuilding(Building.BuildingType.GoldRefinery, buildingLocation); isAgentGoingToBuildSomething = true; } else if (goldCount >= 500 && !buildings.IsBuildingInProgress(Building.BuildingType.Barracks) && buildings.Barracks.Count == 0 && !isAgentGoingToBuildSomething) { if (buildings.GetBase.Position.X + (Game1.graphSize * 3) < Game1.WINDOW_WIDTH) { buildingLocation = new Vector2(buildings.GetBase.Position.X + (Game1.graphSize * 4), buildings.GetBase.Position.Y); } else { buildingLocation = new Vector2(buildings.GetBase.Position.X - (Game1.graphSize * 4), buildings.GetBase.Position.Y); } peon.BuildBuilding(Building.BuildingType.Barracks, buildingLocation); isAgentGoingToBuildSomething = true; } else { peon.MineFromGoldMine(buildings.NearestGoldMine); } } else if (peon.peonState == Peon.PeonState.Idle && peon.GoldCount == peon.GoldCountMax) { if (Vector2.Distance(peon.Position, buildings.GetBase.Position) < 50) { goldCount += peon.GoldCount; peon.GoldCount = 0; } peon.MoveToLocation(buildings.GetBase.Position); } } targetGold = 0; foreach (AIAgent agent in enemyAgents) { if (!agent.isActive) { enemyAgents.Remove(agent); index = 0; break; } else { if (targetGold < agent.GoldCount) { targetBuilding = agent.buildings.GetBase; targetGold = agent.GoldCount; } } } foreach (Soldier soldier in units.Soldiers) { if (soldier.soldierState == Soldier.SoldierState.Idle) { soldier.Attack(targetBuilding); } } }
public override void Update(GameTime gameTime) { base.Update(gameTime); if (!isActive) { goldText.Text = ""; return; } goldText.Position = new Vector2(position.X - 8, position.Y - 8); goldText.Text = goldCount.ToString(); if (peonState == PeonState.Moving) { if (Vector2.Distance(position, curTarget) < slowRadius + 5 && targetPath.Count > 2) { if (!graph.IsNodeBlocked(targetPath.Peek())) { curTarget = targetPath.Pop(); if (!graph.IsNodeBlocked(targetPath.Peek())) { curTarget = (curTarget + targetPath.Pop()) / 2; } else { peonState = PeonState.Idle; return; } } else { peonState = PeonState.Idle; return; } curTarget = new Vector2(curTarget.X + sprite.Width / 2, curTarget.Y + sprite.Height / 2); } else if (Vector2.Distance(position, curTarget) < slowRadius + 5 && targetPath.Count > 0) { if (!graph.IsNodeBlocked(targetPath.Peek())) { curTarget = targetPath.Pop(); } else { peonState = PeonState.Idle; } curTarget = new Vector2(curTarget.X + sprite.Width / 2, curTarget.Y + sprite.Height / 2); } else if (targetPath.Count == 0 && Vector2.Distance(position, curTarget) < stopRadius) { if (graph.IsNodeBlocked(position)) { MoveToLocation(targetLocation); return; } graph.SetNodeBlocked(position); peonState = PeonState.Idle; } //rotate based on orientation base.rotation = (float)Math.Atan2(orientation.X, -orientation.Y); acceleration = curTarget - this.Position; distance = Math.Abs(acceleration.Length()); if (distance < stopRadius) { speed = 0; } else if (distance < slowRadius) { speed = maxSpeed * distance / slowRadius; } else { speed = maxSpeed; } oldVelocity = velocity; acceleration = Vector2.Normalize(curTarget - this.Position) * maxAccel; velocity += velocity * gameTime.ElapsedGameTime.Milliseconds + .5f * acceleration * gameTime.ElapsedGameTime.Milliseconds * gameTime.ElapsedGameTime.Milliseconds; velocity = Vector2.Normalize(velocity) * speed; position += velocity; if (velocity != Vector2.Zero) { orientation = velocity; } } else if (peonState == PeonState.Idle) { if (peonObjective == PeonObjective.GoToBuild) { curNode = graph.GetNode(position); foreach (Edge edge in curNode.Neighbors) { tempNode = edge.GetNeighbor(curNode); //searches through the neighbor nodes to make sure it isnt on the wall or next to something else. if (!tempNode.IsBlocked) { canBuild = true; foreach (Edge innerEdge in tempNode.Neighbors) { if (innerEdge.GetNeighbor(tempNode).IsBlocked && innerEdge.GetNeighbor(tempNode) != curNode) { canBuild = false; } } if (canBuild) { agent.IsAgentGoingToBuildSomething = false; if (buildingType == Building.BuildingType.GoldRefinery) { GoldRefinery newRefinery = new GoldRefinery(game, tempNode, graph, agent); Game.Components.Add(newRefinery); peonObjective = PeonObjective.Build; return; } else if (buildingType == Building.BuildingType.Barracks) { Barracks newBarracks = new Barracks(game, tempNode, graph, agent); Game.Components.Add(newBarracks); peonObjective = PeonObjective.Build; return; } } } } } graph.SetNodeBlocked(position); } else if (peonState == PeonState.Mining) { graph.SetNodeBlocked(position); if (goldMine.GoldRemaining == 0) { peonState = PeonState.Idle; } else if (goldCount != goldCountMax) { timer += gameTime.ElapsedGameTime.Milliseconds; if (timer >= timeToGetGold) { timer = 0; if (goldToGetPerTime > goldMine.GoldRemaining) { goldCount += goldMine.TakeResource(goldMine.GoldRemaining); peonState = PeonState.Idle; } if (goldCount + goldToGetPerTime > goldCountMax) { goldCount += goldMine.TakeResource(goldCountMax - goldCount); } else { goldCount += goldMine.TakeResource(goldToGetPerTime); } } } else { peonState = PeonState.Idle; } } }
public override void Update(GameTime gameTime) { base.Update(gameTime); if (!isActive) { goldText.Text = ""; return; } goldText.Position = new Vector2(position.X - 8, position.Y - 8); goldText.Text = goldCount.ToString(); if (peonState == PeonState.Moving) { if (Vector2.Distance(position, curTarget) < slowRadius + 5 && targetPath.Count > 2) { if (!graph.IsNodeBlocked(targetPath.Peek())) { curTarget = targetPath.Pop(); if (!graph.IsNodeBlocked(targetPath.Peek())) { curTarget = (curTarget + targetPath.Pop()) / 2; } else { peonState = PeonState.Idle; return; } } else { peonState = PeonState.Idle; return; } curTarget = new Vector2(curTarget.X + sprite.Width / 2, curTarget.Y + sprite.Height / 2); } else if (Vector2.Distance(position, curTarget) < slowRadius + 5 && targetPath.Count > 0) { if (!graph.IsNodeBlocked(targetPath.Peek())) { curTarget = targetPath.Pop(); } else { peonState = PeonState.Idle; } curTarget = new Vector2(curTarget.X + sprite.Width / 2, curTarget.Y + sprite.Height / 2); } else if (targetPath.Count == 0 && Vector2.Distance(position, curTarget) < stopRadius) { if (graph.IsNodeBlocked(position)) { MoveToLocation(targetLocation); return; } graph.SetNodeBlocked(position); peonState = PeonState.Idle; } //rotate based on orientation base.rotation = (float)Math.Atan2(orientation.X, -orientation.Y); acceleration = curTarget - this.Position; distance = Math.Abs(acceleration.Length()); if (distance < stopRadius) { speed = 0; } else if (distance < slowRadius) { speed = maxSpeed * distance / slowRadius; } else { speed = maxSpeed; } oldVelocity = velocity; acceleration = Vector2.Normalize(curTarget - this.Position) * maxAccel; velocity += velocity * gameTime.ElapsedGameTime.Milliseconds + .5f * acceleration * gameTime.ElapsedGameTime.Milliseconds * gameTime.ElapsedGameTime.Milliseconds; velocity = Vector2.Normalize(velocity) * speed; position += velocity; if (velocity != Vector2.Zero) { orientation = velocity; } } else if (peonState == PeonState.Idle) { if (peonObjective == PeonObjective.GoToBuild) { curNode = graph.GetNode(position); foreach (Edge edge in curNode.Neighbors) { tempNode = edge.GetNeighbor(curNode); //searches through the neighbor nodes to make sure it isnt on the wall or next to something else. if (!tempNode.IsBlocked) { canBuild = true; foreach (Edge innerEdge in tempNode.Neighbors) { if (innerEdge.GetNeighbor(tempNode).IsBlocked&& innerEdge.GetNeighbor(tempNode) != curNode) { canBuild = false; } } if (canBuild) { agent.IsAgentGoingToBuildSomething = false; if (buildingType == Building.BuildingType.GoldRefinery) { GoldRefinery newRefinery = new GoldRefinery(game, tempNode, graph, agent); Game.Components.Add(newRefinery); peonObjective = PeonObjective.Build; return; } else if (buildingType == Building.BuildingType.Barracks) { Barracks newBarracks = new Barracks(game, tempNode, graph, agent); Game.Components.Add(newBarracks); peonObjective = PeonObjective.Build; return; } } } } } graph.SetNodeBlocked(position); } else if (peonState == PeonState.Mining) { graph.SetNodeBlocked(position); if (goldMine.GoldRemaining == 0) { peonState = PeonState.Idle; } else if (goldCount != goldCountMax) { timer += gameTime.ElapsedGameTime.Milliseconds; if (timer >= timeToGetGold) { timer = 0; if (goldToGetPerTime > goldMine.GoldRemaining) { goldCount += goldMine.TakeResource(goldMine.GoldRemaining); peonState = PeonState.Idle; } if (goldCount + goldToGetPerTime > goldCountMax) { goldCount += goldMine.TakeResource(goldCountMax - goldCount); } else { goldCount += goldMine.TakeResource(goldToGetPerTime); } } } else { peonState = PeonState.Idle; } } }