示例#1
0
        void UpdateEquipmentPosition(ComponentManager cm, int equipmentID, Vector2 playerPos)
        {
            if (equipmentID != 0)
            {
                if (!cm.HasEntityComponent <PositionComponent>(equipmentID))
                {
                    cm.AddComponentsToEntity(equipmentID, new PositionComponent());
                }
                else
                {
                    PositionComponent posComp = cm.GetComponentForEntity <PositionComponent>(equipmentID);

                    if (posComp.Position != playerPos)
                    {
                        posComp.Position = playerPos;
                    }
                }
            }
        }
示例#2
0
        private void SpawnEnemy(EnemySpawnComponent c, int index)
        {
            ComponentManager cm = ComponentManager.GetInstance();
            Random           rand = new Random();
            int x, y;
            PositionComponent p        = new PositionComponent();
            List <IComponent> template = new List <IComponent>(c.EnemyTemplate);
            int    entity;
            Random r = new Random();
            int    shouldEnemyHaveItems = r.Next(0, 3);



            for (int i = 0; i < template.Count; i++)
            {
                template[i] = (IComponent)template[i].Clone();
            }

            for (int i = 0; i < MaxSpawnTries; i++)
            {
                x = rand.Next() % c.SpawnMaxRadius;
                y = rand.Next() % c.SpawnMaxRadius;

                if (c.CollisionComponent == null)
                {
                    p.Position = new Vector2(x, y) + c.SpawnLocation.ToVector2();
                    template.Add(p);
                    entity = EntityManager.GetEntityId();
                    cm.AddComponentsToEntity(entity, template.ToArray());
                    c.EnemyEntities[index] = entity;

                    if (shouldEnemyHaveItems > 1)
                    {
                        cm.AddComponentsToEntity(entity, new IComponent[] {
                            new InteractComponent(InteractType.Loot),
                            new ItemComponent(AddHealth, "Bread",
                                              ItemType.Consumable),
                        });
                    }
                    break;
                }
                else
                {
                    Rectangle bb = c.CollisionComponent.CollisionBox;
                    bb.Location  = c.SpawnLocation;
                    bb.Location += new Point(x, y);
                    if (CollisionSystem.DetectAreaCollision(bb).Count == 0)
                    {
                        p.Position = new Vector2(x, y) + c.SpawnLocation.ToVector2();
                        template.Add(p);
                        entity = EntityManager.GetEntityId();
                        cm.AddComponentsToEntity(entity, template.ToArray());
                        c.EnemyEntities[index] = entity;

                        if (shouldEnemyHaveItems > 1)
                        {
                            cm.AddComponentsToEntity(entity, new IComponent[] {
                                new InteractComponent(InteractType.Loot),
                                new ItemComponent(AddHealth, "Bread",
                                                  ItemType.Consumable),
                            });
                        }
                        break;
                    }
                }
            }
        }
示例#3
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;
                        }
                    }
                }
            }
        }