Exemplo n.º 1
0
        public Forest(CollisionObjectsList obstacles, Vector2 tLeft, int w, int h, int columns, int rows, Texture2D tree, Texture2D bush, Texture2D boulder, Texture2D npc)
        {
            timer         = defaultTimer;
            treeSprite    = tree;
            bushSprite    = bush;
            boulderSprite = boulder;
            npcSprite     = npc;
            topLeft       = tLeft;
            width         = w;
            height        = h;

            var rnd = new Random(DateTime.Now.Millisecond);

            for (int x = 0; x < columns - 1; x++)
            {
                for (int y = 0; y < rows - 1; y++)
                {
                    obstacles.Add(new Tree(new Vector2(rnd.Next(x * width / columns + 16, (x + 1) * width / columns) - 16, rnd.Next(y * height / rows + 16, (y + 1) * height / rows - 16)), treeSprite));
                }
            }
            for (int x = 0; x < columns - 1; x += 2)
            {
                for (int y = 0; y < rows - 1; y += 2)
                {
                    Bush tempBush = new Bush(new Vector2(rnd.Next(x * width / columns + 16, (x + 2) * width / columns), rnd.Next(y * height / rows + 16, (y + 1) * height / rows - 16)), bushSprite);
                    if (obstacles.isCollision(tempBush.HitBox) == null)
                    {
                        obstacles.Add(tempBush);
                    }
                }
            }

            for (int x = 0; x < columns - 1; x += 3)
            {
                for (int y = 0; y < rows - 1; y += 3)
                {
                    Boulder tempBoulder = new Boulder(new Vector2(rnd.Next(x * width / columns + 16, (x + 2) * width / columns), rnd.Next(y * height / rows + 16, (y + 1) * height / rows - 16)), boulderSprite);
                    if (obstacles.isCollision(tempBoulder.HitBox) == null)
                    {
                        obstacles.Add(tempBoulder);
                    }
                }
            }

            for (int x = 0; x < columns - 1; x += 3)
            {
                for (int y = 0; y < rows - 1; y += 3)
                {
                    NPC tempNPC = new NPC(new Vector2(rnd.Next(x * width / columns + 16, (x + 2) * width / columns), rnd.Next(y * height / rows + 16, (y + 1) * height / rows - 16)), npcSprite);
                    if (obstacles.isCollision(tempNPC.HitBox) == null)
                    {
                        obstacles.Add(tempNPC);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void Update(CollisionObjectsList obstacles, GameTime gameTime, Player player, float camW, float camH)
        {
            var rnd = new Random(DateTime.Now.Millisecond);

            timer -= (float)gameTime.ElapsedGameTime.TotalSeconds;
            int tries      = 0;
            int treesClose = 0;

            if (timer <= 0)
            {
                Tree tempTree = new Tree(new Vector2(rnd.Next((int)topLeft.X, width), rnd.Next((int)topLeft.Y, height)), treeSprite);
                while (true)
                {
                    tempTree = new Tree(new Vector2(rnd.Next((int)topLeft.X, width), rnd.Next((int)topLeft.Y, height)), treeSprite);
                    if (obstacles.isCollision(tempTree.HitBox) != null && IsOffScreen(tempTree.Position, player, camW, camH))
                    {
                        treesClose = obstacles.getAdjacentObstacles(tempTree.HitPos).Count;
                        if (treesClose >= rnd.Next(1, 4))
                        {
                            obstacles.Add(tempTree);
                            Console.WriteLine("Time: " + (gameTime.TotalGameTime.TotalSeconds - timeSinceLastTree));
                            timeSinceLastTree = gameTime.TotalGameTime.TotalSeconds;
                        }
                        break;
                    }
                    if (tries > 5)
                    {
                        break;
                    }
                    tries++;
                }
                timer = defaultTimer;
            }
        }
Exemplo n.º 3
0
        public void Update(CollisionObjectsList obstacles, GameTime gameTime, int mapW, int mapH)
        {
            KeyboardState kState = Keyboard.GetState();
            float         dt     = (float)gameTime.ElapsedGameTime.TotalSeconds;

            state = State.Walking;

            hitBox.X = (int)position.X - 13;
            hitBox.Y = (int)position.Y;

            isMoving = false;

            if (kState.IsKeyDown(Keys.Right) && !(kState.IsKeyDown(Keys.Up) || kState.IsKeyDown(Keys.Down)))
            {
                direction = Dir.Right;
                isMoving  = true;
            }

            if (kState.IsKeyDown(Keys.Left) && !(kState.IsKeyDown(Keys.Up) || kState.IsKeyDown(Keys.Down)))
            {
                direction = Dir.Left;
                isMoving  = true;
            }

            if (kState.IsKeyDown(Keys.Up) && !(kState.IsKeyDown(Keys.Left) || kState.IsKeyDown(Keys.Right)))
            {
                direction = Dir.Up;
                isMoving  = true;
            }

            if (kState.IsKeyDown(Keys.Down) && !(kState.IsKeyDown(Keys.Left) || kState.IsKeyDown(Keys.Right)))
            {
                direction = Dir.Down;
                isMoving  = true;
            }
            if (kState.IsKeyDown(Keys.Up) && kState.IsKeyDown(Keys.Left))
            {
                direction = Dir.LeftUp;
                isMoving  = true;
            }
            if (kState.IsKeyDown(Keys.Up) && kState.IsKeyDown(Keys.Right))
            {
                direction = Dir.RightUp;
                isMoving  = true;
            }
            if (kState.IsKeyDown(Keys.Down) && kState.IsKeyDown(Keys.Right))
            {
                direction = Dir.RightDown;
                isMoving  = true;
            }
            if (kState.IsKeyDown(Keys.Down) && kState.IsKeyDown(Keys.Left))
            {
                direction = Dir.LeftDown;
                isMoving  = true;
            }

            if (isMoving)
            {
                CollisionObject tempOb   = null;
                Rectangle       tempRect = hitBox;
                UTurnCheck();
                tempRect.X += (int)Math.Ceiling((speed * dt * Math.Cos((int)direction * Math.PI / 4)));
                if ((tempOb = obstacles.isCollision(tempRect)) == null && tempRect.X < mapW && tempRect.X > 0)
                {
                    position.X += (float)(speed * dt * Math.Cos((int)direction * Math.PI / 4));
                }
                else
                {
                    Collision(tempOb);
                }
                tempRect    = hitBox;
                tempRect.Y -= (int)Math.Ceiling((speed * dt * Math.Sin((int)direction * Math.PI / 4)));
                if ((tempOb = obstacles.isCollision(tempRect)) == null && tempRect.Y < mapH && tempRect.Y > 64)
                {
                    position.Y -= (float)(speed * dt * Math.Sin((int)direction * Math.PI / 4));
                }
                else
                {
                    Collision(tempOb);
                }
            }
            //Player is not moving
            else
            {
            }
            if (kState.IsKeyDown(Keys.LeftShift) && stamina > 0)
            {
                //If the player is actually moving
                if (isMoving && position.X != positionOld.X || position.Y != positionOld.Y)
                {
                    if (speed < maxSpeed)
                    {
                        speed *= 1.01F * dt / 0.0166667F;
                    }
                    if (stamina - dt < 0)
                    {
                        stamina = 0;
                    }
                    else
                    {
                        stamina -= dt;
                    }
                }
                else
                {
                    speed = defaultSpeed;
                }
            }
            else
            {
                speed = defaultSpeed;
            }
            //Spacebar just pressed
            if (kState.IsKeyDown(Keys.Space) && kStateOld.IsKeyUp(Keys.Space))
            {
                if (strength >= 1)
                {
                    strength -= 1;
                    charge   += 0.4f;
                }
            }
            //Spacebar being held
            if (kState.IsKeyDown(Keys.Space) && kStateOld.IsKeyDown(Keys.Space))
            {
                state = State.Charging;
                if (strength >= 1)
                {
                    strength -= 1;
                    charge   += 0.4f;
                }
            }
            //Spacebar released
            if (kState.IsKeyUp(Keys.Space) && kStateOld.IsKeyDown(Keys.Space))
            {
                //MySounds.projectileSound.Play(0.2f, 0.5f, 0f);
                state  = State.Swing;
                swing  = new Swing(position, gameTime, direction, charge);
                charge = 0;
            }
            if (swing != null && !swing.Active)
            {
                swing = null;
            }

            if (stamina > 0 && strength < maxStrength && kState.IsKeyUp(Keys.Space))
            {
                stamina  -= dt * 0.4f;
                strength += dt * 40f;
            }


            anim = animations[(int)state][(int)direction];
            anim.setSpeed(0.25D - (0.2D * speed / maxSpeed));

            if (isMoving)
            {
                anim.Update(gameTime);
            }
            else
            {
                anim.setFrame(0);
            }
            kStateOld    = kState;
            directionOld = direction;
            positionOld  = position;
        }