123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- using Assets.StreetLight.Poco;
- using Assets.StreetLight.Scripts;
- using System;
- using System.Collections;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- using TMPro;
- using Unity.VisualScripting;
- using UnityEngine;
- using UnityEngine.UI;
- namespace Assets.Pong
- {
- public class PongBehavior : MonoBehaviour
- {
- Person playerLeft;
- Person playerRight;
- State state = State.WaitingForPlayers;
- Ball ball;
- BlockingCollection<Action> taskQueue;
- string logFileName;
- enum State
- {
- WaitingForPlayers,
- ReadyForKickoff,
- Running
- }
- PersonManager PersonManager => personManagerLazy.Value;
- public int ScoreLeft { get; set; }
- public int ScoreRight { get; set; }
- Lazy<PersonManager> personManagerLazy;
- private void Awake()
- {
- personManagerLazy = new Lazy<PersonManager>(FindObjectOfType<PersonManager>);
- }
- private void Start()
- {
- PersonManager.PersonAppeared += PersonManager_PersonAppeared;
- PersonManager.PersonDisappeared += PersonManager_PersonDisappeared;
- var sphere = GameObject.Find("Sphere");
- ball = sphere.GetComponent<Ball>();
- taskQueue = new BlockingCollection<Action>();
- logFileName = Path.Combine(@".\Logs", $"Log{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.txt");
- InitializeLogFile();
- }
- private void InitializeLogFile()
- {
- var directoryPath = Path.GetDirectoryName(logFileName);
- if (!Directory.Exists(directoryPath))
- {
- Directory.CreateDirectory(directoryPath);
- }
- if (!File.Exists(logFileName))
- {
- File.WriteAllText(logFileName, string.Empty);
- }
- }
- private void AppendNewLogEntry(string message)
- {
- taskQueue.Add(() => File.AppendAllLines(logFileName, new string[] { $"{DateTime.Now}: {message}" }));
- Task.Run(() => taskQueue.Take().Invoke());
- }
- private void PersonManager_PersonDisappeared(object sender, Person e)
- {
- if (playerLeft.Id == e.Id)
- {
- playerLeft = null;
- if (state == State.Running)
- {
- EndGame();
- }
- }
- else if (playerRight.Id == e.Id)
- {
- playerRight = null;
- if (state == State.Running)
- {
- EndGame();
- }
- }
- }
- private void EndGame()
- {
- AppendNewLogEntry($"Game ended with score {ScoreLeft}:{ScoreRight}.");
- ScoreLeft = 0;
- ScoreRight = 0;
- ball.GoalScored -= Ball_GoalScored;
- ball.Recenter();
- state = State.WaitingForPlayers;
- }
- private void PersonManager_PersonAppeared(object sender, Person e)
- {
- if (playerLeft == null && playerRight == null)
- {
- if (e.GetUnityPosition().x > 0)
- {
- playerRight = e;
- AppendNewLogEntry("Right player joined. Waiting for left player...");
- }
- else
- {
- playerLeft = e;
- AppendNewLogEntry("Left player joined. Waiting for right player...");
- }
- }
- else if (playerLeft == null)
- {
- playerLeft = e;
- AppendNewLogEntry("All players joined. Let's go!");
- StartGame();
- }
- else if (playerRight == null)
- {
- playerRight = e;
- AppendNewLogEntry("All players joined. Let's go!");
- StartGame();
- }
- }
- private void StartGame()
- {
- state = State.Running;
- AppendNewLogEntry($"New game started.");
- ball.GoalScored += Ball_GoalScored;
- ball.Kickoff();
- }
- private void Ball_GoalScored(object sender, Side e)
- {
- var ball = (Ball)sender;
- if (e == Side.Left)
- {
- ScoreRight++;
- AppendNewLogEntry($"Right player scored a goal. Current score: {ScoreLeft}:{ScoreRight}");
- }
- else
- {
- ScoreLeft++;
- AppendNewLogEntry($"Left player scored a goal. Current score: {ScoreLeft}:{ScoreRight}");
- }
- ball.Recenter();
- ball.Kickoff();
- }
- void Update()
- {
- if (playerLeft != null)
- {
- var bumper = GameObject.Find("Bump1");
- bumper.transform.position = new Vector3(bumper.transform.position.x, bumper.transform.position.y, playerLeft.GetUnityPosition().z);
- }
- else
- {
- var bumper = GameObject.Find("Bump1");
- bumper.transform.position = new Vector3(bumper.transform.position.x, bumper.transform.position.y, 0);
- }
- if (playerRight != null)
- {
- var bumper = GameObject.Find("Bump2");
- bumper.transform.position = new Vector3(bumper.transform.position.x, bumper.transform.position.y, playerRight.GetUnityPosition().z);
- }
- else
- {
- var bumper = GameObject.Find("Bump2");
- bumper.transform.position = new Vector3(bumper.transform.position.x, bumper.transform.position.y, 0);
- }
- }
- }
- }
|