-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleft_PlayerManager.cs
352 lines (307 loc) · 10.8 KB
/
left_PlayerManager.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using System;
using System.Linq;
using Unity.VisualScripting;
public class left_PlayerManager : MonoBehaviour
{
private List<Joycon> m_joycons;
private Joycon m_joyconL;
private Joycon m_joyconR;
private Joycon.Button? m_pressedButtonL;
private Joycon.Button? m_pressedButtonR;
private static readonly Joycon.Button[] m_buttons =
Enum.GetValues(typeof(Joycon.Button)) as Joycon.Button[];
public float moveSpeed = 3f; // 移動速度
public float attackRadius; // 攻撃範囲
public Transform highShotPoint; // 高い位置のショットポイント
public Transform CriticalShotPoint;//必殺時のショットポイント
public Transform lowShotPoint; // 低い位置のショットポイント
public Transform shotPoint; // ショットポイント
public GameObject deathEffectPrefab; // 死亡エフェクトのプレハブ
public GameObject GuardObject; // ガードオブジェクト
public GameObject bulletPrefab; // 弾のプレハブ
public GameObject CriticalbulletPrefab;//必殺のプレハブ
public float jumpForce = 5f; // ジャンプ力
public float jump_cnt = 0; // ジャンプ回数
public bool isRight; // 右向きかどうか
public float hp = 10; // ヒットポイント
public float coolTime = 0.3f; //待機時間
public float leftCoolTime; //待機している時間
// HpGauge関連のフィールド
[SerializeField] private Image healthImage;
[SerializeField] private Image burnImage;
[SerializeField] private Image CriticalImage;
public float duration = 0.5f;
public float strength = 20f;
public int vibrate = 100;
private float healthcurrentRate = 1f;
private float criticalcurrentRate = 1f;
Rigidbody2D rb;
Animator animator;
void Start()
{
rb = GetComponent<Rigidbody2D>(); // Rigidbody2Dを取得
animator = GetComponent<Animator>(); // Animatorを取得
HealthSetGauge(1f); // HpGaugeの初期化
CriticalSetGauge(0f);
m_joycons = JoyconManager.Instance.j;
if (m_joycons == null || m_joycons.Count <= 0) return;
m_joyconL = m_joycons.Find(c => c.isLeft);
m_joyconR = m_joycons.Find(c => !c.isLeft);
}
private void Update()
{
JoyControll();
Movement(); // 移動処理
if (m_joyconL != null)
{
Vector3 accel = m_joyconL.GetAccel();
DetectVerticalShake(accel);
}
Shot(); // ショット処理
Jump(); // ジャンプ処理
Guard(); // ガード処理
}
void Movement_1()
{
float x = Input.GetAxis("JoystickHorizontal"); // 方向キー横の入力を取得
if (!isRight && x > 0)
{
transform.Rotate(0f, 180f, 0f); // 右向きに回転
isRight = true;
}
if (isRight && x < 0)
{
transform.Rotate(0f, 180f, 0f); // 左向きに回転
isRight = false;
}
animator.SetFloat("Speed", Mathf.Abs(x)); // アニメーションの速度を設定
rb.velocity = new Vector2(x * moveSpeed, rb.velocity.y); // 移動速度を設定
}
void Movement()
{
// Joy-Conのスティック入力を取得
float x = 0f;
//if (m_joyconL != null)
//{
// x += m_joyconL.GetStick()[0]; // 左Joy-Conの横軸入力を取得
//}
if (m_joyconL != null)
{
x += m_joyconL.GetStick()[0]; // 右Joy-Conの横軸入力を取得
}
if (!isRight && x > 0)
{
transform.Rotate(0f, 180f, 0f); // 右向きに回転
isRight = true;
}
if (isRight && x < 0)
{
transform.Rotate(0f, 180f, 0f); // 左向きに回転
isRight = false;
}
animator.SetFloat("Speed", Mathf.Abs(x)); // アニメーションの速度を設定
rb.velocity = new Vector2(x * moveSpeed, rb.velocity.y); // 移動速度を設定
}
void Shot()
{
leftCoolTime -= Time.deltaTime;
float flag = 0;
if (m_joyconL.GetButton(Joycon.Button.DPAD_RIGHT))
{
shotPoint = highShotPoint; // 高い位置からショット
flag = 1;
}
if (m_joyconL.GetButton(Joycon.Button.DPAD_DOWN))
{
shotPoint = lowShotPoint; // 低い位置からショット
flag = 1;
}
if (flag == 1 && leftCoolTime <= 0)
{
animator.SetTrigger("attack"); // 攻撃アニメーションを再生
Instantiate(bulletPrefab, shotPoint.position, transform.rotation); // 弾を生成
leftCoolTime = coolTime;
}
}
void Jump()
{
Vector3 pos = rb.transform.position;
if (jump_cnt < 2)
{
if (Input.GetButtonDown("Jump"))
{
animator.SetTrigger("jump"); // ジャンプアニメーションを再生
rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse); // ジャンプ力を加える
jump_cnt++;
}
}
if (-2.65f < pos.y && pos.y < -2.50f)
{
jump_cnt = 0; // ジャンプ回数をリセット
}
}
void Guard()
{
float flag = 0;
if (m_joyconL.GetButton(Joycon.Button.DPAD_UP))
{
shotPoint = highShotPoint; // 高い位置でガード
flag = 1;
}
if (m_joyconL.GetButton(Joycon.Button.DPAD_LEFT))
{
shotPoint = lowShotPoint; // 低い位置でガード
flag = 1;
}
if (flag == 1)
{
animator.SetTrigger("guard"); // ガードアニメーションを再生
Instantiate(GuardObject, shotPoint.position, transform.rotation); // ガードオブジェクトを生成
}
}
public void OnDamage()
{
Debug.Log("ダメージを受けた"); // デバッグログを追加
hp -= 1; // ヒットポイントを減少
TakeDamage(0.1f); // HpGaugeの更新
if (hp <= 0)
{
Instantiate(deathEffectPrefab, transform.position, transform.rotation); // 死亡エフェクトを生成
Destroy(gameObject); // プレイヤーオブジェクトを破壊
m_joyconL.SetRumble(160, 320, 0.6f, 200);
}
}
// HpGauge関連のメソッド
public void HealthSetGauge(float value)
{
//DoTweenを連結して動かす
healthImage.DOFillAmount(value, duration)
.OnComplete(() =>
{
burnImage
.DOFillAmount(value, duration / 2f)
.SetDelay(0.5f);
});
//transform.DOShakePosition(
// duration / 2f,
// strength, vibrate);
// シェーダーの確認
Material material = healthImage.material;
Debug.Log("使用しているシェーダー: " + material.shader.name);
// マテリアルのプロパティ確認
Debug.Log("マテリアルのプロパティ: " + material.GetTexture("_MainTex"));
healthcurrentRate = value;
}
public void CriticalSetGauge(float value)
{
//DoTweenを連結して動かす
CriticalImage.DOFillAmount(value, duration);
//.OnComplete(() =>
// {
// burnImage
// .DOFillAmount(value, duration / 2f)
// .SetDelay(0.5f);
// });
//transform.DOShakePosition(
// duration / 2f,
// strength, vibrate);
// シェーダーの確認
Material material = CriticalImage.material;
Debug.Log("使用しているシェーダー: " + material.shader.name);
// マテリアルのプロパティ確認
Debug.Log("マテリアルのプロパティ: " + material.GetTexture("_MainTex"));
criticalcurrentRate = value;
}
public void TakeDamage(float rate)
{
HealthSetGauge(healthcurrentRate - rate);
}
public void GuardClear(float rate)
{
CriticalSetGauge(criticalcurrentRate + rate);
}
private void JoyControll()
{
m_pressedButtonL = null;
m_pressedButtonR = null;
if (m_joycons == null || m_joycons.Count <= 0) return;
foreach (var button in m_buttons)
{
if (m_joyconL.GetButton(button))
{
m_pressedButtonL = button;
}
if (m_joyconR.GetButton(button))
{
m_pressedButtonR = button;
}
}
if (Input.GetKeyDown(KeyCode.Z))
{
m_joyconL.SetRumble(160, 320, 0.6f, 200);
}
if (Input.GetKeyDown(KeyCode.X))
{
m_joyconR.SetRumble(160, 320, 0.6f, 200);
}
}
private void DetectVerticalShake(Vector3 accel)
{
float threshold = 1.0f; // 閾値の設定
if (accel.x > threshold)
{
Debug.Log("Vertical Shake Detected!");
// 縦振りが検出された場合の処理
animator.SetTrigger("attack"); // 攻撃アニメーションを再生
Instantiate(CriticalbulletPrefab, CriticalShotPoint.position, transform.rotation); // 弾を生成
leftCoolTime = coolTime;
}
}
private void OnGUI()
{
var style = GUI.skin.GetStyle("label");
style.fontSize = 24;
if (m_joycons == null || m_joycons.Count <= 0)
{
GUILayout.Label("Joy-Con が接続されていません");
return;
}
if (!m_joycons.Any(c => c.isLeft))
{
GUILayout.Label("Joy-Con (L) が接続されていません");
return;
}
if (!m_joycons.Any(c => !c.isLeft))
{
GUILayout.Label("Joy-Con (R) が接続されていません");
return;
}
GUILayout.BeginHorizontal(GUILayout.Width(960));
foreach (var joycon in m_joycons)
{
var isLeft = joycon.isLeft;
var name = isLeft ? "Joy-Con (L)" : "Joy-Con (R)";
var key = isLeft ? "Z キー" : "X キー";
var button = isLeft ? m_pressedButtonL : m_pressedButtonR;
var stick = joycon.GetStick();
var gyro = joycon.GetGyro();
var accel = joycon.GetAccel();
var orientation = joycon.GetVector();
GUILayout.BeginVertical(GUILayout.Width(480));
GUILayout.Label(name);
GUILayout.Label(key + ":振動");
GUILayout.Label("押されているボタン:" + button);
GUILayout.Label(string.Format("スティック:({0}, {1})", stick[0], stick[1]));
GUILayout.Label("ジャイロ:" + gyro);
GUILayout.Label("加速度:" + accel);
GUILayout.Label("傾き:" + orientation);
GUILayout.EndVertical();
}
GUILayout.EndHorizontal();
}
}