Exemple #1
0
        public void Update(GameTime gameTime)
        {
            ComponentManager cm = ComponentManager.GetInstance();

            foreach (var entity in cm.GetComponentsOfType <PlayerControlComponent>())
            {
                PlayerControlComponent playerComp = (PlayerControlComponent)entity.Value;

                if (cm.HasEntityComponent <InventoryComponent>(entity.Key))
                {
                    InventoryComponent invenComp = cm.GetComponentForEntity <InventoryComponent>(entity.Key);

                    if (invenComp.ItemsToAdd.Count > 0)
                    {
                        foreach (int item in invenComp.ItemsToAdd)
                        {
                            AddItemToInventory(entity.Key, item);
                        }
                        invenComp.ItemsToAdd.Clear();
                    }
                    if (invenComp.ItemsToRemove.Count > 0)
                    {
                        foreach (int item in invenComp.ItemsToRemove)
                        {
                            ItemComponent itemComp = cm.GetComponentForEntity <ItemComponent>(item);
                            invenComp.Items[itemComp.InventoryPosition] = 0;
                            invenComp.AmountOfItems--;
                        }
                        invenComp.ItemsToRemove.Clear();
                    }
                    if (playerComp.Inventory.IsButtonDown())
                    {
                        if (cm.HasEntityComponent <MoveComponent>(entity.Key) && cm.HasEntityComponent <AttackComponent>(entity.Key))
                        {
                            MoveComponent   moveComp   = cm.GetComponentForEntity <MoveComponent>(entity.Key);
                            AttackComponent attackComp = cm.GetComponentForEntity <AttackComponent>(entity.Key);
                            if (invenComp.IsOpen)
                            {
                                attackComp.CanAttack = true;
                                moveComp.CanMove     = true;
                                invenComp.HeldItem   = 0;
                                invenComp.IsOpen     = false;
                            }
                            else
                            {
                                attackComp.CanAttack = false;
                                moveComp.Velocity    = new Vector2(0.0f, 0.0f);
                                moveComp.CanMove     = false;
                                invenComp.IsOpen     = true;
                            }
                        }
                    }
                    if (invenComp.IsOpen)
                    {
                        if (invenComp.selectSlotCurCooldown <= 0.0f)
                        {
                            Vector2 stickDir = playerComp.Movement.GetDirection();

                            invenComp.selectSlotCurCooldown = invenComp.SelectSlotDelay;
                            if (Math.Abs(stickDir.X) > 0.5f || Math.Abs(stickDir.Y) > 0.5f)
                            {
                                //if the stick has been pushed in a direction
                                Point direction = MoveSystem.CalcDirection(stickDir.Y, stickDir.X);
                                Point nextSlot  = invenComp.SelectedSlot + direction;

                                UpdateNextSelectedPos(ref nextSlot, invenComp.SelectedSlot);

                                if (UpdateInventoryFocus(invenComp, nextSlot))
                                {
                                    invenComp.SelectedSlot          = nextSlot;
                                    invenComp.selectSlotCurCooldown = invenComp.SelectSlotDelay;
                                }
                            }
                            //Selecting slot
                            else if (playerComp.Interact.IsButtonDown())
                            {
                                //calculate the location of the selected slot in the items array
                                int selectedArraySlot = invenComp.SelectedSlot.Y + (invenComp.ColumnsRows.X) * invenComp.SelectedSlot.X;
                                //if no item is held
                                if (invenComp.HeldItem == 0)
                                {
                                    if (invenComp.LocationInInventory == LocationInInventory.Equipment)
                                    {
                                        //Unequip equipment
                                        int equipPos = Math.Abs(invenComp.SelectedSlot.X) - 1;
                                        if (AddItemToInventory(entity.Key, invenComp.WeaponBodyHead[equipPos]))
                                        {
                                            UnEquipItemVisually(invenComp.WeaponBodyHead[equipPos], cm);
                                            invenComp.WeaponBodyHead[equipPos] = 0;
                                        }
                                    }
                                    else if (invenComp.LocationInInventory == LocationInInventory.Bagspace)
                                    {
                                        //Picked an item to hold
                                        invenComp.HeldItem = invenComp.Items[selectedArraySlot];
                                    }
                                    else if (invenComp.LocationInInventory == LocationInInventory.Stats)
                                    {
                                        StatsComponent statComp = cm.GetComponentForEntity <StatsComponent>(entity.Key);
                                        if (statComp.SpendableStats > 0)
                                        {
                                            //Increase the selected stat
                                            if (invenComp.SelectedSlot.X == -1)
                                            {
                                                //increase int
                                                statComp.AddInt += 1;
                                            }
                                            else if (invenComp.SelectedSlot.X == -2)
                                            {
                                                //increase stamina
                                                statComp.AddSta += 1;
                                            }
                                            else if (invenComp.SelectedSlot.X == -3)
                                            {
                                                //increase agility
                                                statComp.AddAgi += 1;
                                            }
                                            else if (invenComp.SelectedSlot.X == -4)
                                            {
                                                //increase strength
                                                statComp.AddStr += 1;
                                            }
                                            statComp.SpendableStats--;
                                        }
                                    }
                                    else if (invenComp.LocationInInventory == LocationInInventory.Skills)
                                    {
                                        StatsComponent statComp = cm.GetComponentForEntity <StatsComponent>(entity.Key);
                                        //Choose the skill selected if it has not already been picked and prerequisite requirements have been met
                                        if (ChooseAvailableSkill(ref invenComp, GetSelectedSkillSlot(invenComp.SelectedSlot.X, invenComp.SelectedSlot.Y)) &&
                                            statComp.SpendableStats >= 5)
                                        {
                                            statComp.SpendableStats -= 5;
                                        }
                                    }
                                }
                                else
                                {
                                    //if we do have a held item
                                    ItemComponent heldItemComp = cm.GetComponentForEntity <ItemComponent>(invenComp.HeldItem);
                                    if (invenComp.LocationInInventory == LocationInInventory.Equipment)
                                    {
                                        //if our currently selected slot is in one of the equipment slots
                                        int equipPos = Math.Abs(invenComp.SelectedSlot.X) - 1;
                                        if ((int)heldItemComp.Type == equipPos)
                                        {
                                            int equipToSwap = 0;
                                            if (invenComp.WeaponBodyHead[equipPos] != 0)
                                            {
                                                //if there is an item in the selected slot. Swap locations of the items
                                                equipToSwap = invenComp.WeaponBodyHead[equipPos];
                                                cm.GetComponentForEntity <ItemComponent>(equipToSwap).InventoryPosition = heldItemComp.InventoryPosition;
                                                UnEquipItemVisually(equipToSwap, cm);
                                                invenComp.AmountOfItems++;
                                            }
                                            invenComp.WeaponBodyHead[equipPos] = invenComp.HeldItem;
                                            invenComp.Items[heldItemComp.InventoryPosition] = equipToSwap;
                                            heldItemComp.InventoryPosition = -equipPos;
                                            invenComp.AmountOfItems--;
                                        }
                                    }
                                    else if (invenComp.LocationInInventory == LocationInInventory.Bagspace)
                                    {
                                        int itemToSwap = 0;
                                        if (invenComp.Items[selectedArraySlot] != 0)
                                        {
                                            //Swap item locations
                                            itemToSwap = invenComp.Items[selectedArraySlot];
                                            invenComp.Items[heldItemComp.InventoryPosition] = itemToSwap;
                                            cm.GetComponentForEntity <ItemComponent>(itemToSwap).InventoryPosition = heldItemComp.InventoryPosition;
                                        }
                                        invenComp.Items[selectedArraySlot] = invenComp.HeldItem;
                                        invenComp.Items[heldItemComp.InventoryPosition] = itemToSwap;
                                        heldItemComp.InventoryPosition = selectedArraySlot;
                                    }
                                    invenComp.HeldItem = 0; // no matter what action was taken, the held item should be deselected so we can choose a new one in the future
                                }
                                UpdateActualEquippedItems(ref invenComp, ref cm, entity.Key);
                            }
                            //Quick equip
                            else if (playerComp.Attack.IsButtonDown())
                            {
                                if (invenComp.LocationInInventory == LocationInInventory.Bagspace)
                                {
                                    int           selectedArraySlot = invenComp.SelectedSlot.Y + (invenComp.ColumnsRows.X) * invenComp.SelectedSlot.X;
                                    ItemComponent selectedItemComp  = cm.GetComponentForEntity <ItemComponent>(invenComp.Items[selectedArraySlot]);

                                    if (selectedItemComp != null)
                                    {
                                        if ((int)selectedItemComp.Type <= 2)
                                        {
                                            if (invenComp.HeldItem != 0)
                                            {
                                                invenComp.HeldItem = 0;
                                            }
                                            if (invenComp.WeaponBodyHead[(int)selectedItemComp.Type] == 0)
                                            {
                                                //Equip the item
                                                invenComp.WeaponBodyHead[(int)selectedItemComp.Type] = invenComp.Items[selectedArraySlot];
                                                selectedItemComp.InventoryPosition = -(int)selectedItemComp.Type - 1;
                                                invenComp.Items[selectedArraySlot] = 0;
                                                invenComp.AmountOfItems--;
                                            }
                                            else
                                            {
                                                //The spot is occupied and will be swapped
                                                int itemToMove = invenComp.WeaponBodyHead[(int)selectedItemComp.Type];
                                                invenComp.WeaponBodyHead[(int)selectedItemComp.Type] = invenComp.Items[selectedArraySlot];
                                                invenComp.Items[selectedArraySlot] = itemToMove;

                                                cm.GetComponentForEntity <ItemComponent>(itemToMove).InventoryPosition = selectedItemComp.InventoryPosition;
                                                selectedItemComp.InventoryPosition = -(int)selectedItemComp.Type - 1;
                                                UnEquipItemVisually(itemToMove, cm);
                                            }
                                        }
                                        else if (selectedItemComp.Type == ItemType.Consumable)
                                        {
                                            selectedItemComp.Use(entity.Key, selectedItemComp.InventoryPosition);
                                            invenComp.AmountOfItems--;
                                        }
                                    }
                                }
                                UpdateActualEquippedItems(ref invenComp, ref cm, entity.Key);
                            }
                            //drop selected item
                            else if (playerComp.Back.IsButtonDown())
                            {
                                if (invenComp.LocationInInventory == LocationInInventory.Bagspace)
                                {
                                    int           selectedArraySlot = invenComp.SelectedSlot.Y + (invenComp.ColumnsRows.X) * invenComp.SelectedSlot.X;
                                    ItemComponent selectedItemComp  = cm.GetComponentForEntity <ItemComponent>(invenComp.Items[selectedArraySlot]);

                                    if (selectedItemComp != null)
                                    {
                                        PositionComponent playerPosComp = cm.GetComponentForEntity <PositionComponent>(entity.Key);
                                        cm.AddComponentsToEntity(invenComp.Items[selectedArraySlot], new IComponent[] {
                                            new PositionComponent(playerPosComp.Position),
                                            new InteractComponent(InteractType.Loot)
                                        });
                                        invenComp.Items[selectedArraySlot] = 0;
                                        invenComp.AmountOfItems--;
                                    }
                                }
                            }
                            else if (cm.HasEntityComponent <ActionBarComponent>(entity.Key))
                            {
                                ActionBarComponent actionBComp = cm.GetComponentForEntity <ActionBarComponent>(entity.Key);
                                if (playerComp.ActionBar1.IsButtonDown())
                                {
                                    BindToActionBar(ref invenComp, ref actionBComp, ref cm, 0);
                                }
                                else if (playerComp.ActionBar2.IsButtonDown())
                                {
                                    BindToActionBar(ref invenComp, ref actionBComp, ref cm, 1);
                                }
                                else if (playerComp.ActionBar3.IsButtonDown())
                                {
                                    BindToActionBar(ref invenComp, ref actionBComp, ref cm, 2);
                                }
                                else if (playerComp.ActionBar4.IsButtonDown())
                                {
                                    BindToActionBar(ref invenComp, ref actionBComp, ref cm, 3);
                                }
                                else
                                {
                                    invenComp.selectSlotCurCooldown = 0.0f;
                                }
                            }
                            else
                            {
                                invenComp.selectSlotCurCooldown = 0.0f;
                            }
                        }
                        else
                        {
                            invenComp.selectSlotCurCooldown -= (float)gameTime.ElapsedGameTime.TotalSeconds;
                        }
                    }
                }
            }
        }
