-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entity.cs
235 lines (203 loc) · 6.89 KB
/
Entity.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using UnityEngine;
public class Entity : MonoBehaviour
{
[SerializeField] protected int MAX_HP = -1;
[SerializeField] protected int hp = -1; // デバッグのためにSerializeFieldにしてる
/**<summary> トゲによるダメージを受けるか </summary>*/
[SerializeField] public bool thornDamage = false;
[SerializeField] public bool isCreature = false;
[SerializeField] public bool hasTreasure = false;
/**<summary> 無敵時間 </summary>*/
protected readonly float INVI_TIME = .5f;
protected float lastDamagedTime;
protected uint bornPositionHash { get; private set; }
// Start is called before the first frame update
protected virtual void Start()
{
bornPositionHash = GetHashFromPosition();
if(TryGetComponent(out SpriteRenderer renderer))
{
renderer.sortingOrder = 0;
}
hp = MAX_HP;
lastDamagedTime = -10000;
}
// Update is called once per frame
protected virtual void Update()
{
Game game = Game.Get();
// 無敵時間中の点滅
if (isCreature)
{
int blinkingCount = 5;
float phase = (game.time - lastDamagedTime) / INVI_TIME;
if (phase < 1)
{
bool disappeared
= (int)(phase * blinkingCount * 2) % 2 == 0;
SpriteRenderer renderer = GetComponent<SpriteRenderer>();
Color color = renderer.color;
color.a = disappeared ? 0 : 1;
renderer.color = color;
}
}
}
public virtual bool Damaged(int damage = 1)
{
Game game = Game.Get();
bool damaged = false;
bool die = false;
if (
hp != -1 && // HPの概念がある
game.time - lastDamagedTime >= INVI_TIME // 前回のダメージから一定時間経過
)
{
hp = Mathf.Max(hp - damage, 0);
if (hp > 0)
{
lastDamagedTime = game.time;
if (isCreature)
{
PlayDamageSound();
}
}
else
{
die = true;
}
damaged = true;
}
if(damaged && isCreature)
{
// ダメージの数値を表示
GenerateDamageNumberEffect(damage);
}
if (die)
{
Die();
}
return damaged;
}
public virtual void Die()
{
hp = 0;
Game game = Game.Get();
if (isCreature)
{
PlayDeathSound();
}
// お宝を持っていれば
if (hasTreasure)
{
// お宝を落とす
string name = "Jewel";
GameObject prefab = Resources.Load<GameObject>(@"Prefabs/" + name);
GameObject obj = Instantiate(
prefab,
transform.position,
Quaternion.Euler(0, 0, 0)
);
obj.name = name;
}
Destroy(gameObject);
}
protected void GenerateDamageNumberEffect(int damage)
{
float radius = Random.Range(0.25f, 0.5f);
float theta = Random.Range(0, 2 * Mathf.PI);
// 中心からの位置ズレ
Vector2 pos = radius * new Vector2(Mathf.Cos(theta), Mathf.Sin(theta));
// ダメージの値のエフェクトを生成
string name = "DamageNumberEffect";
GameObject prefab = Resources.Load<GameObject>(@"Prefabs/" + name);
GameObject obj = Instantiate(
prefab,
transform.position + (Vector3)pos,
Quaternion.Euler(0, 0, 0)
);
obj.name = name;
DamageNumberEffect damageNumber
= obj.GetComponent<DamageNumberEffect>();
damageNumber.number = damage;
}
protected virtual void PlayDamageSound()
{
Game game = Game.Get();
game.audioSource.PlayOneShot(
game.enemyDamageSound,
0.55f
);
}
protected virtual void PlayDeathSound()
{
Game game = Game.Get();
game.audioSource.PlayOneShot(
game.enemyDeathSound,
0.45f
);
}
/**
<summary>
現在値から符号無し32ビットの乱数を生成
</summary>
*/
private uint GetHashFromPosition()
{
byte[] xBytes = System.BitConverter.GetBytes(transform.position.x);
byte[] yBytes = System.BitConverter.GetBytes(transform.position.y);
var md5 = new MD5CryptoServiceProvider();
byte[] hash = md5.ComputeHash(xBytes.Concat(yBytes).ToArray());
return (uint)System.BitConverter.ToUInt64(hash);
}
// dir方向に触れているEntityを返す
protected List<Entity> TouchingEntites(Vector2 dir)
{
if(dir != Vector2.zero)
{
dir = dir.normalized;
}
float width = transform.localScale.x;
float height = transform.localScale.y;
Vector3 left = Quaternion.Euler(0, 0, -90) * dir;
Vector3 right = Quaternion.Euler(0, 0, 90) * dir;
Vector3 down = dir;
Vector3 leftStartPoint = transform.position + left * 0.45f * width + down * height * 0.5f;
Vector3 rightStartPoint = transform.position + right * 0.45f * width + down * height * 0.5f;
Vector3 leftEndPoint = leftStartPoint + down * height * 1f / 64f;
Vector3 rightEndPoint = rightStartPoint + down * height * 1f / 64f;
Debug.DrawRay(leftStartPoint, leftEndPoint - leftStartPoint, Color.red);
Debug.DrawRay(rightStartPoint, rightEndPoint - rightStartPoint, Color.red);
Debug.DrawRay(leftEndPoint, rightEndPoint - leftEndPoint, Color.red);
RaycastHit2D[] hitsLeft, hitsRight, hitsCenter;
hitsLeft = Physics2D.LinecastAll(leftStartPoint, leftEndPoint);
hitsRight = Physics2D.LinecastAll(leftStartPoint, rightEndPoint);
hitsCenter = Physics2D.LinecastAll(leftEndPoint, rightEndPoint);
List<RaycastHit2D> hits = new List<RaycastHit2D>();
hits.AddRange(hitsLeft);
hits.AddRange(hitsRight);
hits.AddRange(hitsCenter);
IEnumerable<GameObject> objects
= hits.Select(h => h.collider.gameObject);
objects = objects.Distinct();
objects = objects.Where(o => o.TryGetComponent(out Entity _));
return objects.Select(o => o.GetComponent<Entity>()).ToList();
}
// dir方向に触れているRigidbodyを持つEntityを返す
protected List<Entity> TouchingRigidbodies(Vector2 dir)
{
return TouchingEntites(dir)
.Where(e => e.TryGetComponent(out Rigidbody2D _))
.ToList();
}
// dir方向に触れているKinematicなEntityを返す
protected List<Entity> TouchingKinematics(Vector2 dir)
{
return TouchingRigidbodies(dir)
.Where(e => e.GetComponent<Rigidbody2D>().isKinematic)
.ToList();
}
}