-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer1Script.cs
More file actions
470 lines (426 loc) · 16.1 KB
/
Player1Script.cs
File metadata and controls
470 lines (426 loc) · 16.1 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
using JetBrains.Annotations;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UI;
using UnityEngine.UIElements;
using static UnityEditor.Experimental.GraphView.GraphView;
public class Player1Script : MonoBehaviour
{
// Static settings
public float moveSpeed = 0; // Note this float must be 'public' so it can be accessed by other scripts.
private float boostSpeed;
private float boostCooldownTimer = 0f;
private float boostDurationTimer = 0f;
private bool boostActive = false;
private float deadZoneX = 9.4f;
private float deadZoneY = 9.4f;
private float bulletCooldownTimer;
private int lastBulletFired = 0;
private UnityEngine.Object firedBullet;
private UnityEngine.Object bullet;
private List<SpriteRenderer> listOfBulletSprites = new List<SpriteRenderer>();
private GameObject uiBoostCooldown;
private TextMeshProUGUI uiBoostCooldownText;
private Vector3 lastPosition;
public Vector3 nextPosition;
public Vector3 inertialPosition;
public Vector3 desiredVelocity;
public Vector3 inertialVelocity;
public Vector3 resultantVelocity;
private float previousPositionTime = 0;
private float wAcceleration = 0f;
private float aAcceleration = 0f;
private float sAcceleration = 0f;
private float dAcceleration = 0f;
private bool wPressed = false;
private bool wPressedOnce = false;
private bool aPressed = false;
private bool sPressed = false;
private bool dPressed = false;
private bool fPressedOnce = false;
private bool mouse0PressedOnce = false;
private bool qPressed = false;
private bool ePressed = false;
private float resultantAngle = 0f;
private float desiredAngularVelocity = 0f;
private float inertialAngularVelocity = 0f;
private float brakeAngularVelocity = 0f;
private float resultantAngularVelocity = 0f;
private float previousAngle = 0f;
private float currentAngle = 0f;
private float previousRotationTime = 0f;
// Dynamic settings
public float moveSpeedDefault = 3;
public float boostFactor = 3;
public float boostCooldown = 1.0f;
public float boostDuration = 0.3f;
public float rotationSpeed = 1;
public float bulletCooldown = 1;
public int totalBulletNumber = 3;
public float engineAcceleration = 40f;
public float engineDeceleration = 1f;
public float fps = 100;
// Start is called before the first frame update
void Start()
{
Time.fixedDeltaTime = 1 / fps;
boostSpeed = moveSpeedDefault * boostFactor;
bulletCooldownTimer = bulletCooldown;
firedBullet = Resources.Load("FiredBullet");
bullet = Resources.Load("Bullet");
for (int bulletNumber = 0; bulletNumber < totalBulletNumber; bulletNumber++)
{
UnityEngine.Object bulletInstance = Instantiate(bullet);
SpriteRenderer bulletInstanceSprite = bulletInstance.GetComponent<SpriteRenderer>();
listOfBulletSprites.Add(bulletInstanceSprite);
BulletScript bulletInstanceScript = bulletInstance.GetComponent<BulletScript>();
bulletInstanceScript.totalBulletNumber = totalBulletNumber;
bulletInstanceScript.bulletID = bulletNumber * 2;
}
uiBoostCooldown = GameObject.FindGameObjectWithTag("UIBoostCooldown");
uiBoostCooldownText = uiBoostCooldown.GetComponent<TextMeshProUGUI>();
uiBoostCooldownText.text = "Boost Cooldown: 0.0";
//Component[] components = uiBoostCooldown.GetComponents(typeof(Component));
//foreach (Component component in components)
//{
// Debug.Log(component.ToString());
//}
lastPosition = transform.position;
previousAngle = transform.eulerAngles.z;
}
// Update is called once per frame.
void FixedUpdate()
{
//checkInputs();
//tickCooldowns();
//processInputs();
debug();
}
void Update()
{
tickCooldowns();
checkInputs();
processInputs();
}
void debug()
{
//Debug.Log("wPressed: " + wPressed.ToString() + ", inertialVelocity: " + inertialVelocity.magnitude.ToString() + ", FPS: " + fps.ToString());
//Here is some code to view velocity vectors for Player 1's movement.
//Debug.DrawLine(start: transform.position, end: transform.position + (desiredVelocity.normalized * 30), color: UnityEngine.Color.red, duration: Time.deltaTime);
//Debug.DrawLine(start: transform.position, end: transform.position + (inertialVelocity * 30), color: UnityEngine.Color.white, duration: Time.deltaTime);
}
void checkInputs()
{
// Let us reset the input values from last frame, for this frame.
wPressed = false;
wPressedOnce = false;
aPressed = false;
sPressed = false;
dPressed = false;
fPressedOnce = false;
mouse0PressedOnce = false;
qPressed = false;
ePressed = false;
// Let us update input values for this frame.
if (Input.GetKey(KeyCode.W))
{
wPressed = true;
}
if (Input.GetKeyDown(KeyCode.W))
{
wPressedOnce = true;
}
if (Input.GetKey(KeyCode.A))
{
aPressed = true;
}
if (Input.GetKey(KeyCode.S))
{
sPressed = true;
}
if (Input.GetKey(KeyCode.D))
{
dPressed = true;
}
if (Input.GetKeyDown(KeyCode.F))
{
fPressedOnce = true;
}
if (Input.GetKeyDown(KeyCode.Mouse0))
{
mouse0PressedOnce = true;
}
if (Input.GetKey(KeyCode.Q))
{
qPressed = true;
}
if (Input.GetKey(KeyCode.E))
{
ePressed = true;
}
}
void processInputs()
{
processMovementInputs();
rotateTowardsCursor();
calculateNextPosition();
//checkBoost();
checkFire();
}
void processMovementInputs()
{
if (wPressed)
{
wAcceleration = engineAcceleration;
}
else
{
wAcceleration = 0;
}
if (aPressed)
{
aAcceleration = engineAcceleration;
}
else
{
aAcceleration = 0;
}
if (sPressed)
{
sAcceleration = engineAcceleration;
}
else
{
sAcceleration = 0;
}
if (dPressed)
{
dAcceleration = engineAcceleration;
}
else
{
dAcceleration = 0;
}
}
void rotateTowardsCursor()
{
Vector3 a = transform.up;
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 b = (new Vector3(mousePosition.x, mousePosition.y) - transform.position).normalized;
currentAngle = Vector3.SignedAngle(a, b, transform.forward);
float angularAcceleration = MathF.Sign(resultantAngularVelocity) * rotationSpeed;
float theta = 0;
if (angularAcceleration != 0)
{
theta = (MathF.Pow(resultantAngularVelocity, 2) / (2 * angularAcceleration)) * (MathF.PI / 180);
}
Vector3 c = new Vector3(
(MathF.Cos(theta) * a.x) - (MathF.Sin(theta) * a.y),
(MathF.Sin(theta) * a.x) + (MathF.Cos(theta) * a.y)
);
if (previousRotationTime == 0)
{
inertialAngularVelocity = 0;
}
else
{
inertialAngularVelocity = resultantAngle / previousRotationTime;
}
desiredAngularVelocity = 0;
brakeAngularVelocity = 0;
if (MathF.Sign(currentAngle) * MathF.Sign(Vector3.SignedAngle(c, b, transform.forward)) == 1)
{
desiredAngularVelocity = MathF.Sign(currentAngle) * rotationSpeed * Time.deltaTime;
}
else
{
brakeAngularVelocity = -MathF.Sign(currentAngle) * rotationSpeed * Time.deltaTime;
}
resultantAngularVelocity = inertialAngularVelocity + desiredAngularVelocity + brakeAngularVelocity;
resultantAngle = resultantAngularVelocity * Time.deltaTime;
transform.Rotate(0, 0, resultantAngle);
//Debug.Log(MathF.Sign(currentAngle).ToString() + ", " + MathF.Sign(Vector3.SignedAngle(c, b, transform.forward)).ToString());
//Debug.Log("resultantAV: " + resultantAngularVelocity.ToString() + ", inertialAV: " + inertialAngularVelocity.ToString() +
// ", desiredAV: " + desiredAngularVelocity.ToString() + ", brakeAV: " + brakeAngularVelocity.ToString() +
// ", previousRotationTime: " + previousRotationTime.ToString() +
// ", resultantAngle: " + resultantAngle.ToString());
previousRotationTime = Time.deltaTime;
Debug.DrawLine(start: transform.position, end: transform.position + (a * 5), color: UnityEngine.Color.red, duration: Time.deltaTime);
Debug.DrawLine(start: transform.position, end: transform.position + (b * 5), color: UnityEngine.Color.white, duration: Time.deltaTime);
Debug.DrawLine(start: transform.position, end: transform.position + (c * 5), color: UnityEngine.Color.yellow, duration: Time.deltaTime);
}
void calculateNextPosition()
{
desiredVelocity = (
(transform.up * wAcceleration) +
(-Vector3.right * aAcceleration) +
(-transform.up * sAcceleration) +
(Vector3.right * dAcceleration)
) * Time.deltaTime;
inertialPosition = transform.position - lastPosition;
lastPosition = transform.position;
if (previousPositionTime == 0)
{
inertialVelocity = Vector3.zero;
}
else
{
float inertialMoveSpeedX = inertialPosition.x / previousPositionTime;
float inertialMoveSpeedY = inertialPosition.y / previousPositionTime;
inertialVelocity = new Vector3(inertialMoveSpeedX, inertialMoveSpeedY);
}
resultantVelocity = inertialVelocity + desiredVelocity;
nextPosition = resultantVelocity * Time.deltaTime;
transform.position += nextPosition;
moveSpeed = nextPosition.magnitude / Time.deltaTime;
previousPositionTime = Time.deltaTime;
//Debug.DrawLine(start: transform.position, end: transform.position + (inertialVelocity * 5), color: UnityEngine.Color.yellow, duration: Time.deltaTime);
//Debug.DrawLine(start: transform.position, end: transform.position + (desiredVelocity * 5), color: UnityEngine.Color.red, duration: Time.deltaTime);
}
void checkBoost()
{
if (fPressedOnce && boostCooldownTimer <= 0)
{
boostActive = true;
moveSpeed = boostSpeed;
boostDurationTimer = boostDuration;
uiBoostCooldownText.text = uiBoostCooldownText.text.Remove(16) + "0.0";
}
}
void checkFire()
{
if (mouse0PressedOnce)
{
bool bulletAvailable = false;
for (int i = lastBulletFired; i < listOfBulletSprites.Count; i++)
{
if (listOfBulletSprites[i].enabled)
{
listOfBulletSprites[i].enabled = false;
lastBulletFired = i;
bulletCooldownTimer = bulletCooldown;
bulletAvailable = true;
break;
}
}
if (!bulletAvailable)
{
return;
}
float radiusOffset = 0.6f;
float angle = transform.rotation.eulerAngles.z;
float x = radiusOffset * MathF.Sin((MathF.PI / 180) * (angle + 180));
float y = radiusOffset * MathF.Cos((MathF.PI / 180) * angle);
UnityEngine.Object firedBulletInstance = Instantiate(firedBullet, new Vector3(transform.position.x + x, transform.position.y + y, transform.position.z), transform.rotation);
FiredBulletScript firedBulletScript = firedBulletInstance.GetComponent<FiredBulletScript>();
firedBulletScript.playerMoveSpeed = moveSpeed;
}
}
bool InScene(Vector3 nextPosition)
{
bool InScene = true;
// Edges
if (nextPosition.x > deadZoneX)
{
InScene = false;
transform.position = new Vector3(deadZoneX, nextPosition.y, 0);
}
if (nextPosition.x < -1 * deadZoneX)
{
InScene = false;
transform.position = new Vector3(-1 * deadZoneX, nextPosition.y, 0);
}
if (nextPosition.y > deadZoneY)
{
InScene = false;
transform.position = new Vector3(nextPosition.x, deadZoneY, 0);
}
if (nextPosition.y < -1 * deadZoneY)
{
InScene = false;
transform.position = new Vector3(nextPosition.x, -1 * deadZoneY, 0);
}
// Corners
if (nextPosition.x > deadZoneX && nextPosition.y > deadZoneY)
{
InScene = false;
transform.position = new Vector3(deadZoneX, deadZoneY, 0);
}
if (nextPosition.x < -1 * deadZoneX && nextPosition.y > deadZoneY)
{
InScene = false;
transform.position = new Vector3(-1 * deadZoneX, deadZoneY, 0);
}
if (nextPosition.x < -1 * deadZoneX && nextPosition.y < -1 * deadZoneY)
{
InScene = false;
transform.position = new Vector3(-1 * deadZoneX, -1 * deadZoneY, 0);
}
if (nextPosition.x > deadZoneX && nextPosition.y < -1 * deadZoneY)
{
InScene = false;
transform.position = new Vector3(deadZoneX, -1 * deadZoneY, 0);
}
return InScene;
}
void tickCooldowns()
{
//// Boost cooldown.
//if (boostCooldownTimer > 0)
//{
// boostCooldownTimer -= Time.deltaTime;
// if (boostCooldownTimer <= 0)
// {
// uiBoostCooldownText.text = uiBoostCooldownText.text.Remove(16) + "0.0";
// }
// else
// {
// string uiBoostCooldownString = ((MathF.Truncate(boostCooldownTimer * 10) + 1) / 10f).ToString();
// if (uiBoostCooldownString.Length <= 1 || (uiBoostCooldownString.Length > 1 && uiBoostCooldownString[uiBoostCooldownString.Length-2] != '.'))
// {
// uiBoostCooldownString = uiBoostCooldownString + ".0";
// }
// uiBoostCooldownText.text = uiBoostCooldownText.text.Remove(16) + uiBoostCooldownString;
// }
//}
//// Boost duration.
//if (boostDurationTimer > 0)
//{
// boostDurationTimer -= Time.deltaTime;
//}
//else if (moveSpeed > moveSpeedDefault)
//{
// boostActive = false;
// boostCooldownTimer = boostCooldown;
// moveSpeed = moveSpeedDefault;
//}
// Fire cooldown.
if (bulletCooldownTimer > 0)
{
bulletCooldownTimer -= Time.deltaTime;
}
if (bulletCooldownTimer <= 0)
{
SpriteRenderer lastBulletFiredSprite = listOfBulletSprites[lastBulletFired];
lastBulletFiredSprite.enabled = true;
if (lastBulletFired > 0)
{
lastBulletFired -= 1;
bulletCooldownTimer = bulletCooldown;
}
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
//if (collision.collider.tag == "FiredBullet")
//{
// Destroy(gameObject);
//}
}
}