// Update is called once per frame
        protected void Update()
        {
            ProcessSensoInput();

            //Check Activity Timeouts and if Player Present
            if (_totalForce > playerPresenceForceThreshold)
            {
                lastActivity = Time.time;
            }
            //else {
            //    //if noone is there, reset the center of gravity
            //    _cog = _sensoCenter;
            //}
            _playerPresent = Time.time - lastActivity < activityTimeout;
            if (_playerPresent != _playerPresentLast)
            {
                OnPlayerPresenceChanged?.Invoke(_playerPresent);
            }

            //Check for movement patterns
            if (_totalForce < jumpForceThreshold)
            {
                if (!jump && Mathf.Abs(_jumpTimer) < playerPresenceForceThreshold)
                {
                    OnJumped?.Invoke(_cog.x, _cog.y);
                    if (logging)
                    {
                        Debug.Log("Jumped");
                    }
                    _jumpTimer = 0f;
                    jump       = true;
                }
                else
                {
                    _jumpTimer += Time.deltaTime;
                    if (_jumpTimer > maxJumpTime & jump)
                    {
                        jump = false;
                        if (logging)
                        {
                            Debug.Log("Jump Cancelled");
                        }
                        OnJumpCancelled?.Invoke();
                    }
                }
            }
            else
            {
                if (jump)
                {
                    OnJumpLanded?.Invoke(_cog.x, _cog.y);
                    if (logging)
                    {
                        Debug.Log("Jump landed");
                    }
                }
                jump       = false;
                _jumpTimer = 0f;
            }
        }
Esempio n. 2
0
    // Update is called once per frame
    void LateUpdate()
    {
        //1. UPDATE PLATE STATE (including simulation with keys)
        _plates = (Plate[])Hardware.Plates.Clone();

        Direction[] dirs = (Direction[])System.Enum.GetValues(typeof(Direction));
        foreach (Direction dir in dirs)
        {
            KeyCode key = DirectionToKey(dir);
            if (Input.GetKeyDown(key))
            {
                //Debug.Log("step " + dir);
                _plates[(int)dir] = GetSimulatedPlate(dir, true, true);
            }
            else if (Input.GetKeyUp(key))
            {
                //Debug.Log("release " + dir);
                _plates[(int)dir] = GetSimulatedPlate(dir, false, true);
            }
            else if (Input.GetKey(key))
            {
                //Debug.Log("down " + dir);
                _plates[(int)dir] = GetSimulatedPlate(dir, true, false);
            }
        }

        //Calculate the center of gravity and the cumulated force on all plates
        Vector2 cog    = new Vector2();
        float   weight = 0f;

        for (int i = 0; i < _plates.Length; i++)
        {
            cog    += _plates[i].f * (new Vector2(_plates[i].x, _plates[i].y));
            weight += _plates[i].f;
        }
        if (Mathf.Abs(weight) > 0.01f)
        {
            _cog = 1 / weight * cog;
        }
        else
        {
            _cog = _sensoCenter;
        }
        _totalForce = weight;

        //Check Activity Timeouts and if Player Present
        if (_totalForce > 0.01f)
        {
            lastActivity = Time.time;
        }
        playerPresent = Time.time - lastActivity < activityTimeout;

        //Check for movement patterns
        if (_totalForce < jumpForceThreshold)
        {
            if (!jump && Mathf.Abs(_jumpTimer) < 0.01f)
            {
                OnJumped?.Invoke(_cog.x, _cog.y);
                Debug.Log("Jumped");
                _jumpTimer = 0f;
                jump       = true;
            }
            else
            {
                _jumpTimer += Time.deltaTime;
                if (_jumpTimer > maxJumpTime & jump)
                {
                    jump = false;
                    Debug.Log("Jump Cancelled");
                    OnJumpCancelled?.Invoke();
                }
            }
        }
        else
        {
            if (jump)
            {
                OnJumpLanded?.Invoke(_cog.x, _cog.y);
                Debug.Log("Jump landed");
            }
            jump       = false;
            _jumpTimer = 0f;
        }
    }