;==================================================================
;Project Title: CRYSTALIUM
;Author:        Vladislav Borzunov aka ObelardO
;Email:         Obelardos@gmail.com 
;Version:       0.9 Alpha
;Date:          04.03.13
;Notes:         work with the levels and load resources
;==================================================================

Const MAP_COLLISION = 2

Dim MAP_MATRIX$(1024,512)

Global MAP_ENTITY
Global MAP_LOADED
Global MAP_NAME$
Global MAP_NEXT$
Global MAP_WIDTH
Global MAP_HEIGHT
Global MAP_BACKGROUND
Global MAP_DEBUG
Global MAP_TIME
Global MAP_FRAMEPAUSE

Global IMAGE_GENERAL
Global TEXTURE_GENERAL
Global TEXTURE_GENERAL_NORMAL
Global TEXTURE_EXPLOSION
Global TEXTURE_LIGHT
Global EFFECT_BUMP
Global EFFECT_BUMP_SPECULAR

Global SOUND_CRYSTAL_PICKUP
Global SOUND_PLAYER_LOOP
Global SOUND_BLOCK_REMOVE_A
Global SOUND_BLOCK_REMOVE_B
Global SOUND_BLOCK_REMOVE_C
Global SOUND_BLOCK_REMOVE_D
Global SOUND_DYNAMITE_MOVE
Global SOUND_EXPLOSION_A
Global SOUND_EXPLOSION_B
Global SOUND_STONE_FELL
Global SOUND_MENU_OPEN

; Load game data
Function DataLoad()
	Loggin("[INFO] Game data is loading.")
	xCollisions PLAYER_COLLISION, MAP_COLLISION,   2, 2
	xCollisions PLAYER_COLLISION, STONE_COLLISION, 2, 2
	;xCollisions STONE_COLLISION,  MAP_COLLISION,   2, 2
	MAP_ENTITY             = xCreatePivot()	
	CRYSTAL_ENTITY         = xCreatePivot()
	CRYSTAL_PLAYERPIVOT    = xCreatePivot()
	IMAGE_GENERAL          = xLoadAnimImage("data\textures\general.png", 64, 64, 0, 15)
	TEXTURE_GENERAL        = xLoadAnimTexture("data\textures\general.png", 1, 64, 64, 0, 15)
	TEXTURE_GENERAL_NORMAL = xLoadAnimTexture("data\textures\general_normal.png", 1, 64, 64, 0, 15)
	TEXTURE_EXPLOSION      = xLoadAnimTexture("data\textures\explosion.png", 2, 64, 64, 0, 39) 
	MENU_FONT              = xLoadAnimImage("data\images\font.png", 32, 32, 0, 256)
	MENU_CURSOS            = xLoadImage("data\images\cursor.png")
	TEXTURE_CRYSTAL        = xLoadTexture("data\textures\crystal.png")
      TEXTURE_CRYSTAL_NORMAL = xLoadTexture("data\textures\crystal_normal.png")
      TEXTURE_LIGHT  	     = xLoadTexture("data\textures\light.png",2)
	PLAYER_TEXTURE 	     = xLoadTexture("data\textures\plasm.png",2)
	PLAYER_SHADER          = xLoadFXFile("data\shaders\FireSphere.fx")
	EFFECT_BUMP            = xLoadFXFile("data\shaders\Bump Light.fx")
	EFFECT_BUMP_SPECULAR   = xLoadFXFile("data\shaders\bump.fx")
	SOUND_CRYSTAL_PICKUP   = xLoadSound("data\sounds\crystal_pickup.wav")
	SOUND_PLAYER_LOOP      = xLoad3DSound("data\sounds\player_loop.wav"): xLoopSound SOUND_PLAYER_LOOP
	SOUND_BLOCK_REMOVE_A   = xLoadSound("data\sounds\block_remove_a.wav")
	SOUND_BLOCK_REMOVE_B   = xLoadSound("data\sounds\block_remove_b.wav")
	SOUND_BLOCK_REMOVE_C   = xLoadSound("data\sounds\block_remove_c.wav")
	SOUND_BLOCK_REMOVE_D   = xLoadSound("data\sounds\block_remove_d.wav")
	SOUND_DYNAMITE_MOVE    = xLoadSound("data\sounds\dynamite_move.wav")
	SOUND_MENU_OPEN        = xLoadSound("data\sounds\menu_open.wav")
	SOUND_EXPLOSION_A      = xLoad3DSound("data\sounds\dynamite_explosion_a.wav")
	SOUND_EXPLOSION_B      = xLoad3DSound("data\sounds\dynamite_explosion_b.wav")
	SOUND_STONE_FELL       = xLoad3DSound("data\sounds\stone_felldown.wav")
	Loggin("[INFO] Game data is loaded.")
