This commit is contained in:
2026-01-29 15:31:57 +05:30
commit 17b8f48e8f
162 changed files with 74683 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
using System.Collections;
using UnityEngine;
public class Ball : MonoBehaviour
{
[HideInInspector]public Rigidbody2D rb;
Vector3 startPosition;
void Awake()
{
rb = GetComponent<Rigidbody2D>();
startPosition = transform.position;
}
public void Reset(float duration){
StartCoroutine(CoroutineReset(duration));
}
IEnumerator CoroutineReset(float duration){
float t=0;
Vector3 pos1 = transform.position;
Quaternion rot1 = transform.rotation;
while (t < duration){
t+=Time.deltaTime;
transform.position = Vector3.Lerp(pos1, startPosition, t / duration);
transform.rotation = Quaternion.Lerp(rot1, Quaternion.identity, t/duration);
yield return null;
}
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: f6fe03c800a746a4486ffa0add89e74c
+218
View File
@@ -0,0 +1,218 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using TMPro;
public class GameManager : MonoBehaviour
{
public static Action<Puck> OnSelectedPuckChanged;
public static Action<Team> OnTeamChanged;
public static bool isMoving{
get{
return instance.IsMoving();
}
}
public bool m_isMoving =false;
public static Team SelectedTeam = Team.Red;
public static bool NoPuckSelected {
get{
return instance.selectedPuck == null;
}
}
public EventTrigger gameInputPanel;
public Rigidbody2D ball;
public float puckForce = 1f;
public float ballMoveTime = 3f;
public float puckDragClamp = 5f;
public int redScore,blueScore=0;
[Header("UI")]
public TMP_Text redScoreText;
public TMP_Text blueScoreText;
[Header("Misc")]
public List<Puck> pucks = new List<Puck>();
public static GameManager instance;
void Awake()
{
instance = this;
}
void Start()
{
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerDown;
entry.callback.AddListener(OnPointerDown);
gameInputPanel.triggers.Add(entry);
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerUp;
entry.callback.AddListener(OnPointerUp);
gameInputPanel.triggers.Add(entry);
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.Drag;
entry.callback.AddListener(OnDrag);
gameInputPanel.triggers.Add(entry);
OnSelectedPuckChanged?.Invoke(null);
}
public void RegisterPuck(Puck puck){
pucks.Add(puck);
}
public void DisposePuck(Puck puck){
pucks.Remove(puck);
}
public Vector2 startPosition;
public Vector2 curPosition;
Puck selectedPuck;
void OnPointerDown(BaseEventData eventData)
{
if(IsMoving()){return;} //Do not do anything if shits moving
PointerEventData pointerEventData = eventData as PointerEventData;
startPosition = pointerEventData.position;
curPosition = startPosition;
// Convert screen position to world position
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(new Vector3(pointerEventData.position.x, pointerEventData.position.y, Camera.main.nearClipPlane));
// Find all pucks in the scene
Puck[] pucks = FindObjectsOfType<Puck>();
float minDistance = float.MaxValue;
Puck closestPuck = null;
foreach (Puck puck in pucks)
{
if(puck.team != SelectedTeam){continue;}
float dist = Vector2.Distance(worldPosition, puck.transform.position);
if (dist < minDistance)
{
minDistance = dist;
closestPuck = puck;
}
}
selectedPuck = closestPuck;
OnSelectedPuckChanged?.Invoke(selectedPuck);
}
Coroutine coroutinePostLaunch;
void OnPointerUp(BaseEventData eventData)
{
Debug.Log("Pointer up");
PointerEventData pointerEventData = eventData as PointerEventData;
Vector2 endPosition = pointerEventData.position;
Vector2 direction = endPosition - startPosition;
if(coroutinePostLaunch != null){
StopCoroutine(coroutinePostLaunch);
}
coroutinePostLaunch = StartCoroutine(CoroutinePostLaunch());
selectedPuck.GetComponent<Rigidbody2D>().AddForce(-direction * puckForce * Time.deltaTime, ForceMode2D.Impulse);
startPosition = Vector2.zero;
selectedPuck = null;
OnSelectedPuckChanged?.Invoke(null);
SelectedTeam = SelectedTeam == Team.Red ? Team.Blue : Team.Red;
OnTeamChanged?.Invoke(SelectedTeam);
}
void OnDrag(BaseEventData eventData)
{
PointerEventData pointerEventData = eventData as PointerEventData;
curPosition = pointerEventData.position;
}
IEnumerator CoroutinePostLaunch(){
float t = 0;
while ( t < 1){
t += Time.deltaTime / ballMoveTime;
ball.linearDamping = Mathf.Lerp(0, 10, t);
yield return null;
}
}
bool IsMoving(){
if(freezeInput){return true;}
float minMagnitude = 0.01f;
foreach(Puck puck in pucks){
if(puck.rb.linearVelocity.magnitude > minMagnitude){
return true;
}
}
if(ball.linearVelocity.magnitude > minMagnitude){
return true;
}
return false;
}
void Update()
{
m_isMoving=IsMoving();
}
public void OnGoal(Team team){
freezeInput=true;
if(team == SelectedTeam){
blueScore++;
blueScoreText.text = blueScore.ToString();
}else{
redScore++;
redScoreText.text = redScore.ToString();
}
StartCoroutine(CoroutineOnGoal(team));
}
bool freezeInput = false;
IEnumerator CoroutineOnGoal(Team team){
if(coroutinePostLaunch!=null){
StopCoroutine(coroutinePostLaunch);
}
float t=0;
while(t < 1){
t +=Time.deltaTime * 2f;
ball.linearDamping = Mathf.Lerp(0, 100, t);
yield return null;
}
yield return new WaitForSeconds(2f);
Reset();
}
public void Reset()
{
StartCoroutine(CoroutineReset());
}
IEnumerator CoroutineReset(){
float resetDuration = 0.5f;
foreach(Puck puck in pucks){
puck.Reset(resetDuration);
}
ball.GetComponent<Ball>().Reset(resetDuration);
yield return new WaitForSeconds(resetDuration);
freezeInput=false;
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 12befc4868932ca479d93e14120ffd0e
+15
View File
@@ -0,0 +1,15 @@
using UnityEngine;
public class Goal : MonoBehaviour
{
public Team team;
void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.CompareTag("Player")){
Debug.Log("GOAL!");
Ball ball = collision.gameObject.GetComponent<Ball>();
Debug.Log(ball.name);
GameManager.instance.OnGoal(team);
}
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: f5ca0ff31c184b04da729df1fae08a5e
+125
View File
@@ -0,0 +1,125 @@
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
public class Puck : MonoBehaviour
{
[HideInInspector]public Rigidbody2D rb;
public bool isSelected;
public LineRenderer lineRenderer;
public Team team;
public GameObject redTeamMarker;
public GameObject blueTeamMarker;
public Transform selectedMarker;
public float markerRotateSpeed;
public float markerBounceIntensity;
public float markerBounceSpeed;
Vector3 markerStartScale;
Vector3 startPosition;
float bounceSinA;
void OnValidate()
{
UpdateTeam();
}
void Awake()
{
UpdateTeam();
markerStartScale = selectedMarker.localScale;
rb=GetComponent<Rigidbody2D>();
startPosition = transform.position;
}
void UpdateTeam(){
redTeamMarker.SetActive(team == Team.Red);
blueTeamMarker.SetActive(team == Team.Blue);
}
void Start()
{
GameManager.OnSelectedPuckChanged += OnSelectedPuckChanged;
GameManager.OnTeamChanged += OnTeamChanged;
GameManager.instance.RegisterPuck(this);
}
void OnDestroy()
{
GameManager.OnSelectedPuckChanged -= OnSelectedPuckChanged;
GameManager.OnTeamChanged -= OnTeamChanged;
GameManager.instance.DisposePuck(this);
}
void Update()
{
if (isSelected && lineRenderer != null)
{
Vector3 worldEnd = Camera.main.ScreenToWorldPoint(new Vector3(GameManager.instance.curPosition.x, GameManager.instance.curPosition.y, Camera.main.nearClipPlane));
Vector3 localEnd = transform.InverseTransformPoint(worldEnd);
float clamp = GameManager.instance.puckDragClamp;
localEnd = Vector2.ClampMagnitude(localEnd, clamp);
// Only two points for the line
lineRenderer.positionCount = 2;
lineRenderer.SetPosition(0, Vector3.zero);
lineRenderer.SetPosition(1, new Vector3(localEnd.x, localEnd.y, 0));
}
selectedMarker.gameObject.SetActive(GetMarkerVisibility());
if(GameManager.SelectedTeam == team){
selectedMarker.Rotate(new Vector3(0,0,1), markerRotateSpeed * Time.deltaTime);
bounceSinA += Time.deltaTime * markerBounceSpeed;
float bounce = Mathf.Sin(bounceSinA) * markerBounceIntensity;
selectedMarker.localScale = markerStartScale + (Vector3.one * bounce);
}
}
bool GetMarkerVisibility(){
if(GameManager.isMoving){return false;}
if(GameManager.SelectedTeam == team){
if(GameManager.NoPuckSelected){
return true;
}else{
return isSelected;
}
}
return false;
}
void OnSelectedPuckChanged(Puck puck)
{
isSelected = puck == this;
lineRenderer.enabled = isSelected;
}
void OnTeamChanged(Team team){
}
public void Reset(float duration)
{
StartCoroutine(CoroutineReset(duration));
}
IEnumerator CoroutineReset(float duration){
float t=0;
Vector3 pos1 = transform.position;
Quaternion rot1 = transform.rotation;
while(t<duration){
t+=Time.deltaTime;
transform.position = Vector3.Lerp(pos1, startPosition, t/duration);
transform.rotation = Quaternion.Lerp(rot1, Quaternion.identity, t/duration);
yield return null;
}
}
}
public enum Team{
Red,
Blue
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 4b7edf42d59a3c942b1f2f66b31aaa75