private void Awake() { _magnetic = GetComponent <Magnetic>(); _magnetic.OnPull += _onPlayerPull_Magnetic; _currentState = KlunkState.None; _currentFuel = _maxFuel; OnCurrentFuelChange?.Invoke((float)_currentFuel / _maxFuel); _currentEnergy = _maxEnergy; OnCurrentEnergyChange?.Invoke((float)_currentEnergy / _maxEnergy); _fuelPerSecond = (float)_maxFuel / _fuelTimeDuration; _rechargePerSecond = (float)_maxEnergy / _totalTimeToReloadFullEnergy; _frontAttack = GetComponentInChildren <FrontAttack>(); _frontAttack.gameObject.SetActive(false); _frontAttack.OnTriggerEnterEvent += _frontAttackChecker_OnTriggerEnterEvent; _characterController = GetComponent <KlunkCharController>(); _frictionController = GetComponent <FrictionController>(); var playerInput = GetComponent <PlayerInput>(); _actionMove = playerInput.actions["Move"]; _actionJump = playerInput.actions["Jump"]; _actionJump.performed += _actionJump_performed; _actionJump.canceled += _actionJump_canceled; _actionMov1 = playerInput.actions["Mov1"]; _actionMov2 = playerInput.actions["Mov2"]; _actionMov2.performed += _actionMov2_performed; _actionMov2.canceled += _actionMov2_canceled; inGameUIController = FindObjectOfType <InGameUIController>(); }
private void FixedUpdate() { #if UNITY_EDITOR if (_infinityMode) { _currentEnergy = _maxEnergy; OnCurrentEnergyChange?.Invoke((float)_currentEnergy / _maxEnergy); _currentFuel = _maxFuel; OnCurrentFuelChange?.Invoke((float)_currentFuel / _maxFuel); } #endif if (_currentFuel <= 0) { return; } if (_currentFuel > 0) { _fuelConsumed += _fuelPerSecond * Time.fixedDeltaTime; } if (_fuelConsumed >= 1) { _fuelConsumed -= 1; _currentFuel = Mathf.Clamp(--_currentFuel, 0, _maxFuel); OnCurrentFuelChange?.Invoke((float)_currentFuel / _maxFuel); } if (_currentEnergy < _maxEnergy) { _energyRecharged += _rechargePerSecond * Time.fixedDeltaTime; } if (_energyRecharged >= 1) { _energyRecharged -= 1; _currentEnergy = Mathf.Clamp(++_currentEnergy, 0, _maxEnergy); OnCurrentEnergyChange?.Invoke((float)_currentEnergy / _maxEnergy); } switch (_currentState) { case KlunkState.Dashing: _remainingTimeDash -= Time.fixedDeltaTime; if (!_isDashing) { _isDashing = true; //AudioManager.instance.Play("klunk_dash"); } if (_remainingTimeDash <= 0 || _hitWallWhileDash) { _currentState = KlunkState.None; _characterController.IgnoreAirSpeed(false); _characterController.Move(Vector2.zero, 0, _jump); _characterController.RemoveFreezeConstraint(RigidbodyConstraints.FreezePositionY); _frontAttack.gameObject.SetActive(false); _hitWallWhileDash = false; _frictionController.SetMaxFriction(); _characterController.IgnoreHorizontalClampedSpeed(false); _characterController.IgnoreSpeedSmooth(false); _characterController.DontIncrementSpeed(false); _isDashing = false; OnEndDash?.Invoke(transform.position); } break; case KlunkState.Sk8erBoi: if (_currentEnergy <= 0 || !_actionMov2Pressed) { _currentState = KlunkState.None; _characterController.SetBaseSpeedMultiplier(1); OnEndSkate?.Invoke(transform.position); _isSkating = false; //AudioManager.instance.Stop("klunk_skate"); return; } if (!_isSkating) { _isSkating = true; //AudioManager.instance.Play("klunk_skate"); } _characterController.Move(_actionMove.ReadValue <Vector2>(), _sk8SpeedFactor, _jump); _energyConsumed += _sk8erBoiEnergyCostPerSecond * Time.fixedDeltaTime; if (_energyConsumed >= 1) { _energyConsumed -= 1; _currentEnergy = Mathf.Clamp(--_currentEnergy, 0, _maxEnergy); OnCurrentEnergyChange?.Invoke((float)_currentEnergy / _maxEnergy); } break; default: _characterController.Move(_actionMove.ReadValue <Vector2>(), 1, _jump); if (!_isWalking) { _isWalking = true; //AudioManager.instance.Play("klunk_walk"); } if (_actionMov1.triggered && _currentEnergy >= _dashEnergyCost) { _characterController.IgnoreAirSpeed(true); if (_characterController.FacedRight) { _characterController.Move(Vector2.right, _dashSpeedFactor, _jump); } else { _characterController.Move(-Vector2.right, _dashSpeedFactor, _jump); } _characterController.AddFreezeConstraint(RigidbodyConstraints.FreezePositionY); _frontAttack.gameObject.SetActive(true); _remainingTimeDash = _dashMaxDistance / (_characterController.BaseSpeed * _dashSpeedFactor); _remainingTimeDash -= Time.fixedDeltaTime; _currentState = KlunkState.Dashing; _currentEnergy = Mathf.Clamp(_currentEnergy - _dashEnergyCost, 0, _maxEnergy); OnCurrentEnergyChange?.Invoke((float)_currentEnergy / _maxEnergy); _frictionController.SetZeroFriction(); _characterController.IgnoreHorizontalClampedSpeed(true); _characterController.IgnoreSpeedSmooth(true); _characterController.DontIncrementSpeed(true); OnStartDash?.Invoke(transform.position); temp_startPosition = transform.position; _isWalking = false; //AudioManager.instance.Play("klunk_walk"); return; } else if (_actionMov2Pressed && _currentEnergy > _sk8erBoiEnergyCostPerSecond) { _currentState = KlunkState.Sk8erBoi; _characterController.SetBaseSpeedMultiplier(_sk8SpeedFactor); OnStartSkate?.Invoke(transform.position); _isWalking = false; //AudioManager.instance.Play("klunk_walk"); return; } break; } }