horiaci_drak/Assets/scripts/enemykiller.cs
2022-05-08 09:49:42 +02:00

88 lines
2.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//include textmeshpro
using TMPro;
public class enemykiller : MonoBehaviour
{
// Start is called before the first frame update
//get textmeshpro text
public TextMeshProUGUI text;
public ParticleSystem part;
public List<ParticleCollisionEvent> collisionEvents;
public int score = 0;
public int highscore = 0;
public int lives = 12;
void Start()
{
part = GetComponent<ParticleSystem>();
collisionEvents = new List<ParticleCollisionEvent>();
}
// Update is called once per frame
void Update()
{
if (lives <= 0)
{
text.text = "GAME OVER";
}
else
{
text.text = "Score: " + score + "\nHighscore: " + highscore + "\nLives: " + lives + "\nKings remaining: " + GameObject.FindGameObjectsWithTag("king").Length + "\nEnemies remaining: " + GameObject.FindGameObjectsWithTag("enemy").Length;;
}
}
void OnSoundEnd()
{
}
//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;
//check if object exists
if (other == null)
{
i++;
continue;
}
if (other.tag == "enemy")
{
//play sound from enemy
other.GetComponent<AudioSource>().Play();
//turn off collider
other.GetComponent<Collider>().enabled = false;
//turn off renderer
other.GetComponent<Renderer>().enabled = false;
Debug.Log("enemy killed (" + other.name+ ")");
//increase score
score++;
if (score > highscore)
{
highscore = score;
}
//update text
}
if (other.tag == "king")
{
//play sound from enemy
other.GetComponent<AudioSource>().Play();
//turn off collider
other.GetComponent<Collider>().enabled = false;
//turn off renderer
other.GetComponent<Renderer>().enabled = false;
Debug.Log("killed king" + other.name);
//decrease score
score-= 20;
lives--;
}
i++;
}
}
}