GlobalGameJame/Assets/Scripts/BlockManager.cs

192 lines
5.8 KiB
C#
Raw Normal View History

2020-01-31 20:47:30 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2021-08-17 16:42:47 +00:00
public class BlockManager : MonoBehaviour
2020-01-31 20:47:30 +00:00
{
2020-02-02 09:47:16 +00:00
public enum BlockType {None, Barricade, Door, Wood, Ammo, FireCamp, Pills};
2020-01-31 20:47:30 +00:00
public BlockType blockType = BlockType.None;
2020-01-31 21:32:05 +00:00
public float health = 100;
2020-02-01 15:20:10 +00:00
public float healthRate = 5;
private float healthTime = 5;
2020-02-01 13:26:43 +00:00
private Animation animation;
2020-02-01 15:20:10 +00:00
private PlayerManager player;
2020-02-01 11:11:56 +00:00
private bool action = false;
2020-02-01 18:49:53 +00:00
public List<GameObject> amountModels = new List<GameObject>();
2020-02-02 09:47:16 +00:00
public GameObject pills;
public GameObject prefabPills;
public Vector3 spawnPillsPoint;
2020-01-31 20:47:30 +00:00
2020-02-01 19:58:47 +00:00
public AudioSource audioSource = new AudioSource();
public AudioClip doorSound;
2020-01-31 20:47:30 +00:00
// Start is called before the first frame update
void Start()
{
2020-02-01 15:20:10 +00:00
player = FindObjectOfType<PlayerManager>();
2020-02-01 11:11:56 +00:00
if (blockType == BlockType.Door)
{
2020-02-01 13:26:43 +00:00
animation = this.GetComponent<Animation>();
2020-02-01 11:11:56 +00:00
}
2020-02-01 15:20:10 +00:00
else if (blockType == BlockType.Wood)
{
healthTime = healthRate;
}
2020-02-01 17:53:28 +00:00
else if (blockType == BlockType.Ammo)
{
healthTime = healthRate;
}
2020-02-02 09:47:16 +00:00
else if (blockType == BlockType.Pills)
{
healthTime = healthRate;
}
2020-01-31 20:47:30 +00:00
}
// Update is called once per frame
void Update()
{
SpikeHit();
2020-02-01 15:20:10 +00:00
if (blockType == BlockType.Wood)
{
if (health < 100 && healthTime < Time.time)
{
health += 10;
healthTime = healthRate + Time.time;
}
}
2020-02-01 17:53:28 +00:00
else if (blockType == BlockType.Ammo)
{
if (health < 100 && healthTime < Time.time)
{
health += 10;
healthTime = healthRate + Time.time;
}
}
2020-02-02 09:47:16 +00:00
else if (blockType == BlockType.Pills)
{
if (pills == null && healthTime < Time.time) {
pills = Instantiate(prefabPills, spawnPillsPoint, prefabPills.transform.rotation);
this.GetComponent<BoxCollider>().enabled = true;
}
}
2020-02-01 18:49:53 +00:00
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);
}
}
}
}
2020-02-01 11:11:56 +00:00
}
public void Action()
{
if (blockType == BlockType.Door)
{
2020-02-01 15:20:10 +00:00
if (!animation.isPlaying)
{
2020-02-01 19:58:47 +00:00
audioSource.PlayOneShot(doorSound);
2020-02-01 13:26:43 +00:00
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");
2020-02-01 15:20:10 +00:00
}
}
else if (blockType == BlockType.Wood)
{
if (health > 0 && player.barricadeMaterials < 50) {
health -= 10;
player.barricadeMaterials += (player.barricadeMaterials <= 40 ? 10 : (50 - player.barricadeMaterials));
}
2020-02-01 11:11:56 +00:00
}
2020-02-01 17:53:28 +00:00
else if (blockType == BlockType.Ammo)
{
if (health > 0 && player.ammo < 50)
{
health -= 10;
player.ammo += (player.ammo <= 40 ? 10 : (50 - player.ammo));
}
2020-02-02 09:47:16 +00:00
}
else if (blockType == BlockType.Pills)
{
if (pills != null && player.actualHealth != player.health) {
if (player.actualHealth <= player.health - health)
{
player.actualHealth += health;
}
else
{
player.actualHealth = player.health;
}
Destroy(pills);
this.GetComponent<BoxCollider>().enabled = false;
healthTime = healthRate + Time.time;
}
2020-02-01 17:53:28 +00:00
}
2020-01-31 20:47:30 +00:00
}
2020-02-01 21:54:57 +00:00
void SpikeHit()
2020-02-01 21:54:57 +00:00
{
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))
2020-02-01 21:54:57 +00:00
{
if (hit.collider.CompareTag("Enemy"))
2020-02-01 21:54:57 +00:00
{
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);
}
2020-02-01 21:54:57 +00:00
}
}
}
}
2020-01-31 20:47:30 +00:00
}