ludumdare50/Assets/Scripts/SafeZone.cs
2022-04-04 01:49:17 -04:00

41 lines
1.3 KiB
C#

using NaughtyAttributes;
using UnityEngine;
public class SafeZone : MonoBehaviour {
[field: SerializeField] [field: Required]
public SafeZoneStats Stats { get; private set; }
[SerializeField] Collider2D moatCollider;
[SerializeField] public RectTransform canvas;
public float OutsideDistance => moatCollider.bounds.extents.x + Stats.JumpOffset;
public Vector3 GetOutsidePosition(Vector2 direction) {
return transform.position + OutsideDistance * (Vector3)direction;
}
public Vector3 GetMoatExtents(){
return moatCollider.bounds.extents;
}
private void OnTriggerStay2D(Collider2D other) {
if(other.GetComponent<PlayerMovement>() is {} playerMovement && playerMovement.currentState is PlayerMovement.NormalMovementState){
Vector3 diff = (other.transform.position - transform.position);
//Player is in the zone around the moat
if(diff.magnitude >= moatCollider.bounds.extents.x){
//Spawn ui message over moat
Vector3 dir = diff.normalized;
Vector3 position = transform.position + dir * moatCollider.bounds.extents.x * 0.75f;
Debug.DrawLine(transform.position, position, Color.red);
canvas.position = position;
canvas.gameObject.SetActive(true);
}
}
}
private void OnTriggerExit2D(Collider2D other) {
if(!(other.GetComponent<VampireEntity>() is null)){
canvas.gameObject.SetActive(false);
}
}
}