#Region  Project Attributes
    #ApplicationLabel: Coin Effect
    #VersionCode: 1
    #VersionName: 0.01
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: False
#End Region
Sub Process_Globals
    Private RenderTimer As Timer
End Sub
Sub Globals
    Type vector(x As Float, y As Float)
    Type coin(x As Float, y As Float, w As Float, h As Float, sprite(8) As Bitmap, frame As Int, seekVec As vector, tag As Object, targetReached As Boolean)  
    Private xui As XUI
    Private myCanvas As B4XCanvas
    Private mainPnl As Panel
    Private TargetPoint As vector
    Private vpW, vpH As Float      
    Private cointsCollected As Int = 0
    Private frames As Int = 0
    Private coinList As List
    Private coinCounter As Int = 0
    Private coinSprite As Bitmap
    Private minimumSpeed As Float
End Sub
Sub Activity_Create(FirstTime As Boolean)
    vpW = 100%x
    vpH = 100%y
    coinList.Initialize
    mainPnl.Initialize("mainpnl")
    Activity.AddView(mainPnl,0,0,vpW,vpH)
    Activity.Color = Colors.White
    myCanvas.Initialize(mainPnl)
    RenderTimer.Initialize("RenderTimer",25)
    coinSprite.Initialize(File.DirAssets,"coin.png")
    TargetPoint.Initialize
    TargetPoint.x = vpW*0.2
    TargetPoint.y = vpH*0.2
    minimumSpeed = vpW*0.002
   
    RenderTimer.Enabled = True
End Sub
Sub RenderTimer_Tick
    frames = frames + 1
    resteCanvas
    movecoins
    drawcoins
    updatecoinlist
    myCanvas.Invalidate
End Sub
Sub resteCanvas
    myCanvas.ClearRect(myCanvas.TargetRect)
    myCanvas.DrawText("Touch the Screen",vpW/2,vpH/2,xui.CreateDefaultFont(18),xui.Color_Black,"CENTER")
    myCanvas.DrawText(cointsCollected,TargetPoint.x,TargetPoint.y,xui.CreateDefaultFont(18),xui.Color_Black,"LEFT")
End Sub
Sub movecoins
    For Each mycoin As coin In coinList
        Dim stepx As Float = ((TargetPoint.x-mycoin.w)-mycoin.x)/20
        Dim stepy As Float = ((TargetPoint.y-mycoin.h)-mycoin.y)/20
       
        If stepx < 0 Then stepx = Min(stepx,-minimumSpeed)Else stepx = Max(stepx,minimumSpeed)
        If stepy < 0 Then stepy = Min(stepy,-minimumSpeed)Else stepx = Max(stepy,minimumSpeed)
       
        mycoin.x = mycoin.x+stepx
        mycoin.y = mycoin.y+stepy
        If mycoin.x<=TargetPoint.x-(mycoin.w/2) And mycoin.y<=TargetPoint.y-(mycoin.h/2) Then
             mycoin.targetReached = True
             cointsCollected = cointsCollected + 1
        End If
    Next
End Sub
Sub drawcoins
    For Each mycoin As coin In coinList
        If frames Mod 3 = 0 Then mycoin.frame = (mycoin.frame + 1) Mod mycoin.sprite.Length      
        myCanvas.DrawBitmap(mycoin.sprite(mycoin.frame),createRect(mycoin.x,mycoin.y,mycoin.w,mycoin.h))
    Next
End Sub
Sub updatecoinlist
    For i = coinList.Size-1 To 0 Step -1
        Dim mycoin As coin = coinList.Get(i)
        If mycoin.targetReached Then coinList.RemoveAt(i)
    Next
End Sub
Sub createRect(left As Float, top As Float, width As Float, height As Float) As B4XRect
    Dim myrect As B4XRect
    myrect.Initialize(left,top,left+width,top+height)
    Return myrect
End Sub
Sub mainpnl_Touch (Action As Int, X As Float, Y As Float)
    Select Action
        Case 0 'up
           
        Case 1 'down
            createNewCoin(X,Y)
        Case 2 'move
                   
    End Select
End Sub
Sub createNewCoin(posx As Float, posy As Float)
    Dim mycoin As coin
    mycoin.Initialize
    mycoin.x = posx
    mycoin.y = posy
    mycoin.w = vpW*0.05
    mycoin.h = mycoin.w
    mycoin.seekVec = TargetPoint
    mycoin.tag = coinCounter
    mycoin.frame = 0
    mycoin.targetReached = False
    For x = 0 To mycoin.sprite.Length-1
        Dim width As Float = coinSprite.Width/mycoin.sprite.Length
        Dim left As Float = x * width
        mycoin.sprite(x) = coinSprite.Crop(left,0,width,coinSprite.Height)
    Next
    coinCounter = coinCounter + 1
    coinList.Add(mycoin)  
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub