示例#1
0
        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();
                }
            }
        }
示例#2
0
        /// 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);
        }