Адаптация механик здоровья и врагов для изометрии
Автор: allah228228 • Январь 21, 2023 • Контрольная работа • 1,550 Слов (7 Страниц) • 160 Просмотры
Адаптация механик здоровья и врагов для изометрии
Скрипт IsoLives:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class IsoLives : MonoBehaviour
{
public int lives = 3;
[Header("Interface Settings")]
public Image lifeImage;
public bool extendToRight = true;
public GameObject deathPanel;
[Header("Tags")]
public string livesTag;
public string damagerTag;
List<Image> hearts;
Canvas canvas;
float deathPanelAlphaSpeed = 0.01f;
Vector3 interval;
bool dead = false;
float timeOut = 0.3f;
bool isReady = true;
void Start()
{
canvas = lifeImage.GetComponentInParent<Canvas>();
deathPanel.SetActive(false);
deathPanel.GetComponent<Image>().color = new Color(0, 0, 0, 0);
hearts = new List<Image>();
hearts.Add(lifeImage);
if (extendToRight)
interval = new Vector3(lifeImage.rectTransform.rect.size.x, 0, 0);
else
interval = new Vector3(-lifeImage.rectTransform.rect.size.x, 0, 0);
for (int i = 1; i < lives; i++)
{
Image im = Instantiate(lifeImage);
im.transform.SetParent(canvas.transform);
im.transform.localScale = lifeImage.transform.localScale;
im.transform.localPosition = lifeImage.transform.localPosition;
im.transform.localPosition += interval * i;
hearts.Add(im);
}
}
private void FixedUpdate()
{
if (dead)
{
deathPanel.SetActive(true);
deathPanel.GetComponent<Image>().color = new Color(0, 0, 0, deathPanel.GetComponent<Image>().color.a + deathPanelAlphaSpeed);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
loseLife(collision.gameObject);
addLife(collision.gameObject);
}
private void OnTriggerEnter2D(Collider2D collision)
{
loseLife(collision.gameObject);
addLife(collision.gameObject);
}
private void OnCollisionStay2D(Collision2D collision)
{
loseLife(collision.gameObject);
}
private void OnTriggerStay2D(Collider2D collision)
{
loseLife(collision.gameObject);
}
void addLife(GameObject ob)
{
if (ob.tag == livesTag)
{
lives++;
Image im = Instantiate(lifeImage);
im.transform.SetParent(canvas.transform);
im.transform.localScale = lifeImage.transform.localScale;
im.transform.localPosition = lifeImage.transform.localPosition;
im.transform.localPosition += interval * (lives - 1);
hearts.Add(im);
Destroy(ob);
}
}
private void loseLife(GameObject ob)
{
if (ob.tag == damagerTag && lives > 0&&isReady)
{
lives--;
isReady = false;
Invoke("setReady", timeOut);
Destroy(hearts[lives].gameObject);
hearts.RemoveAt(lives);
if (lives == 0)
...