'
' *   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.Threading
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO

Imports OpenTK
Imports OpenTK.Graphics
Imports OpenTK.Graphics.OpenGL
Imports EZGL2D

Namespace GL2DexampleJump.Game
	Public Class Player
		Inherits Entity

		Private Enum DIRECTION
			RIGHT
			LEFT
		End Enum

		Private Enum STATE
			STANDING
			RUNNING
			JUMPING
		End Enum

		Private canJump As Boolean = False
		Private JUMP_HEIGHT As Single = 9.0F
'INSTANT VB NOTE: The variable direction was renamed since Visual Basic does not allow variables and other class members to have the same name:
		Private direction_Renamed As DIRECTION = DIRECTION.RIGHT
'INSTANT VB NOTE: The variable state was renamed since Visual Basic does not allow variables and other class members to have the same name:
		Private state_Renamed As STATE = STATE.STANDING

		Private ticks As Integer = 0

		Private flipmode As Integer = GL2D.SpriteGL.FLIP_NONE

		Public Sub New()
			width = 28
			height = 38
			x = Constants.TILE_SIZE * 3
			y = Constants.TILE_SIZE * 6
			speed = 2.5F
			dy = 1
			canJump = False

		End Sub

		Public Overloads Sub update(ByVal levelMap As LevelMap, ByVal gameWindow As GameWindow)

			dx = 0
			state_Renamed = STATE.STANDING
			If gameWindow.Keyboard(OpenTK.Input.Key.Right) Then
				dx = speed
				If state_Renamed <> STATE.JUMPING Then
					state_Renamed = STATE.RUNNING
				End If
				direction_Renamed = DIRECTION.RIGHT
			End If

			If gameWindow.Keyboard(OpenTK.Input.Key.Left) Then
				dx = -speed
				If state_Renamed <> STATE.JUMPING Then
					state_Renamed = STATE.RUNNING
				End If
				direction_Renamed = DIRECTION.LEFT
			End If

			If gameWindow.Keyboard(OpenTK.Input.Key.Space) Then
				If canJump Then
					dy = -JUMP_HEIGHT
					canJump = False
					state_Renamed = STATE.JUMPING
				End If
			End If


			Dim collisionType As Collision.CollisionType = Collision.collideOnMap(Me, levelMap)

			If collisionType = Collision.CollisionType.COLLIDE_FLOOR Then
				If Not gameWindow.Keyboard(OpenTK.Input.Key.Space) Then
					canJump = True
				End If
			Else
				canJump = False
			End If

			tileX = CInt(Math.Truncate(x / Constants.TILE_SIZE))
			tileY = CInt(Math.Truncate(y / Constants.TILE_SIZE))


			flipmode = GL2D.SpriteGL.FLIP_NONE
			If direction_Renamed = DIRECTION.LEFT Then
				flipmode = GL2D.SpriteGL.FLIP_H
			End If

			ticks += 1
			If (ticks And 3) = 0 Then
				frame += 1
				frame = (frame) And 3
			End If
			Select Case state_Renamed
				Case STATE.RUNNING
					baseFrame = 0
				Case STATE.STANDING
					baseFrame = 4
				Case STATE.JUMPING
					' No animation for jumping so do nothing

			End Select
		End Sub

		Public Overloads Sub render(ByVal imageAtlas() As GL2D.SpriteGL)

			GL2D.Sprite(CInt(Math.Truncate(x)), CInt(Math.Truncate(y)), flipmode, imageAtlas(baseFrame+frame))

		End Sub

	End Class

End Namespace
