2024-01-28 15:33:36 +03:00
|
|
|
using System;
|
2024-01-30 12:28:34 +03:00
|
|
|
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
|
2024-01-28 15:33:36 +03:00
|
|
|
using Syntriax.Engine.Core;
|
|
|
|
|
|
|
|
namespace Pong.Behaviours;
|
|
|
|
|
2024-01-31 12:17:43 +03:00
|
|
|
public class PongManagerBehaviour : BehaviourOverride
|
2024-01-28 15:33:36 +03:00
|
|
|
{
|
2024-01-31 12:17:43 +03:00
|
|
|
public Action<PongManagerBehaviour>? OnReset { get; set; } = null;
|
|
|
|
public Action<PongManagerBehaviour>? OnFinished { get; set; } = null;
|
|
|
|
public Action<PongManagerBehaviour>? OnScored { get; set; } = null;
|
2024-01-28 15:33:36 +03:00
|
|
|
|
|
|
|
public int ScoreLeft { get; private set; } = 0;
|
|
|
|
public int ScoreRight { get; private set; } = 0;
|
|
|
|
public int ScoreSum => ScoreLeft + ScoreRight;
|
|
|
|
|
2024-01-30 12:43:30 +03:00
|
|
|
public int WinScore { get; } = 5;
|
2024-01-28 15:33:36 +03:00
|
|
|
|
2024-01-31 12:17:43 +03:00
|
|
|
public PongManagerBehaviour() => WinScore = 5;
|
|
|
|
public PongManagerBehaviour(int winScore) => WinScore = winScore;
|
2024-01-30 12:28:34 +03:00
|
|
|
|
|
|
|
protected override void OnFirstActiveFrame()
|
|
|
|
{
|
|
|
|
KeyboardInputsBehaviour? buttonInputs = null!;
|
|
|
|
|
|
|
|
if (!BehaviourController.TryGetBehaviour(out buttonInputs))
|
|
|
|
buttonInputs = BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
|
|
|
|
|
|
|
|
buttonInputs.RegisterOnRelease(Keys.Space, (_, _1) => Reset());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-28 15:33:36 +03:00
|
|
|
public void ScoreToLeft()
|
|
|
|
{
|
|
|
|
ScoreLeft++;
|
2024-01-30 12:43:30 +03:00
|
|
|
OnScored?.Invoke(this);
|
2024-01-28 15:33:36 +03:00
|
|
|
|
|
|
|
CheckFinish();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ScoreToRight()
|
|
|
|
{
|
|
|
|
ScoreRight++;
|
2024-01-30 12:43:30 +03:00
|
|
|
OnScored?.Invoke(this);
|
2024-01-28 15:33:36 +03:00
|
|
|
|
|
|
|
CheckFinish();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
{
|
|
|
|
ScoreLeft = ScoreRight = 0;
|
2024-01-30 12:43:30 +03:00
|
|
|
OnReset?.Invoke(this);
|
2024-01-28 15:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private void CheckFinish()
|
|
|
|
{
|
2024-01-29 12:34:27 +03:00
|
|
|
int halfwayScore = (int)(WinScore * .5f);
|
|
|
|
|
|
|
|
if (ScoreLeft > halfwayScore || ScoreRight > halfwayScore)
|
2024-01-30 12:43:30 +03:00
|
|
|
OnFinished?.Invoke(this);
|
2024-01-28 15:33:36 +03:00
|
|
|
}
|
|
|
|
}
|