Exemplo n.º 1
0
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        protected override void Initialize()
        {
            //Define Systems
            ///////////////////////////////////////////////////////////////
            //Define the render layer
            renderer = new Renderer();

            //XNA Settings
            //Choose one time step setting
            this.TargetElapsedTime = TimeSpan.FromSeconds(1.0f / 300.0f); //Fixed time step
            IsFixedTimeStep        = false;                               //Variable

            //Screen settings
            renderer.setScreenSize(graphics, 1024, 768);

            this.IsMouseVisible = true;

            //Setup Map
            currentMap = new mapManager(this, mapTilesX, mapTilesY);

            //Setup Events
            eventManager = new EventManager(this, currentMap);

            //Setup UI
            uiManager = new UIManager(this, renderer, graphics.GraphicsDevice);

            //Setup Input
            inputManager = new InputManager(currentMap, uiManager);

            //Manage focus
            elementFocus.Clear();

            //Add testing character
            currentMap.setOccupied(new Vector2((int)currentMap.getNumberTilesX() / 2, (int)currentMap.getNumberTilesY() / 2), true);
            currentMap.setOccupyingElement(new Vector2((int)currentMap.getNumberTilesX() / 2, (int)currentMap.getNumberTilesY() / 2), new WorkerElement(this, (int)currentMap.getNumberTilesX() / 2, (int)currentMap.getNumberTilesY() / 2));
            currentMap.setOccupied(new Vector2((int)currentMap.getNumberTilesX() / 2 + 1, (int)currentMap.getNumberTilesY() / 2), true);
            currentMap.setOccupyingElement(new Vector2((int)currentMap.getNumberTilesX() / 2 + 1, (int)currentMap.getNumberTilesY() / 2), new WorkerElement(this, (int)currentMap.getNumberTilesX() / 2 + 1, (int)currentMap.getNumberTilesY() / 2));
            currentMap.setOccupied(new Vector2((int)currentMap.getNumberTilesX() / 2 - 1, (int)currentMap.getNumberTilesY() / 2), true);
            currentMap.setOccupyingElement(new Vector2((int)currentMap.getNumberTilesX() / 2 - 1, (int)currentMap.getNumberTilesY() / 2), new WorkerElement(this, (int)currentMap.getNumberTilesX() / 2 - 1, (int)currentMap.getNumberTilesY() / 2));

            //Variable initialization
            //////////////////////////////////////////////////////////////
            //Generate map
            eventManager.AddEvent(new EventGenerateWorld(this, currentMap, 4, 90, 3, 10));
            waterOffset = new Vector2(0, 0);

            base.Initialize();
        }
Exemplo n.º 2
0
        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();
                }
            }
        }
Exemplo n.º 3
0
        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();
            }
        }
Exemplo n.º 4
0
        private void MoveToDestination()
        {
            //elementToMove.SetStatusMessage("Trying to move");
            //Are we starting a move?
            if (!moveGoingOn && !associatedMap.getOccupied(destination))
            {
                moveGoingOn = true;
                //set Destination As Occupied
                associatedMap.setOccupied(destination, true);
                associatedMap.setOccupyingElement(destination, elementToMove);
            }
            if (moveGoingOn && !this.GetShutdownSmoothly())
            {
                //Process the move
                //Find direction of move
                elementToMove.SetStuck(false);
                float xMove = 0.0f;
                float yMove = 0.0f;
                if (elementToMove.getWorldPositionX() > destination.X)
                {
                    xMove = -1.0f;
                }
                if (elementToMove.getWorldPositionX() < destination.X)
                {
                    xMove = 1.0f;
                }
                if (elementToMove.getWorldPositionY() > destination.Y)
                {
                    yMove = -1.0f;
                }
                if (elementToMove.getWorldPositionY() < destination.Y)
                {
                    yMove = 1.0f;
                }

                Vector2 newAnimationOffset = new Vector2(0, 0);
                newAnimationOffset.X = elementToMove.GetAnimationOffset().X + (gameTime.ElapsedGameTime.Milliseconds * elementToMove.GetSpeed() * xMove);
                newAnimationOffset.Y = elementToMove.GetAnimationOffset().Y + (gameTime.ElapsedGameTime.Milliseconds * elementToMove.GetSpeed() * yMove);
                elementToMove.SetAnimationOffset(newAnimationOffset);

                //Has the move finished?
                if (Math.Abs(newAnimationOffset.X) > GlobalVariables.TILE_SIZE || Math.Abs(newAnimationOffset.Y) > GlobalVariables.TILE_SIZE)
                {
                    associatedMap.setOccupied(new Vector2(elementToMove.getWorldPositionX(), elementToMove.getWorldPositionY()), false);
                    associatedMap.setOccupyingElement(new Vector2(elementToMove.getWorldPositionX(), elementToMove.getWorldPositionY()), null);
                    elementToMove.worldPositionX = (int)destination.X;
                    elementToMove.worldPositionY = (int)destination.Y;
                    elementToMove.SetAnimationOffset(new Vector2(0, 0));
                    if (this.GetCallingEvent() == null)
                    {
                        elementToMove.SetStatusMessage("I'm Here!");
                    }
                    this.SetComplete();
                }
                else
                {
                    elementToMove.SetStatusMessage("Walking");
                }
            }
            else
            {
                elementToMove.SetStuck(true);
                this.SetComplete();
            }
        }
Exemplo n.º 5
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);
        }