|
| 1 | +// Copyright (c) 2014 F. Rick Reich. |
| 2 | + |
| 3 | +using UnityEngine; |
| 4 | +using System; |
| 5 | +using System.Collections; |
| 6 | + |
| 7 | +namespace HutongGames.PlayMaker.Actions |
| 8 | +{ |
| 9 | + [ActionCategory("Advanced Actions")] |
| 10 | + [Tooltip("Create an analog clock using system date time.")] |
| 11 | + public class ClockTimer : FsmStateAction |
| 12 | + { |
| 13 | + |
| 14 | + public FsmInt hours; |
| 15 | + |
| 16 | + public FsmInt minutes; |
| 17 | + |
| 18 | + public FsmInt seconds; |
| 19 | + |
| 20 | + public FsmInt milliseconds; |
| 21 | + |
| 22 | + public FsmGameObject hoursGameObject; |
| 23 | + |
| 24 | + public FsmGameObject minutesGameObject; |
| 25 | + |
| 26 | + public FsmGameObject secondsGameObject; |
| 27 | + |
| 28 | + public FsmGameObject millisecondsGameObject; |
| 29 | + |
| 30 | + public bool everyFrame; |
| 31 | + |
| 32 | + public bool debug; |
| 33 | + |
| 34 | + public override void OnEnter() |
| 35 | + { |
| 36 | + DoTimer(); |
| 37 | + |
| 38 | + if (!everyFrame) |
| 39 | + Finish(); |
| 40 | + } |
| 41 | + |
| 42 | + public override void OnUpdate() |
| 43 | + { |
| 44 | + DoTimer(); |
| 45 | + } |
| 46 | + |
| 47 | + void DoTimer() { |
| 48 | + |
| 49 | + TimeSpan timespan = DateTime.Now.TimeOfDay; |
| 50 | + |
| 51 | + DateTime time = DateTime.Now; |
| 52 | + |
| 53 | + var hoursToDegrees = 360f / 12f; |
| 54 | + var minutesToDegrees = 360f / 60f; |
| 55 | + var secondsToDegrees = 360f / 60f; |
| 56 | + |
| 57 | + var hoursObject = hoursGameObject.Value; |
| 58 | + var minutesObject = minutesGameObject.Value; |
| 59 | + var secondsObject = secondsGameObject.Value; |
| 60 | + var millsecondsObject = millisecondsGameObject.Value; |
| 61 | + |
| 62 | + milliseconds.Value = int.Parse(DateTime.Now.ToString("fff")); |
| 63 | + seconds.Value = int.Parse(DateTime.Now.ToString("ss")); |
| 64 | + minutes.Value = int.Parse(DateTime.Now.ToString("mm")); |
| 65 | + hours.Value = int.Parse(DateTime.Now.ToString("hh")); |
| 66 | + |
| 67 | + |
| 68 | + hoursObject.transform.localRotation = Quaternion.Euler(0f, 0f, (float)timespan.TotalHours * -hoursToDegrees); |
| 69 | + minutesObject.transform.localRotation = Quaternion.Euler(0f, 0f, (float)timespan.TotalMinutes * -minutesToDegrees); |
| 70 | + secondsObject.transform.localRotation = Quaternion.Euler(0f, 0f, time.Second * -secondsToDegrees); |
| 71 | + millsecondsObject.transform.localRotation = Quaternion.Euler(0f, 0f, (float)timespan.TotalSeconds * -secondsToDegrees); |
| 72 | + |
| 73 | + if (debug) |
| 74 | + { |
| 75 | + Debug.DrawLine(hoursObject.transform.position, hoursObject.transform.position + hoursObject.transform.up * 1.5f, Color.yellow); |
| 76 | + Debug.DrawLine(minutesObject.transform.position, minutesObject.transform.position + minutesObject.transform.up * 1.5f, Color.yellow); |
| 77 | + Debug.DrawLine(secondsObject.transform.position, secondsObject.transform.position + secondsObject.transform.up * 1.5f, Color.yellow); |
| 78 | + Debug.DrawLine(millsecondsObject.transform.position, millsecondsObject.transform.position + millsecondsObject.transform.up * 1.5f, Color.red); |
| 79 | + } |
| 80 | + |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments