65 lines
1.6 KiB
C#
65 lines
1.6 KiB
C#
using System;
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
using Syntriax.Engine.Core;
|
|
|
|
namespace Pong.Behaviours;
|
|
|
|
public class PongManagerBehaviour : BehaviourOverride
|
|
{
|
|
public Action<PongManagerBehaviour>? OnReset { get; set; } = null;
|
|
public Action<PongManagerBehaviour>? OnFinished { get; set; } = null;
|
|
public Action<PongManagerBehaviour>? OnScored { get; set; } = null;
|
|
|
|
public int ScoreLeft { get; private set; } = 0;
|
|
public int ScoreRight { get; private set; } = 0;
|
|
public int ScoreSum => ScoreLeft + ScoreRight;
|
|
|
|
public int WinScore { get; } = 5;
|
|
|
|
public PongManagerBehaviour() => WinScore = 5;
|
|
public PongManagerBehaviour(int winScore) => WinScore = winScore;
|
|
|
|
protected override void OnFirstActiveFrame()
|
|
{
|
|
KeyboardInputsBehaviour? buttonInputs = null!;
|
|
|
|
if (!BehaviourController.TryGetBehaviour(out buttonInputs))
|
|
buttonInputs = BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
|
|
|
|
buttonInputs.RegisterOnRelease(Keys.Space, (_, _1) => Reset());
|
|
}
|
|
|
|
|
|
public void ScoreToLeft()
|
|
{
|
|
ScoreLeft++;
|
|
OnScored?.Invoke(this);
|
|
|
|
CheckFinish();
|
|
}
|
|
|
|
public void ScoreToRight()
|
|
{
|
|
ScoreRight++;
|
|
OnScored?.Invoke(this);
|
|
|
|
CheckFinish();
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
ScoreLeft = ScoreRight = 0;
|
|
OnReset?.Invoke(this);
|
|
}
|
|
|
|
private void CheckFinish()
|
|
{
|
|
int halfwayScore = (int)(WinScore * .5f);
|
|
|
|
if (ScoreLeft > halfwayScore || ScoreRight > halfwayScore)
|
|
OnFinished?.Invoke(this);
|
|
}
|
|
}
|