Пример #1
0
        void Update()
        {
            transform.position = PlayerControlsMouse.Get().GetPointingPos();
            transform.rotation = Quaternion.LookRotation(TheCamera.Get().transform.forward, Vector3.up);

            PlayerCharacter player   = PlayerCharacter.GetFirst();
            PlayerControls  controls = PlayerControls.Get(player ? player.player_id : 0);

            MAction maction = current_slot != null && current_slot.GetItem() != null?current_slot.GetItem().FindMergeAction(current_select) : null;

            title.enabled = maction != null && player != null && maction.CanDoAction(player, current_slot, current_select);
            title.text    = maction != null ? maction.title : "";

            bool active = current_slot != null && controls != null && !controls.IsGamePad();

            if (active != icon_group.activeSelf)
            {
                icon_group.SetActive(active);
            }

            icon.enabled = false;
            if (current_slot != null && current_slot.GetItem())
            {
                icon.sprite  = current_slot.GetItem().icon;
                icon.enabled = true;
            }

            timer += Time.deltaTime;
            if (timer > refresh_rate)
            {
                timer = 0f;
                SlowUpdate();
            }
        }
        void Update()
        {
            PlayerControls controls = PlayerControls.Get(player_id);

            if (!controls.IsGamePad())
            {
                return;
            }

            if (controls.IsUIPressLeft())
            {
                Navigate(Vector2.left);
            }
            else if (controls.IsUIPressRight())
            {
                Navigate(Vector2.right);
            }
            else if (controls.IsUIPressUp())
            {
                Navigate(Vector2.up);
            }
            else if (controls.IsUIPressDown())
            {
                Navigate(Vector2.down);
            }

            //Stop navigate if out of focus
            UISlotPanel selected_panel = UISlotPanel.GetFocusedPanel();

            if (selected_panel != null && !selected_panel.IsVisible())
            {
                StopNavigate();
            }
            else if (selected_panel != null && selected_panel.GetSelectSlot() != null && !selected_panel.GetSelectSlot().IsVisible())
            {
                StopNavigate();
            }

            //Controls
            if (controls.IsPressUISelect())
            {
                OnPressSelect();
            }

            if (controls.IsPressUIUse())
            {
                OnPressUse();
            }

            if (controls.IsPressUICancel())
            {
                OnPressCancel();
            }

            if (controls.IsPressAttack())
            {
                OnPressAttack();
            }
        }
Пример #3
0
        protected override void Update()
        {
            base.Update();

            PlayerControls controls = PlayerControls.Get(GetPlayerID());

            if (!controls.IsGamePad())
            {
                if (controls.IsPressAction() || controls.IsPressAttack())
                {
                    CancelSubSelection();
                }
            }
        }
Пример #4
0
        void Update()
        {
            if (building_mode && building_character != null)
            {
                PlayerControls      constrols = PlayerControls.Get(building_character.player_id);
                PlayerControlsMouse mouse     = PlayerControlsMouse.Get();

                if (!position_set)
                {
                    if (constrols.IsGamePad())
                    {
                        //Controller Game Pad building
                        transform.position = building_character.transform.position + building_character.transform.forward * build_distance;
                        transform.rotation = Quaternion.Euler(0f, manual_rotate, 0f) * Quaternion.LookRotation(building_character.GetFacing(), Vector3.up);
                    }
                    else
                    {
                        //Mouse/Touch controls
                        transform.position = mouse.GetPointingPos();
                        transform.rotation = Quaternion.Euler(0f, manual_rotate, 0f) * TheCamera.Get().GetFacingRotation();
                    }

                    //Snap to grid
                    FindAutoPosition();

                    //Show/Hide on mobile
                    if (TheGame.IsMobile())
                    {
                        SetBuildVisible(mouse.IsMouseHold());
                    }
                }

                bool  can_build = CheckIfCanBuild();
                Color color     = can_build ? Color.white : Color.red * 0.9f;
                SetModelColor(new Color(color.r, color.g, color.b, 0.5f), !can_build);
            }

            update_timer += Time.deltaTime;
            if (update_timer > 0.5f)
            {
                update_timer = Random.Range(-0.02f, 0.02f);
                SlowUpdate(); //Optimization
            }
        }
