Ejemplo n.º 1
0
        public void Update(GameTime gameTime)
        {
            ComponentManager cm = ComponentManager.GetInstance();

            foreach (var entity in cm.GetComponentsOfType <PlayerControlComponent>())
            {
                PlayerControlComponent playerControlComponent = (PlayerControlComponent)entity.Value;
                MoveComponent          moveComponent          = cm.GetComponentForEntity <MoveComponent>(entity.Key);
                if (moveComponent.CanMove)
                {
                    moveComponent.Velocity = playerControlComponent.Movement.GetDirection() * moveComponent.Speed;
                    if (playerControlComponent.Movement.GetDirection() != new Vector2(0.0f, 0.0f))
                    {
                        if (cm.HasEntityComponent <AnimationGroupComponent>(entity.Key))
                        {
                            AnimationGroupComponent animGroupComp = cm.GetComponentForEntity <AnimationGroupComponent>(entity.Key);
                        }
                        if (cm.HasEntityComponent <SoundComponent>(entity.Key))
                        {
                            cm.GetComponentForEntity <SoundComponent>(entity.Key).Sounds["Walk"].Action = SoundAction.Play;
                        }
                    }
                    else
                    {
                        cm.GetComponentForEntity <SoundComponent>(entity.Key).Sounds["Walk"].Action = SoundAction.Pause;
                    }
                }
                else
                {
                    cm.GetComponentForEntity <SoundComponent>(entity.Key).Sounds["Walk"].Action = SoundAction.Pause;
                }
            }
        }
Ejemplo n.º 2
0
 private void LoadDependencies()
 {
     this.AC  = Player.Character.GetComponent <AttributesComponent>();
     this.EC  = Player.Character.GetComponent <EquipmentComponent>();
     this.PCC = Player.Character.GetComponent <PlayerControlComponent>();
     this.SC  = Player.Character.GetComponent <ScoreComponent>();
 }
Ejemplo n.º 3
0
        public void Update(GameTime gameTime)
        {
            ComponentManager cm = ComponentManager.GetInstance();

            foreach (var entity in cm.GetComponentsOfType <PlayerControlComponent>())
            {
                PlayerControlComponent playerControl = (PlayerControlComponent)entity.Value;
                if (!cm.GetComponentForEntity <InventoryComponent>(entity.Key).IsOpen&& cm.HasEntityComponent <ActionBarComponent>(entity.Key))
                {
                    ActionBarComponent actionbarComp = cm.GetComponentForEntity <ActionBarComponent>(entity.Key);
                    if (playerControl.ActionBar1.IsButtonDown() && actionbarComp.Slots[0] != null)
                    {
                        if (actionbarComp.Slots[0].IsItem)
                        {
                            actionbarComp.Slots[0].Use(entity.Key, ((ItemComponent)actionbarComp.Slots[0]).InventoryPosition);
                        }
                        else
                        {
                            ((SkillComponent)actionbarComp.Slots[0]).UsingEntities.Add(entity.Key);
                        }
                    }
                    else if (playerControl.ActionBar2.IsButtonDown() && actionbarComp.Slots[1] != null)
                    {
                        if (actionbarComp.Slots[1].IsItem)
                        {
                            actionbarComp.Slots[1].Use(entity.Key, ((ItemComponent)actionbarComp.Slots[1]).InventoryPosition);
                        }
                        else
                        {
                            ((SkillComponent)actionbarComp.Slots[1]).UsingEntities.Add(entity.Key);
                        }
                    }
                    else if (playerControl.ActionBar3.IsButtonDown() && actionbarComp.Slots[2] != null)
                    {
                        if (actionbarComp.Slots[2].IsItem)
                        {
                            actionbarComp.Slots[2].Use(entity.Key, ((ItemComponent)actionbarComp.Slots[2]).InventoryPosition);
                        }
                        else
                        {
                            ((SkillComponent)actionbarComp.Slots[2]).UsingEntities.Add(entity.Key);
                        }
                    }
                    else if (playerControl.ActionBar4.IsButtonDown() && actionbarComp.Slots[3] != null)
                    {
                        if (actionbarComp.Slots[3].IsItem)
                        {
                            actionbarComp.Slots[3].Use(entity.Key, ((ItemComponent)actionbarComp.Slots[3]).InventoryPosition);
                        }
                        else
                        {
                            ((SkillComponent)actionbarComp.Slots[3]).UsingEntities.Add(entity.Key);
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
    // Start is called before the first frame update
    void Start()
    {
        List <Player> players = PlayerManager.GetPlayers();
        int           index   = 0;

        foreach (Player p in players)
        {
            GameObject g = Instantiate(PlayerPrefab);
            Players.Add(g);
            g.SetActive(true);

            Transform spawn = null;
            // If spawn positions are specified, use them.
            if (index < SpawnPositions.Length)
            {
                spawn = SpawnPositions[index];

                // If you've specified some spawn positions, but not enough, we'll use the last in the list but also complain.
            }
            else if (SpawnPositions.Length > 0)
            {
                spawn = SpawnPositions[SpawnPositions.Length - 1];
                Debug.LogError("Ran out of spawn positions for the number of players, using the last one.");
            }

            g.transform.position = spawn.position;
            g.transform.rotation = spawn.rotation;

            // If a camera prefab is set, register it in the CameraManager
            if (CameraPrefab != null)
            {
                var cameraGameobject = Instantiate(CameraPrefab.gameObject, CameraPrefab.transform.position, CameraPrefab.transform.rotation);
                var cameraController = CameraManager.Instance.SetPlayerCamera(cameraGameobject.GetComponent <Camera>(), index);
                cameraController.SetFocus(g);
            }

            g.name = "Player " + p.id;

            // Access the PlayerControlComponent and tell it which Player to get inputs from.
            PlayerControlComponent control = g.GetComponent <PlayerControlComponent>();
            if (control != null)
            {
                control.SetPlayer(p);
            }
            else
            {
                Debug.LogError("PlayerPrefab instantiated by PlayerControlSetup needs a PlayerControlComponent script attached!");
            }

            index++;
        }
    }
Ejemplo n.º 5
0
        public override bool ProcessKeyboard(KeyboardInfo info)
        {
            Point newPosition = Point.Zero;

            if (info.IsKeyReleased(Keys.Up))
            {
                PlayerControlComponent p = ComponentManager.GetComponent <PlayerControlComponent>(0);
                p.SetAction(new MoveAction(EntityManager.Player, new Point(0, -1), 10));
            }
            else if (info.IsKeyReleased(Keys.Down))
            {
                PlayerControlComponent p = ComponentManager.GetComponent <PlayerControlComponent>(0);
                p.SetAction(new MoveAction(EntityManager.Player, new Point(0, 1), 10));
            }
            else if (info.IsKeyReleased(Keys.Left))
            {
                PlayerControlComponent p = ComponentManager.GetComponent <PlayerControlComponent>(0);
                p.SetAction(new MoveAction(EntityManager.Player, new Point(-1, 0), 10));
            }
            else if (info.IsKeyReleased(Keys.Right))
            {
                PlayerControlComponent p = ComponentManager.GetComponent <PlayerControlComponent>(EntityManager.Player);
                p.SetAction(new MoveAction(EntityManager.Player, new Point(1, 0), 10));
            }
            else if (info.IsKeyReleased(Keys.C))
            {
                PlayerControlComponent p = ComponentManager.GetComponent <PlayerControlComponent>(EntityManager.Player);
                p.SetAction(new CloseAction(EntityManager.Player));
            }
            //else if ( info.IsKeyReleased( Keys.Left ) )
            //{
            //	newPosition.X -= 1;
            //	keyHit = true;
            //}
            //else if ( info.IsKeyReleased( Keys.Right ) )
            //{
            //	newPosition.X += 1;
            //	keyHit = true;
            //}


            //// Test location
            //if ( keyHit )
            //{

            //	Map.MoveActor( GameConstants.player, newPosition );
            //	//UpdatePlayerView();

            //}
            return(false);
        }
Ejemplo n.º 6
0
 public static int CreatePlayer()
 {
     if (playerCreated == false)
     {
         //Player = CreateEntity(); //Create Player
         playerCreated = true;
         int Player = 0;
         _entities[0] = Player;
         CellAppearance  ca = new CellAppearance(Colors.Player, Colors.FloorBackgroundFov, 64);
         RenderComponent rc = new RenderComponent(Player, ca, 2);
         ComponentManager.AddComponent(Player, rc);
         PositionComponent pc = new PositionComponent(Player, new Point(0, 0));
         ComponentManager.AddComponent(Player, pc);
         PlayerControlComponent pcc = new PlayerControlComponent(Player);
         ComponentManager.AddComponent(Player, pcc);
     }
     return(0);
 }
Ejemplo n.º 7
0
        public void Update(GameTime gameTime)
        {
            ComponentManager cm = ComponentManager.GetInstance();

            foreach (var entity in cm.GetComponentsOfType <PlayerControlComponent>())
            {
                if (cm.HasEntityComponent <AttackComponent>(entity.Key))
                {
                    AttackComponent attackComponent = cm.GetComponentForEntity <AttackComponent>(entity.Key);
                    if (attackComponent.Type != WeaponType.None)
                    {
                        PlayerControlComponent playerControl = (PlayerControlComponent)entity.Value;
                        if (cm.HasEntityComponent <MoveComponent>(entity.Key))
                        {
                            MoveComponent moveComponent = cm.GetComponentForEntity <MoveComponent>(entity.Key);
                            if (attackComponent.CanAttack)
                            {
                                if (playerControl.Attack.IsButtonDown())
                                {
                                    if (attackComponent.AttackCooldown <= 0.0f)
                                    {
                                        moveComponent.CanMove  = false;
                                        moveComponent.Velocity = new Vector2(0, 0);
                                        cm.GetComponentForEntity <SoundComponent>(entity.Key).Sounds["Attack"].Action = SoundAction.Play;
                                        attackComponent.AttackCooldown = attackComponent.RateOfFire;
                                        attackComponent.IsAttacking    = true;
                                    }
                                }
                                if (attackComponent.AttackCooldown <= 0.0f)
                                {
                                    moveComponent.CanMove = true;
                                }
                            }
                            if (attackComponent.AttackCooldown > 0.0f)
                            {
                                attackComponent.AttackCooldown -= (float)gameTime.ElapsedGameTime.TotalSeconds;
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 8
0
        public void Update(GameTime gameTime)
        {
            ComponentManager cm = ComponentManager.GetInstance();

            foreach (var player in players)
            {
                PlayerComponent        playerComponent         = player.Item1;
                PlayerControlComponent controlComponent        = player.Item2;
                PositionComponent      playerPositionComponent = player.Item3;

                int closestInteractable = FindClosestInteractable(player.Entity, playerPositionComponent.Position);

                if (closestInteractable != -1)
                {
                    if (controlComponent.Interact.IsButtonDown() && !cm.GetComponentForEntity <InventoryComponent>(player.Entity).IsOpen)
                    {
                        InteractComponent interComp = cm.GetComponentForEntity <InteractComponent>(closestInteractable);

                        // Its a trap(Deal damage)
                        if (interComp.Type == InteractType.Trap && cm.HasEntityComponent <AttackComponent>(closestInteractable))
                        {
                            cm.GetComponentForEntity <HealthComponent>(player.Entity).IncomingDamage.Add(closestInteractable);
                        }

                        //Talk(show text)
                        else if (interComp.Type == InteractType.Talk && cm.HasEntityComponent <TextComponent>(closestInteractable))
                        {
                            foreach (var inter in cm.GetComponentsOfType <InteractComponent>())
                            {
                                if (cm.HasEntityComponent <TextComponent>(inter.Key))
                                {
                                    cm.GetComponentForEntity <TextComponent>(inter.Key).IsActive = false;
                                }
                            }
                            cm.GetComponentForEntity <TextComponent>(closestInteractable).IsActive = true;
                        }
                        //Loot(Give item to looter)
                        else if (interComp.Type == InteractType.Loot &&
                                 cm.HasEntityComponent <InventoryComponent>(player.Entity) &&
                                 cm.HasEntityComponent <ItemComponent>(closestInteractable))
                        {
                            if (cm.HasEntityComponent <HealthComponent>(closestInteractable) && cm.GetComponentForEntity <HealthComponent>(closestInteractable).IsAlive)
                            {
                                break;
                            }
                            InventoryComponent invenComp = cm.GetComponentForEntity <InventoryComponent>(player.Entity);
                            if (invenComp.AmountOfItems < invenComp.Items.Length)
                            {
                                //Remove components
                                cm.RemoveComponentFromEntity <InteractComponent>(closestInteractable);
                                cm.RemoveComponentFromEntity <CollisionComponent>(closestInteractable);
                                cm.RemoveComponentFromEntity <PositionComponent>(closestInteractable);
                                //Give the item to the player
                                cm.AddEntityWithComponents(new IComponent[] {
                                    (ItemComponent)cm.GetComponentForEntity <ItemComponent>(closestInteractable).Clone(),
                                });
                                invenComp.ItemsToAdd.Add(closestInteractable);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 9
0
 public void SetFollowCamera(PlayerControlComponent player)
 {
     followCamera.target = player.transform;
 }
Ejemplo n.º 10
0
 public void SetAttackBtn(PlayerControlComponent player)
 {
     attackBtn.onClick.AddListener(player.Attack);
 }
Ejemplo n.º 11
0
 public void SetMoveJoystick(PlayerControlComponent player)
 {
     move_Joystick.onMoveStart.AddListener(player.MoveStart);
     move_Joystick.onMove.AddListener(player.Move);
     move_Joystick.onMoveEnd.AddListener(player.MoveEnd);
 }
Ejemplo n.º 12
0
        public void Update(GameTime gameTime)
        {
            ComponentManager cm = ComponentManager.GetInstance();

            foreach (var entity in cm.GetComponentsOfType <PlayerControlComponent>())
            {
                PlayerControlComponent playerControl = (PlayerControlComponent)entity.Value;
                GamePadState           gamepad;
                switch (playerControl.ControllerType)
                {
                case ControllerType.Keyboard:
                    // Movement
                    KeyboardState keyboard = Keyboard.GetState();
                    Vector2       dir      = new Vector2();
                    if (keyboard.IsKeyDown(Keys.W))
                    {
                        if (keyboard.IsKeyDown(Keys.D))
                        {
                            dir = new Vector2(0.7071f, -0.7071f);
                        }
                        else if (keyboard.IsKeyDown(Keys.A))
                        {
                            dir = new Vector2(-0.7071f, -0.7071f);
                        }
                        else
                        {
                            dir = new Vector2(0.0f, -1.0f);
                        }
                    }
                    else if (keyboard.IsKeyDown(Keys.S))
                    {
                        if (keyboard.IsKeyDown(Keys.D))
                        {
                            dir = new Vector2(0.7071f, 0.7071f);
                        }
                        else if (keyboard.IsKeyDown(Keys.A))
                        {
                            dir = new Vector2(-0.7071f, 0.7071f);
                        }
                        else
                        {
                            dir = new Vector2(0.0f, 1.0f);
                        }
                    }
                    else if (keyboard.IsKeyDown(Keys.A))
                    {
                        dir = new Vector2(-1.0f, 0.0f);
                    }
                    else if (keyboard.IsKeyDown(Keys.D))
                    {
                        dir = new Vector2(1.0f, 0.0f);
                    }
                    else
                    {
                        dir = new Vector2(0.0f, 0.0f);
                    }
                    playerControl.Movement.SetDirection(dir);

                    MouseState mouse = Mouse.GetState();
                    // Attack
                    if (keyboard.IsKeyDown(Keys.Space) && previousKeyboardState.IsKeyUp(Keys.Space))
                    {
                        playerControl.Attack.SetButton(true);
                    }
                    else
                    {
                        playerControl.Attack.SetButton(false);
                    }
                    // Interact
                    if (keyboard.IsKeyDown(Keys.E) && previousKeyboardState.IsKeyUp(Keys.E))
                    {
                        playerControl.Interact.SetButton(true);
                    }
                    else
                    {
                        playerControl.Interact.SetButton(false);
                    }
                    // Menu
                    if (keyboard.IsKeyDown(Keys.Escape) && previousKeyboardState.IsKeyUp(Keys.Escape))
                    {
                        playerControl.Menu.SetButton(true);
                    }
                    else
                    {
                        playerControl.Menu.SetButton(false);
                    }
                    // Inventory
                    if (keyboard.IsKeyDown(Keys.C) && previousKeyboardState.IsKeyUp(Keys.C))
                    {
                        playerControl.Inventory.SetButton(true);
                    }
                    else
                    {
                        playerControl.Inventory.SetButton(false);
                    }
                    // Back
                    if (keyboard.IsKeyDown(Keys.Q) && previousKeyboardState.IsKeyUp(Keys.Q))
                    {
                        playerControl.Back.SetButton(true);
                    }
                    else
                    {
                        playerControl.Back.SetButton(false);
                    }
                    // Actionbar 1
                    if (keyboard.IsKeyDown(Keys.D1) && previousKeyboardState.IsKeyUp(Keys.D1))
                    {
                        playerControl.ActionBar1.SetButton(true);
                    }
                    else
                    {
                        playerControl.ActionBar1.SetButton(false);
                    }
                    // Actionbar 2
                    if (keyboard.IsKeyDown(Keys.D2) && previousKeyboardState.IsKeyUp(Keys.D2))
                    {
                        playerControl.ActionBar2.SetButton(true);
                    }
                    else
                    {
                        playerControl.ActionBar2.SetButton(false);
                    }
                    // Actionbar 3
                    if (keyboard.IsKeyDown(Keys.D3) && previousKeyboardState.IsKeyUp(Keys.D3))
                    {
                        playerControl.ActionBar3.SetButton(true);
                    }
                    else
                    {
                        playerControl.ActionBar3.SetButton(false);
                    }
                    // Actionbar 4
                    if (keyboard.IsKeyDown(Keys.D4) && previousKeyboardState.IsKeyUp(Keys.D4))
                    {
                        playerControl.ActionBar4.SetButton(true);
                    }
                    else
                    {
                        playerControl.ActionBar4.SetButton(false);
                    }
                    // Set previous keyboard state
                    previousKeyboardState = Keyboard.GetState();
                    break;

                case ControllerType.Gamepad1:
                    // Movement
                    gamepad = GamePad.GetState(PlayerIndex.One);
                    GamePadInputs(gamepad, previousGamepadState1, playerControl);
                    // Set previous gamepad state
                    previousGamepadState1 = GamePad.GetState(PlayerIndex.One);
                    break;

                case ControllerType.Gamepad2:
                    // Movement
                    gamepad = GamePad.GetState(PlayerIndex.Two);
                    GamePadInputs(gamepad, previousGamepadState2, playerControl);
                    // Set previous gamepad state
                    previousGamepadState2 = GamePad.GetState(PlayerIndex.Two);
                    break;

                default:
                    break;
                }
            }
        }
Ejemplo n.º 13
0
        private void GamePadInputs(GamePadState gamepad, GamePadState previousGamepadState, PlayerControlComponent playerControl)
        {
            playerControl.Movement.SetDirection(new Vector2(gamepad.ThumbSticks.Left.X, -gamepad.ThumbSticks.Left.Y));
            // Menu
            if (gamepad.IsButtonDown(Buttons.Start) && previousGamepadState.IsButtonUp(Buttons.Start))
            {
                playerControl.Menu.SetButton(true);
            }
            else
            {
                playerControl.Menu.SetButton(false);
            }

            if (gamepad.IsButtonDown(Buttons.RightTrigger))
            {
                // Actionbar 1
                if (gamepad.IsButtonDown(Buttons.A) && previousGamepadState.IsButtonUp(Buttons.A))
                {
                    playerControl.ActionBar1.SetButton(true);
                }
                else
                {
                    playerControl.ActionBar1.SetButton(false);
                }
                // Actionbar 2
                if (gamepad.IsButtonDown(Buttons.B) && previousGamepadState.IsButtonUp(Buttons.B))
                {
                    playerControl.ActionBar2.SetButton(true);
                }
                else
                {
                    playerControl.ActionBar2.SetButton(false);
                }
                // Actionbar 3
                if (gamepad.IsButtonDown(Buttons.X) && previousGamepadState.IsButtonUp(Buttons.X))
                {
                    playerControl.ActionBar3.SetButton(true);
                }
                else
                {
                    playerControl.ActionBar3.SetButton(false);
                }
                // Actionbar 4
                if (gamepad.IsButtonDown(Buttons.Y) && previousGamepadState.IsButtonUp(Buttons.Y))
                {
                    playerControl.ActionBar4.SetButton(true);
                }
                else
                {
                    playerControl.ActionBar4.SetButton(false);
                }
            }
            else
            {
                // Attack
                if (gamepad.IsButtonDown(Buttons.X) && previousGamepadState.IsButtonUp(Buttons.X))
                {
                    playerControl.Attack.SetButton(true);
                }
                else
                {
                    playerControl.Attack.SetButton(false);
                }
                // Interact
                if (gamepad.IsButtonDown(Buttons.A) && previousGamepadState.IsButtonUp(Buttons.A))
                {
                    playerControl.Interact.SetButton(true);
                }
                else
                {
                    playerControl.Interact.SetButton(false);
                }
                // Inventory
                if (gamepad.IsButtonDown(Buttons.Y) && previousGamepadState.IsButtonUp(Buttons.Y))
                {
                    playerControl.Inventory.SetButton(true);
                }
                else
                {
                    playerControl.Inventory.SetButton(false);
                }
                // Back
                if (gamepad.IsButtonDown(Buttons.B) && previousGamepadState.IsButtonUp(Buttons.B))
                {
                    playerControl.Back.SetButton(true);
                }
                else
                {
                    playerControl.Back.SetButton(false);
                }
            }
        }
Ejemplo n.º 14
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;
                        }
                    }
                }
            }
        }
Ejemplo n.º 15
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;
                }
            }
        }