Example #1
0
 public EventMoveTo(Game gameIn, mapManager mapIn, gameElement elementToMoveIn, Vector2 destinationIn, GameTime gameTimeIn)
 {
     this.SetEventName("Move To");
     associatedMap       = mapIn;
     associatedGame      = gameIn;
     elementToMove       = (ActorElement)elementToMoveIn;
     destination         = destinationIn;
     gameTime            = gameTimeIn;
     pathFound           = false;
     flowMapProduced     = false;
     noPathExists        = false;
     movementWasPossible = false;
     nextListTarget      = 0;
     reattemptCounter    = 0;
     originalDestination = destination;
     moveGoingOn         = false;
     currentPath         = new List <Vector2>();
     currentPath.Clear();
     nodeList = new List <MapNode>();
     nodeList.Clear();
     //setup a bool array the size of the map to determine which places have already been reviewed
     checkedMap = new bool[associatedMap.getNumberTilesX(), associatedMap.getNumberTilesY()];
     for (int x = 0; x < associatedMap.getNumberTilesX(); x++)
     {
         for (int y = 0; y < associatedMap.getNumberTilesY(); y++)
         {
             checkedMap[x, y] = false;
         }
     }
 }
Example #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();
                }
            }
        }
        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();
            }
        }
Example #4
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();
        }
Example #5
0
 private void DevelopFlowMap()
 {
     if (nodeList.Count == 0)
     {
         //seed list
         elementToMove.SetStatusMessage("Looking for path");
         MapNode newNode;
         newNode.nodeLocation         = new Vector2(elementToMove.getWorldPositionX(), elementToMove.getWorldPositionY());
         newNode.nodePreviousLocation = new Vector2(elementToMove.getWorldPositionX(), elementToMove.getWorldPositionY());
         nodeList.Add(newNode);
         nextListTarget = 0;
         checkedMap[elementToMove.getWorldPositionX(), elementToMove.getWorldPositionY()] = true;
         //check to see if my end target is populated already, and if I should shift my target
         if (associatedMap.getOccupied(this.destination))
         {
             if (associatedMap.getOccupyingElement(this.destination).Idle() || (associatedMap.getOccupyingElement(this.destination).GetStuck()))
             {
                 this.destination = associatedMap.FindNearest(originalDestination, "Empty");
             }
         }
     }
     else
     {
         //check if target was populated
         if (associatedMap.getOccupied(destination))
         {
             if (associatedMap.getOccupyingElement(destination).GetMovable() && !associatedMap.getOccupyingElement(destination).Idle() && !associatedMap.getOccupyingElement(destination).GetStuck())
             {
                 nodeList.Clear();
             }
         }
         //grow list
         if (nextListTarget >= nodeList.Count)
         {
             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);
             this.SetComplete();
             callingEventManager.AddEvent(reattempt);
         }
         else
         {
             //still nodes to check
             //check 4 cardinal directions
             for (int i = 0; i < 4; i++)
             {
                 Vector2 newNodeLocation = new Vector2(nodeList[nextListTarget].nodeLocation.X, nodeList[nextListTarget].nodeLocation.Y);
                 if (i == 0 && newNodeLocation.Y > 0)
                 {
                     newNodeLocation.Y--;                                  //North
                 }
                 if (i == 1 && newNodeLocation.X < associatedMap.getNumberTilesX() - 1)
                 {
                     newNodeLocation.X++;                                                                   //East
                 }
                 if (i == 2 && newNodeLocation.Y < associatedMap.getNumberTilesY() - 1)
                 {
                     newNodeLocation.Y++;                                                                    //South
                 }
                 if (i == 3 && newNodeLocation.X > 0)
                 {
                     newNodeLocation.X--;                                  //West
                 }
                 //if they are already checked - ignore
                 if (checkedMap[(int)newNodeLocation.X, (int)newNodeLocation.Y] == false)
                 {
                     checkedMap[(int)newNodeLocation.X, (int)newNodeLocation.Y] = true;
                     //if they are already occupied - ignore
                     if (!associatedMap.getOccupied(newNodeLocation))
                     {
                         //at this point it is safe to assume this is a usable node and add it to the list
                         MapNode nodeToAdd;
                         nodeToAdd.nodeLocation         = newNodeLocation;
                         nodeToAdd.nodePreviousLocation = new Vector2(nodeList[nextListTarget].nodeLocation.X, nodeList[nextListTarget].nodeLocation.Y);
                         nodeList.Add(nodeToAdd);
                         //If this new point was our destination - complete this step
                         if (nodeToAdd.nodeLocation.Equals(destination))
                         {
                             elementToMove.SetStatusMessage("Found Path");
                             flowMapProduced = true; //start next step
                             i = 4;                  //exit for loop
                         }
                     }
                 }
             }
             //increment next list target for next cycle
             nextListTarget++;
         }
     }
 }