End Function

; Read and creation map
Function MapLoad(Name$)
	If Not MAP_LOADED
		MapFile$ = OpenFile("data\maps\"+Name+".cmp")
		If MapFile
			Loggin("[INFO] Level "+StrQuote(Name)+" is loading.")
			MAP_NAME = Name
			MAP_LOADED = 1
			xShowEntity PLAYER_ENTITY
			While Not Eof(MapFile)
				MapLine$ = ReadLine(MapFile)
				If Left (MapLine,1) = "{"
					MapLine = Mid(MapLine, 2, Len(MapLine) - 2)
					InGameCommand(mapline)
				Else
					MapWidth = Len(MapLine) / 2 - 1 
					For Width = 0 To MapWidth
						Block$ = Mid(MapLine, Width*2+1, 2)

						; object: fireball
						If Block = "FB"  CreateFireBall(Width*2, Height*2)
						; object: next level
						If Block = "NL" CreateTunnel(Width*2, Height*2)
						; object: trigger
						If Block = "TG" CreateTrigger(Width*2, Height*2)
						; object: Movable stone
						If Block = "MS" CreateStone(Width*2, Height*2)
						; object: Player spawn
						If Block = "PS" PositionPlayer (Width*2, Height*2)
						; object: Normal Crystal
						If Block = "NC" CreateCrystal (Width*2, Height*2)
						; object: Super Crystal
						If Block = "SC" CreateCrystal (Width*2, Height*2, 1)
						; object: Dynamite
						If Block = "DN" CreateDynamite (Width*2, Height*2)
						; object: Wooden crate
						If Block = "WC" CreateDynamite (Width*2, Height*2, "WC", 0)
						; object: Block
						If Int(Block) CreateBlock(Width*2, Height*2, Block)
						
						If block = "TG"  block = ""
						If block = "PS"  block = ""
						If block = "  "  block = ""
						MAP_MATRIX( Width, Abs(height)) = Block
					Next
					Height = Height - 1
					If MapWidth > MAP_WIDTH MAP_WIDTH = MapWidth
				End If
			Wend
			MAP_HEIGHT = Abs(height)
			CloseFile(MapFile)
			Loggin("[INFO] Level "+StrQuote(Name)+" is loaded.")
		Else
			Loggin("[ERROR] Level "+StrQuote(Name)+" does not exist!")
			xHideEntity PLAYER_ENTITY
			CreateMenu("main")
		End If
	Else
		For LocalW = 0 To MAP_WIDTH
			For LocalH = 0 To MAP_HEIGHT
				MAP_MATRIX(LocalW ,LocalH) = ""
			Next
		Next
		DeleteFireBalls()
		DeleteTunnels()
		DeleteTriggers()
		DeleteStones()
		deleteExplosions()
		DeleteDynamites()
		DeleteBlocks()
		DeleteCrystals()
		MAP_WIDTH = 0
		MAP_HEIGHT = 0
		MAP_TIME = 0
		MAP_FRAMEPAUSE = 0
		MAP_LOADED = 0
		CRYSTAL_COUNT = 0
		PLAYER_SCORE = 0
		PLAYER_CRYSTALS = 0
		PLAYER_LIGHTINING# = 22
		PLAYER_CAMERA_ZOOM# = 20
		PLAYER_CAMERA_SHAKE# = 1
		MAp_NEXT = ""
		UpdatePlayers()
		mapload(Name)
	End If
End Function 