[bb] 2D Tile Mapper by Kenshin Yurihara [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:38

Previous topic - Next topic

BlitzBot

Title : 2D Tile Mapper
Author : Kenshin Yurihara
Posted : 1+ years ago

Description : It was my Alpha, 2D tile mapper, that I've been working on the past week. I've dropped the project to try and switch it to 2D-in-3D.

The controls are primitive:( This displays when you hit the H key)
Text 0,72, "W - Scroll UP"
Text 0,84, "S - Scroll Down"
Text 0,96, "A - Scroll Left"
Text 0,108, "D - Scroll Right"
Text 0,120, "UP Key - Move Tile Up"
Text 0,132, "Down Key - Move Tile Down"
Text 0,144, "Left Key - Move Tile Left"
Text 0,156, "Right Key - Move Tile Right"
Text 0,168, "< - Decrease SelTile"
Text 0,180, "> - Increase SelTile"
Text 0,192, "K - Decrease Edit Mode"
Text 0,204, "L - Increase Edit Mode"
Text 0,216, "Entier - Place Tile/Entity/BG Tile/SpawnPoint"
Text 0,228, "Shift - Delete Tile/Entity/BG Tile/SpawnPoint"

I've included something to try and make entering with input work better, but it failed. It is the "IsTyping" thing in the code.

Also, SpawnPoint SPN (Spawn Point Number) is stored using what layer you're currently on.

Layering was never Implemented, due to the dropping of the project.


Code :
Code (blitzbasic) Select
Type Tile
Field X#
Field Y#
Field Frame#
Field Layer#
End Type

Type BGTile
Field X#
Field Y#
Field Frame#
End Type

Type SpawnPoint  
Field X#
Field Y#
Field SPN ; Spawn Point Number :D
End Type

Type Entity
Field X#
Field Y#
Field Name$
Field Frame
Field Layer
End Type

Graphics 800,608,32,2
SetBuffer BackBuffer()



TileSheet = LoadAnimImage("TileSheet.bmp",16,16,0,2)
CamImg = LoadImage("CamImg.bmp")
MaskImage(CamImg,255,0,220)
MaskImage(TileSheet,255,0,220)


CurLayer = 1

CX# = 0
CY# = 0

CurX = 0
CurY = 0
IsTyping = False

SelTile = 0
EditMode = 0
MovX = 0
MovY = 0
MaxTiles = 0
MaxBGTiles = 0
MaxSpawnPoints = 0
MaxEnts = 0

FileName = "MAPITH!.Map"
While Not KeyHit(1) ; Program loop

;SAVING AND OMFG LOADING!;
If KeyHit(26) ; Saving
;IsTyping = True

file = WriteFile(FileName)
WriteInt (file, MaxTiles)

For T.Tile = Each tile
WriteInt (file, TX)
WriteInt (file, TY)
WriteInt (file, TFrame)
WriteInt (file, TLayer)
Next

WriteInt (file, MaxBGTiles)

For B.BGTile = Each BGTile
WriteInt (file, BX)
WriteInt (file, BY)
WriteInt (file, BFrame)
Next  

WriteInt (file, MaxSpawnPoints)

For S.SpawnPoint = Each SpawnPoint
WriteInt (file, SX)
WriteInt (file, SY)
WriteInt (file, SSPN)
Next

WriteInt (file, MaxEnts)

For E.Entity = Each Entity
WriteInt (file, EX)
WriteInt (file, EY)
WriteString (file, EName)
WriteInt (file, Frame)
WriteInt (file, Layer)
Next

CloseFile (file)
EndIf


If KeyHit(27) ; Loading
file = ReadFile(FileName)
Ts = ReadInt(file)

For i = 0 To Ts - 1
T.Tile = New Tile
TX = ReadInt(file)
TY = ReadInt(file)
TFrame = ReadInt(file)
TLayer = ReadInt(file)
Next

BTS = ReadInt (file)

For i2 = 0 To BTS - 1
B.BGTile = New BGTile
BX = ReadInt(file)
BY = ReadInt(file)
BFrame = ReadInt(file)
Next  

SPs = ReadInt(file)

For i3 = 0 To SPs - 1
S.SpawnPoint = New SpawnPoint
SX = ReadInt(file)
SY = ReadInt(file)
SSPN = ReadInt(file)
Next

ETs = ReadInt(file)

For i4 = 0 To ETs - 1
E.Entity = New Entity
EX = ReadInt(file)
EY = ReadInt(file)
EName = ReadString(file)
EFrame = ReadInt(file)
ELayer = ReadInt(file)
Next

CloseFile (file)
EndIf


;Render
ClsColor 0,0,100
Cls

If IsTyping = True

If KeyHit(28)
IsTyping = False
EndIf

EndIf

If IsTyping = False
;Mov(e)X and Mov(e)Y
If KeyHit(2)
MovX = CurX
MovY = CurY
EndIf


;Layer
If KeyHit(23)
CurLayer = CurLayer - 1
EndIf

If KeyHit(24)
CurLayer = CurLayer + 1
EndIf

;Edit Mode
If KeyHit(37)
EditMode  = EditMode - 1
EndIf

If KeyHit(38)
EditMode = EditMode + 1
EndIf

If EditMode < 0
EditMode = 0
EndIf

If EditMode > 3
EditMode = 3
EndIf

;End Edit Mode

If KeyDown(35) ;Help Button
Text 0,0, "Edit Mode:" + EditMode
Text 0,12, "Tile Frame:" + SelTile
Text 0,24, "Current Layer:" + CurLayer
Text 0,36, "Camera X:" + CX#
Text 0,48, "Camera Y:" + CY#
Text 0,60, "Controls:"
Text 0,72, "W - Scroll UP"
Text 0,84, "S - Scroll Down"
Text 0,96, "A - Scroll Left"
Text 0,108, "D - Scroll Right"
Text 0,120, "UP Key - Move Tile Up"
Text 0,132, "Down Key - Move Tile Down"
Text 0,144, "Left Key - Move Tile Left"
Text 0,156, "Right Key - Move Tile Right"
Text 0,168, "< - Decrease SelTile"
Text 0,180, "> - Increase SelTile"
Text 0,192, "K - Decrease Edit Mode"
Text 0,204, "L - Increase Edit Mode"
Text 0,216, "Entier - Place Tile/Entity/BG Tile/SpawnPoint"
Text 0,228, "Shift - Delete Tile/Entity/BG Tile/SpawnPoint"
Text 0,240, "Move X:" + MovX
Text 0,256, "Move Y:" + MovY
Text 0,268, "Cur X:" + CurX
Text 0,280, "Cur Y:" + CurY
Text 0,292, "Tiles:" + MaxTiles
Text 0,304, "BGTiles:" + MaxBGTiles
Text 0,316, "SpawnPoints:" + MaxSpawnPoints
Text 0,328, "Entites:" + MaxEnts
EndIf

;Seletected tile
If KeyHit(51)
SelTile = SelTile - 1
EndIf

If KeyHit(52)
SelTile = SelTile + 1
EndIf

If SelTile > 5 Then
SelTile = 0
EndIf

If SelTile < 0 Then
SelTile = 5
EndIf
;End Selected Tile

;Program Clean UP
If KeyHit(1)

For T.Tile = Each Tile
Delete T
Next

For B.BGTile = Each BGTile
Delete B
Next

For S.SpawnPoint = Each SpawnPoint
Delete S
Next

For E.Entity = Each Entity
Delete E
Next

FreeImage(CamImg)
FreeImage(TileSheet)

EndIf
;End Program Clean Up

;Creation
If KeyHit(28)

If EditMode = 0

For T.Tile = Each Tile

If TX = CurX + CX# And TY = CurY + CY#
Delete T
MaxTiles = MaxTiles - 1
EndIf
Next

T.Tile = New Tile
TX = CurX + CX#
TY = CurY + CY#
TFrame = SelTile
TLayer = CurLayer

MaxTiles = MaxTiles + 1
EndIf

If EditMode = 1

For B.BGTile = Each BGTile

If BX = CurX And BY = CurY
Delete B
MaxBGTiles = MaxBGTiles - 1
EndIf
Next

B.BGTile = New BGTile
BX = CurX + CX#
BY = CurY + CY#
BFrame = SelTile


MaxBGTiles = MaxBGTiles + 1
EndIf

If EditMode = 2

For S.SpawnPoint = Each SpawnPoint
If SX = CurX And SY = CurY
Delete S
MaxSpawnPoints = MaxSpawnPoints - 1
EndIf
Next

S.SpawnPoint = New SpawnPoint
SX = CurX + CX#
SY = CurY + CY#
SSPN = CurLayer

MaxSpawnPoints = MaxSpawnPoints + 1
EndIf

If EditMode = 3

For E.Entity = Each Entity

If EX = CurX And EY = CurY
Delete B
MaxEnts = MaxEnts - 1
EndIf
Next

E.Entity = New Entity
EX = CurX + CX#
EY = CurY + CY#
ELayer = CurLayer
EFrame = SelTile
IsTyping = True
EName = Input("Enter Entity Name:")


MaxEnts = MaxEnts + 1
EndIf



EndIf ;Total End Creation


;Destruction
If KeyHit(54)

;For Loop
For T.Tile = Each Tile

If TX = CurX And TY = CurY
Delete T
MaxTiles = MaxTiles - 1
EndIf

Next
;End Foor Loop

For B.BGTile = Each BGTile

If BX = CurX And BY = CurY
Delete B
MaxBGTiles = MaxBGTiles - 1
EndIf
Next

For S.SpawnPoint = Each SpawnPoint
If SX = CurX And SY = CurY
Delete S
MaxSpawnPoints = MaxSpawnPoints - 1
EndIf
Next


For E.Entity = Each Entity

If EX = CurX And EY = CurY
Delete B
MaxEnts = MaxEnts - 1
EndIf
Next

EndIf

;End Destruction

If KeyHit(200) ; Move Up
CurY = CurY - 16
EndIf

If KeyHit(208) ; Move Down
CurY = CurY + 16
EndIf

If KeyHit(203) ; Move Left
CurX = CurX - 16
EndIf

If KeyHit(205) ; Move Right
CurX = CurX + 16
EndIf


If KeyHit(17) ; Scroll UP :D
CY# = CY# - 16
EndIf

If KeyHit(31) ;Scroll Down :D
CY# = CY# + 16
EndIf  

If KeyHit(30) ; Scroll Left
CX# = CX# - 16
EndIf

If KeyHit(32) ; Scroll Right
CX# = CX# + 16
EndIf


EndIf ;End if isTyping = false

;Tile Drawing
For T.Tile = Each Tile

If ImagesOverlap(TileSheet,TX, TY,CamImg,CX#,CY#)
DrawImage TileSheet, Ceil((Abs(TX - CX#) / 16) * 16), Ceil((Abs(TY - CY#) / 16) * 16), TFrame#
EndIf

Next

For B.BGTile = Each BGTile

If ImagesOverlap(TileSheet,BX, BY,CamImg,CX#,CY#)
DrawImage TileSheet, Ceil((Abs(BX - CX#) / 16) * 16), Ceil((Abs(BY - CY#) / 16) * 16), BFrame#
EndIf

Next

For S.SpawnPoint = Each SpawnPoint

If ImagesOverlap(TileSheet,SX, SY,CamImg,CX#,CY#)
Oval Ceil((Abs(SX - CX#) / 16) * 16), Ceil((Abs(SY - CY#) / 16) * 16),16,16,1
EndIf

Next


For E.Entity = Each Entity

If ImagesOverlap(TileSheet,EX, EY,CamImg,CX#,CY#)
DrawImage TileSheet, Ceil((Abs(EX - CX#) / 16) * 16), Ceil((Abs(EY - CY#) / 16) * 16), EFrame#
Text EX, EY + 5, "Name:" + EName
EndIf

Next

;End Tile Drawing

If EditMode <> 2
DrawImage TileSheet, CurX, CurY, SelTile

Else
Oval CurX,CurY,16,16,1
EndIf
DrawImage CamImg, CX#, CY#
Flip
;End Render

Wend ;End Program Loop


Comments :


Abazek(Posted 1+ years ago)

 I couldn't get this to run in Blitz3D, kept getting a "Memory Access Violation" error.It runs fine in BlitzPlus, as long as you provide two bitmap files for it to load (LoadAminImage - 16x112, and LoadImage - 800x600).  But it will crash when you exit.


Kenshin Yurihara(Posted 1+ years ago)

 It works fine for me and yes, the 800x600 image is the "camera" I created scrolling by collisions. It also doesn't crash for me when I exit. On the other hand, I noticed I hadn't implemented some things in this piece of the code.So I added to the program clean up, when escape is hit.Try it now. ^^


Kenshin Yurihara(Posted 1+ years ago)

 This tile mapper, is receiving a entire remake. As I am planning to use it in my next project, although I'm not sure if I will released the code for it. I cannot/will not fix this version up anymore, if you find any problems and you cannot seem to fix them on your own hit me up and I MIGHT, fix it. Sorry :D