'
' *   EZ GL2D Platfom Game engine Example
' *   Source is provided "as is" without express or implied warranty of any kind.
' * 
'

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text

Namespace GL2DexampleJump.Game
	Public Class Collision
		Public Enum CollisionType
			COLLIDE_NONE
			COLLIDE_FLOOR
			COLLIDE_WALL
			COLLIDE_CEILING

		End Enum

		Private Shared tileX As Integer
		Private Shared tileY As Integer
		Public Shared Function collideWalls(ByVal offset As Single, ByVal e As Entity, ByVal levelMap As LevelMap) As Boolean

			Dim ix As Integer = CInt(Math.Truncate(e.getX() + offset))
			Dim iy As Integer = CInt(Math.Truncate(e.getY()))

			Dim hei As Integer = CInt(Math.Truncate(e.getHeight()))

			Dim tileYpixels As Integer = iy - (iy Mod Constants.TILE_SIZE)
			Dim testEnd As Integer = iy + hei

			tileX = ix \ Constants.TILE_SIZE
			tileY = tileYpixels \ Constants.TILE_SIZE


			Dim map(,) As Integer = levelMap.getMap()
			Do While tileYpixels <= testEnd
				If map(tileX,tileY) > 0 Then
					Return True
				End If
				tileY += 1
				tileYpixels += Constants.TILE_SIZE
			Loop

			Return False
		End Function

		Public Shared Function collideFloors(ByVal offset As Single, ByVal e As Entity, ByVal levelMap As LevelMap) As Boolean

			Dim ix As Integer = CInt(Math.Truncate(e.getX()))
			Dim iy As Integer = CInt(Math.Truncate(e.getY() + offset))

			Dim wid As Integer = CInt(Math.Truncate(e.getWidth()))

			Dim tileXpixels As Integer = ix - (ix Mod Constants.TILE_SIZE)
			Dim testEnd As Integer = ix + wid

			tileX = tileXpixels \ Constants.TILE_SIZE
			tileY = iy \ Constants.TILE_SIZE


			Dim map(,) As Integer = levelMap.getMap()
			Do While tileXpixels <= testEnd
				If map(tileX,tileY) > 0 Then
					Return True
				End If
				tileX += 1
				tileXpixels += Constants.TILE_SIZE
			Loop

			Return False
		End Function

		Public Shared Function collideOnMap(ByVal e As Entity, ByVal levelMap As LevelMap) As CollisionType

			tileX = 0
			tileY = 0
'INSTANT VB NOTE: The variable collision was renamed since it may cause conflicts with calls to static members of the user-defined type with this name:
			Dim collision_Renamed As CollisionType = CollisionType.COLLIDE_NONE

			If e.getDx() > 0 Then

				If collideWalls(e.getDx() + e.getWidth(), e, levelMap) Then
					e.setX((tileX * Constants.TILE_SIZE) - e.getWidth() - 1)
					collision_Renamed = CollisionType.COLLIDE_WALL
				Else
					e.addX(e.getDx())
				End If


			ElseIf e.getDx() < 0 Then

				If collideWalls(e.getDx(), e, levelMap) Then
					e.setX((tileX + 1) * Constants.TILE_SIZE + 1)
					collision_Renamed = CollisionType.COLLIDE_WALL
				Else
					e.addX(e.getDx())
				End If

			End If


			If e.getDy() < 0 Then

				If collideFloors(e.getDy(), e, levelMap) Then
					e.setY((tileY + 1) * Constants.TILE_SIZE + 1)
					e.setDy(0)
					collision_Renamed = CollisionType.COLLIDE_CEILING
				Else
					e.addY(e.getDy())
					e.addDy(Constants.GRAVITY)
				End If

			Else

				If collideFloors(e.getDy() + e.getHeight(), e, levelMap) Then
					e.setY((tileY * Constants.TILE_SIZE) - e.getHeight() - 1)
					e.setDy(1)
					collision_Renamed = CollisionType.COLLIDE_FLOOR
				Else
					e.addY(e.getDy())
					e.addDy(Constants.GRAVITY)
				End If
			End If


			Return collision_Renamed

		End Function
	End Class
End Namespace
