Tilemaps: Editor and TM based games | How to!?

Advanced game related topics
User avatar
bembulak
Enthusiast
Enthusiast
Posts: 576
Joined: Mon Mar 06, 2006 3:53 pm
Location: Austria

Tilemaps: Editor and TM based games | How to!?

Post by bembulak »

Hi folks!

For a couple of month I am wondering, how to realize a tilebased-game (just tile-based, e.g. top-view, not ISO).
Due to the fact, that my knowledge in coding and games is minimalistic, I wonder if you can help me a little with understanding some very basics.

I was already searching the web and the forums, but to be honest: either the found information was not, what I was looking for, or far exceeded my understanding.

What do I want to do
The creation of a tile-based game in top view. Should be some kind of shooter. In first instance it will only have one level. No menu, no options or anything...
The level should not be a data-section at the end of the source-code, but should be loaded from a file. The routine/algoritm which loads the level-file should be capable of "loading the level file and the SFX and GFX according to the level and displaying it". This is the point, which causes headache.

I already realized, that I will have to build some kind of "TileEditor". And this seems to be the biggest problem. I don't have a glue, where to start!
(I know, that there are some tileeditors on the web, but none of them brought illumination to my mind...)
  • Use Windows and gadgets or a screen?
  • Which gadgets to use?
  • How to save the map?
  • Which information must the map contain?
  • Is it possible to build that at all? (Remember my lack of knowledge)
I hope, someone can give me some kind of support. I mean, I do have an very raw overview like above, but I do not know where to start. It would be a pain to start working on an editor and than realize, that it was all for nothing. I want to work with some prospection. (Although I know, that it is a normal part of life and coding, that we do have to try, see our mistakes and try to make it better next time.)

Thanks to all.
cheers,

bembulak
dciple
New User
New User
Posts: 1
Joined: Thu Oct 05, 2006 2:24 pm

Post by dciple »

order Krylar's book "programming 2D scrolling games"
http://www.krylarskreations.com/pb_book.shtml

or here http://www.shareit.com/product.html?pro ... nguageid=1

It is written in purebasic for purebasic users :)
User avatar
bembulak
Enthusiast
Enthusiast
Posts: 576
Joined: Mon Mar 06, 2006 3:53 pm
Location: Austria

Post by bembulak »

Thanks for the tip, but is there nothing more about that?
It's not like I want pre-fabbed code snippets, but discuss about that. See it more as an theoretical/philosophical approach.
(Although I think about ordering the book, thanks again)
cheers,

bembulak
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

I believe I released the source for my "Teh Warg" game editor. Its a bit ugly but it works.

Code: Select all

; WargEngine 2.0 Map Editor
; Copyright 2006 DracSoft
; License: GPL (www.gnu.org) - See gpl.txt
;     Do NOT change this header! This program and any derivative works MUST be released publicly
;     as open source under the GPL! For more information please see the license and explanations
;     at www.gnu.org. See license.txt for more information.
;
;     Only the source code is open source, your art and other assets are not covered by the GPL.

IncludeFile "./LinuxINI.pb"

#Version="2.0"

;STRUCTURES
Structure ANIM_STRUCT
  name.s
  sprite.l
  frames.l
  framewidth.l
  fps.f
  currentFrame.l
  x.l
  y.l
  lastAnimMs.l
  drawstage.l
  temporary.b
EndStructure
Structure TILE_STRUCT
  name.s
  sprite.l
EndStructure
Structure SETTINGS_STRUCT
  MidWindowX.l
  MidWindowY.l
  MidPlayerX.l
  MidPlayerY.l
  TitleScreen.l
  GameTitle.s
  ScreenHeight.l
  ScreenWidth.l
  MapWidth.l
  DebugMode.l
  MapHeight.l
  WarpSound.l
  TileWidth.l
  TileHeight.l
  StartMap.s
  StartX.l
  StartY.l
  StartHealth.l
  StartDamage.l
  LengthOfDay.l   ;how long is a day in seconds
  PlayerSprite.s
  NightTile.l     ;sprite for nighttime overlay
  FontSize.l
  StatusFontSize.l
  StatusTextOffset.l
  StatusBarHeight.l
  StatusHealthChar.s
  PlayerAttackSound.l
  PlayerHitSound.l
  PlayerMoveSound.l
  PlayerDeathSound.l
  PlayerHeight.l
  PlayerWidth.l
  PlayerClipX.l
  PlayerClipY.l
  MoveFrameCount.l
  AttackFrameCount.l
  PlayerAttackDelay.l ;Ms until attack again
  MonsterAttackDelay.l ;Ms until attack again
  DeathSprite.l
  WindowWidth.l
  WindowHeight.l
  AnimationSpeed.l    ;Frames per second
  PlayerStartSpeed.l  ;Pixels per second
  AIChangeFrequency.l ;in milliseconds
  AIVisionLength.l    ;# of pixels
  AIVisionWidth.l     ;# of pixels
  MapTextSpeed.l      ;characters per second
