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
+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)
{