162 lines
4.8 KiB
C#
162 lines
4.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class BlockManager : MonoBehaviour
|
|
{
|
|
public enum BlockType {None, Barricade, Door, Wood, Ammo, FireCamp};
|
|
public BlockType blockType = BlockType.None;
|
|
public float health = 100;
|
|
public float healthRate = 5;
|
|
private float healthTime = 5;
|
|
private Animation animation;
|
|
private PlayerManager player;
|
|
private bool action = false;
|
|
public List<GameObject> amountModels = new List<GameObject>();
|
|
|
|
public AudioSource audioSource = new AudioSource();
|
|
public AudioClip doorSound;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
player = FindObjectOfType<PlayerManager>();
|
|
if (blockType == BlockType.Door)
|
|
{
|
|
animation = this.GetComponent<Animation>();
|
|
}
|
|
else if (blockType == BlockType.Wood)
|
|
{
|
|
healthTime = healthRate;
|
|
}
|
|
else if (blockType == BlockType.Ammo)
|
|
{
|
|
healthTime = healthRate;
|
|
}
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
SpikeHit();
|
|
if (blockType == BlockType.Wood)
|
|
{
|
|
if (health < 100 && healthTime < Time.time)
|
|
{
|
|
health += 10;
|
|
healthTime = healthRate + Time.time;
|
|
}
|
|
}
|
|
else if (blockType == BlockType.Ammo)
|
|
{
|
|
if (health < 100 && healthTime < Time.time)
|
|
{
|
|
health += 10;
|
|
healthTime = healthRate + Time.time;
|
|
}
|
|
}
|
|
viewHealthAs();
|
|
}
|
|
|
|
void viewHealthAs()
|
|
{
|
|
if (blockType == BlockType.Wood) {
|
|
if (amountModels.Count > 0)
|
|
{
|
|
for (int i = 0; i < amountModels.Count; i++)
|
|
{
|
|
if (i < ((100 - health) / 3.7f))
|
|
{
|
|
amountModels[i].SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
amountModels[i].SetActive(true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if (blockType == BlockType.Ammo)
|
|
{
|
|
if (amountModels.Count > 0)
|
|
{
|
|
for (int i = 0; i < amountModels.Count; i++)
|
|
{
|
|
if (i < ((100 - health) / 4.5f))
|
|
{
|
|
amountModels[i].SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
amountModels[i].SetActive(true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Action()
|
|
{
|
|
if (blockType == BlockType.Door)
|
|
{
|
|
if (!animation.isPlaying)
|
|
{
|
|
audioSource.PlayOneShot(doorSound);
|
|
if (!action)
|
|
{
|
|
animation["Door"].speed = 1;
|
|
animation["Door"].time = 0;
|
|
action = true;
|
|
}
|
|
else
|
|
{
|
|
animation["Door"].speed = -1;
|
|
animation["Door"].time = animation["Door"].length;
|
|
action = false;
|
|
}
|
|
animation.Play("Door");
|
|
}
|
|
}
|
|
else if (blockType == BlockType.Wood)
|
|
{
|
|
if (health > 0 && player.barricadeMaterials < 50) {
|
|
health -= 10;
|
|
player.barricadeMaterials += (player.barricadeMaterials <= 40 ? 10 : (50 - player.barricadeMaterials));
|
|
}
|
|
}
|
|
else if (blockType == BlockType.Ammo)
|
|
{
|
|
if (health > 0 && player.ammo < 50)
|
|
{
|
|
health -= 10;
|
|
player.ammo += (player.ammo <= 40 ? 10 : (50 - player.ammo));
|
|
}
|
|
}
|
|
}
|
|
|
|
void SpikeHit()
|
|
{
|
|
if (blockType == BlockType.Barricade) {
|
|
RaycastHit hit;
|
|
if (Physics.BoxCast(this.transform.position + new Vector3(-0.05f, 0.75f, 1.0f), new Vector3(0.5f, 0.75f, 1.0f), this.transform.forward, out hit, transform.rotation,1.0f))
|
|
{
|
|
if (hit.collider.CompareTag("Enemy"))
|
|
{
|
|
EnemyManager enemyManager = hit.collider.transform.GetComponent<EnemyManager>();
|
|
if (enemyManager != null)
|
|
{
|
|
enemyManager.Damage(100);
|
|
health = 0;
|
|
for (int i = 0; i < amountModels.Count; i++)
|
|
{
|
|
amountModels[i].transform.parent = null;
|
|
Destroy(amountModels[i], 4);
|
|
}
|
|
Destroy(this.gameObject);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|