2
|
1 using UnityEngine;
|
|
2 using System.Collections;
|
|
3
|
|
4 /// キャラクター基底クラス.
|
|
5 /// SpriteRendererが必要.
|
|
6 [RequireComponent (typeof(SpriteRenderer))]
|
|
7 public class Token : MonoBehaviour
|
|
8 {
|
|
9 /// プレハブ取得.
|
|
10 /// プレハブは必ず"Resources/Prefabs/"に配置すること.
|
|
11 public static GameObject GetPrefab (GameObject prefab, string name)
|
|
12 {
|
|
13 return prefab ?? (prefab = Resources.Load ("Prefabs/" + name) as GameObject);
|
|
14 }
|
|
15
|
|
16 /// インスタンスを生成してスクリプトを返す.
|
|
17 public static Type CreateInstance<Type> (GameObject prefab, Vector3 p, float direction = 0.0f, float speed = 0.0f) where Type : Token
|
|
18 {
|
|
19 GameObject g = Instantiate (prefab, p, Quaternion.identity) as GameObject;
|
|
20 Type obj = g.GetComponent<Type> ();
|
|
21 obj.SetVelocity (direction, speed);
|
|
22 return obj;
|
|
23 }
|
|
24
|
|
25 public static Type CreateInstance2<Type> (GameObject prefab, float x, float y, float direction = 0.0f, float speed = 0.0f) where Type : Token
|
|
26 {
|
|
27 Vector3 pos = new Vector3 (x, y, 0);
|
|
28 return CreateInstance<Type> (prefab, pos, direction, speed);
|
|
29 }
|
|
30
|
|
31 /// 生存フラグ.
|
|
32 bool _exists = false;
|
|
33
|
|
34 public bool Exists {
|
|
35 get { return _exists; }
|
|
36 set { _exists = value; }
|
|
37 }
|
|
38
|
|
39 /// アクセサ.
|
|
40 /// レンダラー.
|
|
41 SpriteRenderer _renderer = null;
|
|
42
|
|
43 public SpriteRenderer Renderer {
|
|
44 get { return _renderer ?? (_renderer = gameObject.GetComponent<SpriteRenderer> ()); }
|
|
45 }
|
|
46
|
|
47 /// 描画フラグ.
|
|
48 public bool Visible {
|
|
49 get { return Renderer.enabled; }
|
|
50 set { Renderer.enabled = value; }
|
|
51 }
|
|
52
|
|
53 /// ソーティングレイヤー名.
|
|
54 public string SortingLayer {
|
|
55 get { return Renderer.sortingLayerName; }
|
|
56 set { Renderer.sortingLayerName = value; }
|
|
57 }
|
|
58
|
|
59 /// ソーティング・オーダー.
|
|
60 public int SortingOrder {
|
|
61 get { return Renderer.sortingOrder; }
|
|
62 set { Renderer.sortingOrder = value; }
|
|
63 }
|
|
64
|
|
65 /// 座標(X).
|
|
66 public float X {
|
|
67 set {
|
|
68 Vector3 pos = transform.position;
|
|
69 pos.x = value;
|
|
70 transform.position = pos;
|
|
71 }
|
|
72 get { return transform.position.x; }
|
|
73 }
|
|
74
|
|
75 /// 座標(Y).
|
|
76 public float Y {
|
|
77 set {
|
|
78 Vector3 pos = transform.position;
|
|
79 pos.y = value;
|
|
80 transform.position = pos;
|
|
81 }
|
|
82 get { return transform.position.y; }
|
|
83 }
|
|
84
|
|
85 /// 座標を足し込む.
|
|
86 public void AddPosition (float dx, float dy)
|
|
87 {
|
|
88 X += dx;
|
|
89 Y += dy;
|
|
90 }
|
|
91
|
|
92 /// 座標を設定する.
|
|
93 public void SetPosition (float x, float y)
|
|
94 {
|
|
95 Vector3 pos = transform.position;
|
|
96 pos.Set (x, y, 0);
|
|
97 transform.position = pos;
|
|
98 }
|
|
99
|
|
100 /// スケール値(X).
|
|
101 public float ScaleX {
|
|
102 set {
|
|
103 Vector3 scale = transform.localScale;
|
|
104 scale.x = value;
|
|
105 transform.localScale = scale;
|
|
106 }
|
|
107 get { return transform.localScale.x; }
|
|
108 }
|
|
109
|
|
110 /// スケール値(Y).
|
|
111 public float ScaleY {
|
|
112 set {
|
|
113 Vector3 scale = transform.localScale;
|
|
114 scale.y = value;
|
|
115 transform.localScale = scale;
|
|
116 }
|
|
117 get { return transform.localScale.y; }
|
|
118 }
|
|
119
|
|
120 /// スケール値を設定.
|
|
121 public void SetScale (float x, float y)
|
|
122 {
|
|
123 Vector3 scale = transform.localScale;
|
|
124 scale.Set (x, y, (x + y) / 2);
|
|
125 transform.localScale = scale;
|
|
126 }
|
|
127
|
|
128 /// スケール値(X/Y).
|
|
129 public float Scale {
|
|
130 get {
|
|
131 Vector3 scale = transform.localScale;
|
|
132 return (scale.x + scale.y) / 2.0f;
|
|
133 }
|
|
134 set {
|
|
135 Vector3 scale = transform.localScale;
|
|
136 scale.x = value;
|
|
137 scale.y = value;
|
|
138 transform.localScale = scale;
|
|
139 }
|
|
140 }
|
|
141
|
|
142 /// スケール値を足し込む.
|
|
143 public void AddScale (float d)
|
|
144 {
|
|
145 Vector3 scale = transform.localScale;
|
|
146 scale.x += d;
|
|
147 scale.y += d;
|
|
148 transform.localScale = scale;
|
|
149 }
|
|
150
|
|
151 /// スケール値をかける.
|
|
152 public void MulScale (float d)
|
|
153 {
|
|
154 transform.localScale *= d;
|
|
155 }
|
|
156
|
|
157 /// 剛体.
|
|
158 Rigidbody2D _rigidbody2D = null;
|
|
159
|
|
160 public Rigidbody2D RigidBody {
|
|
161 get { return _rigidbody2D ?? (_rigidbody2D = gameObject.GetComponent<Rigidbody2D> ()); }
|
|
162 }
|
|
163
|
|
164 /// 移動量を設定.
|
|
165 public void SetVelocity (float direction, float speed)
|
|
166 {
|
|
167 Vector2 v;
|
|
168 v.x = Util.CosEx (direction) * speed;
|
|
169 v.y = Util.SinEx (direction) * speed;
|
|
170 RigidBody.velocity = v;
|
|
171 }
|
|
172
|
|
173 /// 移動量を設定(X/Y).
|
|
174 public void SetVelocityXY (float vx, float vy)
|
|
175 {
|
|
176 Vector2 v;
|
|
177 v.x = vx;
|
|
178 v.y = vy;
|
|
179 RigidBody.velocity = v;
|
|
180 }
|
|
181
|
|
182 /// 移動量をかける.
|
|
183 public void MulVelocity (float d)
|
|
184 {
|
|
185 RigidBody.velocity *= d;
|
|
186 }
|
|
187
|
|
188 /// 移動量(X).
|
|
189 public float VX {
|
|
190 get { return RigidBody.velocity.x; }
|
|
191 set {
|
|
192 Vector2 v = RigidBody.velocity;
|
|
193 v.x = value;
|
|
194 RigidBody.velocity = v;
|
|
195 }
|
|
196 }
|
|
197
|
|
198 /// 移動量(Y).
|
|
199 public float VY {
|
|
200 get { return RigidBody.velocity.y; }
|
|
201 set {
|
|
202 Vector2 v = RigidBody.velocity;
|
|
203 v.y = value;
|
|
204 RigidBody.velocity = v;
|
|
205 }
|
|
206 }
|
|
207
|
|
208 /// 方向.
|
|
209 public float Direction {
|
|
210 get {
|
|
211 Vector2 v = GetComponent<Rigidbody2D>().velocity;
|
|
212 return Mathf.Atan2 (v.y, v.x) * Mathf.Rad2Deg;
|
|
213 }
|
|
214 }
|
|
215
|
|
216 /// 速度.
|
|
217 public float Speed {
|
|
218 get {
|
|
219 Vector2 v = GetComponent<Rigidbody2D>().velocity;
|
|
220 return Mathf.Sqrt (v.x * v.x + v.y * v.y);
|
|
221 }
|
|
222 }
|
|
223
|
|
224 /// 重力.
|
|
225 public float GravityScale {
|
|
226 get { return RigidBody.gravityScale; }
|
|
227 set { RigidBody.gravityScale = value; }
|
|
228 }
|
|
229
|
|
230 /// 回転角度.
|
|
231 public float Angle {
|
|
232 set { transform.eulerAngles = new Vector3 (0, 0, value); }
|
|
233 get { return transform.eulerAngles.z; }
|
|
234 }
|
|
235
|
|
236 /// スプライトの設定.
|
|
237 public void SetSprite (Sprite sprite)
|
|
238 {
|
|
239 Renderer.sprite = sprite;
|
|
240 }
|
|
241
|
|
242 /// 色設定.
|
|
243 public void SetColor (float r, float g, float b)
|
|
244 {
|
|
245 var c = Renderer.color;
|
|
246 c.r = r;
|
|
247 c.g = g;
|
|
248 c.b = b;
|
|
249 Renderer.color = c;
|
|
250 }
|
|
251
|
|
252 /// アルファ値を設定.
|
|
253 public void SetAlpha (float a)
|
|
254 {
|
|
255 var c = Renderer.color;
|
|
256 c.a = a;
|
|
257 Renderer.color = c;
|
|
258 }
|
|
259
|
|
260 /// アルファ値を取得.
|
|
261 public float GetAlpha ()
|
|
262 {
|
|
263 var c = Renderer.color;
|
|
264 return c.a;
|
|
265 }
|
|
266
|
|
267 /// アルファ値.
|
|
268 public float Alpha {
|
|
269 set { SetAlpha (value); }
|
|
270 get { return GetAlpha (); }
|
|
271 }
|
|
272
|
|
273 /// サイズを設定.
|
|
274 float _width = 0.0f;
|
|
275 float _height = 0.0f;
|
|
276
|
|
277 public void SetSize (float width, float height)
|
|
278 {
|
|
279 _width = width;
|
|
280 _height = height;
|
|
281 }
|
|
282
|
|
283 /// スプライトの幅.
|
|
284 public float SpriteWidth {
|
|
285 get { return Renderer.bounds.size.x; }
|
|
286 }
|
|
287
|
|
288 /// スプライトの高さ.
|
|
289 public float SpriteHeight {
|
|
290 get { return Renderer.bounds.size.y; }
|
|
291 }
|
|
292
|
|
293 /// コリジョン(円).
|
|
294 CircleCollider2D _circleCollider = null;
|
|
295
|
|
296 public CircleCollider2D CircleCollider {
|
|
297 get { return _circleCollider ?? (_circleCollider = GetComponent<CircleCollider2D> ()); }
|
|
298 }
|
|
299 // 円コリジョンの半径.
|
|
300 public float CollisionRadius {
|
|
301 get { return CircleCollider.radius; }
|
|
302 set { CircleCollider.radius = value; }
|
|
303 }
|
|
304 // 円コリジョンの有効無効を設定する.
|
|
305 public bool CircleColliderEnabled {
|
|
306 get { return CircleCollider.enabled; }
|
|
307 set { CircleCollider.enabled = value; }
|
|
308 }
|
|
309
|
|
310 /// コリジョン(矩形).
|
|
311 BoxCollider2D _boxCollider = null;
|
|
312
|
|
313 public BoxCollider2D BoxCollider {
|
|
314 get { return _boxCollider ?? (_boxCollider = GetComponent<BoxCollider2D> ()); }
|
|
315 }
|
|
316
|
|
317 /// 矩形コリジョンの幅.
|
|
318 public float BoxColliderWidth {
|
|
319 get { return BoxCollider.size.x; }
|
|
320 set {
|
|
321 var size = BoxCollider.size;
|
|
322 size.x = value;
|
|
323 BoxCollider.size = size;
|
|
324 }
|
|
325 }
|
|
326
|
|
327 /// 矩形コリジョンの高さ.
|
|
328 public float BoxColliderHeight {
|
|
329 get { return BoxCollider.size.y; }
|
|
330 set {
|
|
331 var size = BoxCollider.size;
|
|
332 size.y = value;
|
|
333 BoxCollider.size = size;
|
|
334 }
|
|
335 }
|
|
336 // 箱コリジョンのサイズを設定する.
|
|
337 public void SetBoxColliderSize (float w, float h)
|
|
338 {
|
|
339 BoxCollider.size.Set (w, h);
|
|
340 }
|
|
341 // 箱コリジョンの有効無効を設定する
|
|
342 public bool BoxColliderEnabled {
|
|
343 get { return BoxCollider.enabled; }
|
|
344 set { BoxCollider.enabled = value; }
|
|
345 }
|
|
346
|
|
347 /// 移動して画面内に収めるようにする.
|
|
348 public void ClampScreenAndMove (Vector2 v)
|
|
349 {
|
|
350 Vector2 min = GetWorldMin ();
|
|
351 Vector2 max = GetWorldMax ();
|
|
352 Vector2 pos = transform.position;
|
|
353 pos += v;
|
|
354
|
|
355 // 画面内に収まるように制限をかける.
|
|
356 pos.x = Mathf.Clamp (pos.x, min.x, max.x);
|
|
357 pos.y = Mathf.Clamp (pos.y, min.y, max.y);
|
|
358
|
|
359 // プレイヤーの座標を反映.
|
|
360 transform.position = pos;
|
|
361 }
|
|
362
|
|
363 /// 画面内に収めるようにする.
|
|
364 public void ClampScreen ()
|
|
365 {
|
|
366 Vector2 min = GetWorldMin ();
|
|
367 Vector2 max = GetWorldMax ();
|
|
368 Vector2 pos = transform.position;
|
|
369 // 画面内に収まるように制限をかける.
|
|
370 pos.x = Mathf.Clamp (pos.x, min.x, max.x);
|
|
371 pos.y = Mathf.Clamp (pos.y, min.y, max.y);
|
|
372
|
|
373 // プレイヤーの座標を反映.
|
|
374 transform.position = pos;
|
|
375 }
|
|
376
|
|
377 /// 画面外に出たかどうか.
|
|
378 public bool IsOutside ()
|
|
379 {
|
|
380 Vector2 min = GetWorldMin ();
|
|
381 Vector2 max = GetWorldMax ();
|
|
382 Vector2 pos = transform.position;
|
|
383 if (pos.x < min.x || pos.y < min.y) {
|
|
384 return true;
|
|
385 }
|
|
386 if (pos.x > max.x || pos.y > max.y) {
|
|
387 return true;
|
|
388 }
|
|
389 return false;
|
|
390 }
|
|
391
|
|
392 /// 画面の左下のワールド座標を取得する.
|
|
393 public Vector2 GetWorldMin (bool noMergin = false)
|
|
394 {
|
|
395 Vector2 min = Camera.main.ViewportToWorldPoint (Vector2.zero);
|
|
396 if (noMergin) {
|
|
397 // そのまま返す.
|
|
398 return min;
|
|
399 }
|
|
400
|
|
401 // 自身のサイズを考慮する.
|
|
402 min.x += _width;
|
|
403 min.y += _height;
|
|
404 return min;
|
|
405 }
|
|
406
|
|
407 /// 画面右上のワールド座標を取得する.
|
|
408 public Vector2 GetWorldMax (bool noMergin = false)
|
|
409 {
|
|
410 Vector2 max = Camera.main.ViewportToWorldPoint (Vector2.one);
|
|
411 if (noMergin) {
|
|
412 // そのまま返す.
|
|
413 return max;
|
|
414 }
|
|
415
|
|
416 // 自身のサイズを考慮する.
|
|
417 max.x -= _width;
|
|
418 max.y -= _height;
|
|
419 return max;
|
|
420 }
|
|
421
|
|
422 /// 消滅(メモリから削除).
|
|
423 public void DestroyObj ()
|
|
424 {
|
|
425 Destroy (gameObject);
|
|
426 }
|
|
427
|
|
428 /// アクティブにする.
|
|
429 public virtual void Revive ()
|
|
430 {
|
|
431 gameObject.SetActive (true);
|
|
432 Exists = true;
|
|
433 Visible = true;
|
|
434 }
|
|
435
|
|
436 /// 消滅する(オーバーライド可能)
|
|
437 /// ただし base.Vanish()を呼ばないと消滅しなくなることに注意
|
|
438 public virtual void Vanish ()
|
|
439 {
|
|
440 VanishCannotOverride();
|
|
441 }
|
|
442 /// 消滅する(オーバーライド禁止)
|
|
443 public void VanishCannotOverride ()
|
|
444 {
|
|
445 gameObject.SetActive (false);
|
|
446 Exists = false;
|
|
447 }
|
|
448 }
|