Simple game control logic Clock and logic clock

Source: Internet
Author: User

Simple game control logic Clock and logic clock

Source: http://blog.csdn.net/u010019717

Author: Sun Guangdong time:


In unity, coroutine can provide latency functions. But most of the time we don't want to use it, so we can control the time in Update.

So I encapsulated this class.

To use this clock, first instantiate it, call the Reset function to set the correct time value, and call Update each frame for Update.
Any class to be notified of events must implement the IClockListener interface,
And use the AddListener method to subscribe to events. You can use RemoveListener to remove the listener (very powerful !)
The clock can be paused using the Pause method independently of Time. timeScale (and resume with Unpause)

Using System. collections. generic; namespace Gamelogic {public class Clock {private float time; private int timeInSeconds; private readonly List <IClockListener> listeners; // listener List # regionpublic bool IsPaused {get; private set ;} public bool IsDone {get; private set;} public float Time {get {return time ;}} public int TimeInSeconds {get {return timeInSeconds ;}} # endregion // constructor public Clock () {listeners = new List <IClockListener> (); IsPaused = true; Reset (0);} public void AddClockListener (IClockListener listener) {listeners. add (listener);} public void RemoveClockListener (IClockListener listener) {listeners. remove (listener);} public void Reset (float startTime) {time = startTime; IsDone = false; CheckIfTimeInSecondsChanged ();} public void Unpause () {IsPaused = false ;} public void Pause () {IsPaused = true;} // time public void Update () {if (IsPaused) return; if (IsDone) return; time-= UnityEngine. time. deltaTime; CheckIfTimeInSecondsChanged (); if (time <= 0) {time = 0; IsDone = true; for (int I = 0; I <listeners. count; I ++) {listeners [I]. onTimeOut () ;}}// determines whether a second change has occurred. private void CheckIfTimeInSecondsChanged () {var newTimeInSeonds = (int) time; if (newTimeInSeonds = timeInSeconds) return; timeInSeconds = newTimeInSeonds; for (int I = 0; I <listeners. count; I ++) {listeners [I]. onSecondsChanged (timeInSeconds) ;}}// interface of the clock listener type public interface IClockListener {void OnSecondsChanged (int seconds); void OnTimeOut ();}}


Then I tested it in unity4.6. Countdown:

Using UnityEngine. UI; namespace Gamelogic. examples {public class ClockTest: IClockListener {public Text clockText; public Text messageText; private Clock clock; // clock object public void Start () {Clock = new clock (); Clock. addClockListener (this); // listen to the Reset ();} public void Update () {clock. update ();} public void Pause () {clock. pause ();} public void Unpause () {clock. unpause ();} public void Reset () {clock. reset (5); clock. unpause () ;}# region IClockListener methods // method of implementing the interface public void OnSecondsChanged (int seconds) {clockText. text = clock. timeInSeconds. toString ();} public void OnTimeOut () {messageText. gameObject. setActive (true) ;}# endregion }}

Not bad!



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.