Пример #5
0
        void Update()
        {
            if (TheGame.Get().IsPaused())
            {
                return;
            }

            if (IsDead())
            {
                return;
            }

            if (rider != null)
            {
                PlayerControls      controls  = PlayerControls.Get(rider.player_id);
                PlayerControlsMouse mcontrols = PlayerControlsMouse.Get();
                Vector3             tmove     = Vector3.zero;
                Vector3             cam_move  = TheCamera.Get().GetRotation() * controls.GetMove();
                if (mcontrols.IsJoystickActive())
                {
                    Vector2 joystick = mcontrols.GetJoystickDir();
                    cam_move = TheCamera.Get().GetRotation() * new Vector3(joystick.x, 0f, joystick.y);
                }
                tmove = cam_move * ride_speed;
                if (tmove.magnitude > 0.1f)
                {
                    character.DirectMoveToward(tmove);
                }

                //Character stuck
                if (tmove.magnitude < 0.1f && character.IsStuck())
                {
                    character.Stop();
                }
            }

            //Animations
            if (animator.enabled)
            {
                animator.SetBool("Move", IsMoving());
                animator.SetBool("Run", IsMoving());
            }
        }
Пример #6
0
        protected override void RefreshPanel()
        {
            base.RefreshPanel();

            PlayerCharacter player = GetPlayer();

            if (player != null)
            {
                CraftStation station = player.Crafting.GetCraftStation();
                if (current_staton != station)
                {
                    current_staton = station;
                    RefreshCategories();
                }
            }

            //Gamepad auto controls
            PlayerControls controls    = PlayerControls.Get(GetPlayerID());
            CraftSubPanel  sub_panel   = CraftSubPanel.Get(GetPlayerID());
            UISlotPanel    focus_panel = UISlotPanel.GetFocusedPanel();

            if (focus_panel != this && focus_panel != sub_panel && !TheUI.Get().IsBlockingPanelOpened() &&
                controls.IsGamePad() && player != null && !player.Crafting.IsBuildMode())
            {
                Focus();
                CraftInfoPanel.Get(GetPlayerID())?.Hide();
            }
            if (focus_panel == this)
            {
                selection_index = Mathf.Clamp(selection_index, 0, CountActiveSlots() - 1);

                UISlot slot = GetSelectSlot();
                if (prev_slot != slot || !sub_panel.IsVisible())
                {
                    OnClick(slot);
                    sub_panel.selection_index = 0;
                    prev_slot = slot;
                }
            }
        }
Пример #7
0
        //Make sure there is no obstacles in between the player and the building, this applies only if placing with keyboard/gamepad
        public bool CheckIfAccessible()
        {
            PlayerControls controls = PlayerControls.Get(building_character.player_id);
            bool           game_pad = controls != null && controls.IsGamePad();

            if (position_set || !game_pad)
            {
                return(true); //Dont check this is placing with mouse or if position already set
            }
            if (building_character != null)
            {
                Vector3 center       = building_character.GetColliderCenter();
                Vector3 build_center = transform.position + Vector3.up * build_ground_dist;
                Vector3 dir          = build_center - center;

                RaycastHit h1;
                bool       f1 = PhysicsTool.RaycastCollision(center, dir, out h1);

                return(!f1 || h1.collider.GetComponentInParent <Buildable>() == this);
            }
            return(false);
        }
        void Update()
        {
            if (TheGame.Get().IsPaused())
            {
                return;
            }

            if (character.IsDead())
            {
                return;
            }

            build_timer += Time.deltaTime;
            craft_timer += Time.deltaTime;

            PlayerControls controls = PlayerControls.Get(character.player_id);

            //Cancel building
            if (controls.IsPressUICancel() || controls.IsPressPause())
            {
                CancelBuilding();
            }

            //Cancel crafting
            if (current_crafting != null && character.IsMoving())
            {
                CancelCrafting();
            }

            //Complete crafting after timer
            if (current_crafting != null)
            {
                if (craft_timer > current_crafting.craft_duration)
                {
                    CompleteCrafting();
                }
            }
        }
