aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Assets/Scripts/Controllers/Hoop.cs
blob: 488c38b9b4eef989847742359eb3ff4b81270fe8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using UnityEngine;

namespace Controllers
{
	[RequireComponent(typeof(AudioSource))]
	[RequireComponent(typeof(AudioSource))]
	public class Hoop : MonoBehaviour
	{
		internal GameController controller;
		
		[SerializeField] private BoxCollider2D Rim;
		[SerializeField] private BoxCollider2D Net;
		[SerializeField] private AudioSource shotSound;
		[SerializeField] private AudioSource crowdSound;

		private Animator HoopAnimator;

		private void Awake() => HoopAnimator = GetComponentInChildren<Animator>();

		private void OnTriggerEnter2D(Collider2D other)
		{
			if (other.GetComponent<Ball>() == null) return;

			// We don't want people shooting up the hoop so check the velocity of the ball to make sure it is going down.
			if (Rim.IsTouching(other) && Net.IsTouching(other) && (other.GetComponent<Rigidbody2D>().velocity.y < 0))
			{
				HoopAnimator.Rebind();
				shotSound.Play();
				if (this == controller.PlayerHoop)
					controller.player.Score(Rim.transform.position);
				else if (this == controller.EnemyHoop)
					controller.enemy.Score(Rim.transform.position);
				crowdSound.Play();
			}
		}
	}
}