Exemple #2
0
        public void Update(GameTime gameTime)
        {
            ComponentManager cm = ComponentManager.GetInstance();
            List <Tuple <int, PositionComponent> > players = new List <Tuple <int, PositionComponent> >();

            foreach (var player in cm.GetComponentsOfType <PlayerComponent>())
            {
                players.Add(new Tuple <int, PositionComponent>(player.Key, cm.GetComponentForEntity <PositionComponent>(player.Key)));
            }

            foreach (var entity in cm.GetComponentsOfType <AttackComponent>())
            {
                if (cm.HasEntityComponent <AIComponent>(entity.Key) && cm.HasEntityComponent <PositionComponent>(entity.Key))
                {
                    AIComponent       ai      = cm.GetComponentForEntity <AIComponent>(entity.Key);
                    PositionComponent posComp = cm.GetComponentForEntity <PositionComponent>(entity.Key);

                    //find closest target
                    int   closestEntity = 0;
                    float closestDist   = float.MaxValue;

                    for (int i = 0; i < players.Count; i++)
                    {
                        float dist = Vector2.Distance(players[i].Item2.Position, posComp.Position);
                        if (dist < ai.DetectRange && dist < closestDist)
                        {
                            closestEntity = players[i].Item1;
                            closestDist   = dist;
                        }
                    }

                    if (closestEntity != 0)
                    {
                        ai.TargetEntity = closestEntity;
                    }
                    else
                    {
                        ai.TargetEntity = 0;
                    }

                    // Do some check here: cm.HasEntity....<movecomp> / <attackcomp> ?
                    MoveComponent   moveComp        = cm.GetComponentForEntity <MoveComponent>(entity.Key);
                    AttackComponent attackComponent = cm.GetComponentForEntity <AttackComponent>(entity.Key);
                    if (ai.TargetEntity != 0)
                    {
                        Vector2            pointToTarget, pointToTCompare;
                        Point              nextDir;
                        PositionComponent  posOftarget     = cm.GetComponentForEntity <PositionComponent>(ai.TargetEntity);
                        CollisionComponent collComp        = cm.GetComponentForEntity <CollisionComponent>(entity.Key);
                        Vector2            unNormalizedDir = new Vector2(posOftarget.Position.X - posComp.Position.X, posOftarget.Position.Y - posComp.Position.Y);
                        float              distance        = (float)Math.Sqrt(unNormalizedDir.X * unNormalizedDir.X + unNormalizedDir.Y * unNormalizedDir.Y);
                        Vector2            direction       = new Vector2(unNormalizedDir.X / distance, unNormalizedDir.Y / distance);

                        nextDir         = MoveSystem.CalcDirection(direction.X, direction.Y);
                        pointToTarget   = posOftarget.Position + new Vector2(-nextDir.X * (collComp.CollisionBox.Width), -nextDir.Y * (collComp.CollisionBox.Height));
                        pointToTCompare = posComp.Position + new Vector2(nextDir.X * (collComp.CollisionBox.Width), nextDir.Y * (collComp.CollisionBox.Height));
                        ai.Destination  = pointToTarget.ToPoint();

                        if (attackComponent.AttackCooldown <= 0.0f &&
                            Vector2.Distance(posComp.Position, pointToTarget) <= (collComp.CollisionBox.Width * Math.Abs(nextDir.X) + collComp.CollisionBox.Height * Math.Abs(nextDir.Y)) / 2 &&
                            !cm.GetComponentForEntity <KnockbackComponent>(entity.Key).KnockbackActive)
                        {
                            moveComp.Direction             = nextDir;
                            moveComp.CanMove               = false;
                            attackComponent.AttackCooldown = attackComponent.RateOfFire;
                            attackComponent.IsAttacking    = true;
                            cm.GetComponentForEntity <SoundComponent>(entity.Key).Sounds["Attack"].Action = SoundAction.Play;
                            if (cm.HasEntityComponent <AnimationGroupComponent>(entity.Key))
                            {
                                AnimationGroupComponent animGroupComp = cm.GetComponentForEntity <AnimationGroupComponent>(entity.Key);
                                int anim = GetAnimationRow(moveComp.Direction) + 8;
                                if (animGroupComp.ActiveAnimation != anim)
                                {
                                    animGroupComp.ActiveAnimation = anim;
                                }
                            }
                        }
                    }
                    if (attackComponent.AttackCooldown > 0.0f)
                    {
                        attackComponent.AttackCooldown -= (float)gameTime.ElapsedGameTime.TotalSeconds;
                    }
                    else
                    {
                        moveComp.CanMove = true;
                    }
                }
            }
        }
