2022-04-27 19:12:09 +02:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
2022-04-27 20:35:00 +02:00
|
|
|
//include textmeshpro
|
|
|
|
using TMPro;
|
2022-04-27 19:12:09 +02:00
|
|
|
|
|
|
|
public class enemykiller : MonoBehaviour
|
|
|
|
{
|
|
|
|
// Start is called before the first frame update
|
2022-04-27 19:22:52 +02:00
|
|
|
//get textmeshpro text
|
|
|
|
public TextMeshProUGUI text;
|
2022-04-27 19:12:09 +02:00
|
|
|
public ParticleSystem part;
|
|
|
|
public List<ParticleCollisionEvent> collisionEvents;
|
2022-04-27 19:22:52 +02:00
|
|
|
public int score = 0;
|
2022-04-28 19:47:24 +02:00
|
|
|
public int highscore = 0;
|
|
|
|
public int lives = 3;
|
2022-04-27 19:12:09 +02:00
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
part = GetComponent<ParticleSystem>();
|
|
|
|
collisionEvents = new List<ParticleCollisionEvent>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
//on particle coolision
|
|
|
|
void OnParticleCollision(GameObject otherx)
|
|
|
|
{
|
|
|
|
int numCollisionEvents = part.GetCollisionEvents(otherx, collisionEvents);
|
|
|
|
int i = 0;
|
|
|
|
while (i < numCollisionEvents)
|
|
|
|
{
|
|
|
|
GameObject other = collisionEvents[i].colliderComponent.gameObject;
|
2022-04-28 19:47:24 +02:00
|
|
|
//check if object exists
|
|
|
|
if (other == null)
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
2022-04-27 19:12:09 +02:00
|
|
|
if (other.tag == "enemy")
|
|
|
|
{
|
|
|
|
Destroy(other);
|
2022-04-28 19:47:24 +02:00
|
|
|
Debug.Log("enemy killed (" + other.name+ ")");
|
2022-04-27 19:22:52 +02:00
|
|
|
//spawn new enemy
|
|
|
|
GameObject enemyclone = Instantiate(other, new Vector3(Random.Range(10, 100), Random.Range(-70, -60), Random.Range(-90, 0)), Quaternion.identity);
|
|
|
|
//set enemy name
|
|
|
|
enemyclone.name = "enemy" + i;
|
|
|
|
enemyclone.tag = "enemy";
|
|
|
|
//increase score
|
|
|
|
score++;
|
2022-04-28 19:47:24 +02:00
|
|
|
if (score > highscore)
|
|
|
|
{
|
|
|
|
highscore = score;
|
|
|
|
}
|
2022-04-27 19:22:52 +02:00
|
|
|
//update text
|
2022-04-28 19:47:24 +02:00
|
|
|
text.text = "Score: " + score + "\nHighscore: " + highscore + "\nLives: " + lives;
|
2022-04-27 19:12:09 +02:00
|
|
|
}
|
|
|
|
if (other.tag == "king")
|
|
|
|
{
|
|
|
|
Destroy(other);
|
2022-04-28 19:47:24 +02:00
|
|
|
Debug.Log("killed king" + other.name);
|
2022-04-27 19:22:52 +02:00
|
|
|
//spawn new king
|
|
|
|
GameObject kingclone = Instantiate(other, new Vector3(Random.Range(10, 100), Random.Range(-70, -60), Random.Range(-90, -20)), Quaternion.identity);
|
|
|
|
//set king name
|
|
|
|
kingclone.name = "king" + i;
|
|
|
|
kingclone.tag = "king";
|
2022-04-28 19:47:24 +02:00
|
|
|
GameObject kingclone2 = Instantiate(other, new Vector3(Random.Range(10, 100), Random.Range(-70, -60), Random.Range(-90, -20)), Quaternion.identity);
|
|
|
|
//set king name
|
|
|
|
kingclone2.name = "kingy" + i;
|
|
|
|
kingclone2.tag = "king";
|
2022-04-27 19:22:52 +02:00
|
|
|
//decrease score
|
2022-04-28 19:47:24 +02:00
|
|
|
score-= 20;
|
|
|
|
lives--;
|
|
|
|
if (lives <= 0)
|
|
|
|
{
|
|
|
|
text.text = "GAME OVER";
|
|
|
|
}
|
2022-04-27 19:22:52 +02:00
|
|
|
//update text
|
2022-04-28 19:47:24 +02:00
|
|
|
text.text = "Score: " + score + "\nHighscore: " + highscore + "\nLives: " + lives;
|
2022-04-27 19:12:09 +02:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|