EndStructure

;CONSTANTS
Enumeration 0
  #MODE_TILE
  #MODE_FGTILE
  #MODE_FGTILE2
  #MODE_BGTILE
  #MODE_WALL
  #MODE_MONSTERWALL
  #MODE_WARP
  #MODE_SCRIPT
  #MODE_ANIMATION
EndEnumeration

;GLOBALS
Global gameFolder.s
Global currentX.l
Global currentY.l
Global topTile.l
Global currentTimeMs.l
Global currentTile.l
Global gameSettings.SETTINGS_STRUCT
Global currentMode.l
Global clickX.l
Global clickY.l
Global loadedMap.s
NewList TileList.TILE_STRUCT()
NewList ObjectList.s()
NewList WarpList.s()
NewList MonsterList.s()
NewList AnimDataList.ANIM_STRUCT()
NewList AnimList.ANIM_STRUCT()

;PROCEDURES
Procedure LoadAnimData()
  ClearList(AnimDataList())
  
  fid=ReadFile(#PB_Any,gameFolder+"/animations.ini")
  If fid<>0
    While Eof(fid)=0
      mon.s=ReadString()
      If mon<>""
        AddElement(AnimDataList())
        AnimDataList()\name=Trim(StringField(mon,1,","))
        AnimDataList()\sprite=LoadSprite(#PB_Any,gameFolder+"/animations/"+Trim(StringField(mon,2,",")))
        TransparentSpriteColor(AnimDataList()\sprite,0,255,255)
        AnimDataList()\frames=Val(StringField(mon,3,","))
        AnimDataList()\framewidth=Val(StringField(mon,4,","))
        AnimDataList()\fps=ValF(StringField(mon,5,","))
        AnimDataList()\drawstage=Val(StringField(mon,6,","))
      EndIf 
    Wend 
    CloseFile(fid)
  EndIf 
EndProcedure

Procedure LoadTiles()
  ExamineDirectory(0,gameFolder+"/tiles","*.png")
  ftype=NextDirectoryEntry()
  
  While ftype<>0
    If ftype=1
      AddElement(TileList())
      TileList()\sprite=LoadSprite(#PB_Any,gameFolder+"/tiles/"+DirectoryEntryName())
      TransparentSpriteColor(TileList()\sprite,0,255,255)
      TileList()\name=Trim(ReplaceString(DirectoryEntryName(),".png",""))
    EndIf       
    
    ftype=NextDirectoryEntry()
  Wend 
EndProcedure

Procedure.l GetSpriteFromName(n.s)
  ForEach TileList()
    If TileList()\name=n
      ProcedureReturn TileList()\Sprite 
    EndIf 
  Next 
  ProcedureReturn 0
EndProcedure

Declare LoadMap(name.s)
Declare DrawScreen()

Procedure Initialize()
  InitSprite()
  TransparentSpriteColor(#PB_Default,0,255,255)
  InitMouse()
  InitKeyboard()
  UsePNGImageDecoder()
  
  x=Val(ProgramParameter())
  y=Val(ProgramParameter())
  If x=0
    x=800
  EndIf
  If y=0
    y=600
  EndIf
  
  gameFolder=ProgramParameter()
  If gameFolder=""
    gameFolder=PathRequester("Choose the folder of the WargEngine mod...","./warg3")
  EndIf 
  If gameFolder=""
    End 
  EndIf 
  
  ;load settings
  gameSettings\MapWidth=Val(Pref_ReadString("Settings","MapWidth",gameFolder+"/settings.ini"))
  gameSettings\MapHeight=Val(Pref_ReadString("Settings","MapHeight",gameFolder+"/settings.ini"))
  gameSettings\ScreenHeight=Val(Pref_ReadString("Settings","ScreenHeight",gameFolder+"/settings.ini"))
  gameSettings\ScreenWidth=Val(Pref_ReadString("Settings","ScreenWidth",gameFolder+"/settings.ini"))
  gameSettings\TileWidth=Val(Pref_ReadString("Settings","TileWidth",gameFolder+"/settings.ini"))
  gameSettings\TileHeight=Val(Pref_ReadString("Settings","TileHeight",gameFolder+"/settings.ini"))
  
  ;load list of monster names
  fid=ReadFile(#PB_Any,gameFolder+"\monsters.ini")
  If fid<>0
    While Eof(fid)=0
      AddElement(MonsterList())
      MonsterList()=StringField(ReadString(),1,",")
    Wend 
  EndIf 
  
  ;open window and init arrays  
  OpenWindow(0,0,0,x,y,#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered|#PB_Window_SizeGadget|#PB_Window_TitleBar|#PB_Window_SystemMenu,"WargEngine Editor")
  OpenWindowedScreen(WindowID(),0,0,x,y,1,0,0)
  
  LoadTiles()
  LoadAnimData()
  SelectElement(TileList(),0)
  currentTile=TileList()\sprite
  
  Dim TMap.l(gameSettings\MapWidth-1,gameSettings\MapHeight-1)
  Dim FMap.l(gameSettings\MapWidth-1,gameSettings\MapHeight-1)
  Dim FMap2.l(gameSettings\MapWidth-1,gameSettings\MapHeight-1)
  Dim AMap.s(gameSettings\MapWidth-1,gameSettings\MapHeight-1)
  Dim BMap.l(gameSettings\MapWidth-1,gameSettings\MapHeight-1)
  
  For i=0 To gameSettings\MapWidth-1
    For j=0 To gameSettings\MapHeight-1
      AMap(i,j)="."
      FMap(i,j)=0
      FMap2(i,j)=0
      BMap(i,j)=0
      TMap(i,j)=TileList()\sprite
    Next
  Next 
  
  n.s=InputRequester("","Enter the map name to load or blank for new map:","")
  If n<>""
    LoadMap(n)
  EndIf 
  currentTimeMs=ElapsedMilliseconds()
EndProcedure

Procedure LoadMap(name.s)
  loadedMap=name
  
  fid=ReadFile(#PB_Any,gameFolder+"/maps/"+name+".gtl")
  If fid<>0
    For i=0 To gameSettings\mapHeight-1
      str.s=ReadString()
      For j=0 To gameSettings\mapWidth-1
        n.s=StringField(str,j+1," ")
        TMap(j,i)=GetSpriteFromName(n.s)
      Next 
    Next 
    CloseFile(fid)
  EndIf 
  
  fid=ReadFile(#PB_Any,gameFolder+"/maps/"+name+".btl")
  If fid<>0
    For i=0 To gameSettings\mapHeight-1
      str.s=ReadString()
      For j=0 To gameSettings\mapWidth-1
        n.s=StringField(str,j+1," ")
        BMap(j,i)=GetSpriteFromName(n.s)
      Next 
    Next 
    CloseFile(fid)
  EndIf 
  
  fid=ReadFile(#PB_Any,gameFolder+"/maps/"+name+".ftl")
  If fid<>0
    For i=0 To gameSettings\mapHeight-1
      str.s=ReadString()
      For j=0 To gameSettings\mapWidth-1
        n.s=StringField(str,j+1," ")
        FMap(j,i)=GetSpriteFromName(n.s)
      Next 
    Next 
    
    For i=0 To gameSettings\mapHeight-1
      str.s=ReadString()
      For j=0 To gameSettings\mapWidth-1
        n.s=StringField(str,j+1," ")
        FMap2(j,i)=GetSpriteFromName(n.s)
      Next 
    Next 
    CloseFile(fid)
  EndIf 
  
  fid=ReadFile(#PB_Any,gameFolder+"/maps/"+name+".atl")
  If fid<>0
    For i=0 To gameSettings\mapHeight-1
      str.s=ReadString()
      For j=0 To gameSettings\mapWidth-1
        n.s=StringField(str,j+1," ")
        AMap(j,i)=n.s
      Next 
    Next
    CloseFile(fid)
  EndIf
  
  fid=ReadFile(#PB_Any,gameFolder+"/maps/"+name+".obl")
  If fid<>0
    While Eof(fid)=0
      str.s=ReadString()
      If str<>""
        AddElement(ObjectList())
        ObjectList()=str
      EndIf 
    Wend 
    CloseFile(fid)
  EndIf
  
  fid=ReadFile(#PB_Any,gameFolder+"/maps/"+name+".ani")
  If fid<>0
    While Eof(fid)=0
      str.s=ReadString()
      If str<>""
        AddElement(AnimList())
        AnimList()\name=StringField(str,1,",")
        AnimList()\x=Val(StringField(str,2,","))
        AnimList()\y=Val(StringField(str,3,","))
        AnimList()\temporary=Val(StringField(str,4,","))
        
        ForEach AnimDataList()
          If UCase(AnimDataList()\name)=UCase(AnimList()\name)
            AnimList()\sprite=AnimDataList()\sprite
            AnimList()\frames=AnimDataList()\frames
            AnimList()\framewidth=AnimDataList()\framewidth
            AnimList()\fps=AnimDataList()\fps
            AnimList()\drawstage=AnimDataList()\drawstage
          EndIf
        Next 
      EndIf 
    Wend 
    CloseFile(fid)
  EndIf
  
  fid=ReadFile(#PB_Any,gameFolder+"/maps/"+name+".wpl")
  If fid<>0
    While Eof(fid)=0
      str.s=ReadString()
      If str<>""
        AddElement(WarpList())
        WarpList()=str
      EndIf 
    Wend 
    CloseFile(fid)
  EndIf
EndProcedure

Procedure.l GetTileSprite(i)
  SelectElement(TileList(),i)
  ProcedureReturn TileList()\sprite
EndProcedure

Procedure.s GetTileName(spriteid)
  ForEach TileList()
    If TileList()\sprite=spriteid
      ProcedureReturn TileList()\name 
    EndIf 
  Next 
  ProcedureReturn "."
EndProcedure

Procedure SaveMap(name.s)
  fid=CreateFile(#PB_Any,gameFolder+"/maps/"+name+".gtl")
  If fid<>0
    For j=0 To gameSettings\mapHeight-1
      For i=0 To gameSettings\mapwidth-1
        WriteString(GetTileName(TMap(i,j))+" ")
      Next 
      WriteStringN("")
    Next      
    CloseFile(fid)
  EndIf
  
  fid=CreateFile(#PB_Any,gameFolder+"/maps/"+name+".ftl")
  If fid<>0
    For j=0 To gameSettings\mapHeight-1
      For i=0 To gameSettings\mapwidth-1
        WriteString(GetTileName(FMap(i,j))+" ")
      Next 
      WriteStringN("")
    Next     
    
    For j=0 To gameSettings\mapHeight-1
      For i=0 To gameSettings\mapwidth-1
        WriteString(GetTileName(FMap2(i,j))+" ")
      Next 
      WriteStringN("")
    Next      
    CloseFile(fid)
  EndIf
  
  fid=CreateFile(#PB_Any,gameFolder+"/maps/"+name+".btl")
  If fid<>0
    For j=0 To gameSettings\mapHeight-1
      For i=0 To gameSettings\mapwidth-1
        WriteString(GetTileName(BMap(i,j))+" ")
      Next 
      WriteStringN("")
    Next      
    CloseFile(fid)
  EndIf
  
  fid=CreateFile(#PB_Any,gameFolder+"/maps/"+name+".atl")
  If fid<>0
    For j=0 To gameSettings\mapHeight-1
      For i=0 To gameSettings\mapwidth-1
        WriteString(AMap(i,j)+" ")
      Next 
      WriteStringN("")
    Next      
    CloseFile(fid)
  EndIf 
  
  For j=0 To gameSettings\mapHeight-1
    For i=0 To gameSettings\mapwidth-1
      If AMap(i,j)="S"
        fid=ReadFile(#PB_Any,gameFolder+"/maps/"+name+"_"+Str(i)+"_"+Str(j)+".ds")
        If fid=0
          fid=CreateFile(#PB_Any,gameFolder+"/maps/"+name+"_"+Str(i)+"_"+Str(j)+".ds")
          If fid<>0
            WriteString("")
            CloseFile(fid)
          EndIf 
        EndIf
      EndIf 
    Next 
  Next      
  
  fid=CreateFile(#PB_Any,gameFolder+"/maps/"+name+".obl")
  If fid<>0
    ForEach ObjectList()
      WriteStringN(ObjectList())
    Next
    CloseFile(fid)
  EndIf 
  
  fid=CreateFile(#PB_Any,gameFolder+"/maps/"+name+".ani")
  If fid<>0
    ForEach AnimList()
      WriteStringN(AnimList()\name+","+Str(AnimList()\x)+","+Str(AnimList()\y)+","+Str(AnimList()\temporary))
    Next
    CloseFile(fid)
  EndIf 
  
  fid=CreateFile(#PB_Any,gameFolder+"/maps/"+name+".wpl")
  If fid<>0
    ForEach WarpList()
      WriteStringN(WarpList())
    Next
    CloseFile(fid)
  EndIf 
EndProcedure

Procedure WaitForNextMouseClick()
  Repeat
    event=WindowEvent()
  
    ExamineKeyboard()
    If KeyboardPushed(#PB_Key_Up)
      currenty-1
      If currenty<0
        currenty=0
      EndIf 
    ElseIf KeyboardPushed(#PB_Key_Down)
      currenty+1
      If currenty>gameSettings\mapheight-1
        currenty=gameSettings\mapheight-1
      EndIf 
    ElseIf KeyboardPushed(#PB_Key_Left)
      currentx-1
      If currentx<0
        currentx=0
      EndIf 
    ElseIf KeyboardPushed(#PB_Key_Right)  
      currentx+1
      If currentx>gameSettings\mapwidth-1
        currentx=gameSettings\mapwidth-1
      EndIf 
    EndIf 
    
    DrawScreen()
    Delay(33) ; 30fps
    
    ExamineMouse()
  Until MouseButton(1)<>0
  
  Repeat
    ExamineMouse()
  Until MouseButton(1)=0
  
  clickX=MouseX()
  clickY=MouseY()
EndProcedure

Procedure CreateWarp(n.l)
  dest.s=InputRequester("New warp","Enter the destination map:","")
  MessageRequester("Source","Click the x/y position for the warp to be on.")
  WaitForNextMouseClick()
  sx=clickX/gameSettings\tilewidth+currentX
  sy=clickY/gameSettings\tileheight+currentY
  MessageRequester("Destination","Click the x/y position for the warp to go to.")
  WaitForNextMouseClick()
  dx=clickX/gameSettings\tilewidth+currentX
  dy=clickY/gameSettings\tileheight+currentY
  
  AddElement(WarpList())
  WarpList()=Str(sx)+","+Str(sy)+","+dest+","+Str(dx)+","+Str(dy)
  AMap(sx,sy)="W"
  
  MessageRequester("","Warp created.")
EndProcedure

Procedure CreateObject(n.l)
  name.s=InputRequester("New object","Enter the name of the object:","")
  graphic.s=InputRequester("New object","Enter the graphic file of the object:","")
  dmg.s=InputRequester("New object","Enter the damage of the object:","")
  health.s=InputRequester("New object","Enter the health the object adds to the players max:","")
  speed.s=InputRequester("New object","Enter the speed the object adds:","")
  heal.s=InputRequester("New object","Enter the heal amount of the object:","")  
  
  MessageRequester("Position","Click the x/y position for the object to be on.")
  WaitForNextMouseClick()
  sx=clickX/gameSettings\tilewidth+currentX
  sy=clickY/gameSettings\tileheight+currentY
 
  AddElement(ObjectList())
  ObjectList()=name+","+graphic+","+Str(sx)+","+Str(sy)+","+dmg+","+health+","+speed+","+heal
  
  MessageRequester("","Object created.")
EndProcedure

Procedure CreateAnimation()
  ReleaseMouse(1)
  
  OpenWindow(1,0,0,300,30,#PB_Window_SystemMenu|#PB_Window_WindowCentered,"Choose Animation Type")
  UseWindow(1)
  ActivateWindow()
  CreateGadgetList(WindowID())
  ComboBoxGadget(999,0,0,300,200)
  ForEach AnimDataList()
  AddGadgetItem(999,-1,AnimDataList()\name)  
  Next 
  CloseGadgetList()
  
  Repeat
    event=WaitWindowEvent()
  Until event=#PB_EventCloseWindow
  anim.s=GetGadgetText(999)
  CloseWindow(1)
  UseWindow(0)
  ActivateWindow()
  
  If MessageRequester("","Is this a temporary animation?",#PB_MessageRequester_YesNo)=#PB_MessageRequester_Yes
    temp=1
  Else
    temp=0
  EndIf 
  
  ReleaseMouse(0)
  If anim<>""
    ForEach AnimDataList()
      If UCase(AnimDataList()\name)=UCase(anim)
        MessageRequester("","Choose the topleft x/y position for the animation.")  
        WaitForNextMouseClick()
        
        sx=clickX+gameSettings\tilewidth*currentX
        sy=clickY+gameSettings\tileheight*currentY
        AddElement(AnimList())
        AnimList()\name=anim 
        AnimList()\x=sx
        AnimList()\y=sy
        AnimList()\sprite=AnimDataList()\sprite
        AnimList()\frames=AnimDataList()\frames
        AnimList()\framewidth=AnimDataList()\framewidth
        AnimList()\fps=AnimDataList()\fps
        AnimList()\drawstage=AnimDataList()\drawstage
        AnimList()\temporary=temp
        Break 
      EndIf 
    Next 
  EndIf   
EndProcedure

Procedure CreateMonster()
  ReleaseMouse(1)
  
  OpenWindow(1,0,0,300,30,#PB_Window_SystemMenu|#PB_Window_WindowCentered,"Choose Monster Type")
  UseWindow(1)
  ActivateWindow()
  CreateGadgetList(WindowID())
  ComboBoxGadget(999,0,0,300,200)
  ForEach MonsterList()
  AddGadgetItem(999,-1,MonsterList())  
  Next 
  CloseGadgetList()
  
  Repeat
    event=WaitWindowEvent()
  Until event=#PB_EventCloseWindow
  mtype.l=GetGadgetState(999)+1
  CloseWindow(1)
  UseWindow(0)
  ActivateWindow()
  
  MessageRequester("","Choose the x/y position for the monster.")  
  ReleaseMouse(0)
  WaitForNextMouseClick()
  sx=clickX/gameSettings\tilewidth+currentX
  sy=clickY/gameSettings\tileheight+currentY
  AMap(sx,sy)=Str(mtype)
EndProcedure

Procedure ShowModeWindow()
  ReleaseMouse(1)
  OpenWindow(1,0,0,300,30,#PB_Window_SystemMenu|#PB_Window_WindowCentered,"Choose Editor Mode")
  UseWindow(1)
  ActivateWindow()
  CreateGadgetList(WindowID())
  ComboBoxGadget(999,0,0,300,200)
  
  AddGadgetItem(999,-1,"Ground Tiles")  
  AddGadgetItem(999,-1,"Foreground Tiles")  
  AddGadgetItem(999,-1,"Foreground Tiles - Layer 2")  
  AddGadgetItem(999,-1,"Background Tiles")    
  AddGadgetItem(999,-1,"Wall Tiles")  
  AddGadgetItem(999,-1,"No Monster Tiles")  
  AddGadgetItem(999,-1,"Warp Tiles")  
  AddGadgetItem(999,-1,"Script Tiles")
  AddGadgetItem(999,-1,"Animations")
  SetGadgetState(999,currentMode)
  
  CloseGadgetList()
  
  Repeat
    event=WaitWindowEvent()
  Until event=#PB_EventCloseWindow
  currentMode=GetGadgetState(999)
  CloseWindow(1)
  UseWindow(0)
  ActivateWindow()
  ReleaseMouse(0)
EndProcedure

Procedure HandleInput()
  ExamineKeyboard()
  If KeyboardReleased(#PB_Key_Escape)
    End 
  ElseIf KeyboardReleased(#PB_Key_1)
    currentMode=#MODE_TILE
  ElseIf KeyboardReleased(#PB_Key_2)
    currentMode=#MODE_BGTILE
  ElseIf KeyboardReleased(#PB_Key_3)
    currentMode=#MODE_FGTILE
  ElseIf KeyboardReleased(#PB_Key_4)
    currentMode=#MODE_FGTILE2
  ElseIf KeyboardReleased(#PB_Key_5)
    currentMode=#MODE_WALL
  ElseIf KeyboardReleased(#PB_Key_6)
    currentMode=#MODE_MONSTERWALL
  ElseIf KeyboardReleased(#PB_Key_7)
    currentMode=#MODE_WARP
  ElseIf KeyboardReleased(#PB_Key_8)
    currentMode=#MODE_SCRIPT
  ElseIf KeyboardReleased(#PB_Key_9)
    currentMode=#MODE_ANIMATION
  ElseIf KeyboardReleased(#PB_Key_Space)
    ShowModeWindow()
  ElseIf KeyboardReleased(#PB_Key_F)
    For i=0 To gameSettings\mapwidth-1
      For j=0 To gameSettings\mapheight-1
        TMap(i,j)=currentTile
      Next
    Next   
  ElseIf KeyboardPushed(#PB_Key_PageUp)
    Toptile - 1
    If topTile<0
      Toptile=0
    EndIf 
  ElseIf KeyboardPushed(#PB_Key_PageDown)
    Toptile + 1
    If topTile > CountList(TileList())-1
      topTile=CountList(TileList())-1
    EndIf 
  ElseIf KeyboardPushed(#PB_Key_Up)
    currenty-1
    If currenty<0
      currenty=0
    EndIf 
  ElseIf KeyboardPushed(#PB_Key_Down)
    currenty+1
    If currenty>gameSettings\mapheight-1
      currenty=gameSettings\mapheight-1
    EndIf 
  ElseIf KeyboardPushed(#PB_Key_Left)
    currentx-1
    If currentx<0
      currentx=0
    EndIf 
  ElseIf KeyboardPushed(#PB_Key_Right)  
    currentx+1
    If currentx>gameSettings\mapwidth-1
      currentx=gameSettings\mapwidth-1
    EndIf 
  ElseIf KeyboardReleased(#PB_Key_S)
    SaveMap(InputRequester("","Enter the name of the map to save: ",loadedMap))
  ElseIf KeyboardReleased(#PB_Key_W)
    CreateWarp(-1)
  ElseIf KeyboardReleased(#PB_Key_O)
    CreateObject(-1)
  ElseIf KeyboardReleased(#PB_Key_M)
    CreateMonster()
  ElseIf KeyboardReleased(#PB_Key_A)
    CreateAnimation()
  EndIf 
  
  ExamineMouse()
  If MouseButton(1) And MouseX()>WindowWidth()-(gameSettings\tilewidth*3)-5
    row=MouseX()-(WindowWidth()-(gameSettings\tilewidth*3)-5)
    row=row/gameSettings\tilewidth
    
    ttile=topTile+((WindowHeight()/gameSettings\tileheight)*row)
    
    currentTile=GetTileSprite(ttile+MouseY()/gameSettings\tileheight)
  Else 
    Select currentMode
      Case #MODE_TILE
        If MouseButton(1)
          x=MouseX()/gameSettings\tilewidth+currentX
          y=MouseY()/gameSettings\tileheight+currentY
          TMap(x,y)=currentTile
        EndIf 
        
      Case #MODE_FGTILE
        If MouseButton(1)
          x=MouseX()/gameSettings\tilewidth+currentX
          y=MouseY()/gameSettings\tileheight+currentY
          FMap(x,y)=currentTile
        EndIf
        
      Case #MODE_FGTILE2
        If MouseButton(1)
          x=MouseX()/gameSettings\tilewidth+currentX
          y=MouseY()/gameSettings\tileheight+currentY
          FMap2(x,y)=currentTile
        EndIf 
      
      Case #MODE_BGTILE
        If MouseButton(1)
          x=MouseX()/gameSettings\tilewidth+currentX
          y=MouseY()/gameSettings\tileheight+currentY
          BMap(x,y)=currentTile
        EndIf 
      
      Case #MODE_WALL
        If MouseButton(1)
          x=MouseX()/gameSettings\tilewidth+currentX
          y=MouseY()/gameSettings\tileheight+currentY
          aMap(x,y)="X"
        EndIf 
      
      Case #MODE_WARP
        If MouseButton(1)
          x=MouseX()/gameSettings\tilewidth+currentX
          y=MouseY()/gameSettings\tileheight+currentY
          aMap(x,y)="W"
        EndIf 
      
      Case #MODE_MONSTERWALL
        If MouseButton(1)
          x=MouseX()/gameSettings\tilewidth+currentX
          y=MouseY()/gameSettings\tileheight+currentY
          AMap(x,y)="M"
        EndIf 
      
      Case #MODE_SCRIPT
        If MouseButton(1)
          x=MouseX()/gameSettings\tilewidth+currentX
          y=MouseY()/gameSettings\tileheight+currentY
          AMap(x,y)="S"
        EndIf 
    EndSelect
    
    If MouseButton(2)
      x=MouseX()/gameSettings\tilewidth+currentX
      y=MouseY()/gameSettings\tileheight+currentY
      If currentMode=#MODE_FGTILE
        FMap(x,y)=0
      ElseIf currentMode=#MODE_FGTILE2
        FMap2(x,y)=0
      ElseIf currentMode=#MODE_BGTILE
        BMap(x,y)=0
      ElseIf currentMode=#MODE_ANIMATION
        ForEach AnimList()
          If AnimList()\x/gameSettings\tilewidth = x And AnimList()\y/gameSettings\tileheight = y
            DeleteElement(AnimList())
            Break 
          EndIf           
        Next 
      Else
        AMap(x,y)="."
      EndIf 
    EndIf 
  EndIf 
EndProcedure

Procedure DrawAnims(stage.l)
  ForEach AnimList()
    If AnimList()\drawstage=stage
      ClipSprite(AnimList()\sprite,AnimList()\currentFrame*AnimList()\framewidth,0,AnimList()\framewidth,SpriteHeight(AnimList()\sprite))
      DisplayTransparentSprite(AnimList()\sprite,AnimList()\x-(currentX*gamesettings\tilewidth),AnimList()\y-(currentY*gamesettings\tileheight))
    EndIf 
  Next 
EndProcedure

Procedure DrawScreen()
  FlipBuffers(0)
  ClearScreen(255,0,255)
  
  For i=0 To gameSettings\mapwidth-1
    For j=0 To gameSettings\mapheight-1
      DisplaySprite(TMap(i,j),(i-currentX)*gamesettings\tilewidth,(j-currentY)*gamesettings\tileheight)
    Next
  Next
  
  DrawAnims(1)
  
  For i=0 To gameSettings\mapwidth-1
    For j=0 To gameSettings\mapheight-1
      DisplayTransparentSprite(BMap(i,j),(i-currentX)*gamesettings\tilewidth,(j-currentY)*gamesettings\tileheight)
    Next
  Next
  
  DrawAnims(2)
  
  For i=0 To gameSettings\mapwidth-1
    For j=0 To gameSettings\mapheight-1
      DisplayTransparentSprite(FMap(i,j),(i-currentX)*gamesettings\tilewidth,(j-currentY)*gamesettings\tileheight)
    Next
  Next
  
  DrawAnims(3)
  
  For i=0 To gameSettings\mapwidth-1
    For j=0 To gameSettings\mapheight-1
      DisplayTransparentSprite(FMap2(i,j),(i-currentX)*gamesettings\tilewidth,(j-currentY)*gamesettings\tileheight)
    Next
  Next
  
  DrawAnims(4)
  
  StartDrawing(ScreenOutput())
    If KeyboardPushed(#PB_Key_Tab)=0
      DrawingMode(0)
      FrontColor(0,0,0)
      BackColor(255,255,255)
      For i=0 To gameSettings\mapwidth-1
        For j=0 To gameSettings\mapheight-1
          If AMap(i,j)<>"."
            Locate((i-currentx)*gamesettings\tilewidth+gameSettings\tilewidth/2,(j-currenty)*gamesettings\tileheight+gameSettings\tileheight/2)
            DrawText(AMap(i,j))
          EndIf 
        Next
      Next 
          
      BackColor(220,0,0)
      FrontColor(255,255,255)
      ForEach ObjectList()
        x=Val(StringField(ObjectList(),3,","))
        y=Val(StringField(ObjectList(),4,","))
        Locate((x-currentX)*gamesettings\tilewidth,(y-currentY)*gamesettings\tileheight)
        DrawText(StringField(ObjectList(),1,","))
      Next
    EndIf   
  
    DrawingMode(0)
    Box(WindowWidth()-(gamesettings\tilewidth*3)-5,0,5,WindowHeight(),RGB(255,0,0))
    Box(WindowWidth()-(gamesettings\tilewidth*3),0,gameSettings\tileWidth*3,WindowHeight(),RGB(0,0,0))
  StopDrawing()
  
  y=0
  For i = topTile To Toptile+(WindowHeight()/gameSettings\tileheight)-1
    If i<CountList(TileList())
      SelectElement(TileList(),i)
      DisplaySprite(TileList()\sprite,WindowWidth()-gamesettings\tilewidth*3,y*gamesettings\TileHeight)
      y+1
    EndIf 
  Next 
  
  y=0
  For i = topTile+(WindowHeight()/gameSettings\tileheight) To Toptile+(WindowHeight()/gameSettings\tileheight)*2-1
    If i<CountList(TileList())
      SelectElement(TileList(),i)
      DisplaySprite(TileList()\sprite,WindowWidth()-gamesettings\tilewidth*2,y*gamesettings\TileHeight)
      y+1
    EndIf 
  Next 
  
  y=0
  For i = topTile+(WindowHeight()/gameSettings\tileheight)*2 To Toptile+(WindowHeight()/gameSettings\tileheight)*3-1
    If i<CountList(TileList())
      SelectElement(TileList(),i)
      DisplaySprite(TileList()\sprite,WindowWidth()-gamesettings\tilewidth,y*gamesettings\TileHeight)
      y+1
    EndIf 
  Next 
  
  StartDrawing(ScreenOutput())
    DrawingMode(1)
    Line(MouseX(),MouseY(),10,10,RGB(255,255,255))
    Line(MouseX()+1,MouseY(),10,10,RGB(255,255,255))
    Line(MouseX(),MouseY()+1,10,10,RGB(255,255,255))
    Line(MouseX()+2,MouseY(),10,10,RGB(0,0,0))
    Line(MouseX(),MouseY()+2,10,10,RGB(0,0,0))
  StopDrawing()  
EndProcedure

Procedure DoAnimations()
  ;custom map animations
  ForEach AnimList()
    If AnimList()\fps>0
      If (currentTimeMs-AnimList()\LastAnimMs) >= (1000/AnimList()\fps)
        AnimList()\currentFrame + 1
        If AnimList()\currentFrame >= AnimList()\frames
          AnimList()\currentFrame = 0
        EndIf
        AnimList()\lastAnimMs=currentTimeMs
      EndIf 
    EndIf 
  Next 
EndProcedure

;MAIN FUNCTION
;{
Initialize()
Repeat
  event=WindowEvent()
  
  currentTimeMs=ElapsedMilliseconds()
  
  HandleInput()
  DoAnimations()
  DrawScreen()
  Delay(33) ; 30fps
Until event=#PB_Event_CloseWindow

End
;}
User avatar
bembulak
Enthusiast
Enthusiast
Posts: 576
Joined: Mon Mar 06, 2006 3:53 pm
Location: Austria

Post by bembulak »

:shock:

Wow (not World of Warcraft....)
Amazing. I hope, I can understand this source! Thanks a lot. This will push my work ahead!

btw: is a 3.94 code, isn't it!?

edit: problematic, since I do not have a LINUX.ini on my system. ;)
cheers,

bembulak
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

linuxini.pb was posted by me in tricks and tips I believe.
Post Reply