mp fundementals and turn timer

This commit is contained in:
2026-02-14 18:18:45 +05:30
parent 614c38bdaf
commit 02dee0b86d
64 changed files with 7472 additions and 82 deletions
+5 -5
View File
@@ -40,7 +40,7 @@ public class Ball : MonoBehaviour
float otherSpeed = collision.relativeVelocity.magnitude;
float velocityMult = 1- Mathf.Clamp01(rb.linearVelocity.magnitude / maxWallStuckVelocity);
Debug.Log($"{collision.collider.name} hit ball @ {otherSpeed}, ball speed: {rb.linearVelocity.magnitude}");
// Debug.Log($"{collision.collider.name} hit ball @ {otherSpeed}, ball speed: {rb.linearVelocity.magnitude}");
int mult=0;
if(touchingB){
mult++;
@@ -56,16 +56,16 @@ public class Ball : MonoBehaviour
}
velocityMult *= mult;
if (touchingR) {
rb.AddForce(Vector2.left * otherSpeed *wallStuckFixForce * velocityMult * Time.deltaTime);
rb.AddForce(Vector2.left * otherSpeed *wallStuckFixForce * velocityMult );
}
if (touchingL) {
rb.AddForce(Vector2.right * otherSpeed * wallStuckFixForce * velocityMult * Time.deltaTime);
rb.AddForce(Vector2.right * otherSpeed * wallStuckFixForce * velocityMult );
}
if (touchingT) {
rb.AddForce(Vector2.down * otherSpeed * wallStuckFixForce * velocityMult * Time.deltaTime);
rb.AddForce(Vector2.down * otherSpeed * wallStuckFixForce * velocityMult );
}
if (touchingB) {
rb.AddForce(Vector2.up *otherSpeed * wallStuckFixForce * velocityMult * Time.deltaTime);
rb.AddForce(Vector2.up *otherSpeed * wallStuckFixForce * velocityMult );
}
}
+1 -1
View File
@@ -30,7 +30,7 @@ public class DevSettingsPanel : MonoBehaviour
void Populate()
{
DevOptionItem puckForce = Instantiate(devOptionItemPrefab, contentParent);
puckForce.Setup("Puck Force", 2500, 6000, GameManager.instance.puckForce);
puckForce.Setup("Puck Force", 40, 120, GameManager.instance.puckForce);
puckForce.OnChanged += OnPuckForceChanged;
DevOptionItem puckDragClampMax = Instantiate(devOptionItemPrefab, contentParent);
+63
View File
@@ -0,0 +1,63 @@
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using DG.Tweening;
public class GameCanvas : MonoBehaviour
{
public static GameCanvas instance;
void Awake()
{
instance = this;
}
public EventTrigger gameInputPanel;
public Image turnTimerSliderTransform;
public Color redColor = Color.red;
public Color blueColor = Color.blue;
public TMP_Text txtWhosTurn;
public TMP_Text txtTurnTimer;
public Image turnTimerRound;
void Update(){
UpdateTurnTimer();
}
Team prevTeam = Team.Red;
bool init=false;
void UpdateTurnTimer(){
if(NetPlayer.localPlayer == null){return;}
if(!init){
init=true;
SwitchTeams();
}
float turnTimerFillAmount = 1 - (GameManager.instance.turnTimerCounter / GameManager.instance.turnTimer);
if(prevTeam != GameManager.instance.SelectedTeam){
SwitchTeams();
prevTeam = GameManager.instance.SelectedTeam;
}
turnTimerSliderTransform.transform.localScale = new Vector3(turnTimerFillAmount, 1, 1);
txtTurnTimer.text = (GameManager.instance.turnTimer - GameManager.instance.turnTimerCounter).ToString("F0");
turnTimerRound.fillAmount = turnTimerFillAmount;
}
void SwitchTeams(){
bool isMyTurn = GameManager.instance.SelectedTeam == NetPlayer.localPlayer.myTeam;
Color newColor = GameManager.instance.SelectedTeam == Team.Red ? redColor : blueColor;
turnTimerSliderTransform.DOColor(newColor, 0.5f);
txtWhosTurn.DOFade(0, 0.25f).OnComplete(() => {
txtWhosTurn.text = isMyTurn ? "Your Turn" : "Opponent's Turn";
txtWhosTurn.DOFade(1, 0.25f);
});
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 09dd2727c9588024781c4d5160f19c4d
+6
View File
@@ -0,0 +1,6 @@
using System;
public static class GameEvents{
public static Action<Puck> OnSelectedPuckChanged;
public static Action<Team> OnTeamChanged;
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 0e7ff01514aafb34d805be0d1662de29
+108 -58
View File
@@ -8,21 +8,29 @@ using Mirror;
public class GameManager : NetworkBehaviour
{
public static Action<Puck> OnSelectedPuckChanged;
public static Action<Team> OnTeamChanged;
public static bool isMoving{
get{
return instance.IsMoving();
}
}
[SyncVar]
public bool m_isMoving =false;
public static Team SelectedTeam = Team.Red;
[SyncVar(hook = nameof(OnSelectedTeamChanged))]
public Team SelectedTeam = Team.Red;
void OnSelectedTeamChanged(Team oldTeam, Team newTeam){
Debug.Log($"Selected team changed from {oldTeam} to {newTeam}");
OnTeamChanged?.Invoke(newTeam);
}
public static bool NoPuckSelected {
get{
return instance.selectedPuck == null;
}
}
public EventTrigger gameInputPanel;
public int turnTimer = 10;
[SyncVar]
public float turnTimerCounter;
public Rigidbody2D ball;
public float puckForce = 20f;
public AnimationCurve puckForceCurve = AnimationCurve.Linear(0,3,1,1);
@@ -30,7 +38,10 @@ public class GameManager : NetworkBehaviour
public float puckDragClampMax = 5f;
public float puckDragMinClamp = 1f;
public int redScore,blueScore=0;
[SyncVar(hook = nameof(OnScoreChanged))]
public int redScore=0;
[SyncVar(hook = nameof(OnScoreChanged))]
public int blueScore=0;
[Header("UI")]
public TMP_Text redScoreText;
@@ -41,6 +52,7 @@ public class GameManager : NetworkBehaviour
[Header("Misc")]
public List<Puck> pucks = new List<Puck>();
public float curPuckPullForce;
[SyncVar]
public int hitCounter = 0;
public static GameManager instance;
@@ -71,24 +83,13 @@ public class GameManager : NetworkBehaviour
void Start()
{
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerDown;
entry.callback.AddListener(OnPointerDown);
gameInputPanel.triggers.Add(entry);
GameEvents.OnSelectedPuckChanged?.Invoke(null);
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerUp;
entry.callback.AddListener(OnPointerUp);
gameInputPanel.triggers.Add(entry);
#if UNITY_EDITOR
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.Drag;
entry.callback.AddListener(OnDrag);
gameInputPanel.triggers.Add(entry);
OnSelectedPuckChanged?.Invoke(null);
Application.targetFrameRate = 100;
#else
Application.targetFrameRate = isServer ? 30 : 100;
#endif
}
@@ -101,18 +102,34 @@ public class GameManager : NetworkBehaviour
}
public Vector2 startPosition;
public Vector2 curPosition;
Puck selectedPuck;
void OnPointerDown(BaseEventData eventData)
[SyncVar(hook = nameof(OnSelectedPuckChanged))]
[SerializeField]Puck selectedPuck;
void OnSelectedPuckChanged(Puck oldPuck, Puck newPuck){
// Debug.Log($"Selected puck changed from {oldPuck} to {newPuck}");
GameEvents.OnSelectedPuckChanged?.Invoke(newPuck);
}
public static Puck SelectedPuck{
get{
return instance.selectedPuck;
}
set{
instance.SetSelectedPuck(value);
}
}
void SetSelectedPuck(Puck puck)
{
if(IsMoving()){return;} //Do not do anything if shits moving
PointerEventData pointerEventData = eventData as PointerEventData;
startPosition = pointerEventData.position;
curPosition = startPosition;
selectedPuck = puck;
GameEvents.OnSelectedPuckChanged?.Invoke(puck);
}
public Puck GetClosestPuck(Vector2 position){
if(IsMoving()){return null;} //Do not do anything if shits moving
// Convert screen position to world position
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(new Vector3(pointerEventData.position.x, pointerEventData.position.y, Camera.main.nearClipPlane));
// Vector3 worldPosition = Camera.main.ScreenToWorldPoint(new Vector3(position.x, position.y, Camera.main.nearClipPlane));
// Find all pucks in the scene
Puck[] pucks = FindObjectsOfType<Puck>();
@@ -122,7 +139,7 @@ public class GameManager : NetworkBehaviour
foreach (Puck puck in pucks)
{
if(puck.team != SelectedTeam){continue;}
float dist = Vector2.Distance(worldPosition, puck.transform.position);
float dist = Vector2.Distance(position, puck.transform.position);
if (dist < minDistance)
{
minDistance = dist;
@@ -130,22 +147,15 @@ public class GameManager : NetworkBehaviour
}
}
selectedPuck = closestPuck;
OnSelectedPuckChanged?.Invoke(selectedPuck);
return closestPuck;
}
Coroutine coroutinePostLaunch;
void OnPointerUp(BaseEventData eventData)
{
Debug.Log("Pointer up");
PointerEventData pointerEventData = eventData as PointerEventData;
Vector2 endPosition = pointerEventData.position;
// Vector2 direction = endPosition - startPosition;
Vector2 direction = selectedPuck.lineEnd;
Coroutine coroutinePostLaunch;
public void OnPointerUp(Vector2 direction)
{
if(direction.magnitude < puckDragMinClamp){
selectedPuck = null;
OnSelectedPuckChanged?.Invoke(null);
startPosition = Vector2.zero;
GameEvents.OnSelectedPuckChanged?.Invoke(null);
return;
}
if(coroutinePostLaunch != null){
@@ -153,26 +163,43 @@ public class GameManager : NetworkBehaviour
}
coroutinePostLaunch = StartCoroutine(CoroutinePostLaunch());
curPuckPullForce = direction.magnitude;
float force = puckForce * Time.deltaTime * puckForceCurve.Evaluate(curPuckPullForce);
float force = puckForce * puckForceCurve.Evaluate(curPuckPullForce);
Debug.Log($"force = {puckForce} * {puckForceCurve.Evaluate(curPuckPullForce)} = {force}");
Debug.Log($"launching puck at {direction} with {force * -direction} force");
selectedPuck.GetComponent<Rigidbody2D>().AddForce(-direction * force, ForceMode2D.Impulse);
hitCounter++;
startPosition = Vector2.zero;
selectedPuck = null;
OnSelectedPuckChanged?.Invoke(null);
SelectedTeam = SelectedTeam == Team.Red ? Team.Blue : Team.Red;
OnTeamChanged?.Invoke(SelectedTeam);
GameEvents.OnSelectedPuckChanged?.Invoke(null);
SwitchTeams();
}
void OnDrag(BaseEventData eventData)
{
PointerEventData pointerEventData = eventData as PointerEventData;
curPosition = pointerEventData.position;
if(selectedPuck!=null){
curPuckPullForce = selectedPuck.lineEnd.magnitude;
void SwitchTeams(){
if(!isServer){
Debug.LogWarning("SwitchTeams called on client, skipping");
return;
}
StartCoroutine(CoroutineSwitchTeams());
}
bool switchingTeams=false;
IEnumerator CoroutineSwitchTeams(){
switchingTeams=true;
for(int i=0; i < 3; i++){
yield return null;
}
while(m_isMoving){
yield return null;
}
turnTimerCounter = 0;
SelectedTeam = SelectedTeam == Team.Red ? Team.Blue : Team.Red;
OnTeamChanged?.Invoke(SelectedTeam);
switchingTeams=false;
}
@@ -203,10 +230,29 @@ public class GameManager : NetworkBehaviour
void Update()
{
m_isMoving=IsMoving();
if(isServer){
m_isMoving=IsMoving();
HandleTurnTimer();
}
}
void HandleTurnTimer(){
if(turnTimerCounter < turnTimer){
if(!isMoving && !switchingTeams){
turnTimerCounter += Time.deltaTime;
}
}else{
SwitchTeams();
turnTimerCounter = 0;
}
}
public void OnGoal(Team team){
if(!isServer){
Debug.LogWarning("OnGoal called on client, skipping");
return;}
freezeInput=true;
if(hitCounter == 1){
@@ -220,14 +266,12 @@ public class GameManager : NetworkBehaviour
if(team == Team.Blue){
blueScore++;
blueScoreText.text = blueScore.ToString();
SelectedTeam = Team.Red;
}else{
redScore++;
redScoreText.text = redScore.ToString();
SelectedTeam = Team.Blue;
}
OnTeamChanged?.Invoke(SelectedTeam);
SwitchTeams();
if(blueScore >= 3){
@@ -265,6 +309,12 @@ public class GameManager : NetworkBehaviour
Reset();
}
void OnScoreChanged(int oldScore, int newScore){
Debug.Log($"Score changed from {oldScore} to {newScore}");
redScoreText.text = redScore.ToString();
blueScoreText.text = blueScore.ToString();
}
public void Reset(bool kickoff = false)
{
+8 -3
View File
@@ -1,12 +1,15 @@
using Mirror;
using UnityEngine;
public class Goal : MonoBehaviour
public class Goal : NetworkBehaviour
{
public Team team;
void OnTriggerEnter2D(Collider2D collision)
{
{
if(!isServer){return;}
if(collision.gameObject.CompareTag("Player")){
Debug.Log($"GOAL! {team.ToString()}", gameObject);
Debug.Log($"GOAL! {team}", gameObject);
Ball ball = collision.gameObject.GetComponent<Ball>();
Debug.Log(ball.name);
GameManager.instance.OnGoal(team);
@@ -20,6 +23,8 @@ public class Goal : MonoBehaviour
void OnTriggerExit2D(Collider2D collision)
{
if(!isServer){return;}
if(collision.gameObject.CompareTag("Puck")){
Debug.Log("Puck exit goal", gameObject);
Puck puck = collision.gameObject.GetComponent<Puck>();
+144
View File
@@ -0,0 +1,144 @@
using UnityEngine;
using Mirror;
using UnityEngine.EventSystems;
public class NetPlayer : NetworkBehaviour
{
[SyncVar]
public Team myTeam;
// set the team to red if there are no other players, if not set blue
public override void OnStartLocalPlayer()
{
base.OnStartLocalPlayer();
// Count current NetPlayers in the scene
NetPlayer[] players = FindObjectsOfType<NetPlayer>();
if (players.Length <= 1)
{
myTeam = Team.Red;
CmdSetTeam(Team.Red);
}
else
{
myTeam = Team.Blue;
CmdSetTeam(Team.Blue);
}
}
[Command]
void CmdSetTeam(Team team){
myTeam = team;
}
public static NetPlayer localPlayer;
public static Vector2 curPosition;
public Vector2 startPosition;
public float curPuckPullForce;
void Start(){
if(isLocalPlayer){
localPlayer = this;
SetupInputPanel();
}
}
void OnDestroy(){
if(localPlayer == this){
localPlayer = null;
}
}
void SetupInputPanel(){
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerDown;
entry.callback.AddListener(OnPointerDown);
GameCanvas.instance.gameInputPanel.triggers.Add(entry);
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerUp;
entry.callback.AddListener(OnPointerUp);
GameCanvas.instance.gameInputPanel.triggers.Add(entry);
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.Drag;
entry.callback.AddListener(OnDrag);
GameCanvas.instance.gameInputPanel.triggers.Add(entry);
}
void OnPointerDown(BaseEventData eventData){
if(GameManager.instance.SelectedTeam != myTeam){return;}
if(GameManager.instance.m_isMoving){return;}
PointerEventData pointerEventData = eventData as PointerEventData;
Vector2 position = pointerEventData.position;
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(new Vector3(position.x, position.y, Camera.main.nearClipPlane));
curPosition= position;
Puck closestPuck = GameManager.instance.GetClosestPuck(worldPosition);
GameManager.SelectedPuck = closestPuck;
//Temporarily set the puck until server validates
CmdOnPointerDown(worldPosition);
}
[Command]
void CmdOnPointerDown(Vector2 position){
if(GameManager.instance.SelectedTeam != myTeam){return;}
Puck closestPuck = GameManager.instance.GetClosestPuck(position);
GameManager.SelectedPuck = closestPuck;
RpcOnPointerDown(closestPuck.gameObject);
}
[ClientRpc]
void RpcOnPointerDown(GameObject puckGo){
if(puckGo == null){return;}
if(!isLocalPlayer){return;}
Puck puck = puckGo.GetComponent<Puck>();
if(puck!=GameManager.SelectedPuck){
Debug.LogError("Selected puck changed on client, This cant happen. Resetting to servers choice", gameObject);
}
GameManager.SelectedPuck = puck;
}
void OnDrag(BaseEventData eventData){
PointerEventData pointerEventData = eventData as PointerEventData;
curPosition = pointerEventData.position;
if(GameManager.SelectedPuck!=null){
curPuckPullForce = GameManager.SelectedPuck.lineEnd.magnitude;
}
}
void OnPointerUp(BaseEventData eventData){
if(GameManager.SelectedPuck==null){return;}
if(GameManager.instance.SelectedTeam != myTeam){return;}
PointerEventData pointerEventData = eventData as PointerEventData;
Vector2 position = pointerEventData.position;
Vector2 direction = GameManager.SelectedPuck.lineEnd;
CmdOnPointerUp(direction);
}
[Command]
void CmdOnPointerUp(Vector2 direction){
if(GameManager.instance.SelectedTeam != myTeam){return;}
Debug.Log("Pointer up");
GameManager.instance.OnPointerUp(direction);
RpcOnPointerUp();
}
[ClientRpc]
void RpcOnPointerUp(){
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 63314f7b4865af348a747f86b6aab5a6
+18 -8
View File
@@ -1,12 +1,15 @@
using System.Collections;
using Mirror;
using UnityEngine;
using UnityEngine.EventSystems;
public class Puck : MonoBehaviour
public class Puck : NetworkBehaviour
{
[HideInInspector]public Rigidbody2D rb;
[SyncVar]
public bool isSelected;
public LineRenderer lineRenderer;
[SyncVar]
public Team team;
public GameObject redTeamMarker;
public GameObject blueTeamMarker;
@@ -38,7 +41,7 @@ public class Puck : MonoBehaviour
void Start()
{
GameManager.OnSelectedPuckChanged += OnSelectedPuckChanged;
GameEvents.OnSelectedPuckChanged += OnSelectedPuckChanged;
GameManager.OnTeamChanged += OnTeamChanged;
GameManager.instance.RegisterPuck(this);
@@ -46,7 +49,7 @@ public class Puck : MonoBehaviour
void OnDestroy()
{
GameManager.OnSelectedPuckChanged -= OnSelectedPuckChanged;
GameEvents.OnSelectedPuckChanged -= OnSelectedPuckChanged;
GameManager.OnTeamChanged -= OnTeamChanged;
GameManager.instance.DisposePuck(this);
@@ -56,9 +59,9 @@ public class Puck : MonoBehaviour
{
if (isSelected && lineRenderer != null)
{
Vector3 worldEnd = Camera.main.ScreenToWorldPoint(new Vector3(GameManager.instance.curPosition.x, GameManager.instance.curPosition.y, Camera.main.nearClipPlane));
Vector3 worldEnd = Camera.main.ScreenToWorldPoint(new Vector3(NetPlayer.curPosition.x, NetPlayer.curPosition.y, Camera.main.nearClipPlane));
lineEnd = transform.InverseTransformPoint(worldEnd);
lineEnd = transform.InverseTransformPoint(worldEnd);
float clamp = GameManager.instance.puckDragClampMax;
lineEnd = Vector2.ClampMagnitude(lineEnd, clamp);
@@ -69,7 +72,7 @@ public class Puck : MonoBehaviour
}
selectedMarker.gameObject.SetActive(GetMarkerVisibility());
if(GameManager.SelectedTeam == team){
if(GameManager.instance.SelectedTeam == team){
selectedMarker.Rotate(new Vector3(0,0,1), markerRotateSpeed * Time.deltaTime);
bounceSinA += Time.deltaTime * markerBounceSpeed;
float bounce = Mathf.Sin(bounceSinA) * markerBounceIntensity;
@@ -78,9 +81,11 @@ public class Puck : MonoBehaviour
}
bool GetMarkerVisibility(){
if(GameManager.isMoving){return false;}
if(GameManager.instance.m_isMoving){return false;}
if(NetPlayer.localPlayer == null){return false;}
if(NetPlayer.localPlayer.myTeam != team){return false;}
if(GameManager.SelectedTeam == team){
if(GameManager.instance.SelectedTeam == team){
if(GameManager.NoPuckSelected){
return true;
}else{
@@ -95,6 +100,11 @@ public class Puck : MonoBehaviour
{
isSelected = puck == this;
lineRenderer.enabled = isSelected;
if(NetPlayer.localPlayer == null){return;}
if(team != NetPlayer.localPlayer.myTeam){
lineRenderer.widthMultiplier = 0;
}
}
void OnTeamChanged(Team team){