1+ using System . Collections . Generic ;
12using UnityEngine ;
23
34
45public class EnemyScript : MonoBehaviour
56{
6- public InventoryScript inventoryScript ;
7+ // public InventoryScript inventoryScript;
78 public GameControlScript gameControlScript ;
89
9- public GameObject bodyPrefab ;
10- private GameObject player ;
10+ public GameObject bodyPrefab , bulletPrefab ;
11+ private List < GameObject > players = new List < GameObject > ( ) ;
12+ public GameObject shootingTarget ;
1113 public int job ;
1214 public float cooldown = 10 ;
1315 public int health = 100 ;
1416 private float speed = 1.2f ;
15- public float knifeCooldown = 0.5f ;
17+ public float knifeCooldown = 0.5f , attackCooldown = 0.5f ;
1618 float randomSkinColor ;
19+ private GameObject destinedTarget = null ;
1720 void Start ( )
1821 {
19- player = GameObject . Find ( "Player" ) ;
20- inventoryScript = GameObject . Find ( "Inventory" ) . GetComponent < InventoryScript > ( ) ;
22+
23+ // inventoryScript = GameObject.Find("Inventory").GetComponent<InventoryScript>();
2124 gameControlScript = GameObject . Find ( "GameControl" ) . GetComponent < GameControlScript > ( ) ;
22- /*
23- if(Random.Range(0f,4f) < 0.5f) {
24- Debug.Log("Inspector");
25- job = 1;
26- gameControlScript.amountOfInspectors++;
27- GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("Images/Inspector");
28- }
29- else job = 0;
30- */
25+
3126 if ( job == 1 )
3227 {
3328 gameControlScript . amountOfInspectors ++ ;
3429 GetComponent < SpriteRenderer > ( ) . sprite = Resources . Load < Sprite > ( "Images/Inspector" ) ;
3530 }
31+ if ( job == 2 )
32+ {
33+ health = 170 ;
34+ speed = 3 ;
35+ GetComponent < SpriteRenderer > ( ) . sprite = Resources . Load < Sprite > ( "Images/Police" ) ;
36+ }
37+
38+ foreach ( GameObject index in GameObject . FindGameObjectsWithTag ( "Player" ) )
39+ {
40+ if ( gameControlScript . canPlayer2Play )
41+ players . Add ( index ) ;
42+ else
43+ {
44+ if ( ! index . name . Contains ( "2" ) )
45+ players . Add ( index ) ;
46+ }
47+ }
48+
49+ destinedTarget = players [ Random . Range ( 0 , players . Count ) ] ;
3650
3751 health = 100 ;
3852 knifeCooldown = 0.5f ;
@@ -45,15 +59,17 @@ void Start()
4559
4660 void Update ( )
4761 {
48- if ( inventoryScript == null )
49- inventoryScript = GameObject . Find ( "Inventory" ) . GetComponent < InventoryScript > ( ) ;
62+
63+ //if(inventoryScript == null)
64+ // inventoryScript = GameObject.Find("Inventory").GetComponent<InventoryScript>();
5065
5166 if ( gameControlScript == null )
5267 gameControlScript = GameObject . Find ( "GameControl" ) . GetComponent < GameControlScript > ( ) ;
5368 if ( ! gameControlScript . ISPAUSED )
5469 {
5570 cooldown -= Time . deltaTime ;
56- knifeCooldown -= Time . deltaTime ;
71+ knifeCooldown -= Time . deltaTime ;
72+ attackCooldown -= Time . deltaTime ;
5773 }
5874 if ( health <= 0 ) {
5975 GameObject body = gameControlScript . CreateParticle ( bodyPrefab , transform . position , 2.5f , 2 , false , 0 ) ;
@@ -64,12 +80,35 @@ void Update()
6480 gameControlScript . amountOfInspectors -- ;
6581 Destroy ( gameObject ) ;
6682 }
83+
84+ if ( attackCooldown <= 0 && job == 2 )
85+ {
86+ GameObject bulletClone = Instantiate ( bulletPrefab , transform . position , Quaternion . identity ) ;
87+ GameObject target = getClosestPlayer ( ) ;
88+ Vector3 direction ;
89+
90+ if ( target == null )
91+ direction = transform . position + new Vector3 ( 10 , 0 , 0 ) ;
92+ else
93+ direction = target . transform . position - transform . position ;
94+
95+ bulletClone . transform . position += new Vector3 ( 0.5f , 0f , 0 ) ;
96+ float angle = Mathf . Atan2 ( direction . y , direction . x ) * Mathf . Rad2Deg ;
97+ bulletClone . transform . rotation = Quaternion . Euler ( 0f , 0f , angle ) ;
98+
99+ bulletClone . transform . Rotate ( 0 , 0 , Random . Range ( - 5f , 6f ) ) ;
100+ Rigidbody2D bulletbp = bulletClone . GetComponent < Rigidbody2D > ( ) ;
101+ bulletbp . linearVelocity = bulletClone . transform . right * 20 ;
102+ bulletClone . name += "Enemy" ;
103+ attackCooldown = 0.15f ;
104+ Destroy ( bulletClone , 2f ) ;
105+ }
67106 }
68107
69108 void FixedUpdate ( )
70109 {
71110 if ( cooldown <= 0 && ! gameControlScript . ISPAUSED )
72- MoveTowards ( player . transform . position ) ;
111+ MoveTowards ( destinedTarget . transform . position ) ;
73112 }
74113 public void MoveTowards ( Vector3 targetPosition )
75114 {
@@ -96,27 +135,56 @@ public void MoveTowards(Vector3 targetPosition)
96135
97136 private void OnTriggerEnter2D ( Collider2D collision )
98137 {
99- if ( collision . CompareTag ( "Bullet" ) )
138+ if ( collision . CompareTag ( "Bullet" ) && collision . name . Contains ( "Player" ) )
100139 {
101140 gameControlScript . Blood ( 1 , gameObject ) ;
102141 Destroy ( collision . gameObject ) ;
103142 health -= 20 ;
104143 }
105144 }
145+
106146 private void OnTriggerStay2D ( Collider2D collision )
107147 {
108- if ( collision . CompareTag ( "Player" ) && inventoryScript . currentItem ( ) . Contains ( "Knife" ) )
148+ if ( collision . CompareTag ( "Player" ) )
109149 {
110- if ( knifeCooldown <= 0 )
150+ InventoryScript inventoryScript = GameObject . Find ( "Inventory" ) . GetComponent < InventoryScript > ( ) ;
151+ if ( inventoryScript . currentItem ( ) . Contains ( "Knife" ) && knifeCooldown <= 0 )
111152 {
112153 gameControlScript . Blood ( 2 , gameObject ) ;
113- health -= 50 ;
154+ health -= 40 ;
114155 knifeCooldown = 0.5f ;
115156 }
157+ else
158+ {
159+ if ( attackCooldown <= 0 && job == 0 )
160+ {
161+ gameControlScript . Blood ( 2 , collision . gameObject ) ;
162+ collision . GetComponent < PlayerScript > ( ) . health -= 15 ;
163+ collision . GetComponent < PlayerScript > ( ) . checkHealth ( ) ;
164+ attackCooldown = 1f ;
165+ }
166+ }
116167 }
117- if ( collision . CompareTag ( "Player" ) )
168+ }
169+
170+ public GameObject getClosestPlayer ( )
171+ {
172+ GameObject [ ] players = GameObject . FindGameObjectsWithTag ( "Player" ) ;
173+
174+ GameObject closest = null ;
175+ float closestDistance = Mathf . Infinity ;
176+
177+ foreach ( GameObject player in players )
118178 {
179+ float distance = Vector3 . Distance ( player . transform . position , transform . position ) ;
180+
181+ if ( distance < closestDistance )
182+ {
183+ closestDistance = distance ;
184+ closest = player ;
185+ }
119186 }
120187
188+ return closest ;
121189 }
122190}
0 commit comments