Beispiel #1
0
 public void Update(KeyboardState state, Tile[][] tiles)
 {
     if (pickUpCounter > 0)
     {
         pickUpCounter--;
         if (pickUpCounter == 0)
         {
             CanBePickedUp = true;
         }
     }
     if (isFalling)
     {
         newLocation.Y += verticalVelocity;
         newLocation.X += horizontalVelocity;
         verticalVelocity++;
         Collisions.CollideWithTiles(tiles, this);
         location = newLocation;
         hitBox   = new Rectangle((int)location.X, (int)location.Y, width, height);
     }
     else
     {
         yOffsetCounter++;
         if (yOffsetCounter > 5)
         {
             yOffsetCounter  = 0;
             currentYOffset += yOffsetDirection;
             if (currentYOffset > 1 || currentYOffset < -5)
             {
                 yOffsetDirection *= -1;
             }
         }
     }
 }
Beispiel #2
0
 public void Update(Tile[][] tiles)
 {
     newLocation    = location;
     newLocation.X += horizontalVelocity;
     if (Collisions.CollideWithTiles(tiles, this))
     {
         owner.RemoveProjectile(this);
     }
     location = newLocation;
     hitBox   = new Rectangle((int)location.X, (int)location.Y, width, height);
 }
Beispiel #3
0
        virtual public void Update(Player player, Tile[][] tiles)
        {
            Collisions.CollideWithTiles(tiles, this);
            location = newLocation;
            hitBox   = new Rectangle((int)location.X, (int)location.Y, width, height);

            if (state == "hurt")
            {
                hurtCounter--;
                if (hurtCounter <= 0)
                {
                    hurtCounter = 0;
                    state       = "normal";
                }
            }
            else if (player.swordIsActive && Collisions.EntityCollisions(player.swordHitBox, hitBox))
            {
                GetHit(player, player.swingFacing);
                player.swordIsActive = false;
            }
            else
            {
                foreach (Projectile projectile in player.Projectiles)
                {
                    if (Collisions.EntityCollisions(projectile.hitBox, hitBox))
                    {
                        string direction = "left";
                        if (projectile.horizontalVelocity > 0)
                        {
                            direction = "right";
                        }
                        GetHit(player, direction);
                        player.RemoveProjectile(projectile);
                        break;
                    }
                }
            }

            if (!player.invulnerable && Collisions.EntityCollisions(player.hitBox, hitBox))
            {
                player.GetHit("left", GetDamage());
            }
        }
Beispiel #4
0
        public void Update(KeyboardState state, MouseState mouseState, OrthographicCamera camera)
        {
            player.Update(state, tiles, mouseState);
            for (int i = enemies.Count - 1; i >= 0; i--) // updating may cause enemy to be removed, so iterate backwards.
            {
                enemies[i].Update(player, tiles);
            }
            spawners.ForEach(spawner => spawner.Update(enemies));
            items.ForEach(item => item.Update(state, tiles));
            NPCList.ForEach(npc => npc.Update(state, tiles, mouseState));

            foreach (Tile[] row in tiles)
            {
                foreach (Tile tile in row)
                {
                    if (tile is Tiles.UpdatableTile)
                    {
                        ((Tiles.UpdatableTile)tile).Update(player);
                    }
                }
            }


            if (state.IsKeyDown(Keys.Q) && !previousQPressed)
            {
                foreach (NPC character in NPCList)
                {
                    if ((character.GetDialogBox() != null && character.GetDialogBox().GetIsActive()) || Collisions.EntityCollisions(player.hitBox, character.hitBox))
                    {
                        character.ToggleDialog(screenWidth, screenHeight, graphicsDevice);
                        break;
                    }
                }
            }
            previousQPressed = state.IsKeyDown(Keys.Q);

            Item[] itemTempList = new Item[items.Count];
            items.CopyTo(itemTempList);
            items.Clear();
            foreach (Item item in itemTempList)
            {
                if (Collisions.EntityCollisions(item.hitBox, player.hitBox) && item.CanBePickedUp)
                {
                    player.AddToInventory(item);
                }
                else
                {
                    items.Add(item);
                }
            }

            Vector2 playerScreenLocation = camera.WorldToScreen(player.location.X, player.location.Y);

            if (playerScreenLocation.X > screenWidth - 500)
            {
                camera.Move(new Vector2(playerScreenLocation.X - (screenWidth - 500), 0));
            }
            else if (playerScreenLocation.X < 500)
            {
                camera.Move(new Vector2(-1 * (500 - playerScreenLocation.X), 0));
            }

            if (playerScreenLocation.Y > screenHeight - 300)
            {
                camera.Move(new Vector2(0, playerScreenLocation.Y - (screenHeight - 300)));
            }
            else if (playerScreenLocation.Y < 300)
            {
                camera.Move(new Vector2(0, (-1 * (300 - playerScreenLocation.Y))));
            }

            if (camera.Position.X < 50)
            {
                camera.Move(new Vector2((-1 * camera.Position.X) + 50, 0));
            }
            else if (camera.Position.X > ((width - 1) * 50) - camera.BoundingRectangle.Width)
            {
                camera.Move(new Vector2((((width - 1) * 50) - camera.BoundingRectangle.Width) - camera.Position.X, 0));
            }

            if (camera.Position.Y < 50)
            {
                camera.Move(new Vector2(0, (-1 * camera.Position.Y) + 50));
            }
            else if (camera.Position.Y > ((height - 1) * 50) - camera.BoundingRectangle.Height)
            {
                camera.Move(new Vector2(0, (((height - 1) * 50) - camera.BoundingRectangle.Height) - camera.Position.Y));
            }
        }
