Skip to content

Instantly share code, notes, and snippets.

@sunny352
Created August 12, 2015 02:29
Show Gist options
  • Save sunny352/902bc04f2a14c0a62bb0 to your computer and use it in GitHub Desktop.
Save sunny352/902bc04f2a14c0a62bb0 to your computer and use it in GitHub Desktop.
蓄力功能计时器
using UnityEngine;
using System.Collections;
using System;
public class StorageEnergy
{
public static float CurrentTime { get { return Time.fixedTime; } }
public float Persent { get; private set; }
public Action onStart;
public Action onFinished;
public Action onInterupt;
public Action onStop;
private float m_preSecond = 0.0f;
private float m_chargeSecond = 0.0f;
private float m_startTime = 0.0f;
private enum ENChargeState
{
enNone,
enPreStart,
enCharging,
enChargeFinished,
}
private ENChargeState m_chargeState;
public void Init(float preSecond, float chargeSecond)
{
m_preSecond = preSecond;
m_chargeSecond = chargeSecond;
m_chargeState = ENChargeState.enNone;
}
public void Start()
{
m_startTime = CurrentTime;
m_chargeState = ENChargeState.enPreStart;
}
public void Stop()
{
if (Persent < 1.0f)
{
if (null != onInterupt)
{
onInterupt();
}
}
else
{
if (null != onStop)
{
onStop();
}
}
m_chargeState = ENChargeState.enNone;
Persent = 0.0f;
}
public void OnStart()
{
if (null != onStart)
{
onStart();
}
}
public void OnFinished()
{
if (null != onFinished)
{
onFinished();
}
}
public void FixedUpdate()
{
if (ENChargeState.enNone != m_chargeState)
{
switch (m_chargeState)
{
case ENChargeState.enPreStart:
if (CurrentTime - m_startTime > m_preSecond)
{
m_chargeState = ENChargeState.enCharging;
OnStart();
}
break;
case ENChargeState.enCharging:
if (CurrentTime - m_startTime - m_preSecond > m_chargeSecond)
{
m_chargeState = ENChargeState.enChargeFinished;
OnFinished();
}
else
{
Persent = (CurrentTime - m_startTime - m_preSecond) / m_chargeSecond;
}
break;
case ENChargeState.enChargeFinished:
Persent = 1.0f;
break;
default:
m_chargeState = ENChargeState.enNone;
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment