I'm trying to write my present game as modular and OOP as I can, but I am having a little problem because not all of the Global variables I have made actually seem to be Global in scope. A generic example:
I have my main program:
Import "Map.bmx"
Import "KeyResponse.bmx"
Global BackgroundImage:TImage = LoadAnimImage ( "incbin::resource/tiles.png",CellSize,CellSize,FirstFrame,MaxNumFrames,FILTEREDIMAGE|MASKEDIMAGE)
Global Map:TileMap = TileMap.Create (BackgroundImage,MapSizeRows,MapSizeColumns,CellSize,MapTop,MapLeft)
...etc.
While Not (EndGame=-999)
Cls
Map.Draw()
'****** Key pressed here, and generates a code to be sent to the keypress function ****
CheckKeys(Map,button)
Flip
FlushMem
Wend
I create my TileMap Type and such in the Map.bmx file, and it works fine...
Type TileMap
Field HorMapSize = 0, VerMapSize = 0
Field HorCellSize = 0, VerCellSize = 0
Field Background:TImage = Null
Field TopCorner:Float = 0.0, LeftCorner:Float = 0.0
Field TileType:Int [MaxHorMapCells,MaxVerMapCells]
Field TopBorder:Float = 0.0, LeftBorder:Float =0.0
Field PortHorSize = 0, PortVerSize = 0
'***************** New ********************
Method New()
Local x,y
For x = 0 To MaxHorMapCells-1
For y = 0 To MaxVerMapCells-1
TileType[x,y]=EmptyCell
'Print TileType[x,y] 'Error Checking
Next
Next
End Method
'******************* Create ************************
Function Create:TileMap (TempImage:TImage,TempRows:Int,TempColumns:Int, TempCellSize:Int, TempTop:Float, TempLeft:Float)
Local TempMap:TileMap = New TileMap
TempMap.HorMapSize = TempRows
TempMap.VerMapSize = TempColumns
TempMap.HorCellSize = TempCellSize
TempMap.VerCellSize = TempCellSize
TempMap.Background = TempImage
TempMap.TopCorner = TempTop
TempMap.LeftCorner = TempLeft
'Error Checking
'Print TempMap.HorMapSize
'Print TempMap.VerMapSize
'Print TempMap.HorCellSize
'Print TempMap.VerCellSize
'Print TempMap.TopCorner
'Print TempMap.LeftCorner
Return TempMap
End Function
'******************** LoadCells *************************
Method LoadCells (mapfile:String)
Local temp:String = Null
Local x,y
Local in = ReadStream (mapfile)
For y = 0 To VerMapSize - 1
For x = 0 To HorMapSize - 1
If Not Eof(in)
temp = ReadLine (in)
TileType[x,y] = Int(temp)
EndIf
'Print TileType[x,y] 'Error Checking
Next
Next
CloseStream in
End Method
'************************ SetWindowSize *********************
Method SetPortSize (TempLeft:Float,TempTop:Float, TempHor, TempVer)
TopBorder = TempTop
LeftBorder = TempLeft
PortHorSize = TempHor
PortVerSize = TempVer
End Method
'************************ Draw ***************************
Method Draw()
SetBlend(MASKBLEND|ALPHABLEND)
SetRotation(0)
SetAlpha(1)
SetScale(1,1)
SetColor(255,255,255)
Local x,y
Local ViewX:Int = 0,ViewY:Int = 0,ViewSideLen:Int = 0,ViewDownLen:Int = 0 'Stores the Viewport size to be restored later
GetViewport(ViewX,ViewY,ViewSideLen,ViewDownLen)
SetViewport(TopBorder,LeftBorder,PortHorSize,PortVerSize) 'Sets Viewport to visible window
For y = 0 To VerMapSize - 1
Local YPos:Float = (y+1)*VerCellSize+TopCorner
If YPos >= TopBorder And (YPos - VerCellSize) <= (TopBorder+PortVerSize)
For x = 0 To HorMapSize - 1
Local XPos:Float = (x+1)*HorCellSize+LeftCorner
If XPos >= TopBorder And (XPos - HorCellSize) <= (LeftBorder+PortHorSize)
If TileType[x,y]<>EmptyCell
DrawImage (Background,(XPos - HorCellSize),(YPos - VerCellSize),TileType [x,y])
EndIf
EndIf
Next
EndIf
Next
SetViewport(ViewX,ViewY,ViewSideLen,ViewDownLen) 'Resets Viewport
End Method
'************************* MoveMap ***************************
Method MoveMap(direction_x:Int,direction_y:Int)
LeftCorner:+direction_x
TopCorner:+direction_y
If LeftCorner < -(HorMapSize*HorCellSize)+PortHorSize+LeftBorder Then LeftCorner = -(HorMapSize*HorCellSize)+PortHorSize+LeftBorder
If TopCorner < -(VerMapSize*VerCellSize)+PortVerSize+TopBorder Then TopCorner = -(VerMapSize*VerCellSize)+PortVerSize+TopBorder
If LeftCorner > (LeftBorder) Then LeftCorner = (LeftBorder)
If TopCorner > (TopBorder) Then TopCorner = (TopBorder)
End Method
End Type
My problem comes with Key entry. If I capture a keypress in the main loop, then send it off to the function in my KeyResponse.bmx (below) I get an error that says the type TileMap is not recognized. I thought that since "Map" was a global variable it could be accessed from anywhere in the program, but that seems to only be true from anywhere in the individual .bmx file. Here's the two ways I have tried to code a key response in that file:
If button=1 Then Map.MoveMap (0,-MoveStep) 'This direct call to map doesn't work!
....
Function CheckKeys (TempMap:TileMap, pressed:Int)
If pressed = 1 Then TempMap.Move Map (0,-MoveStep) 'This doesn't work either! It says it doesn't understand "TileType"...
Any help understanding the scope of types and variables across Imported .bmx files would be appreciated...
|