GlobalGameJam-2021/Assets/Scripts/DamageObjectsManager.cs

134 lines
3.8 KiB
C#
Raw Normal View History

2021-01-30 12:51:35 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DamageObjectsManager : MonoBehaviour
{
2021-01-30 16:44:13 +00:00
public enum ObjectType { Object, HideSpikes, ShowSpikes, StaticSaw, RidingSaw };
2021-01-30 12:51:35 +00:00
public ObjectType objectType = ObjectType.Object;
private Animation animation;
private bool animate = false;
public bool instaKill = false;
public float damage = 10f;
public float damageRate = 5f;
2021-01-30 17:24:29 +00:00
public Transform fromPosition;
public Transform toPosition;
private bool back = false;
2021-01-30 12:51:35 +00:00
private float actualDamageRate = 0f;
2021-01-30 16:44:13 +00:00
private bool interact = false;
private PlayerManager playerManager;
2021-01-30 12:51:35 +00:00
void Start()
{
animation = gameObject.GetComponent<Animation>();
}
// Update is called once per frame
void Update()
{
2021-01-30 16:44:13 +00:00
if (interact)
{
if (objectType == ObjectType.HideSpikes)
{
if (!animate)
{
animation["Spikes"].speed = 1f;
animation["Spikes"].normalizedTime = 0f;
animation.Play("Spikes");
animate = true;
}
}
if (objectType == ObjectType.ShowSpikes)
{
if (actualDamageRate < Time.time)
{
actualDamageRate = Time.time + damageRate;
playerManager.damage(damage, instaKill);
}
}
if (objectType == ObjectType.StaticSaw)
{
if (!animation.isPlaying)
{
2021-01-30 17:24:29 +00:00
animation.Play("SawA");
2021-01-30 16:44:13 +00:00
}
if (actualDamageRate < Time.time)
{
actualDamageRate = Time.time + damageRate;
playerManager.damage(damage, instaKill);
}
}
if (objectType == ObjectType.RidingSaw)
{
if (actualDamageRate < Time.time)
{
actualDamageRate = Time.time + damageRate;
playerManager.damage(damage, instaKill);
}
}
}
2021-01-30 17:24:29 +00:00
if (objectType == ObjectType.RidingSaw)
{
if (fromPosition != null && toPosition != null)
{
if (this.transform.localPosition == fromPosition.localPosition)
{
back = true;
}
else if (this.transform.localPosition == toPosition.localPosition)
{
back = false;
}
if (back)
{
this.transform.localPosition = Vector3.MoveTowards(this.transform.localPosition, toPosition.localPosition, 5 * Time.deltaTime);
}
else
{
this.transform.localPosition = Vector3.MoveTowards(this.transform.localPosition, fromPosition.localPosition, 5 * Time.deltaTime);
}
}
}
2021-01-30 12:51:35 +00:00
}
public void damagePlayer(PlayerManager playerManager)
{
if (actualDamageRate < Time.time)
{
actualDamageRate = Time.time + damageRate;
playerManager.damage(damage, instaKill);
}
}
private void OnTriggerStay(Collider other)
{
2021-01-30 16:44:13 +00:00
if (other.gameObject.tag == "Player")
2021-01-30 12:51:35 +00:00
{
2021-01-30 16:44:13 +00:00
interact = true;
playerManager = other.gameObject.GetComponent<PlayerManager>();
2021-01-30 12:51:35 +00:00
}
2021-01-30 16:44:13 +00:00
}
2021-01-30 12:51:35 +00:00
2021-01-30 16:44:13 +00:00
private void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
2021-01-30 12:51:35 +00:00
{
2021-01-30 16:44:13 +00:00
interact = false;
playerManager = null;
2021-01-30 12:51:35 +00:00
}
}
public void SpikesBack()
{
animate = false;
animation["Spikes"].speed = -1f;
animation.Play("Spikes");
}
}