Skip to content

Commit 2edbd55

Browse files
committed
player: Add events for ball creation and destruction.
1 parent 4a4eabf commit 2edbd55

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

VisualPinball.Unity/VisualPinball.Unity/Game/Player.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public class Player : MonoBehaviour
5252

5353
public event EventHandler OnUpdate;
5454

55+
public event EventHandler<BallEvent> OnBallCreated;
56+
public event EventHandler<BallEvent> OnBallDestroyed;
57+
5558
[HideInInspector] [SerializeField] public string debugUiId;
5659
[HideInInspector] [SerializeField] public string physicsEngineId;
5760

@@ -485,6 +488,15 @@ public void OnEvent(in EventData eventData)
485488
}
486489
}
487490

491+
internal void BallCreated(Entity ballEntity, GameObject ball)
492+
{
493+
OnBallCreated?.Invoke(this, new BallEvent(ballEntity, ball));
494+
}
495+
internal void BallDestroyed(Entity ballEntity, GameObject ball)
496+
{
497+
OnBallDestroyed?.Invoke(this, new BallEvent(ballEntity, ball));
498+
}
499+
488500
#endregion
489501

490502
#region API
@@ -579,4 +591,16 @@ private static void HandleInput(object obj, InputActionChange change)
579591
}
580592
}
581593
}
594+
595+
public readonly struct BallEvent
596+
{
597+
public readonly Entity BallEntity;
598+
public readonly GameObject Ball;
599+
600+
public BallEvent(Entity ballEntity, GameObject ball)
601+
{
602+
BallEntity = ballEntity;
603+
Ball = ball;
604+
}
605+
}
582606
}

VisualPinball.Unity/VisualPinball.Unity/VPT/Ball/BallManager.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,17 @@ public void CreateEntity(GameObject ballGo, int id, in float3 worldPos, in float
148148
}
149149

150150
NumBalls++;
151+
152+
_player.BallCreated(entity, ballGo);
151153
}
152154

153155
public void DestroyEntity(Entity ballEntity)
154156
{
157+
var ballGo = _player.Balls[ballEntity];
158+
_player.BallDestroyed(ballEntity, ballGo);
159+
155160
// destroy game object
156-
Object.DestroyImmediate(_player.Balls[ballEntity]);
161+
Object.DestroyImmediate(ballGo);
157162
_player.Balls.Remove(ballEntity);
158163

159164
// destroy entity

0 commit comments

Comments
 (0)