Exemple #1
0
    void SetShopInventory(ShopKeeperState sks)
    {
        if (sks == null)
        {
            return;
        }

        currentState  = sks;
        shopInventory = currentState.GetShopInventory();
        SortInventory();
    }
        public override void knockBack(Vector2 direction, float magnitude, int damage, Entity attacker)
        {
            if (state == ShopKeeperState.Normal)
            {
                for (int i = 0; i < items.Length; i++)
                {
                    if (itemsForSale[i] == GlobalGameConstants.itemType.NoItem)
                    {
                        continue;
                    }

                    Vector2 drawItemPos = this.position + new Vector2((-2 * GlobalGameConstants.TileSize.X) + (i * 2f * GlobalGameConstants.TileSize.X), (2.5f * GlobalGameConstants.TileSize.Y));

                    items[i].Position          = drawItemPos;
                    InGameGUI.prices[i].active = false;
                }

                attackerTarget = attacker;

                knockBackTimer = 0;
                state          = ShopKeeperState.KnockBack;
                velocity       = 2 * Vector2.Normalize(direction);

                animationTime = 0;

                health -= damage;

                parentWorld.Particles.pushBloodParticle(CenterPoint);
                parentWorld.Particles.pushBloodParticle(CenterPoint);
                parentWorld.Particles.pushBloodParticle(CenterPoint);
            }
            else if (state == ShopKeeperState.Enraged)
            {
                attackerTarget = attacker;

                health -= damage;

                animationTime = 0;

                knockBackTimer = 0;
                state          = ShopKeeperState.KnockBack;
                velocity       = 2 * Vector2.Normalize(direction);

                parentWorld.Particles.pushBloodParticle(CenterPoint);
                parentWorld.Particles.pushBloodParticle(CenterPoint);
                parentWorld.Particles.pushBloodParticle(CenterPoint);
            }
        }
        /// <summary>
        /// Enrages the shopkeeper.
        /// </summary>
        public void poke()
        {
            if (state == ShopKeeperState.Enraged)
            {
                return;
            }

            for (int i = 0; i < items.Length; i++)
            {
                if (itemsForSale[i] == GlobalGameConstants.itemType.NoItem)
                {
                    continue;
                }

                Vector2 drawItemPos = this.position + new Vector2((-2 * GlobalGameConstants.TileSize.X) + (i * 2f * GlobalGameConstants.TileSize.X), (2.5f * GlobalGameConstants.TileSize.Y));

                items[i].Position          = drawItemPos;
                InGameGUI.prices[i].active = false;
            }

            for (int it = 0; it < parentWorld.EntityList.Count; it++)
            {
                if (!(parentWorld.EntityList[it] is Player || parentWorld.EntityList[it] is Enemy))
                {
                    continue;
                }

                if (attackerTarget == null)
                {
                    attackerTarget = parentWorld.EntityList[it];
                    continue;
                }

                if (Vector2.Distance(CenterPoint, parentWorld.EntityList[it].CenterPoint) < Vector2.Distance(CenterPoint, attackerTarget.CenterPoint))
                {
                    attackerTarget = parentWorld.EntityList[it];
                }
            }

            state = ShopKeeperState.Enraged;
        }
        public ShopKeeper(LevelState parentWorld, Vector2 position)
        {
            this.parentWorld = parentWorld;
            this.position    = position;
            this.velocity    = Vector2.Zero;
            this.dimensions  = GlobalGameConstants.TileSize;

            this.direction_facing = GlobalGameConstants.Direction.Down;

            greetingMessage = "Welcome to " + CampaignLobbyState.randomNames[Game1.rand.Next() % CampaignLobbyState.randomNames.Length] + "'s shop!";
            playerInRange   = false;

            state = ShopKeeperState.Normal;

            health              = 40;
            projectile          = new FireBall(new Vector2(-10, -10), 0.0f);
            projectile.active   = false;
            fireballDelayPassed = 0.0f;

            switchItemPressed = false;

            shopKeeperFrameAnimationTest = AnimationLib.getFrameAnimationSet("fireball");
            buyPic         = AnimationLib.getFrameAnimationSet("buyPic");
            leftBuyButton  = AnimationLib.getFrameAnimationSet("gamepadLB");
            rightBuyButton = AnimationLib.getFrameAnimationSet("gamepadRB");

            //test shop data for now
            {
                for (int i = 0; i < 3; i++)
                {
                    int rValue = Game1.rand.Next() % 14;
                    if (rValue == 9)
                    {
                        rValue = 4;
                    }                                // casting an int to item enum; no index for 9

                    itemsForSale[i] = (GlobalGameConstants.itemType)rValue;
                }

                for (int i = 0; i < 3; i++)
                {
                    itemPrices[i] = GlobalGameConstants.WeaponDictionary.weaponInfo[(int)itemsForSale[i]].price;
                    itemIcons[i]  = GlobalGameConstants.WeaponDictionary.weaponInfo[(int)itemsForSale[i]].pickupImage;
                }
            }

            items[0] = new Pickup(parentWorld, new Vector2(-500, -500), itemsForSale[0]);
            items[1] = new Pickup(parentWorld, new Vector2(-500, -500), itemsForSale[1]);
            items[2] = new Pickup(parentWorld, new Vector2(-500, -500), itemsForSale[2]);
            parentWorld.EntityList.AddRange(items);

            directionAnims = new AnimationLib.SpineAnimationSet[4];
            directionAnims[(int)GlobalGameConstants.Direction.Up]   = AnimationLib.loadNewAnimationSet("shopUp");
            directionAnims[(int)GlobalGameConstants.Direction.Down] = AnimationLib.loadNewAnimationSet("shopDown");
            directionAnims[(int)GlobalGameConstants.Direction.Left] = AnimationLib.loadNewAnimationSet("shopRight");
            directionAnims[(int)GlobalGameConstants.Direction.Left].Skeleton.FlipX = true;
            directionAnims[(int)GlobalGameConstants.Direction.Right] = AnimationLib.loadNewAnimationSet("shopRight");
            for (int i = 0; i < 4; i++)
            {
                directionAnims[i].Animation = directionAnims[i].Skeleton.Data.FindAnimation("idle");
            }

            for (int i = 0; i < 3; i++)
            {
                InGameGUI.prices[i].active      = true;
                InGameGUI.prices[i].description = GlobalGameConstants.WeaponDictionary.weaponInfo[(int)itemsForSale[i]].name;
                InGameGUI.prices[i].price       = GlobalGameConstants.WeaponDictionary.weaponInfo[(int)itemsForSale[i]].price.ToString();
                InGameGUI.prices[i].position    = position + new Vector2((-3 * GlobalGameConstants.TileSize.X) + (i * 3f * GlobalGameConstants.TileSize.X), (2.5f * GlobalGameConstants.TileSize.Y)) - (Game1.tenbyFive14.MeasureString(InGameGUI.prices[i].price) / 2) + new Vector2(0, 16);
            }
        }
        public override void update(GameTime currentTime)
        {
            animationTime += currentTime.ElapsedGameTime.Milliseconds / 1000f;

            if (health <= 0 && state != ShopKeeperState.Dying)
            {
                state             = ShopKeeperState.Dying;
                animationTime     = 0;
                projectile.active = false;

                this.death = true;

                directionAnims[(int)direction_facing].Animation = directionAnims[(int)direction_facing].Skeleton.Data.FindAnimation(Game1.rand.Next() % 3 == 0 ? "die" : Game1.rand.Next() % 2 == 0 ? "die2" : "die3");
            }

            if (state == ShopKeeperState.Dying)
            {
                windingUp = false;

                velocity = Vector2.Zero;
            }
            else if (state == ShopKeeperState.Enraged)
            {
                if (!windingUp && fireballDelayPassed > fireballDelay - 150f)
                {
                    animationTime = 0;
                    directionAnims[(int)direction_facing].Animation = directionAnims[(int)direction_facing].Skeleton.Data.FindAnimation("attack");
                    windingUp = true;
                }
                else if (windingUp)
                {
                    directionAnims[(int)direction_facing].Animation = directionAnims[(int)direction_facing].Skeleton.Data.FindAnimation("attack");
                }
                else if (!windingUp)
                {
                    directionAnims[(int)direction_facing].Animation = directionAnims[(int)direction_facing].Skeleton.Data.FindAnimation("chase");
                }

                if (attackerTarget != null)
                {
                    double theta = Math.Atan2(CenterPoint.Y - attackerTarget.CenterPoint.Y, CenterPoint.X - attackerTarget.CenterPoint.X);

                    if (theta > Math.PI / 4 && theta < (Math.PI) - (Math.PI / 4))
                    {
                        direction_facing = GlobalGameConstants.Direction.Up;
                    }
                    else if (theta > (Math.PI) - (Math.PI / 4) || theta < (-Math.PI) + (Math.PI / 4))
                    {
                        direction_facing = GlobalGameConstants.Direction.Right;
                    }
                    else if (theta < Math.PI / 4 && theta > Math.PI / -4)
                    {
                        direction_facing = GlobalGameConstants.Direction.Left;
                    }
                    else if (theta < Math.PI / -4 && theta > (-Math.PI) + (Math.PI / 4))
                    {
                        direction_facing = GlobalGameConstants.Direction.Down;
                    }

                    double angle = Math.Atan2(attackerTarget.Position.Y - position.Y, attackerTarget.Position.X - position.X);

                    if (Vector2.Distance(CenterPoint, attackerTarget.CenterPoint) - distanceToMaintainFromAttacker > GlobalGameConstants.TileSize.Y)
                    {
                        velocity = FireBall.fireBallVelocity / 4 * new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));

                        velocity += FireBall.fireBallVelocity / 8 * new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
                    }
                    else if (Vector2.Distance(CenterPoint, attackerTarget.CenterPoint) - distanceToMaintainFromAttacker < -1 * GlobalGameConstants.TileSize.Y)
                    {
                        velocity = -1 * FireBall.fireBallVelocity / 4 * new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
                    }
                }
                if (projectile.active)
                {
                    projectile.position  += FireBall.fireBallVelocity * new Vector2((float)Math.Cos(projectile.direction), (float)Math.Sin(projectile.direction));
                    projectile.timeAlive += currentTime.ElapsedGameTime.Milliseconds;

                    if (projectile.timeAlive > FireBall.fireBallDurationTime || parentWorld.Map.hitTestWall(projectile.center))
                    {
                        projectile.active   = false;
                        fireballDelayPassed = 0.0f;
                        attackPoint         = new Vector2(-1, -1);
                    }

                    for (int i = 0; i < parentWorld.EntityList.Count; i++)
                    {
                        if (parentWorld.EntityList[i] == this || parentWorld.EntityList[i] is Pickup)
                        {
                            continue;
                        }

                        if (Vector2.Distance(projectile.center, parentWorld.EntityList[i].CenterPoint) < GlobalGameConstants.TileSize.X)
                        {
                            parentWorld.EntityList[i].knockBack(new Vector2((float)Math.Cos(projectile.direction), (float)Math.Sin(projectile.direction)), 4.0f, 10);
                            projectile.active   = false;
                            fireballDelayPassed = -200f;
                            attackPoint         = new Vector2(-1, -1);
                        }
                    }
                }
                else
                {
                    fireballDelayPassed += currentTime.ElapsedGameTime.Milliseconds;

                    if (attackerTarget != null)
                    {
                        if (fireballDelayPassed > fireballDelay && attackPoint != new Vector2(-1, -1))
                        {
                            projectile          = new FireBall(position, (float)Math.Atan2(attackPoint.Y - position.Y, attackPoint.X - position.X));
                            windingUp           = false;
                            fireballDelayPassed = 0;

                            parentWorld.Particles.pushDotParticle2(position, (float)Math.Atan2(attackPoint.Y - position.Y, attackPoint.X - position.X) + 0.2f, Color.OrangeRed, 5.0f);
                            parentWorld.Particles.pushDotParticle2(position, (float)Math.Atan2(attackPoint.Y - position.Y, attackPoint.X - position.X) - 0.2f, Color.OrangeRed, 5.0f);
                        }
                        else if (fireballDelayPassed > attackPointSetDelay && attackPoint == new Vector2(-1, -1))
                        {
                            attackPoint = attackerTarget.CenterPoint;
                        }
                    }
                }
            }
            else if (state == ShopKeeperState.Normal)
            {
                directionAnims[(int)direction_facing].Animation = directionAnims[(int)direction_facing].Skeleton.Data.FindAnimation("idle");

                //render prices
                if (Vector2.Distance(CenterPoint, parentWorld.CameraFocus.CenterPoint) > 720)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        InGameGUI.prices[i].active = false;
                    }
                }
                else
                {
                    for (int i = 0; i < 3; i++)
                    {
                        if (itemsForSale[i] != GlobalGameConstants.itemType.NoItem)
                        {
                            InGameGUI.prices[i].active = true;
                        }
                    }
                }

                if (Vector2.Distance(CenterPoint, parentWorld.CameraFocus.CenterPoint) < 500 && !playerInRange)
                {
                    playerInRange = true;

                    parentWorld.pushMessage(greetingMessage);
                }
                else if (Vector2.Distance(CenterPoint, parentWorld.CameraFocus.CenterPoint) > 500 && playerInRange)
                {
                    playerInRange = false;
                }

                for (int it = 0; it < parentWorld.EntityList.Count; it++)
                {
                    if (parentWorld.EntityList[it] is Player && ((Player)parentWorld.EntityList[it]).Index == InputDevice2.PPG_Player.Player_1)
                    {
                        if (distance(parentWorld.EntityList[it].Position, position) < GlobalGameConstants.TileSize.X * GlobalGameConstants.TilesPerRoomHigh / 2)
                        {
                            playerOverlap = false;

                            for (int i = 0; i < 3; i++)
                            {
                                Vector2 drawItemPos = position + new Vector2((-3 * GlobalGameConstants.TileSize.X) + (i * 3f * GlobalGameConstants.TileSize.X), (2.5f * GlobalGameConstants.TileSize.Y));

                                if (distance(drawItemPos + GlobalGameConstants.TileSize / 2, parentWorld.EntityList[it].CenterPoint) < 32 && itemsForSale[i] != GlobalGameConstants.itemType.NoItem)
                                {
                                    playerOverlap                = true;
                                    buyLocation                  = parentWorld.EntityList[it].Position - new Vector2(0, 48);
                                    overlapIndex                 = i;
                                    InGameGUI.prices[i].price    = InGameGUI.prices[i].description;
                                    InGameGUI.prices[i].position = position + new Vector2((-3 * GlobalGameConstants.TileSize.X) + (i * 3f * GlobalGameConstants.TileSize.X), (2.5f * GlobalGameConstants.TileSize.Y)) - (Game1.tenbyFive14.MeasureString(InGameGUI.prices[i].description) / 2) + new Vector2(0, 16);

                                    if (switchItemPressed && !(InputDevice2.IsPlayerButtonDown(InputDevice2.PPG_Player.Player_1, InputDevice2.PlayerButton.SwitchItem1) || InputDevice2.IsPlayerButtonDown(InputDevice2.PPG_Player.Player_1, InputDevice2.PlayerButton.SwitchItem2)) && GameCampaign.Player_Coin_Amount >= itemPrices[i])
                                    {
                                        items[i].Position = drawItemPos;

                                        purchaseTransaction(itemPrices[i]);
                                        itemsForSale[i]            = GlobalGameConstants.itemType.NoItem;
                                        InGameGUI.prices[i].active = false;
                                    }
                                }
                                else
                                {
                                    if ((int)itemsForSale[i] >= 0)
                                    {
                                        InGameGUI.prices[i].price    = GlobalGameConstants.WeaponDictionary.weaponInfo[(int)itemsForSale[i]].priceString;
                                        InGameGUI.prices[i].position = position + new Vector2((-3 * GlobalGameConstants.TileSize.X) + (i * 3f * GlobalGameConstants.TileSize.X), (2.5f * GlobalGameConstants.TileSize.Y)) - (Game1.tenbyFive14.MeasureString(InGameGUI.prices[i].price) / 2) + new Vector2(0, 16);
                                    }
                                }
                            }
                        }
                    }
                }

                if (InputDevice2.IsPlayerButtonDown(InputDevice2.PPG_Player.Player_1, InputDevice2.PlayerButton.SwitchItem1) || (InputDevice2.IsPlayerButtonDown(InputDevice2.PPG_Player.Player_1, InputDevice2.PlayerButton.SwitchItem2)) && !switchItemPressed)
                {
                    switchItemPressed = true;
                }
                else if (!(InputDevice2.IsPlayerButtonDown(InputDevice2.PPG_Player.Player_1, InputDevice2.PlayerButton.SwitchItem1) || InputDevice2.IsPlayerButtonDown(InputDevice2.PPG_Player.Player_1, InputDevice2.PlayerButton.SwitchItem2)) && switchItemPressed)
                {
                    switchItemPressed = false;
                }
            }
            else if (state == ShopKeeperState.KnockBack)
            {
                directionAnims[(int)direction_facing].Animation = directionAnims[(int)direction_facing].Skeleton.Data.FindAnimation("hurt");

                knockBackTimer += currentTime.ElapsedGameTime.Milliseconds;

                if (knockBackTimer > knockBackDuration)
                {
                    state = ShopKeeperState.Enraged;
                }

                if (projectile.active)
                {
                    projectile.position  += FireBall.fireBallVelocity * new Vector2((float)Math.Cos(projectile.direction), (float)Math.Sin(projectile.direction));
                    projectile.timeAlive += currentTime.ElapsedGameTime.Milliseconds;

                    if (projectile.timeAlive > FireBall.fireBallDurationTime || parentWorld.Map.hitTestWall(projectile.center))
                    {
                        projectile.active   = false;
                        fireballDelayPassed = 0.0f;
                        attackPoint         = new Vector2(-1, -1);
                    }

                    for (int i = 0; i < parentWorld.EntityList.Count; i++)
                    {
                        if (parentWorld.EntityList[i] == this)
                        {
                            continue;
                        }

                        if (Vector2.Distance(projectile.center, parentWorld.EntityList[i].CenterPoint) < GlobalGameConstants.TileSize.X)
                        {
                            parentWorld.EntityList[i].knockBack(new Vector2((float)Math.Cos(projectile.direction), (float)Math.Sin(projectile.direction)), 4.0f, 10);
                            projectile.active   = false;
                            fireballDelayPassed = -200f;
                            attackPoint         = new Vector2(-1, -1);
                        }
                    }
                }
            }
            else if (state == ShopKeeperState.InvalidState)
            {
                throw new Exception("Shopkeeper was thrown into an invalid state: " + position.X + "," + position.Y);
            }

            directionAnims[(int)direction_facing].Animation.Apply(directionAnims[(int)direction_facing].Skeleton, animationTime, state == ShopKeeperState.KnockBack || state == ShopKeeperState.Dying || windingUp ? false : true);

            Vector2 step     = position + velocity;
            Vector2 finalPos = parentWorld.Map.reloactePosition(position, step, dimensions);

            position = finalPos;
        }