Exemple #3
0
        public void Update(GameTime gameTime)
        {
            ComponentManager cm = ComponentManager.GetInstance();
            GameStateManager gm = GameStateManager.GetInstance();

            ActivateMenuButtons();
            ActivateMenuBackground();
            ClearMenu();
            DecrementSelectCooldown = true;
            exitPause = false;
            bool enterPause = false;

            foreach (var controlEntity in cm.GetComponentsOfType <PlayerControlComponent>())
            {
                PlayerControlComponent controlComp = (PlayerControlComponent)controlEntity.Value;

                // If we are in some kind of menu state
                if (GameStateManager.GetInstance().State == GameState.Menu)
                {
                    // Apply effects on menu background
                    foreach (var menuBackground in cm.GetComponentsOfType <MenuBackgroundComponent>())
                    {
                        MenuBackgroundComponent men = (MenuBackgroundComponent)menuBackground.Value;

                        if (men.HasFadingEffect && men.IsActive)
                        {
                            FadeEffect(gameTime, men);
                        }
                        if (men.HasMovingEffect && men.IsActive)
                        {
                            MoveEffect(gameTime, men);
                        }
                    }
                    if (DecrementSelectCooldown)
                    {
                        SelectCooldown         -= (float)gameTime.ElapsedGameTime.TotalSeconds;
                        DecrementSelectCooldown = false;
                    }

                    // Makes the menu button selection smooth
                    if (SelectCooldown <= 0.0f)
                    {
                        Vector2 stickDir = controlComp.Movement.GetDirection();
                        //Check navigation in the menu
                        if (Math.Abs(stickDir.Y) > 0.5f)
                        {
                            //if the stick has been pushed in a direction
                            Point direction = MoveSystem.CalcDirection(stickDir.X, stickDir.Y);
                            cm.GetComponentForEntity <MenuButtonComponent>(ActiveButtonsList[SelectedButton]).Ishighlighted = false;
                            //stop sound for last button
                            if (cm.HasEntityComponent <SoundComponent>(ActiveButtonsList[SelectedButton]))
                            {
                                cm.GetComponentForEntity <SoundComponent>(ActiveButtonsList[SelectedButton]).Sounds["Selected"].Action = SoundAction.Stop;
                            }
                            SelectedButton = (SelectedButton + direction.Y) % ActiveButtonsList.Count;
                            if (SelectedButton < 0)
                            {
                                SelectedButton = ActiveButtonsList.Count - 1;
                            }
                            cm.GetComponentForEntity <MenuButtonComponent>(ActiveButtonsList[SelectedButton]).Ishighlighted = true;
                            //start sound for new button selected
                            if (cm.HasEntityComponent <SoundComponent>(ActiveButtonsList[SelectedButton]))
                            {
                                cm.GetComponentForEntity <SoundComponent>(ActiveButtonsList[SelectedButton]).Sounds["Selected"].Action = SoundAction.Play;
                            }
                            SelectCooldown = MaxSelectCooldown;
                        }
                    }

                    // Check if highlighted button was pressed "use"
                    if (controlComp.Interact.IsButtonDown())
                    {
                        if (cm.HasEntityComponent <SoundComponent>(ActiveButtonsList[SelectedButton]))
                        {
                            cm.GetComponentForEntity <SoundComponent>(ActiveButtonsList[SelectedButton]).Sounds["Pressed"].Action = SoundAction.Play;
                        }
                        cm.GetComponentForEntity <MenuButtonComponent>(ActiveButtonsList[SelectedButton]).Use();
                    }

                    // 1 Player
                    if (gm.State == GameState.OnePlayerGame)
                    {
                        //remove the temporary menu controller
                        cm.RemoveEntity(controlEntity.Key);
                        cm.AddEntityWithComponents(factory.CreatePlayerOne(100, 128));
                        MenuStateManager.GetInstance().State = MenuState.None;
                        gm.State = GameState.Restart;
                        break;
                    }
                    // 2 Players
                    if (gm.State == GameState.TwoPlayerGame)
                    {
                        //remove the temporary menu controller
                        cm.RemoveEntity(controlEntity.Key);
                        cm.AddEntityWithComponents(factory.CreatePlayerOne(100, 128));
                        cm.AddEntityWithComponents(factory.CreatePlayerTwo(256, 128));
                        MenuStateManager.GetInstance().State = MenuState.None;
                        gm.State = GameState.Restart;
                        break;
                    }
                }
                if (controlComp.Menu.IsButtonDown())
                {
                    //Debug.WriteLine(gm.State + " c " + gm.LastState);
                    if (MenuStateManager.GetInstance().State == MenuState.PauseMainMenu && gm.State == GameState.Menu)
                    {
                        //// Exit the Pausemenu if menu button is pressed from GameState "Menu"
                        Debug.WriteLine(gm.LastState + " a " + gm.State);
                        gm.State = gm.LastState;
                        MenuStateManager.GetInstance().State = MenuState.None;
                    }
                    else if ((gm.State == GameState.Game || gm.State == GameState.GameOver) && gm.LastState != GameState.Menu)
                    {
                        // Enter the PauseMenu if menu button is pressed from GameState "Game"
                        Debug.WriteLine(gm.LastState + " b " + gm.State);
                        gm.State = GameState.Menu;
                        MenuStateManager.GetInstance().State = MenuState.PauseMainMenu;
                    }
                    else
                    {
                        gm.LastState = gm.State;
                    }
                }

                if (gm.State == GameState.ExitToMenu)
                {
                    ActiveButtonsList.Clear();
                    SelectedButton = 0;
                    MenuStateManager.GetInstance().State = MenuState.MainMenu;
                }
            }
        }