Advertisement
TizzyT

TileMapLoader -TizzyT

Jan 7th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.54 KB | None | 0 0
  1. Imports System.Collections.Concurrent
  2. Imports System.IO
  3. Imports System.Threading
  4. Public Class TileMapLoader
  5.     Public Structure Coordinate
  6.         Public H As Integer
  7.         Public X, Y As UInteger
  8.         Public Sub New(ByVal H As Integer, ByVal X As UInteger, ByVal Y As UInteger)
  9.             Me.H = H : Me.X = X : Me.Y = Y
  10.         End Sub
  11.         Public Sub New(ByVal ID As String)
  12.             Dim Parts As String() = ID.Split("-")
  13.             H = Integer.Parse(Parts(0), Globalization.NumberStyles.HexNumber)
  14.             X = UInteger.Parse(Parts(1), Globalization.NumberStyles.HexNumber)
  15.             Y = UInteger.Parse(Parts(2), Globalization.NumberStyles.HexNumber)
  16.         End Sub
  17.         Public Overrides Function ToString() As String
  18.             Return H.ToString("X8") & "-" & X.ToString("X8") & "-" & Y.ToString("X8")
  19.         End Function
  20.     End Structure
  21.     Private Sections As New ConcurrentDictionary(Of Coordinate, Msec)
  22.     Private DisposeThread As New Thread(Sub()
  23.                                             While True
  24.                                                 For Each Msec As Msec In Sections.Values
  25.                                                     Try
  26.                                                         Msec.Age()
  27.                                                     Catch ex As Exception
  28.                                                     End Try
  29.                                                 Next
  30.                                                 Thread.Sleep(1000)
  31.                                             End While
  32.                                         End Sub)
  33.     Public Sub New()
  34.         DisposeThread.Start()
  35.     End Sub
  36.     Public Sub PreLoad(ByVal H As Integer, ByVal X As UInteger, ByVal Y As UInteger)
  37.         Tile(H, X, Y)
  38.         Tile(H, X - 250, Y - 250) : Tile(H, X, Y - 250) : Tile(H, X + 250, Y - 250)
  39.         Tile(H, X - 250, Y) : Tile(H, X + 250, Y)
  40.         Tile(H, X - 250, Y + 250) : Tile(H, X, Y + 250) : Tile(H, X + 250, Y + 250)
  41.         Dim HH As Integer = H - 1
  42.         Tile(HH, X, Y)
  43.         Tile(HH, X - 250, Y - 250) : Tile(HH, X, Y - 250) : Tile(HH, X + 250, Y - 250)
  44.         Tile(HH, X - 250, Y) : Tile(HH, X + 250, Y)
  45.         Tile(HH, X - 250, Y + 250) : Tile(HH, X, Y + 250) : Tile(HH, X + 250, Y + 250)
  46.         HH = H + 1
  47.         Tile(HH, X, Y)
  48.         Tile(HH, X - 250, Y - 250) : Tile(HH, X, Y - 250) : Tile(HH, X + 250, Y - 250)
  49.         Tile(HH, X - 250, Y) : Tile(HH, X + 250, Y)
  50.         Tile(HH, X - 250, Y + 250) : Tile(HH, X, Y + 250) : Tile(HH, X + 250, Y + 250)
  51.     End Sub
  52.     Public Function Tile(ByVal H As Integer, ByVal X As UInteger, ByVal Y As UInteger) As UInteger
  53.         Dim Coordinate As New Coordinate(H, Math.Floor(X / 501), Math.Floor(Y / 501))
  54.         If Sections.ContainsKey(Coordinate) Then
  55.             Return Sections(Coordinate)(X Mod 501, Y Mod 501)
  56.         Else
  57.             Dim newMsec As New Msec(Coordinate)
  58.             AddHandler newMsec.Expired, Sub()
  59.                                             Dim M As Msec = Nothing
  60.                                             If Sections.TryRemove(Coordinate, M) Then M.Dispose()
  61.                                         End Sub
  62.             Sections.TryAdd(Coordinate, newMsec)
  63.             Return 0
  64.         End If
  65.     End Function
  66.     Private Class Msec
  67.         Implements IDisposable
  68.         Public Event Expired()
  69.         Private Tiles(500, 500) As UInteger
  70.         Private Loaded As Boolean = False
  71.         Private TTL As Byte = 20
  72.         Private LoadThread As New Thread(Sub(ByVal Coord As Coordinate)
  73.                                              Dim CoordStr As String = "Map\" & Coord.ToString & ".msec"
  74.                                              Dim Data As Byte()
  75.                                              If File.Exists(CoordStr) Then Data = File.ReadAllBytes(CoordStr) Else Exit Sub
  76.                                              Try
  77.                                                  Dim idx As Integer = 0
  78.                                                  For y As Integer = 0 To 500
  79.                                                      For x As Integer = 0 To 500
  80.                                                          Tiles(x, y) = BitConverter.ToUInt32(Data, idx)
  81.                                                          idx += 4
  82.                                                      Next
  83.                                                  Next
  84.                                                  Loaded = True
  85.                                              Catch ex As Exception
  86.                                                  Erase Tiles
  87.                                              Finally
  88.                                                  Erase Data
  89.                                              End Try
  90.                                          End Sub)
  91.         Default Public ReadOnly Property Tile(ByVal X As Integer, ByVal Y As Integer) As UInteger
  92.             Get
  93.                 TTL = 60
  94.                 If Loaded Then Return Tiles(X, Y)
  95.                 Return 0
  96.             End Get
  97.         End Property
  98.         Public Sub Age()
  99.             If TTL > 0 Then TTL -= 1
  100.             If TTL = 0 Then RaiseEvent Expired()
  101.         End Sub
  102.         Public Sub New(ByVal Coordinate As Coordinate)
  103.             LoadThread.Start(Coordinate)
  104.         End Sub
  105.         Public Sub Dispose() Implements IDisposable.Dispose
  106.             Try
  107.                 LoadThread.Abort()
  108.             Catch ex As Exception
  109.             End Try
  110.             Loaded = False
  111.             Erase Tiles
  112.         End Sub
  113.     End Class
  114. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement