Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Collections.Concurrent
- Imports System.IO
- Imports System.Threading
- Public Class TileMapLoader
- Public Structure Coordinate
- Public H As Integer
- Public X, Y As UInteger
- Public Sub New(ByVal H As Integer, ByVal X As UInteger, ByVal Y As UInteger)
- Me.H = H : Me.X = X : Me.Y = Y
- End Sub
- Public Sub New(ByVal ID As String)
- Dim Parts As String() = ID.Split("-")
- H = Integer.Parse(Parts(0), Globalization.NumberStyles.HexNumber)
- X = UInteger.Parse(Parts(1), Globalization.NumberStyles.HexNumber)
- Y = UInteger.Parse(Parts(2), Globalization.NumberStyles.HexNumber)
- End Sub
- Public Overrides Function ToString() As String
- Return H.ToString("X8") & "-" & X.ToString("X8") & "-" & Y.ToString("X8")
- End Function
- End Structure
- Private Sections As New ConcurrentDictionary(Of Coordinate, Msec)
- Private DisposeThread As New Thread(Sub()
- While True
- For Each Msec As Msec In Sections.Values
- Try
- Msec.Age()
- Catch ex As Exception
- End Try
- Next
- Thread.Sleep(1000)
- End While
- End Sub)
- Public Sub New()
- DisposeThread.Start()
- End Sub
- Public Sub PreLoad(ByVal H As Integer, ByVal X As UInteger, ByVal Y As UInteger)
- Tile(H, X, Y)
- Tile(H, X - 250, Y - 250) : Tile(H, X, Y - 250) : Tile(H, X + 250, Y - 250)
- Tile(H, X - 250, Y) : Tile(H, X + 250, Y)
- Tile(H, X - 250, Y + 250) : Tile(H, X, Y + 250) : Tile(H, X + 250, Y + 250)
- Dim HH As Integer = H - 1
- Tile(HH, X, Y)
- Tile(HH, X - 250, Y - 250) : Tile(HH, X, Y - 250) : Tile(HH, X + 250, Y - 250)
- Tile(HH, X - 250, Y) : Tile(HH, X + 250, Y)
- Tile(HH, X - 250, Y + 250) : Tile(HH, X, Y + 250) : Tile(HH, X + 250, Y + 250)
- HH = H + 1
- Tile(HH, X, Y)
- Tile(HH, X - 250, Y - 250) : Tile(HH, X, Y - 250) : Tile(HH, X + 250, Y - 250)
- Tile(HH, X - 250, Y) : Tile(HH, X + 250, Y)
- Tile(HH, X - 250, Y + 250) : Tile(HH, X, Y + 250) : Tile(HH, X + 250, Y + 250)
- End Sub
- Public Function Tile(ByVal H As Integer, ByVal X As UInteger, ByVal Y As UInteger) As UInteger
- Dim Coordinate As New Coordinate(H, Math.Floor(X / 501), Math.Floor(Y / 501))
- If Sections.ContainsKey(Coordinate) Then
- Return Sections(Coordinate)(X Mod 501, Y Mod 501)
- Else
- Dim newMsec As New Msec(Coordinate)
- AddHandler newMsec.Expired, Sub()
- Dim M As Msec = Nothing
- If Sections.TryRemove(Coordinate, M) Then M.Dispose()
- End Sub
- Sections.TryAdd(Coordinate, newMsec)
- Return 0
- End If
- End Function
- Private Class Msec
- Implements IDisposable
- Public Event Expired()
- Private Tiles(500, 500) As UInteger
- Private Loaded As Boolean = False
- Private TTL As Byte = 20
- Private LoadThread As New Thread(Sub(ByVal Coord As Coordinate)
- Dim CoordStr As String = "Map\" & Coord.ToString & ".msec"
- Dim Data As Byte()
- If File.Exists(CoordStr) Then Data = File.ReadAllBytes(CoordStr) Else Exit Sub
- Try
- Dim idx As Integer = 0
- For y As Integer = 0 To 500
- For x As Integer = 0 To 500
- Tiles(x, y) = BitConverter.ToUInt32(Data, idx)
- idx += 4
- Next
- Next
- Loaded = True
- Catch ex As Exception
- Erase Tiles
- Finally
- Erase Data
- End Try
- End Sub)
- Default Public ReadOnly Property Tile(ByVal X As Integer, ByVal Y As Integer) As UInteger
- Get
- TTL = 60
- If Loaded Then Return Tiles(X, Y)
- Return 0
- End Get
- End Property
- Public Sub Age()
- If TTL > 0 Then TTL -= 1
- If TTL = 0 Then RaiseEvent Expired()
- End Sub
- Public Sub New(ByVal Coordinate As Coordinate)
- LoadThread.Start(Coordinate)
- End Sub
- Public Sub Dispose() Implements IDisposable.Dispose
- Try
- LoadThread.Abort()
- Catch ex As Exception
- End Try
- Loaded = False
- Erase Tiles
- End Sub
- End Class
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement