60 lines
1.9 KiB
C#
60 lines
1.9 KiB
C#
using Fruitomation.Global;
|
|
using UnityEngine;
|
|
|
|
namespace Fruitomation.Game
|
|
{
|
|
public class FanBuilding : Building
|
|
{
|
|
[Header("Fan Specific Items")]
|
|
[SerializeField] private TriggerDetector TriggerDetector;
|
|
[SerializeField] private Animator FanAnimator;
|
|
[SerializeField] private Transform FanOrigin;
|
|
|
|
private void Start()
|
|
{
|
|
TriggerDetector.SetAction(other =>
|
|
{
|
|
if (other.transform.parent.TryGetComponent(out Rigidbody2D body))
|
|
{
|
|
Vector3 start = FanOrigin.position;
|
|
Vector3 end = body.gameObject.transform.position;
|
|
Vector3 diff = end - start;
|
|
Vector3 dir = diff.normalized;
|
|
float mag = diff.magnitude;
|
|
int mask = LayerMask.GetMask("Buildings");
|
|
|
|
RaycastHit2D hit = Physics2D.Raycast(start, dir, mag, mask);
|
|
if (hit.collider is null)
|
|
{
|
|
Vector3 force = new(-transform.localScale.x, 0f, 0f);
|
|
Debug.Log(mag);
|
|
|
|
body.AddForce(force * (12f / mag), ForceMode2D.Force);
|
|
}
|
|
}
|
|
}, TriggerType.Stay);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (GameStateController.Is(GameState.Simulation))
|
|
{
|
|
if (FanAnimator.speed == 0)
|
|
{
|
|
FanAnimator.Play(0, 0, 0f); // Play from beginning
|
|
FanAnimator.speed = 1; // Playing
|
|
}
|
|
}
|
|
|
|
else
|
|
{
|
|
if (FanAnimator.speed != 0)
|
|
{
|
|
FanAnimator.Play(0, 0, 0f); // Jump back to default frame
|
|
FanAnimator.speed = 0; // Paused
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|