281 lines
6.5 KiB
C#
281 lines
6.5 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using OHM.UnityToolkit;
|
|
using OHM.UnityToolkit.Vibration;
|
|
using UnityEngine.Serialization;
|
|
|
|
public class RichManagePlayer : CoreBehaviour
|
|
{
|
|
public VisualPlayerRichPoor VisualPlayerRichPoor;
|
|
|
|
[SerializeField]
|
|
[FormerlySerializedAs("pickUpItemHandler")]
|
|
private PickUpItemHandler _pickUpItemHandler;
|
|
|
|
[SerializeField]
|
|
[FormerlySerializedAs("continuePickUpHandler")]
|
|
private ContinuePickUpHandler _continuePickUpHandler;
|
|
|
|
[SerializeField]
|
|
[FormerlySerializedAs("thresholdRichHandler")]
|
|
private ThresholdRichHandler _thresholdRichHandler;
|
|
|
|
[SerializeField]
|
|
[Header("FX")]
|
|
[FormerlySerializedAs("fxGainContinue")]
|
|
private FX _fxGainContinue;
|
|
|
|
[SerializeField]
|
|
[FormerlySerializedAs("fxLooseContinue")]
|
|
private FX _fxLooseContinue;
|
|
|
|
[SerializeField]
|
|
[FormerlySerializedAs("fxGainPickUp")]
|
|
private FX _fxGainPickUp;
|
|
|
|
[SerializeField]
|
|
[FormerlySerializedAs("fxLoosePickUp")]
|
|
private FX _fxLoosePickUp;
|
|
|
|
[SerializeField]
|
|
private Transform spawnFx;
|
|
|
|
[SerializeField]
|
|
[Header("Sound")]
|
|
private SFX GoodCloth;
|
|
|
|
[SerializeField]
|
|
private SFX BadCloth;
|
|
|
|
private Player player;
|
|
|
|
private float _protectedUntil = -1f;
|
|
public int targetRichPoor;
|
|
|
|
public int tmpTargetRichPoor;
|
|
|
|
private bool _nextGameOver;
|
|
|
|
private bool _needUpdateTargetRichPoor;
|
|
|
|
private float _needUpdateContinueRichPoor;
|
|
|
|
public int richPoor { get; private set; }
|
|
|
|
public int curScore => richPoor;
|
|
|
|
public event Action onLose;
|
|
|
|
public event Action<float> onChangeMoney;
|
|
|
|
public event Action<long> onUpdateScore;
|
|
|
|
public void Setup(Player p)
|
|
{
|
|
_continuePickUpHandler.Setup();
|
|
player = p;
|
|
targetRichPoor = (tmpTargetRichPoor = base.CoreDef.player.richValueStart);
|
|
ForceRichPoorValue(base.CoreDef.player.richValueStart, init: true);
|
|
_pickUpItemHandler.onPickUpItem += OnPickUpItem;
|
|
_continuePickUpHandler.onEnterContinueItem += OnEnterContinueItem;
|
|
_continuePickUpHandler.onExitContinueItem += OnExitContinueItem;
|
|
_thresholdRichHandler.onThresholdZone += OnThresholdZone;
|
|
VisualPlayerRichPoor.onChangeCloth += OnChangeCloth;
|
|
_fxGainContinue.StopParticles();
|
|
_fxLooseContinue.StopParticles();
|
|
}
|
|
|
|
public void Go()
|
|
{
|
|
VisualPlayerRichPoor.playerGauge.ActiveGauge(active: true);
|
|
VisualPlayerRichPoor.ForceGaugeValue(richPoor);
|
|
}
|
|
|
|
public void Revive()
|
|
{
|
|
_protectedUntil = Time.time + base.CoreDef.reviveProtectionDuration;
|
|
targetRichPoor = (tmpTargetRichPoor = base.CoreDef.player.richValueStart);
|
|
ForceRichPoorValue(base.CoreDef.player.richValueStart, init: true);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (player.Started && !player.isDie)
|
|
{
|
|
UpdateRichPoorValue();
|
|
}
|
|
}
|
|
|
|
private void UpdateRichPoorValue()
|
|
{
|
|
if (_needUpdateContinueRichPoor != 0f)
|
|
{
|
|
int zoneTarget = ((_needUpdateContinueRichPoor != -1f) ? 1 : 0);
|
|
int t = targetRichPoor;
|
|
_needUpdateTargetRichPoor = true;
|
|
NeedUpdate(zoneTarget, (int)(base.CoreDef.player.speedIncValueOnZone * (float)base.CoreDef.player.maxValueRich), ref t);
|
|
int delta = t - targetRichPoor;
|
|
if (delta == 0)
|
|
{
|
|
_fxGainContinue.StopParticles();
|
|
_fxLooseContinue.StopParticles();
|
|
}
|
|
if (this.onChangeMoney != null)
|
|
{
|
|
this.onChangeMoney(base.CoreDef.player.maxValueRich * delta);
|
|
}
|
|
targetRichPoor = t;
|
|
}
|
|
if (_needUpdateTargetRichPoor)
|
|
{
|
|
int v = richPoor;
|
|
_needUpdateTargetRichPoor = NeedUpdate(targetRichPoor, (int)(base.CoreDef.player.SpeedRichPoorValue * (float)base.CoreDef.player.maxValueRich), ref v);
|
|
richPoor = v;
|
|
ForceRichPoorValue(v);
|
|
}
|
|
}
|
|
|
|
private bool NeedUpdate(int target, int speed, ref int toUpdate)
|
|
{
|
|
if (toUpdate > target)
|
|
{
|
|
int v = toUpdate - (int)(Time.deltaTime * (float)speed);
|
|
toUpdate = ((v > target) ? v : target);
|
|
return v > target;
|
|
}
|
|
if (toUpdate < target)
|
|
{
|
|
int v = (int)(Time.deltaTime * (float)speed) + toUpdate;
|
|
toUpdate = ((v < target) ? v : target);
|
|
return v < target;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public void SetRichPoorValue(int value, bool force = false)
|
|
{
|
|
targetRichPoor = value;
|
|
tmpTargetRichPoor = value;
|
|
if (force)
|
|
{
|
|
ForceRichPoorValue(value, init: true);
|
|
}
|
|
}
|
|
|
|
private void ForceRichPoorValue(int value, bool init = false)
|
|
{
|
|
richPoor = value;
|
|
float ratio = (float)value / (float)base.CoreDef.player.maxValueRich;
|
|
player.anim.SetFloat("Rich", ratio);
|
|
|
|
player.movePlayer.ChangeSpeedRatio(ratio);
|
|
VisualPlayerRichPoor.UpdateRichPoorValue(richPoor, init);
|
|
VisualPlayerRichPoor.playerGauge.ActiveMax(richPoor >= base.CoreDef.player.maxValueRich - 1);
|
|
if (richPoor <= 0 && base.CoreDef.player.gameOverAt0)
|
|
{
|
|
if (this.onLose != null)
|
|
{
|
|
this.onLose();
|
|
}
|
|
return;
|
|
}
|
|
_nextGameOver = false;
|
|
VisualPlayerRichPoor.playerGauge.ActiveDanger(active: false);
|
|
}
|
|
|
|
public bool Protected()
|
|
{
|
|
return _protectedUntil > Time.time;
|
|
}
|
|
|
|
public void LooseOrWinMoneyWithFx(int v, bool specialEndLevel = false)
|
|
{
|
|
if (v == 0)
|
|
{
|
|
return;
|
|
}
|
|
if (v < 0)
|
|
{
|
|
if (Protected())
|
|
{
|
|
return;
|
|
}
|
|
_fxLoosePickUp.PlayInstance(spawnFx, null);
|
|
}
|
|
else
|
|
{
|
|
_fxGainPickUp.PlayInstance(spawnFx, null);
|
|
}
|
|
_needUpdateTargetRichPoor = true;
|
|
targetRichPoor += v;
|
|
if (specialEndLevel)
|
|
{
|
|
tmpTargetRichPoor += v;
|
|
if (this.onUpdateScore != null)
|
|
{
|
|
this.onUpdateScore(tmpTargetRichPoor);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
tmpTargetRichPoor = targetRichPoor;
|
|
int clamped = Mathf.Clamp(targetRichPoor, 0, base.CoreDef.player.maxValueRich);
|
|
targetRichPoor = (tmpTargetRichPoor = clamped);
|
|
if (this.onUpdateScore != null)
|
|
{
|
|
this.onUpdateScore(clamped);
|
|
}
|
|
}
|
|
if (this.onChangeMoney != null)
|
|
{
|
|
this.onChangeMoney(v);
|
|
}
|
|
VibrationManager.Vibrate(VibrationManager.VibrationType.SMALL);
|
|
}
|
|
|
|
private void OnPickUpItem(PickUpItem item)
|
|
{
|
|
LooseOrWinMoneyWithFx((int)item.valueChange, item.specialPickupEnd);
|
|
}
|
|
|
|
private void OnThresholdZone(ThresholdRichZone zone)
|
|
{
|
|
bool canPass = richPoor >= zone.thresholdToPass;
|
|
zone.TryTakeThePass(canPass);
|
|
if (!canPass && this.onLose != null)
|
|
{
|
|
this.onLose();
|
|
}
|
|
}
|
|
|
|
private void OnEnterContinueItem(ConutinuePickUpZone zone)
|
|
{
|
|
if (zone.typeItem != 0)
|
|
{
|
|
_fxGainContinue.PlayParticles();
|
|
_fxLooseContinue.StopParticles();
|
|
_needUpdateContinueRichPoor = 1f;
|
|
}
|
|
else
|
|
{
|
|
_fxGainContinue.StopParticles();
|
|
_fxLooseContinue.PlayParticles();
|
|
_needUpdateContinueRichPoor = -1f;
|
|
}
|
|
}
|
|
|
|
private void OnExitContinueItem()
|
|
{
|
|
_fxGainContinue.StopParticles();
|
|
_fxLooseContinue.StopParticles();
|
|
_needUpdateContinueRichPoor = 0f;
|
|
}
|
|
|
|
private void OnChangeCloth(bool good)
|
|
{
|
|
(good ? GoodCloth : BadCloth).Play(base.transform);
|
|
player.anim.SetBool("bad", !good);
|
|
player.anim.SetBool("good", good);
|
|
}
|
|
}
|