Beispiel #5
0
        public void Update(KeyboardState keyboardState, Tile[][] tiles, MouseState mouseState)
        {
            newLocation = location;

            if (invulnerableTimer > 0)
            {
                invulnerableTimer--;
                if (this.state == "hurt" && invulnerableTimer < 80)
                {
                    this.state = "invulnerable";
                }
                if (invulnerableTimer == 0)
                {
                    invulnerable = false;
                    this.state   = "normal";
                }
            }

            if (!previousKeyboardState.IsKeyDown(Keys.OemComma) && keyboardState.IsKeyDown(Keys.OemComma))
            {
                if (inventory.RemoveFromInventory(new InventoryItem(new Items.HealthPotion(1), new Vector2(0, 0))))
                {
                    health += 50;
                    if (health > maxHealth)
                    {
                        health = maxHealth;
                    }
                }
            }
            if (!previousKeyboardState.IsKeyDown(Keys.OemPeriod) && keyboardState.IsKeyDown(Keys.OemPeriod))
            {
                if (inventory.RemoveFromInventory(new InventoryItem(new Items.ManaPotion(1), new Vector2(0, 0))))
                {
                    mana = maxMana;
                }
            }
            if (!previousKeyboardState.IsKeyDown(Keys.I) && keyboardState.IsKeyDown(Keys.I))
            {
                Game1.ToggleMenu(inventory);
            }
            if (!previousKeyboardState.IsKeyDown(Keys.O) && keyboardState.IsKeyDown(Keys.O))
            {
                Game1.ToggleMenu(equipmentMenu);
            }
            if (!(keyboardState.IsKeyDown(Keys.A) ^ keyboardState.IsKeyDown(Keys.D))) // if both or neither are pressed
            {
                textureChangeCounter = 5;
                currentTextureState  = 1;
            }
            else
            {
                if (textureChangeCounter <= 0)
                {
                    currentTextureState++;
                    if (currentTextureState > 2)
                    {
                        currentTextureState = 0;
                    }
                    textureChangeCounter = 5;
                }
                if (keyboardState.IsKeyDown(Keys.A))
                {
                    if (previousKeyboardState.IsKeyDown(Keys.A) && !isFalling)
                    {
                        textureChangeCounter--;
                    }
                    newLocation.X -= 4;
                    facing         = "left";
                }
                if (keyboardState.IsKeyDown(Keys.D))
                {
                    if (previousKeyboardState.IsKeyDown(Keys.D) && !isFalling)
                    {
                        textureChangeCounter--;
                    }
                    newLocation.X += 4;
                    facing         = "right";
                }
            }

            if (keyboardState.IsKeyDown(Keys.LeftControl))
            {
                Console.WriteLine(location.X + ", " + location.Y);
            }
            if (keyboardState.IsKeyDown(Keys.Space) && !isFalling)
            {
                isFalling        = true;
                verticalVelocity = -20;
            }

            if (projectileCooldown > 0)
            {
                projectileCooldown--;
            }
            if (keyboardState.IsKeyDown(Keys.J) && projectileCooldown == 0 && mana >= 25)
            {
                if (swingFacing == "right")
                {
                    Projectiles.Add(new Projectile(new Vector2(location.X + (width / 2) - (30 / 2), location.Y + (height / 2) - (30 / 2)),
                                                   projectileTexture, 10, currentLocation, this));
                }
                else if (swingFacing == "left")
                {
                    Projectiles.Add(new Projectile(new Vector2(location.X + (width / 2) - (30 / 2), location.Y + (height / 2) - (30 / 2)),
                                                   projectileTexture, -10, currentLocation, this));
                }
                projectileCooldown = 60;
                mana -= 25;
            }
            if (keyboardState.IsKeyDown(Keys.F) && !previousKeyboardState.IsKeyDown(Keys.F) && equipmentMenu.GetEquippedItem() != null && swinging == false)
            {
                if (equipmentMenu.GetEquippedItem().GetItem().GetType() == typeof(Items.SwordItem))
                {
                    swinging      = true;
                    swingFacing   = facing;
                    swordIsActive = true;
                }
                else if (equipmentMenu.GetEquippedItem().GetItem().GetType() == typeof(Items.ScytheItem))
                {
                    swinging       = true;
                    swingFacing    = facing;
                    scytheIsActive = true;
                }
            }


            if (!swinging)
            {
                swingFacing = facing;
            }
            if (swinging)
            {
                swordTextureChangeCounter--;
                if (swordTextureChangeCounter < 0)
                {
                    swordTextureChangeCounter = 5;
                    currentSwordTextureState++;
                    if (currentSwordTextureState >= 2)
                    {
                        swinging                 = false;
                        scytheIsActive           = false;
                        swordIsActive            = false;
                        currentSwordTextureState = 0;
                    }
                }
            }

            if (isFalling)
            {
                newLocation.Y += verticalVelocity;
                verticalVelocity++;
            }

            newHitBox = new Rectangle((int)newLocation.X, (int)newLocation.Y, width, height);
            Collisions.CollideWithTiles(tiles, this);
            location = newLocation;
            hitBox   = newHitBox;
            if (facing == "left")
            {
                swordHitBox = new Rectangle((int)location.X - swordOffset, (int)location.Y, swordOffset, height);
            }
            else if (facing == "right")
            {
                swordHitBox = new Rectangle((int)location.X + width, (int)location.Y, swordOffset, height);
            }

            if (keyboardState.IsKeyDown(Keys.E) && !previousKeyboardState.IsKeyDown(Keys.E))
            {
                foreach (Portal portal in currentLocation.GetPortals())
                {
                    if (Collisions.EntityCollisions(hitBox, portal.hitBox))
                    {
                        Travel(portal.GetDestination(), portal.GetPositionDestination());
                    }
                }
            }

            if (manaRegenCooldown > 0)
            {
                manaRegenCooldown--;
            }
            if (manaRegenCooldown == 0 && (mana < maxMana))
            {
                mana++;
                manaRegenCooldown = 10;
            }

            for (int i = Projectiles.Count - 1; i >= 0; i--) // some elements may be removed, so iterate backwards.
            {
                Projectiles[i].Update(tiles);
            }

            previousKeyboardState = keyboardState;
        }