-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathScratchyObject.cs
182 lines (160 loc) · 4.91 KB
/
ScratchyObject.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class ScratchyObject : MonoBehaviour
{
private int TimerLimit = 20;
private ScratchyTimer[] Timers;
public ScratchyObject()
{
Timers = new ScratchyTimer[TimerLimit];
}
// Use this for initialization
public virtual void Start()
{
this.OnStart();
}
// Update is called once per frame
public virtual void Update()
{
this.OnUpdate();
}
// FixedUpdate is called regularly
public void FixedUpdate()
{
// Update any timers
for (int i = 0; i < Timers.Length; i++ )
{
ScratchyTimer t = Timers[i];
if (t != null && t.Active)
{
t.Update(Time.fixedDeltaTime);
}
}
this.OnFixedUpdate();
}
// Never put anything in these virutal functions. They are for the derived class only
public virtual void OnStart()
{
}
public virtual void OnUpdate()
{
}
public virtual void OnFixedUpdate()
{
}
public ScratchyTimer Wait(float delay, TimerCallback callback)
{
ScratchyTimer timer = GetTimerFromPool();
timer.Init(delay, false, callback);
return timer;
}
public ScratchyTimer Forever(float every, TimerCallback callback)
{
ScratchyTimer timer = GetTimerFromPool();
timer.Init(every, true, callback);
return timer;
}
public ScratchyTimer Repeat(int count, float every, TimerCallback callback)
{
ScratchyTimer timer = GetTimerFromPool();
timer.Init(every, true, callback);
timer.CountLimit = count;
return timer;
}
private ScratchyTimer GetTimerFromPool()
{
ScratchyTimer timer = null;
int timerIndex = -1;
for (int i = 0; i < Timers.Length; i++ )
{
timer = Timers[i];
if (timer == null || !timer.Active)
{
timerIndex = i;
break;
}
}
if (timerIndex == -1)
{
throw new System.Exception("Object exceeded TimerLimit");
}
if (timer == null)
{
timer = new ScratchyTimer();
this.Timers[timerIndex] = timer;
}
return timer;
}
/// <summary>
/// Exit the game
/// </summary>
public void StopAll()
{
Debug.Log("stop all");
Application.Quit();
}
public void DrawRectangle(Bounds bt, Color c)
{
Debug.DrawLine(bt.min, new Vector3(bt.min.x, bt.max.y, bt.min.z), c);
Debug.DrawLine(bt.max, new Vector3(bt.min.x, bt.max.y, bt.min.z), c);
Debug.DrawLine(bt.min, new Vector3(bt.max.x, bt.min.y, bt.min.z), c);
Debug.DrawLine(bt.max, new Vector3(bt.max.x, bt.min.y, bt.min.z), c);
}
public void DrawDot(Vector3 pos, float size, Color c)
{
Debug.DrawLine(new Vector3(pos.x - size, pos.y, pos.z), new Vector3(pos.x + size, pos.y, pos.z), c);
Debug.DrawLine(new Vector3(pos.x, pos.y - size, pos.z), new Vector3(pos.x, pos.y + size, pos.z), c);
}
public List<ScratchySprite> GetSprites<T>() where T : ScratchySprite
{
Type t = typeof(T);
if (ScratchySprite.Instances.ContainsKey(t))
{
return ScratchySprite.Instances[t];
}
return new List<ScratchySprite>();
}
public void PointTowardsMouse(float angleAdjust = 0)
{
Vector3 objectInScreenSpace = Camera.main.WorldToScreenPoint(this.transform.position);
Vector3 mousePos = Input.mousePosition;
mousePos.x -= objectInScreenSpace.x;
mousePos.y -= objectInScreenSpace.y;
mousePos.z = objectInScreenSpace.z;
var angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, 0, angle + angleAdjust);
}
/// <summary>
/// Clone another object at the location of this ScratchySprite
/// </summary>
/// <param name="prefab">The prefab to instantiate</param>
/// <returns>The new cloned object</returns>
public ScratchySprite Clone(GameObject prefab)
{
return Clone(prefab, this.transform.position, this.transform.rotation);
}
public static ScratchySprite Clone(GameObject prefab, Vector3 position)
{
return Clone(prefab, position, Quaternion.identity);
}
public static ScratchySprite Clone(GameObject prefab, Vector3 position, Quaternion rotation)
{
var gameObject = (GameObject)Instantiate(prefab, position, Quaternion.identity);
ScratchySprite sprite;
try
{
sprite = gameObject.GetComponent<ScratchySprite>();
}
catch
{
sprite = null;
}
if (sprite == null)
{
throw new Exception("Clone only works for ScratchySprite objects");
}
return sprite;
}
}