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) { 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(); } }
private void PerformNextMove() { bool canContinue = true; //Check if we have successfully moved, otherwise reattempt is occupier is moving if ((int)currentPath[1].X == elementToMove.getWorldPositionX() && (int)currentPath[1].Y == elementToMove.getWorldPositionY()) { //remove that step currentPath.RemoveAt(0); } else { if (associatedMap.getOccupied(new Vector2(currentPath[1].X, currentPath[1].Y))) { if (associatedMap.getOccupyingElement(new Vector2(currentPath[1].X, currentPath[1].Y)).GetMovable()) { if (elementToMove.GetStuck()) { //already stuck, stop movement elementToMove.KillLinkedMovement(); this.ShutdownSmoothly(); } else { elementToMove.SetStuck(true); } elementToMove.SetStatusMessage("Someone is in my spot!"); this.ShutdownSmoothly(); //instead of kill canContinue = false; if (associatedMap.getOccupied(elementToMove.GetFinalDestination())) { if (!associatedMap.getOccupyingElement(elementToMove.GetFinalDestination()).Moving()) { reattemptCounter++; EventMoveTo reattempt = new EventMoveTo(associatedGame, associatedMap, elementToMove, associatedMap.FindNearest(originalDestination, "Empty"), this.gameTime); //transfer the calling event to the new event as we are giving up control reattempt.setCallingEvent(this.GetCallingEvent()); reattempt.SetReattemptCounter(reattemptCounter); reattempt.SetOriginalDestination(originalDestination); elementToMove.ReplaceLinkedMovement(reattempt); callingEventManager.AddEvent(reattempt); } else { reattemptCounter++; EventMoveTo reattempt = new EventMoveTo(associatedGame, associatedMap, elementToMove, destination, this.gameTime); //transfer the calling event to the new event as we are giving up control reattempt.setCallingEvent(this.GetCallingEvent()); reattempt.SetReattemptCounter(reattemptCounter); elementToMove.ReplaceLinkedMovement(reattempt); callingEventManager.AddEvent(reattempt); } } else { reattemptCounter++; EventMoveTo reattempt = new EventMoveTo(associatedGame, associatedMap, elementToMove, destination, this.gameTime); //transfer the calling event to the new event as we are giving up control reattempt.setCallingEvent(this.GetCallingEvent()); reattempt.SetReattemptCounter(reattemptCounter); elementToMove.ReplaceLinkedMovement(reattempt); callingEventManager.AddEvent(reattempt); } } } } if (canContinue) { Event nextMove = new EventMoveTo(associatedGame, associatedMap, elementToMove, currentPath[1], this.gameTime); callingEventManager.AddEvent(nextMove); //suspend until that move event ends this.Suspend(nextMove); } else { this.SetComplete(); } }