-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
MysteryShip.cs
93 lines (74 loc) · 2.18 KB
/
MysteryShip.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
using UnityEngine;
[RequireComponent(typeof(BoxCollider2D))]
public class MysteryShip : MonoBehaviour
{
public float speed = 5f;
public float cycleTime = 30f;
public int score = 300;
private Vector2 leftDestination;
private Vector2 rightDestination;
private int direction = -1;
private bool spawned;
private void Start()
{
// Transform the viewport to world coordinates so we can set the mystery
// ship's destination points
Vector3 leftEdge = Camera.main.ViewportToWorldPoint(Vector3.zero);
Vector3 rightEdge = Camera.main.ViewportToWorldPoint(Vector3.right);
// Offset each destination by 1 unit so the ship is fully out of sight
leftDestination = new Vector2(leftEdge.x - 1f, transform.position.y);
rightDestination = new Vector2(rightEdge.x + 1f, transform.position.y);
Despawn();
}
private void Update()
{
if (!spawned) return;
if (direction == 1) {
MoveRight();
} else {
MoveLeft();
}
}
private void MoveRight()
{
transform.position += speed * Time.deltaTime * Vector3.right;
if (transform.position.x >= rightDestination.x) {
Despawn();
}
}
private void MoveLeft()
{
transform.position += speed * Time.deltaTime * Vector3.left;
if (transform.position.x <= leftDestination.x) {
Despawn();
}
}
private void Spawn()
{
direction *= -1;
if (direction == 1) {
transform.position = leftDestination;
} else {
transform.position = rightDestination;
}
spawned = true;
}
private void Despawn()
{
spawned = false;
if (direction == 1) {
transform.position = rightDestination;
} else {
transform.position = leftDestination;
}
Invoke(nameof(Spawn), cycleTime);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.layer == LayerMask.NameToLayer("Laser"))
{
Despawn();
GameManager.Instance.OnMysteryShipKilled(this);
}
}
}