-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemyScript.cs
More file actions
112 lines (98 loc) · 2.82 KB
/
EnemyScript.cs
File metadata and controls
112 lines (98 loc) · 2.82 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyScript : MonoBehaviour
{
private float speed = 1.5f;
private float prevTime = 0.0f;
private bool isGoingLeft = true;
private UIScript UI = null;
private GameManagerScript GM = null;
private PlayerScript player = null;
[SerializeField] private GameObject enemyDeathPrefab = null;
[SerializeField] private GameObject enemyDamagePrefab = null;
[SerializeField] private int enemyId;
[SerializeField] private int lives;
// Start is called before the first frame update
void Start()
{
GM = GameObject.Find("GameManager").GetComponent<GameManagerScript>();
UI = GameObject.Find("Canvas").GetComponent<UIScript>();
if (enemyId == 0)
{
speed = 1.5f;
lives = 0;
}
else if (enemyId == 1)
{
speed = 2.0f;
lives = 0;
}
}
// Update is called once per frame
void Update()
{
if (GM.gameOver)
{
Destroy(this.gameObject);
}
if (isGoingLeft)
{
transform.Translate(Vector3.left * speed * Time.deltaTime);
}
else
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
if (Time.time > prevTime + 1.0f)
{
if (Random.Range(0, 10) > 5)
{
isGoingLeft = !isGoingLeft;
}
prevTime = Time.time;
}
if (transform.position.x >= 8.2f)
{
isGoingLeft = true;
}
else if (transform.position.x <= -8.2f)
{
isGoingLeft = false;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Laser"))
{
if (collision.transform.parent != null)
Destroy(collision.transform.parent.gameObject);
else
Destroy(collision.gameObject);
}
else if (collision.CompareTag("Player"))
{
//lives?
PlayerScript P = collision.GetComponent<PlayerScript>();
if (P != null) P.Damage();
}
if (enemyId == 1 && lives >= 1)
{
Instantiate(enemyDamagePrefab, transform.position, Quaternion.identity);
lives--;
}
else
{
if (enemyId == 0)
{
UI.UpdateScore(10);
}
else
{
UI.UpdateScore(15);
}
Instantiate(enemyDeathPrefab, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
}