Imports System.Threading.Tasks
Partial Public Class MainPage
Inherits UserControl
Public Sub New()
InitializeComponent()
End Sub
Private Async Sub UserControl_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles MyBase.Loaded
While True
' Gameplay screen...
Dim dat = GameLogic.InitializeGameData()
frame.Child = GameLogic.MakeGameScreen(dat)
While dat.remaining > 0 AndAlso dat.active > 0
Dim delay = TaskEx.Delay(10) ' start the clock now
GameLogic.MovePaddle(dat, mouseX)
GameLogic.UpdateBalls(dat)
Await delay ' and wait for however much longer we needed
End While
' Victory screen...
Dim VictoryUI = GameLogic.MakeVictoryScreen(dat.remaining)
frame.Child = VictoryUI.Frame
Await TaskEx.Delay(3000)
VictoryUI.Sound.Stop()
' Introduction screen...
Dim IntroUI = GameLogic.MakeIntroScreen()
frame.Child = IntroUI.Frame
Await ButtonClick(IntroUI.Button)
mouseX = -1
End While
End Sub
Dim mouseX = -1
Private Sub UserControl_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove
mouseX = e.GetPosition(frame.Child).X
End Sub
' A helper method to make it easier to await for a button-click
Function ButtonClick(ByVal b As Button) As Task
Dim tcs As New TaskCompletionSource(Of Object)
Dim handler As RoutedEventHandler = Sub(sender, e)
RemoveHandler b.Click, handler
tcs.TrySetResult(Nothing)
End Sub
AddHandler b.Click, handler
Return tcs.Task
End Function
End Class