Пример #9
0
        void Start()
        {
            active_device = InputManager.ActiveDevice;

            PlayerControls controls = PlayerControls.Get(player_id);

            controls.gamepad_linked = true;

            controls.gamepad_action = () => { return(WasPressed(active_device, action)); };
            controls.gamepad_attack = () => { return(WasPressed(active_device, attack) || WasPressed(active_device, attack2)); };
            controls.gamepad_jump   = () => { return(WasPressed(active_device, jump)); };
            controls.gamepad_use    = () => { return(WasPressed(active_device, use)); };
            controls.gamepad_craft  = () => { return(WasPressed(active_device, craft)); };
            controls.gamepad_accept = () => { return(WasPressed(active_device, menu_accept)); };
            controls.gamepad_cancel = () => { return(WasPressed(active_device, menu_cancel)); };
            controls.gamepad_pause  = () => { return(WasPressed(active_device, menu_pause)); };

            controls.gamepad_camera = () => { return(new Vector2(-GetAxis(active_device, camera_left) + GetAxis(active_device, camera_right), 0f)); };

            controls.gamepad_move     = () => { return(GetTwoAxis(active_device, InputControlType.LeftStickX, InputControlType.LeftStickY)); };
            controls.gamepad_freelook = () => { return(GetTwoAxis(active_device, InputControlType.RightStickX, InputControlType.RightStickY)); };
            controls.gamepad_menu     = () => { return(GetTwoAxisThreshold(active_device, InputControlType.LeftStickX, InputControlType.LeftStickY) + GetTwoAxisPress(active_device, InputControlType.DPadX, InputControlType.DPadY)); };
            controls.gamepad_dpad     = () => { return(GetTwoAxisPress(active_device, InputControlType.DPadX, InputControlType.DPadY)); };
        }
        private void Update()
        {
            if (TheGame.Get().IsPaused())
            {
                return;
            }

            if (IsDead())
            {
                return;
            }

            //Save position
            Data.position = GetPosition();

            //Controls
            PlayerControls controls = PlayerControls.Get(player_id);

            //Stop sleep
            if (is_action || IsMoving() || sleep_target == null)
            {
                StopSleep();
            }

            //Activate Selectable when near
            Vector3 move_dir = auto_move_target - transform.position;

            if (auto_move && !is_action && auto_move_select != null && move_dir.magnitude < auto_move_select.use_range)
            {
                auto_move = false;
                auto_move_select.Use(this, auto_move_target);
                auto_move_select = null;
            }

            //Finish construction when near clicked spot
            Buildable current_buildable = character_craft.GetCurrentBuildable();

            if (auto_move && !is_action && character_craft.ClickedBuild() && current_buildable != null && move_dir.magnitude < current_buildable.build_distance)
            {
                auto_move = false;
                character_craft.StartCraftBuilding(auto_move_target);
            }

            //Stop move & drop when near clicked spot
            if (auto_move && !is_action && move_dir.magnitude < moving_threshold * 2f)
            {
                auto_move = false;
                character_inventory.DropItem(auto_move_drop_inventory, auto_move_drop);
            }

            //Stop attacking if target cant be attacked anymore (tool broke, or target died...)
            if (!character_combat.CanAttack(auto_move_attack))
            {
                auto_move_attack = null;
            }

            //Ride animal
            if (is_riding)
            {
                if (riding_animal == null || riding_animal.IsDead())
                {
                    StopRide();
                    return;
                }

                transform.position = riding_animal.GetRideRoot();
                transform.rotation = Quaternion.LookRotation(riding_animal.transform.forward, Vector3.up);
            }

            //Controls
            if (IsControlsEnabled() && !is_action)
            {
                //Check if panel is focused
                KeyControlsUI ui_controls = KeyControlsUI.Get(player_id);
                bool          panel_focus = controls.gamepad_controls && ui_controls != null && ui_controls.IsPanelFocus();
                if (!panel_focus)
                {
                    //Press Action button
                    if (controls.IsPressAction())
                    {
                        if (character_craft.CanBuild())
                        {
                            character_craft.StartCraftBuilding();
                        }
                        else if (!panel_focus)
                        {
                            InteractWithNearest();
                        }
                    }

                    //Press attack
                    if (Combat.can_attack && controls.IsPressAttack())
                    {
                        AttackNearest();
                    }

                    //Press jump
                    if (character_jump != null && controls.IsPressJump())
                    {
                        character_jump.Jump();
                    }
                }

                if (controls.IsPressUISelect())
                {
                    if (character_craft.CanBuild())
                    {
                        character_craft.StartCraftBuilding();
                    }
                }
            }

            //Stop riding
            if (is_riding && (controls.IsPressJump() || controls.IsPressAction() || controls.IsPressUICancel()))
            {
                StopRide();
            }
        }
        void FixedUpdate()
        {
            if (TheGame.Get().IsPaused())
            {
                rigid.velocity = Vector3.zero;
                return;
            }

            if (IsDead())
            {
                return;
            }

            PlayerControls      controls  = PlayerControls.Get(player_id);
            PlayerControlsMouse mcontrols = PlayerControlsMouse.Get();
            Vector3             tmove     = Vector3.zero;

            //Update auto move for moving targets
            GameObject auto_move_obj = null;

            if (auto_move_select != null && auto_move_select.type == SelectableType.Interact)
            {
                auto_move_obj = auto_move_select.gameObject;
            }
            if (auto_move_attack != null)
            {
                auto_move_obj = auto_move_attack.gameObject;
            }

            if (auto_move && auto_move_obj != null)
            {
                Vector3 diff = auto_move_obj.transform.position - auto_move_target;
                if (diff.magnitude > 1f)
                {
                    auto_move_target      = auto_move_obj.transform.position;
                    auto_move_target_next = auto_move_obj.transform.position;
                    CalculateNavmesh(); //Recalculate navmesh because target moved
                }
            }

            //Navmesh calculate next path
            if (auto_move && use_navmesh && path_found && path_index < nav_paths.Length)
            {
                auto_move_target_next = nav_paths[path_index];
                Vector3 move_dir_total = auto_move_target_next - transform.position;
                move_dir_total.y = 0f;
                if (move_dir_total.magnitude < 0.2f)
                {
                    path_index++;
                }
            }

            //AUTO Moving (after mouse click)
            auto_move_timer += Time.fixedDeltaTime;
            if (auto_move && auto_move_timer > 0.02f) //auto_move_timer to let the navmesh time to calculate a path
            {
                Vector3 move_dir_total = auto_move_target - transform.position;
                Vector3 move_dir_next  = auto_move_target_next - transform.position;
                Vector3 move_dir       = move_dir_next.normalized * Mathf.Min(move_dir_total.magnitude, 1f);
                move_dir.y = 0f;

                float move_dist = Mathf.Min(GetMoveSpeed(), move_dir.magnitude * 10f);
                tmove = move_dir.normalized * move_dist;
            }
            //Keyboard/gamepad moving
            else if (IsControlsEnabled())
            {
                Vector3 cam_move = TheCamera.Get().GetRotation() * controls.GetMove();
                if (mcontrols.IsJoystickActive() && !character_craft.IsBuildMode())
                {
                    Vector2 joystick = mcontrols.GetJoystickDir();
                    cam_move = TheCamera.Get().GetRotation() * new Vector3(joystick.x, 0f, joystick.y);
                }
                tmove = cam_move * GetMoveSpeed();
            }

            //CancelAction
            if (is_action && can_cancel_action && tmove.magnitude > 0.1f)
            {
                CancelAction();
            }

            //Stop moving if doing action
            if (is_action)
            {
                tmove = Vector3.zero;
            }

            //Check if grounded
            DetectGrounded();

            //Add Falling to the move vector
            if (!is_grounded || IsJumping())
            {
                if (!IsJumping())
                {
                    fall_vect = Vector3.MoveTowards(fall_vect, Vector3.down * fall_speed, fall_gravity * Time.fixedDeltaTime);
                }
                tmove += fall_vect;
            }
            //Add slope angle
            else if (is_grounded)
            {
                tmove = Vector3.ProjectOnPlane(tmove.normalized, ground_normal).normalized *tmove.magnitude;
            }

            //Apply the move calculated previously
            move           = Vector3.Lerp(move, tmove, move_accel * Time.fixedDeltaTime);
            rigid.velocity = move;

            //Calculate Facing
            if (!is_action && IsMoving())
            {
                facing = new Vector3(move.x, 0f, move.z).normalized;
            }

            //Rotate character with right joystick when not in free rotate mode
            bool freerotate = TheCamera.Get().IsFreeRotation();

            if (!is_action && !freerotate && controls.IsGamePad())
            {
                Vector2 look  = controls.GetFreelook();
                Vector3 look3 = TheCamera.Get().GetRotation() * new Vector3(look.x, 0f, look.y);
                if (look3.magnitude > 0.5f)
                {
                    facing = look3.normalized;
                }
            }

            //Apply the facing
            Quaternion targ_rot = Quaternion.LookRotation(facing, Vector3.up);

            rigid.MoveRotation(Quaternion.RotateTowards(rigid.rotation, targ_rot, rotate_speed * Time.fixedDeltaTime));

            //Fronted (need to be done after facing to avoid issues)
            DetectFronted();

            //Check the average traveled movement (allow to check if character is stuck)
            Vector3 last_frame_travel = transform.position - prev_pos;

            move_average = Vector3.MoveTowards(move_average, last_frame_travel, 1f * Time.fixedDeltaTime);
            prev_pos     = transform.position;

            //Stop auto move
            bool stuck_somewhere = move_average.magnitude <0.02f && auto_move_timer> 1f;

            if (stuck_somewhere)
            {
                auto_move = false;
            }

            //Stop the click auto move when moving with keyboard/joystick/gamepad
            if (controls.IsMoving() || mcontrols.IsJoystickActive() || mcontrols.IsDoubleTouch())
            {
                StopAutoMove();
            }
        }
        public bool IsGamePad()
        {
            PlayerControls controls = PlayerControls.Get(player_id);

            return(controls ? controls.IsGamePad() : false);
        }
        protected override void Update()
        {
            base.Update();

            PlayerCharacter character = GetPlayer();
            int             gold      = (character != null) ? character.Data.gold : 0;

            if (gold_value != null)
            {
                gold_value.text = gold.ToString();
            }

            //Init inventories from here because they are disabled
            foreach (ItemSlotPanel panel in item_slot_panels)
            {
                panel.InitPanel();
            }

            //Fx visibility
            damage_fx_timer += Time.deltaTime;

            if (build_mode_text != null)
            {
                build_mode_text.enabled = IsBuildMode();
            }

            if (tps_cursor != null)
            {
                tps_cursor.enabled = TheCamera.Get().IsLocked();
            }

            if (character != null && !character.IsDead() && character.Attributes.IsDepletingHP())
            {
                DoDamageFXInterval();
            }

            //Cold FX
            if (character != null && !character.IsDead())
            {
                PlayerCharacterHeat characterHeat = PlayerCharacterHeat.Get(character.player_id);
                if (cold_fx != null && characterHeat != null)
                {
                    cold_fx.SetVisible(characterHeat.IsCold());
                }
                if (damage_fx != null && characterHeat != null && characterHeat.IsColdDamage())
                {
                    DoDamageFXInterval();
                }
            }

            //Controls
            PlayerControls controls = PlayerControls.Get(player_id);

            if (controls.IsPressCraft())
            {
                CraftPanel.Get(player_id)?.Toggle();
                ActionSelectorUI.Get(player_id)?.Hide();
                ActionSelector.Get(player_id)?.Hide();
            }

            //Backpack panel
            BagPanel bag_panel = BagPanel.Get(player_id);

            if (character != null && bag_panel != null)
            {
                InventoryItemData item  = character.Inventory.GetBestEquippedBag();
                ItemData          idata = ItemData.Get(item?.item_id);
                if (idata != null)
                {
                    bag_panel.ShowBag(character, item.uid, idata.bag_size);
                }
                else
                {
                    bag_panel.HideBag();
                }
            }
        }