Attribute VB_Name = "modPlayer"
Option Explicit

Private mPlayerX As Long
Private mPlayerY As Long
Private mPlayerReady As Boolean

Public Sub InitialisePlayer(ByVal startX As Long, ByVal startY As Long)

    mPlayerReady = False
    
    If Not IsTileWalkable(startX, startY) Then
        Err.Raise vbObjectError + 200, , "Invalid starting position."
    End If
    
    mPlayerX = startX
    mPlayerY = startY
    mPlayerReady = True
    
    DrawPlayer
    UpdatePlayerStatus "The adventure begins."
    
End Sub

Private Sub DrawPlayer()

    Dim target As Range
    
    Set target = GetScreenCell(mPlayerX, mPlayerY)
    
    With target
        .Value2 = "@"
        .Font.Color = RGB(114, 216, 244)
        .Font.Bold = True
    End With
        
End Sub

Private Sub UpdatePlayerStatus(ByVal message As String)

    With ThisWorkbook.Worksheets("Screen")
        .Range("B23").Value2 = "X: " & mPlayerX & " | Y: " & mPlayerY
        .Range("K23").Value2 = message
    End With
    
End Sub

Private Sub MovePlayer(ByVal deltaX As Long, ByVal deltaY As Long)

    Dim newX As Long
    Dim newY As Long
    
    If Not mPlayerReady Then Exit Sub
    
    newX = mPlayerX + deltaX
    newY = mPlayerY + deltaY
    
    'check whether the destination is passable
    If Not IsTileWalkable(newX, newY) Then
        UpdatePlayerStatus GetTerrainMessage(newX, newY, True)
        Exit Sub
    End If
    
    'prevent squeezing through two blocked diagonal neighbours
    If deltaX <> 0 And deltaY <> 0 Then
    
        If Not IsTileWalkable(mPlayerX + deltaX, mPlayerY) And Not IsTileWalkable(mPlayerX, mPlayerY + deltaY) Then
            UpdatePlayerStatus "You cannot squeeze through that gap."
            Exit Sub
        End If
        
    End If
    
    'restore the terrain at the old position
    RedrawTerrainTile mPlayerX, mPlayerY
    
    'update the player's actual position
    mPlayerX = newX
    mPlayerY = newY
    
    'draw the player at the new position
    DrawPlayer
    
    UpdatePlayerStatus GetTerrainMessage(newX, newY, False)

End Sub

Public Sub MoveNW()

    MovePlayer -1, -1
    
End Sub

Public Sub MoveNorth()

    MovePlayer 0, -1
    
End Sub

Public Sub MoveNE()

    MovePlayer 1, -1
    
End Sub

Public Sub MoveWest()

    MovePlayer -1, 0
    
End Sub

Public Sub MoveEast()

    MovePlayer 1, 0
    
End Sub

Public Sub MoveSW()

    MovePlayer -1, 1
    
End Sub

Public Sub MoveSouth()

    MovePlayer 0, 1
    
End Sub

Public Sub MoveSE()

    MovePlayer 1, 1
    
End Sub

Public Sub WaitTurn()

    If Not mPlayerReady Then Exit Sub
    
    UpdatePlayerStatus "You wait."
    
End Sub

