Example #1
0
        override public void draw(Renderer renderer)
        {
            //Find proper atlas position
            int atlasX = 0;
            int atlasY = 0;

            int numberNorth = 0;
            int numberSouth = 0;
            int numberEast  = 0;
            int numberWest  = 0;

            //check north
            if (associatedMap.checkOccupied(new Vector2(this.getWorldPositionX(), this.getWorldPositionY() - 1), "Water"))
            {
                numberNorth++;
                if (associatedMap.checkOccupied(new Vector2(this.getWorldPositionX(), this.getWorldPositionY() - 2), "Water"))
                {
                    numberNorth++;
                }
            }
            //check east
            if (associatedMap.checkOccupied(new Vector2(this.getWorldPositionX() + 1, this.getWorldPositionY()), "Water"))
            {
                numberEast++;
                if (associatedMap.checkOccupied(new Vector2(this.getWorldPositionX() + 2, this.getWorldPositionY()), "Water"))
                {
                    numberEast++;
                }
            }
            //check south
            if (associatedMap.checkOccupied(new Vector2(this.getWorldPositionX(), this.getWorldPositionY() + 1), "Water"))
            {
                numberSouth++;
                if (associatedMap.checkOccupied(new Vector2(this.getWorldPositionX(), this.getWorldPositionY() + 2), "Water"))
                {
                    numberSouth++;
                }
            }
            //check west
            if (associatedMap.checkOccupied(new Vector2(this.getWorldPositionX() - 1, this.getWorldPositionY()), "Water"))
            {
                numberWest++;
                if (associatedMap.checkOccupied(new Vector2(this.getWorldPositionX() - 2, this.getWorldPositionY()), "Water"))
                {
                    numberWest++;
                }
            }
            //find position
            atlasY = 0 + (Math.Abs(numberNorth * numberSouth + 1) / 2 * 32);
            atlasX = 0 + (Math.Abs(numberEast * numberWest + 1) / 2 * 32);

            //Generate the texture
            //Convert normal map
            //Find the world position of top left
            normalOrigin1X = ((this.getWorldPositionX() * GlobalVariables.TILE_SIZE) + (int)this.GetAnimationOffset().X) % (normalSource.Width / 2);
            normalOrigin1Y = (this.getWorldPositionY() * GlobalVariables.TILE_SIZE) % (normalSource.Height / 2);
            normalOrigin2X = (this.getWorldPositionX() * GlobalVariables.TILE_SIZE) % (normalSource.Width / 2);
            normalOrigin2Y = ((this.getWorldPositionY() * GlobalVariables.TILE_SIZE) + (int)this.GetAnimationOffset().Y) % (normalSource.Height / 2);

            //generate normal map 1
            sourceRectangle = new Rectangle(normalOrigin1X, normalOrigin1Y, GlobalVariables.TILE_SIZE, GlobalVariables.TILE_SIZE);
            normalSource.GetData(0, sourceRectangle, data, 0, data.Length);
            normalMap1.SetData(data);

            //generate normal map 2
            //check for overflow on origin.x for first normal map
            sourceRectangle2 = new Rectangle(normalOrigin2X, normalOrigin2Y, GlobalVariables.TILE_SIZE, GlobalVariables.TILE_SIZE);
            normalSource.GetData(0, sourceRectangle2, data2, 0, data2.Length);
            normalMap2.SetData(data2);

            //Draw the proper normal position
            renderer.endDrawing();
            waterNormalMap.Parameters["SpriteTexture"].SetValue(texture);
            waterNormalMap.Parameters["SpriteNormal"].SetValue(normalMap1);
            waterNormalMap.Parameters["SpriteNormal2"].SetValue(normalMap2);
            renderer.startShadedDrawing(waterNormalMap);
            renderer.drawTexturedRectangle(0 + (this.getWorldPositionX() * GlobalVariables.TILE_SIZE), 0 + (this.getWorldPositionY() * GlobalVariables.TILE_SIZE + (GlobalVariables.TILE_SIZE / 2)) - 5, GlobalVariables.TILE_SIZE, GlobalVariables.TILE_SIZE, this.texture);
            //Draw water wall if square above is not water
            renderer.endDrawing();
            renderer.startDrawing();
        }
        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();
                }
            }
        }