November 24, 2020, 09:57:21 AM

Author Topic: [bb] 2D Tile Mapper by Kenshin Yurihara [ 1+ years ago ]  (Read 682 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] 2D Tile Mapper by Kenshin Yurihara [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
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
  1. Type Tile
  2. Field X#
  3. Field Y#
  4. Field Frame#
  5. Field Layer#
  6. End Type
  7.  
  8. Type BGTile
  9. Field X#
  10. Field Y#
  11. Field Frame#
  12. End Type
  13.  
  14. Type SpawnPoint  
  15. Field X#
  16. Field Y#
  17. Field SPN ; Spawn Point Number :D
  18. End Type
  19.  
  20. Type Entity
  21. Field X#
  22. Field Y#
  23. Field Name$
  24. Field Frame
  25. Field Layer
  26. End Type
  27.  
  28. Graphics 800,608,32,2
  29. SetBuffer BackBuffer()
  30.  
  31.  
  32.  
  33. TileSheet = LoadAnimImage("TileSheet.bmp",16,16,0,2)
  34. CamImg = LoadImage("CamImg.bmp")
  35. MaskImage(CamImg,255,0,220)
  36. MaskImage(TileSheet,255,0,220)
  37.  
  38.  
  39. CurLayer = 1
  40.  
  41. CX# = 0
  42. CY# = 0
  43.  
  44. CurX = 0
  45. CurY = 0
  46. IsTyping = False
  47.  
  48. SelTile = 0
  49. EditMode = 0
  50. MovX = 0
  51. MovY = 0
  52. MaxTiles = 0
  53. MaxBGTiles = 0
  54. MaxSpawnPoints = 0
  55. MaxEnts = 0
  56.  
  57. FileName = "MAPITH!.Map"
  58. While Not KeyHit(1) ; Program loop
  59.  
  60. ;SAVING AND OMFG LOADING!;
  61. If KeyHit(26) ; Saving
  62. ;IsTyping = True
  63.  
  64. file = WriteFile(FileName)
  65. WriteInt (file, MaxTiles)
  66.  
  67. For T.Tile = Each tile
  68. WriteInt (file, TX)
  69. WriteInt (file, TY)
  70. WriteInt (file, TFrame)
  71. WriteInt (file, TLayer)
  72. Next
  73.  
  74. WriteInt (file, MaxBGTiles)
  75.  
  76. For B.BGTile = Each BGTile
  77. WriteInt (file, BX)
  78. WriteInt (file, BY)
  79. WriteInt (file, BFrame)
  80. Next  
  81.  
  82. WriteInt (file, MaxSpawnPoints)
  83.  
  84. For S.SpawnPoint = Each SpawnPoint
  85. WriteInt (file, SX)
  86. WriteInt (file, SY)
  87. WriteInt (file, SSPN)
  88. Next
  89.  
  90. WriteInt (file, MaxEnts)
  91.  
  92. For E.Entity = Each Entity
  93. WriteInt (file, EX)
  94. WriteInt (file, EY)
  95. WriteString (file, EName)
  96. WriteInt (file, Frame)
  97. WriteInt (file, Layer)
  98. Next
  99.  
  100. CloseFile (file)
  101. EndIf
  102.  
  103.  
  104. If KeyHit(27) ; Loading
  105. file = ReadFile(FileName)
  106. Ts = ReadInt(file)
  107.  
  108. For i = 0 To Ts - 1
  109. T.Tile = New Tile
  110. TX = ReadInt(file)
  111. TY = ReadInt(file)
  112. TFrame = ReadInt(file)
  113. TLayer = ReadInt(file)
  114. Next
  115.  
  116. BTS = ReadInt (file)
  117.  
  118. For i2 = 0 To BTS - 1
  119. B.BGTile = New BGTile
  120. BX = ReadInt(file)
  121. BY = ReadInt(file)
  122. BFrame = ReadInt(file)
  123. Next  
  124.  
  125. SPs = ReadInt(file)
  126.  
  127. For i3 = 0 To SPs - 1
  128. S.SpawnPoint = New SpawnPoint
  129. SX = ReadInt(file)
  130. SY = ReadInt(file)
  131. SSPN = ReadInt(file)
  132. Next
  133.  
  134. ETs = ReadInt(file)
  135.  
  136. For i4 = 0 To ETs - 1
  137. E.Entity = New Entity
  138. EX = ReadInt(file)
  139. EY = ReadInt(file)
  140. EName = ReadString(file)
  141. EFrame = ReadInt(file)
  142. ELayer = ReadInt(file)
  143. Next
  144.  
  145. CloseFile (file)
  146. EndIf
  147.  
  148.  
  149. ;Render
  150. ClsColor 0,0,100
  151. Cls
  152.  
  153. If IsTyping = True
  154.  
  155. If KeyHit(28)
  156. IsTyping = False
  157. EndIf
  158.  
  159. EndIf
  160.  
  161. If IsTyping = False
  162. ;Mov(e)X and Mov(e)Y
  163. If KeyHit(2)
  164. MovX = CurX
  165. MovY = CurY
  166. EndIf
  167.  
  168.  
  169. ;Layer
  170. If KeyHit(23)
  171. CurLayer = CurLayer - 1
  172. EndIf
  173.  
  174. If KeyHit(24)
  175. CurLayer = CurLayer + 1
  176. EndIf
  177.  
  178. ;Edit Mode
  179. If KeyHit(37)
  180. EditMode  = EditMode - 1
  181. EndIf
  182.  
  183. If KeyHit(38)
  184. EditMode = EditMode + 1
  185. EndIf
  186.  
  187. If EditMode < 0
  188. EditMode = 0
  189. EndIf
  190.  
  191. If EditMode > 3
  192. EditMode = 3
  193. EndIf
  194.  
  195. ;End Edit Mode
  196.  
  197. If KeyDown(35) ;Help Button
  198. Text 0,0, "Edit Mode:" + EditMode
  199. Text 0,12, "Tile Frame:" + SelTile
  200. Text 0,24, "Current Layer:" + CurLayer
  201. Text 0,36, "Camera X:" + CX#
  202. Text 0,48, "Camera Y:" + CY#
  203. Text 0,60, "Controls:"
  204. Text 0,72, "W - Scroll UP"
  205. Text 0,84, "S - Scroll Down"
  206. Text 0,96, "A - Scroll Left"
  207. Text 0,108, "D - Scroll Right"
  208. Text 0,120, "UP Key - Move Tile Up"
  209. Text 0,132, "Down Key - Move Tile Down"
  210. Text 0,144, "Left Key - Move Tile Left"
  211. Text 0,156, "Right Key - Move Tile Right"
  212. Text 0,168, "< - Decrease SelTile"
  213. Text 0,180, "> - Increase SelTile"
  214. Text 0,192, "K - Decrease Edit Mode"
  215. Text 0,204, "L - Increase Edit Mode"
  216. Text 0,216, "Entier - Place Tile/Entity/BG Tile/SpawnPoint"
  217. Text 0,228, "Shift - Delete Tile/Entity/BG Tile/SpawnPoint"
  218. Text 0,240, "Move X:" + MovX
  219. Text 0,256, "Move Y:" + MovY
  220. Text 0,268, "Cur X:" + CurX
  221. Text 0,280, "Cur Y:" + CurY
  222. Text 0,292, "Tiles:" + MaxTiles
  223. Text 0,304, "BGTiles:" + MaxBGTiles
  224. Text 0,316, "SpawnPoints:" + MaxSpawnPoints
  225. Text 0,328, "Entites:" + MaxEnts
  226. EndIf
  227.  
  228. ;Seletected tile
  229. If KeyHit(51)
  230. SelTile = SelTile - 1
  231. EndIf
  232.  
  233. If KeyHit(52)
  234. SelTile = SelTile + 1
  235. EndIf
  236.  
  237. If SelTile > 5 Then
  238. SelTile = 0
  239. EndIf
  240.  
  241. If SelTile < 0 Then
  242. SelTile = 5
  243. EndIf
  244. ;End Selected Tile
  245.  
  246. ;Program Clean UP
  247. If KeyHit(1)
  248.  
  249. For T.Tile = Each Tile
  250. Delete T
  251. Next
  252.  
  253. For B.BGTile = Each BGTile
  254. Delete B
  255. Next
  256.  
  257. For S.SpawnPoint = Each SpawnPoint
  258. Delete S
  259. Next
  260.  
  261. For E.Entity = Each Entity
  262. Delete E
  263. Next
  264.  
  265. FreeImage(CamImg)
  266. FreeImage(TileSheet)
  267.  
  268. EndIf
  269. ;End Program Clean Up
  270.  
  271. ;Creation
  272. If KeyHit(28)
  273.  
  274. If EditMode = 0
  275.  
  276. For T.Tile = Each Tile
  277.  
  278. If TX = CurX + CX# And TY = CurY + CY#
  279. Delete T
  280. MaxTiles = MaxTiles - 1
  281. EndIf
  282. Next
  283.  
  284. T.Tile = New Tile
  285. TX = CurX + CX#
  286. TY = CurY + CY#
  287. TFrame = SelTile
  288. TLayer = CurLayer
  289.  
  290. MaxTiles = MaxTiles + 1
  291. EndIf
  292.  
  293. If EditMode = 1
  294.  
  295. For B.BGTile = Each BGTile
  296.  
  297. If BX = CurX And BY = CurY
  298. Delete B
  299. MaxBGTiles = MaxBGTiles - 1
  300. EndIf
  301. Next
  302.  
  303. B.BGTile = New BGTile
  304. BX = CurX + CX#
  305. BY = CurY + CY#
  306. BFrame = SelTile
  307.  
  308.  
  309. MaxBGTiles = MaxBGTiles + 1
  310. EndIf
  311.  
  312. If EditMode = 2
  313.  
  314. For S.SpawnPoint = Each SpawnPoint
  315. If SX = CurX And SY = CurY
  316. Delete S
  317. MaxSpawnPoints = MaxSpawnPoints - 1
  318. EndIf
  319. Next
  320.  
  321. S.SpawnPoint = New SpawnPoint
  322. SX = CurX + CX#
  323. SY = CurY + CY#
  324. SSPN = CurLayer
  325.  
  326. MaxSpawnPoints = MaxSpawnPoints + 1
  327. EndIf
  328.  
  329. If EditMode = 3
  330.  
  331. For E.Entity = Each Entity
  332.  
  333. If EX = CurX And EY = CurY
  334. Delete B
  335. MaxEnts = MaxEnts - 1
  336. EndIf
  337. Next
  338.  
  339. E.Entity = New Entity
  340. EX = CurX + CX#
  341. EY = CurY + CY#
  342. ELayer = CurLayer
  343. EFrame = SelTile
  344. IsTyping = True
  345. EName = Input("Enter Entity Name:")
  346.  
  347.  
  348. MaxEnts = MaxEnts + 1
  349. EndIf
  350.  
  351.  
  352.  
  353. EndIf ;Total End Creation
  354.  
  355.  
  356. ;Destruction
  357. If KeyHit(54)
  358.  
  359. ;For Loop
  360. For T.Tile = Each Tile
  361.  
  362. If TX = CurX And TY = CurY
  363. Delete T
  364. MaxTiles = MaxTiles - 1
  365. EndIf
  366.  
  367. Next
  368. ;End Foor Loop
  369.  
  370. For B.BGTile = Each BGTile
  371.  
  372. If BX = CurX And BY = CurY
  373. Delete B
  374. MaxBGTiles = MaxBGTiles - 1
  375. EndIf
  376. Next
  377.  
  378. For S.SpawnPoint = Each SpawnPoint
  379. If SX = CurX And SY = CurY
  380. Delete S
  381. MaxSpawnPoints = MaxSpawnPoints - 1
  382. EndIf
  383. Next
  384.  
  385.  
  386. For E.Entity = Each Entity
  387.  
  388. If EX = CurX And EY = CurY
  389. Delete B
  390. MaxEnts = MaxEnts - 1
  391. EndIf
  392. Next
  393.  
  394. EndIf
  395.  
  396. ;End Destruction
  397.  
  398. If KeyHit(200) ; Move Up
  399. CurY = CurY - 16
  400. EndIf
  401.  
  402. If KeyHit(208) ; Move Down
  403. CurY = CurY + 16
  404. EndIf
  405.  
  406. If KeyHit(203) ; Move Left
  407. CurX = CurX - 16
  408. EndIf
  409.  
  410. If KeyHit(205) ; Move Right
  411. CurX = CurX + 16
  412. EndIf
  413.  
  414.  
  415. If KeyHit(17) ; Scroll UP :D
  416. CY# = CY# - 16
  417. EndIf
  418.  
  419. If KeyHit(31) ;Scroll Down :D
  420. CY# = CY# + 16
  421. EndIf  
  422.  
  423. If KeyHit(30) ; Scroll Left
  424. CX# = CX# - 16
  425. EndIf
  426.  
  427. If KeyHit(32) ; Scroll Right
  428. CX# = CX# + 16
  429. EndIf
  430.  
  431.  
  432. EndIf ;End if isTyping = false
  433.  
  434. ;Tile Drawing
  435. For T.Tile = Each Tile
  436.  
  437. If ImagesOverlap(TileSheet,TX, TY,CamImg,CX#,CY#)
  438. DrawImage TileSheet, Ceil((Abs(TX - CX#) / 16) * 16), Ceil((Abs(TY - CY#) / 16) * 16), TFrame#
  439. EndIf
  440.  
  441. Next
  442.  
  443. For B.BGTile = Each BGTile
  444.  
  445. If ImagesOverlap(TileSheet,BX, BY,CamImg,CX#,CY#)
  446. DrawImage TileSheet, Ceil((Abs(BX - CX#) / 16) * 16), Ceil((Abs(BY - CY#) / 16) * 16), BFrame#
  447. EndIf
  448.  
  449. Next
  450.  
  451. For S.SpawnPoint = Each SpawnPoint
  452.  
  453. If ImagesOverlap(TileSheet,SX, SY,CamImg,CX#,CY#)
  454. Oval Ceil((Abs(SX - CX#) / 16) * 16), Ceil((Abs(SY - CY#) / 16) * 16),16,16,1
  455. EndIf
  456.  
  457. Next
  458.  
  459.  
  460. For E.Entity = Each Entity
  461.  
  462. If ImagesOverlap(TileSheet,EX, EY,CamImg,CX#,CY#)
  463. DrawImage TileSheet, Ceil((Abs(EX - CX#) / 16) * 16), Ceil((Abs(EY - CY#) / 16) * 16), EFrame#
  464. Text EX, EY + 5, "Name:" + EName
  465. EndIf
  466.  
  467. Next
  468.  
  469. ;End Tile Drawing
  470.  
  471. If EditMode <> 2
  472. DrawImage TileSheet, CurX, CurY, SelTile
  473.  
  474. Else
  475. Oval CurX,CurY,16,16,1
  476. EndIf
  477. DrawImage CamImg, CX#, CY#
  478. Flip
  479. ;End Render
  480.  
  481. 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


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal