revival/game/Assets/Scripts/Player/PlayerPower.cs

89 lines
2.0 KiB
C#
Raw Normal View History

2021-02-15 19:09:32 +01:00
using System;
using System.Collections;
using System.Collections.Generic;
2021-04-16 20:30:49 +02:00
using Ktyl.Util;
2021-02-15 19:09:32 +01:00
using UnityEngine;
using UnityEngine.Events;
[CreateAssetMenu(menuName = "KernelPanic/Player/Power", fileName = "PlayerPower.asset")]
public class PlayerPower : ScriptableObject
{
2021-03-13 03:01:20 +01:00
2021-02-15 19:09:32 +01:00
[SerializeField]
private bool _regenerateOnGround;
[SerializeField]
private bool _regenerateInAir;
2021-03-13 03:01:20 +01:00
public float regenerateTime => _regenerateTime;
[SerializeField] private float _regenerateTime;
2021-02-15 19:09:32 +01:00
private bool _unlocked;
private bool _cheated;
private bool _consumed;
2021-03-13 03:01:20 +01:00
public float cooldown => _cooldown;
private float _cooldown;
public float timeSinceConsume => _timeSinceConsume;
public float _timeSinceConsume;
2021-02-15 19:09:32 +01:00
private UnityEvent _onUnlock = new UnityEvent();
public UnityEvent OnUnlock => _onUnlock;
2021-04-16 20:30:49 +02:00
[SerializeField] private GameEvent _onConsume;
2021-02-15 19:09:32 +01:00
public void UpdatePower( float deltaTime, bool cheat, bool grounded)
{
2021-03-13 03:01:20 +01:00
2021-02-15 19:09:32 +01:00
_cheated = cheat;
bool regenerate = grounded ? _regenerateOnGround : _regenerateInAir;
if ( _consumed && regenerate )
{
_timeSinceConsume += deltaTime;
2021-03-13 03:01:20 +01:00
_cooldown = _regenerateTime - timeSinceConsume;
2021-02-15 19:09:32 +01:00
if ( _timeSinceConsume > _regenerateTime )
{
2021-03-13 03:01:20 +01:00
2021-02-15 19:09:32 +01:00
this.Reset();
}
}
}
public bool IsUnlocked => _unlocked || _cheated;
public bool CanConsume => IsUnlocked && !_consumed;
public void Consume()
2021-04-16 20:30:49 +02:00
{
_onConsume.Raise();
_consumed = true;
}
2021-02-15 19:09:32 +01:00
public void Reset()
{
_consumed = false;
_timeSinceConsume = 0f;
2021-03-16 16:49:54 +01:00
_cooldown = 0f;
2021-02-15 19:09:32 +01:00
}
public void Unlock()
{
if ( !_unlocked )
{
_onUnlock.Invoke();
_unlocked = true;
}
}
private void Awake()
{
_unlocked = false;
_cheated = false;
}
}