public override void RunEvent(EventManager callingEventManager) { Random random = new Random(); if (currentCycle == 0) { Vector2 randomTreePosition = new Vector2(random.Next() % associatedMap.getNumberTilesX(), random.Next() % associatedMap.getNumberTilesY()); if (!associatedMap.getOccupied(randomTreePosition)) { associatedMap.setOccupied(randomTreePosition, true); associatedMap.setOccupyingElement(randomTreePosition, new treeElement(associatedGame, (int)randomTreePosition.X, (int)randomTreePosition.Y)); numberOfTrees--; } } else { Vector2 randomTreePosition = new Vector2(random.Next() % associatedMap.getNumberTilesX(), random.Next() % associatedMap.getNumberTilesY()); randomTreePosition = associatedMap.FindNearest(randomTreePosition, "Tree"); int xMod = (random.Next() % spread) - (spread / 2); int yMod = (random.Next() % spread) - (spread / 2); randomTreePosition.X = randomTreePosition.X + xMod; randomTreePosition.Y = randomTreePosition.Y + yMod; if (associatedMap.TilePositionInBounds(randomTreePosition)) { if (!associatedMap.getOccupied(randomTreePosition)) { associatedMap.setOccupied(randomTreePosition, true); associatedMap.setOccupyingElement(randomTreePosition, new treeElement(associatedGame, (int)randomTreePosition.X, (int)randomTreePosition.Y)); numberOfTrees--; } } } if (numberOfTrees <= 0) { currentCycle++; if (currentCycle >= numberOfCycles) { this.SetComplete(); } } }
public override void RunEvent(EventManager callingEventManager) { Random random = new Random(); Vector2 randomRockPosition = new Vector2(random.Next() % associatedMap.getNumberTilesX(), random.Next() % associatedMap.getNumberTilesY()); if (!associatedMap.getOccupied(randomRockPosition)) { associatedMap.setOccupied(randomRockPosition, true); associatedMap.setOccupyingElement(randomRockPosition, new RockElement(associatedGame, (int)randomRockPosition.X, (int)randomRockPosition.Y)); numberOfRocks--; } if (numberOfRocks <= 0) { this.SetComplete(); } }
public override void RunEvent(EventManager callingEventManager) { if (!nextToRock) { if (seekingRock) { retryCounter++; focusElement.SetStatusMessage("Couldn't get to a Rock"); //keep a retry counter and retry up to 5 times if (retryCounter < retryAttempts) { EventHarvestRocks retryEvent = new EventHarvestRocks(associatedGame, associatedMap, associatedEventManager, focusElement, gameTime, repeating); retryEvent.retryCounter = retryCounter; callingEventManager.AddEvent(retryEvent); } //reset on success seekingRock = false; this.SetComplete(); } //check if next to Rock Vector2 testLocation = new Vector2(-1, -1); for (int i = 0; i < 4; i++) { if (i == 0) { testLocation = new Vector2(focusElement.getWorldPositionX() + 1, focusElement.getWorldPositionY()); } if (i == 1) { testLocation = new Vector2(focusElement.getWorldPositionX() - 1, focusElement.getWorldPositionY()); } if (i == 2) { testLocation = new Vector2(focusElement.getWorldPositionX(), focusElement.getWorldPositionY() + 1); } if (i == 3) { testLocation = new Vector2(focusElement.getWorldPositionX(), focusElement.getWorldPositionY() - 1); } if (associatedMap.TilePositionInBounds(testLocation)) { if (associatedMap.getOccupied(testLocation)) { if (associatedMap.getOccupyingElement(testLocation).GetElementName() == "Rock") { nextToRock = true; i = 4; } } } } if (nextToRock) { rockLocation.X = testLocation.X; rockLocation.Y = testLocation.Y; } //if not, find Rock else { seekingRock = true; Vector2 RockToTarget = associatedMap.FindNearest(focusElement.getWorldPositionVector(), "Rock"); //check if a real Rock was found if (RockToTarget.X == -1 && RockToTarget.Y == -1) { //couldn't find a Rock focusElement.SetStatusMessage("Can't see any Rocks!"); this.SetComplete(); } else { //move to Rock EventMoveTo movingEvent = new EventMoveTo(associatedGame, associatedMap, focusElement, RockToTarget, gameTime); if (focusElement.Moving()) { focusElement.ReplaceLinkedMovement(movingEvent); associatedEventManager.AddEvent(movingEvent); this.Suspend(movingEvent); } else { focusElement.LinkToMoveEvent(movingEvent); associatedEventManager.AddEvent(movingEvent); this.Suspend(movingEvent); } } } } if (nextToRock) { focusElement.SetStatusMessage("Getting ready to harvest Rock"); //hit Rock if (associatedMap.getOccupied(rockLocation)) { //success, reset retry counter retryCounter = 0; if (associatedMap.getOccupyingElement(rockLocation).GetElementName() == "Rock") { associatedMap.getOccupyingElement(rockLocation).UpdateCurrentHealth(5); if (associatedMap.getOccupyingElement(rockLocation).currentHealth <= 0) { //Rock is out of HP - generate the rock pile associatedMap.setOccupyingElement(rockLocation, new rockResourceElement(associatedGame, (int)rockLocation.X, (int)rockLocation.Y, (RockElement)associatedMap.getOccupyingElement(rockLocation))); if (repeating) { associatedEventManager.AddEvent(new EventHarvestRocks(associatedGame, associatedMap, associatedEventManager, focusElement, gameTime, true)); } this.SetComplete(); } } //it's likely someone else has mined it down already else { if (repeating) { associatedEventManager.AddEvent(new EventHarvestRocks(associatedGame, associatedMap, associatedEventManager, focusElement, gameTime, true)); } } } //this.SetComplete(); } }
public override void RunEvent(EventManager callingEventManagerIn) { callingEventManager = callingEventManagerIn; //Check if the goal is one move away and if it is not occupied if (Math.Abs(elementToMove.getWorldPositionX() - destination.X) + Math.Abs(elementToMove.getWorldPositionY() - destination.Y) <= 1) { if (moveGoingOn || !associatedMap.getOccupied(destination)) { //In this case we either only have to move one step - or this is a component of a larger path this.MoveToDestination(); } else { elementToMove.SetStuck(true); //kill user event if this wasn't a child if (elementToMove.GetFinalDestination() == destination) { this.SetComplete(); elementToMove.KillLinkedMovement(); } else { this.SetComplete(); } } } else { if (reattemptCounter > reattemptLimit) { elementToMove.SetStatusMessage("Ran out of attempts"); this.ShutdownSmoothly(); elementToMove.KillLinkedMovement(); } if (!this.GetShutdownSmoothly()) { if (pathFound) { movementWasPossible = true; this.PerformNextMove(); } else { if (flowMapProduced) { this.GeneratePath(); } else { this.DevelopFlowMap(); } } } else { elementToMove.KillLinkedMovement(); this.SetComplete(); } } if (!this.GetShutdownSmoothly()) { //reached destination if ((Math.Abs(elementToMove.getWorldPositionX() - elementToMove.GetFinalDestination().X) + Math.Abs(elementToMove.getWorldPositionY() - elementToMove.GetFinalDestination().Y) == 0)) { elementToMove.KillLinkedMovement(); } } }
/// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { //Update key presses inputManager.Update(); //Check for key presses if (inputManager.EscapeButtonPressed()) { Exit(); } if (inputManager.SpawnTreeButtonReleased()) { eventManager.AddEvent(new EventAddTrees(this, currentMap, 1, 1, 0)); } if (inputManager.SpawnRockButtonReleased()) { eventManager.AddEvent(new EventAddRocks(this, currentMap, 1)); } //Check left mouse functions if (inputManager.LeftMouseButtonReleased()) { if (inputManager.DragFinished()) { elementFocus.Clear(); currentMap.GetAllElementsInArea(elementFocus, inputManager.GetMouseDragStartTile(), inputManager.GetMouseDragEndTile()); } else if (inputManager.MouseOverMap() && currentMap.getOccupied(inputManager.GetCurrentMouseTile(currentMap))) { currentMap.getOccupyingElement(inputManager.GetCurrentMouseTile(currentMap)).UpdateCurrentHealth(5); elementFocus.Clear(); elementFocus.Add(currentMap.getOccupyingElement(inputManager.GetCurrentMouseTile(currentMap))); } else if (uiManager.OverHarvestIcon(inputManager) && elementFocus.Count > 0) { for (int i = 0; i < elementFocus.Count; i++) { if (elementFocus[i].GetMovable()) { ((ActorElement)elementFocus[i]).SetJob(new GathererJob()); } } } else if (uiManager.OverMineIcon(inputManager) && elementFocus.Count > 0) { for (int i = 0; i < elementFocus.Count; i++) { if (elementFocus[i].GetMovable()) { eventManager.AddEvent(new EventHarvestRocks(this, currentMap, eventManager, elementFocus[i], gameTime, true)); } } } else { elementFocus.Clear(); } } //Check right mouse functions if (inputManager.RightMouseButtonReleased()) { //cycle through all focuses for (int i = 0; i < elementFocus.Count; i++) { if (elementFocus[i].GetMovable()) { //This is an actor ActorElement currentActor = (ActorElement)elementFocus[i]; //clear any previous stuck condition currentActor.SetStuck(false); EventMoveTo movingEvent = new EventMoveTo(this, currentMap, elementFocus[i], inputManager.GetCurrentMouseTile(currentMap), gameTime); if (currentActor.Moving()) { currentActor.ReplaceLinkedMovement(movingEvent); eventManager.AddEvent(movingEvent); } else { currentActor.LinkToMoveEvent(movingEvent); eventManager.AddEvent(movingEvent); } } } if (elementFocus.Count == 0) { //if no focus, spawn elements to test with if (!currentMap.getOccupied(inputManager.GetCurrentMouseTile(currentMap))) { currentMap.setOccupied(inputManager.GetCurrentMouseTile(currentMap), true); //currentMap.setOccupyingElement(inputManager.GetCurrentMouseTile(currentMap), new WorkerElement(this, (int)inputManager.GetCurrentMouseTile(currentMap).X, (int)inputManager.GetCurrentMouseTile(currentMap).Y)); currentMap.setOccupyingElement(inputManager.GetCurrentMouseTile(currentMap), new WaterElement(this, (int)inputManager.GetCurrentMouseTile(currentMap).X, (int)inputManager.GetCurrentMouseTile(currentMap).Y, currentMap)); } } } //Process jobs //TODO make run for everyone if (elementFocus.Count > 0) { if (elementFocus[0].HasJob() && elementFocus[0].Idle()) { ((ActorElement)elementFocus[0]).SetIdle(true); ((ActorElement)elementFocus[0]).GetJob().ProcessJobPriorities(this, currentMap, eventManager, elementFocus[0], gameTime); } } //Run events eventManager.RunEvents(); base.Update(gameTime); }
public override void RunEvent(EventManager callingEventManager) { if (!nextToTree) { if (seekingTree) { focusElement.SetStatusMessage("Couldn't get to a tree"); //reset on success seekingTree = false; this.SetComplete(); } else { //check if next to tree Vector2 testLocation = new Vector2(-1, -1); for (int i = 0; i < 4; i++) { if (i == 0) { testLocation = new Vector2(focusElement.getWorldPositionX() + 1, focusElement.getWorldPositionY()); } if (i == 1) { testLocation = new Vector2(focusElement.getWorldPositionX() - 1, focusElement.getWorldPositionY()); } if (i == 2) { testLocation = new Vector2(focusElement.getWorldPositionX(), focusElement.getWorldPositionY() + 1); } if (i == 3) { testLocation = new Vector2(focusElement.getWorldPositionX(), focusElement.getWorldPositionY() - 1); } if (associatedMap.checkOccupied(testLocation, "Tree")) { nextToTree = true; treeLocation.X = testLocation.X; treeLocation.Y = testLocation.Y; } } } //if not, find tree if (!nextToTree) { seekingTree = true; Vector2 treeToTarget = associatedMap.FindNearestPathable(focusElement.getWorldPositionVector(), "Tree", true); //check if a real tree was found if (treeToTarget.X == -1 && treeToTarget.Y == -1) { //couldn't find a tree focusElement.SetStatusMessage("Can't see any trees!"); this.SetComplete(); } else { EventMoveTo movingEvent = new EventMoveTo(associatedGame, associatedMap, focusElement, treeToTarget, gameTime); if (focusElement.Moving()) { focusElement.ReplaceLinkedMovement(movingEvent); associatedEventManager.AddEvent(movingEvent); this.Suspend(movingEvent); } else { focusElement.LinkToMoveEvent(movingEvent); associatedEventManager.AddEvent(movingEvent); this.Suspend(movingEvent); } } } } else { focusElement.SetStatusMessage("Getting ready to harvest tree"); //hit tree if (associatedMap.getOccupied(treeLocation)) { //success, reset retry counter if (associatedMap.getOccupyingElement(treeLocation).GetElementName() == "Tree") { associatedMap.getOccupyingElement(treeLocation).UpdateCurrentHealth(1); if (associatedMap.getOccupyingElement(treeLocation).currentHealth <= 0) { //Tree is out of HP - generate the logs associatedMap.setOccupyingElement(treeLocation, new woodResourceElement(associatedGame, (int)treeLocation.X, (int)treeLocation.Y, (treeElement)associatedMap.getOccupyingElement(treeLocation))); this.SetComplete(); } } else { this.SetComplete(); } } else { this.SetComplete(); } } }