Beispiel #1
0
 public static bool Released(this TKeys now, TKeys key, TKeys last)
 {
     return(last.Has(key) && !now.Has(key));
 }
Beispiel #2
0
 public static bool Pressed(this TKeys now, TKeys key, TKeys last)
 {
     return(!last.Has(key) && now.Has(key));
 }
Beispiel #3
0
    void Update()
    {
        if (controller == null)
        {
            controller = GetComponentInChildren <CharacterController>();
        }
        doNextFrame?.Invoke();
        doNextFrame = null;
        if (animator == null)
        {
            animator = GetComponentInChildren <Animator>();
        }

        //transform.rotation = Quaternion.Euler(0, yaw, 0);
        //head.localRotation = Quaternion.Euler(pitch, 0, 0);

        if (velocity.y > 0 && (controller.collisionFlags & CollisionFlags.Above) != 0)
        {
            velocity.y = 0;
        }


        CheckForSnapToGround();
        UpdateDroneStuff();

        Vector3 moveInput = moveInputFunc?.Invoke() ?? Vector3.zero;
        Vector3 aimInput  = aimInputFunc?.Invoke() ?? Vector3.zero;
        TKeys   keyInput  = keysInputFunc?.Invoke() ?? TKeys.None;

        if (moveInput.sqrMagnitude > 1)
        {
            moveInput = moveInput.normalized;
        }


        Vector3 newMoveDir = moveInput;

        if (moveRoot != null && moveInput.sqrMagnitude > 0.0f)
        {
            Quaternion q  = moveRoot.rotation;
            Vector3    qe = q.eulerAngles;
            q = Quaternion.Euler(0, qe.y, 0);

            Vector3 forward = moveRoot.forward; forward.y = 0; forward.Normalize();
            Vector3 right   = moveRoot.right; right.y = 0; right.Normalize();

            newMoveDir = forward * moveInput.z + right * moveInput.x;

            //forward = transform.forward;		forward.y = 0;	forward.Normalize();
            //right = transform.right;			right.y = 0;	right.Normalize();

            //forward = Vector3.Project(forward, moveDir);
            //right = Vector3.Project(right, moveDir);

            //moveDir = forward + right * sideSpeedRatio;
        }

        float moveRate = 1.0f;

        if (moveInput.sqrMagnitude > tapThreshold)
        {
            lastMoveDir  = moveInput;
            lastMoveTime = 0;
        }
        else
        {
            lastMoveTime += Time.deltaTime;
        }
        if (keyInput.Has(TKeys.Sprint))
        {
            moveRate *= sprintPower;
        }
        moveDir = Vector3.Lerp(moveDir, newMoveDir, Time.deltaTime * (moveRate * responseBase / mass));

        if (Mathf.Abs(newMoveDir.x) > 0 || Mathf.Abs(newMoveDir.z) > 0)
        {
            Vector3 target = new Vector3(newMoveDir.x, 0, newMoveDir.z);
            //float target = Mathf.Sign(moveInput.x);
            Vector3 fwd = transform.forward;
            fwd = Vector3.Slerp(fwd, target, Time.deltaTime * lookDampening);

            Debug.DrawLine(transform.position, transform.position + fwd * 4);
        }

        RagCam rcam = moveRoot.GetComponent <RagCam>();

        if (rcam)
        {
            rcam.camHardPush = rcam.mousePushCamera ? Vector3.zero : aimInput;
        }
        if (aimInput.sqrMagnitude > 0)
        {
            Vector3 targetPos = transform.position + aimInput;
            targetPos.y = transform.position.y;
            Quaternion prevRotation = transform.rotation;
            transform.LookAt(targetPos, Vector3.up);
            transform.rotation = Quaternion.Lerp(prevRotation, transform.rotation, Time.deltaTime * animResponse);
        }

        Vector3 targetMoveXYZ = new Vector3(Vector3.Dot(moveDir, transform.right), 0, Vector3.Dot(moveDir, transform.forward));

        animMoveXYZ = Vector3.Lerp(animMoveXYZ, targetMoveXYZ, Time.deltaTime * animResponse);

        aniData.Record("MoveX", animMoveXYZ.x);
        aniData.Record("MoveZ", animMoveXYZ.z);
        aniData.Record("MoveAnimSpeed", 2.2f - .4f * moveDir.magnitude);

        bool fire = keyInput.Has(TKeys.Fire);

        aniData.Record("Fire", fire);
        if (muzzleflash != null)
        {
            if (guides.Length > 0)
            {
                muzzleflash.transform.position = guides[0].position;
                muzzleflash.transform.LookAt(guides[0].position + guides[0].forward, Vector3.up);
            }
            if (fire)
            {
                muzzleflash.gameObject.BroadcastMessage("Play", SendMessageOptions.DontRequireReceiver);
            }
        }

        aniData.Record("Crouch", keyInput.Has(TKeys.Crouch));
        aniData.Record("Reload", false);
        aniData.Record("Throw", keyInput.Has(TKeys.Throw));
        if (keyInput.Pressed(TKeys.Use, lastKeys))
        {
            if (false)
            {
                //Use(trackedUsable);
            }
            else
            {
                aniData.Record("Reload", keyInput.Has(TKeys.Use));
            }
        }

        if (keyInput.Pressed(TKeys.SwitchWeapons, lastKeys))
        {
            ChangeWeapon(currentWeaponKind == 0 ? 1 : 0);
        }
        else
        {
            aniData.Record("SwapWeapon", false);
        }


        Vector3 movement = moveDir * speed;

        movement += velocity;
        controller.CheckMoveBack(movement * Time.deltaTime);
        lastInput = moveInput;
        lastKeys  = keyInput;

        if (animator != null && animator.enabled)
        {
            aniData.SetTo(animator);
        }
    }
Beispiel #4
0
 public static bool Has(this TKeys compare, TKeys key)
 {
     return((key & compare) != TKeys.None);
 }
Beispiel #5
0
    void Start()
    {
        aniData    = new AnimatorData();
        animator   = GetComponentInChildren <Animator>();
        controller = GetComponentInChildren <CharacterController>();

        if (!IS_PLAYER && !USE_TEST_CONTROLS && marker != null)
        {
            marker.gameObject.SetActive(false);
        }

        if (USE_TEST_CONTROLS)
        {
            moveInputFunc = () => {
                Vector3 input = new Vector3();
                if (Input.GetKey(KeyCode.A))
                {
                    input.x = -1;
                }
                if (Input.GetKey(KeyCode.D))
                {
                    input.x = 1;
                }

                if (Input.GetKey(KeyCode.W))
                {
                    input.z = 1;
                }
                if (Input.GetKey(KeyCode.S))
                {
                    input.z = -1;
                }

                if (Input.GetKey(KeyCode.Space))
                {
                    input.y = 1f;
                }
                return(input);
            };
            aimInputFunc = () => {
                Ray        mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit rayhit;
                if (Physics.Raycast(mouseRay, out rayhit))
                {
                    Vector3 p = rayhit.point;
                    if (marker != null)
                    {
                        marker.position = p;
                        //marker.LookAt(transform);
                    }
                    p.y = transform.position.y;

                    Vector3 diff = (p - transform.position) / 20f;
                    if (diff.sqrMagnitude > 1.0f)
                    {
                        diff.Normalize();
                    }
                    return(diff);
                }

                return(Vector3.zero);
            };
            keysInputFunc = () => {
                TKeys v = TKeys.None;
                v = Input.GetKey(KeyCode.C) ? (v | TKeys.Crouch) : v;
                v = Input.GetKey(KeyCode.R) ? (v | TKeys.Use) : v;

                v = Input.GetKey(KeyCode.F) ? (v | TKeys.AltFire) : v;
                v = Input.GetKey(KeyCode.G) ? (v | TKeys.Throw) : v;
                v = Input.GetKey(KeyCode.LeftShift) ? (v | TKeys.Sprint) : v;
                v = Input.GetMouseButton(0) ? (v | TKeys.Fire) : v;

                v = Input.GetKey(KeyCode.Alpha1) ? (v | TKeys.Action1) : v;
                v = Input.GetKey(KeyCode.Alpha2) ? (v | TKeys.Action2) : v;
                v = Input.GetKey(KeyCode.Alpha3) ? (v | TKeys.Action3) : v;
                v = Input.GetKey(KeyCode.Alpha4) ? (v | TKeys.Action4) : v;
                v = Input.GetKey(KeyCode.Alpha5) ? (v | TKeys.Action5) : v;
                v = Input.GetKey(KeyCode.Alpha6) ? (v | TKeys.Action6) : v;
                v = Input.GetKey(KeyCode.Alpha7) ? (v | TKeys.Action7) : v;
                v = Input.GetKey(KeyCode.Alpha8) ? (v | TKeys.Action8) : v;
                v = Input.GetKey(KeyCode.Alpha9) ? (v | TKeys.Action9) : v;
                v = Input.GetKey(KeyCode.Alpha0) ? (v | TKeys.Action0) : v;

                v = Input.GetKey(KeyCode.Tab) ? (v | TKeys.SwitchWeapons) : v;

                return(v);
            };
        }
    }