tiledrop.bmx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. Strict
  2. Incbin "media/back.png"
  3. Incbin "media/blocks.png"
  4. Incbin "media/part.png"
  5. Incbin "media/pointer.PNG"
  6. Incbin "media/shine.png"
  7. Graphics 640,480
  8. Global backIm:TImage = LoadImage("incbin::media/back.png")
  9. AutoMidHandle 1
  10. Global blocks:TImage = LoadAnimImage("incbin::media/blocks.png",32,32,0,16)
  11. Global partImg:TImage = LoadImage("incbin::media/part.png")
  12. Global mousePoint:TImage= LoadImage("incbin::media/pointer.PNG")
  13. Global shine_img:TImage = LoadImage("incbin::media/shine.png")
  14. AutoMidHandle 0
  15. Global Map[8,14]
  16. Global t1x,t1y,t2x,t2y
  17. Global mouse_left_state
  18. Global selection_done
  19. Global rotation# = 0
  20. Global Center_X#
  21. Global Center_Y#
  22. Global mt1,mt2
  23. Global Tile_Rad#
  24. Global THE_Axis
  25. Global FLIPSPEED=8
  26. Global Scn_Flash#=0
  27. Global shine_pos#=0
  28. HideMouse
  29. While Not KeyDown(KEY_ESCAPE)
  30. Cls
  31. DrawLayout()
  32. If selection_done =0
  33. FillGrid()
  34. EndIf
  35. DrawGrid()
  36. If selection_done
  37. do_swap_tiles()
  38. Else
  39. UpdateSelection()
  40. EndIf
  41. SetBlend MASKBLEND
  42. DrawText mouse_left_state,0,0
  43. DrawText t1x+","+t1y,0,12
  44. DrawText t2x+","+t2y,0,23
  45. DrawImage mousePoint,MouseX(),MouseY()
  46. Flip
  47. Wend
  48. End
  49. Function UpdateSelection()
  50. Local x=MouseX()-192
  51. Local y=MouseY()-24
  52. Local dx,dy
  53. If MouseDown(1)
  54. Select mouse_left_state
  55. Case 0
  56. If x>=0 And x<256
  57. If y>=0 And y<448
  58. 'select tile1
  59. t1x=Floor(x/32.0)
  60. t1y=Floor(y/32.0)
  61. EndIf
  62. EndIf
  63. 'get tile 1
  64. mouse_left_state=1
  65. Case 2
  66. If x>=0 And x<256
  67. If y>=0 And y<448
  68. 'select tile1
  69. t2x=Floor(x/32.0)
  70. t2y=Floor(y/32.0)
  71. EndIf
  72. EndIf
  73. mouse_left_state=3
  74. EndSelect
  75. Else
  76. Select mouse_left_state
  77. Case 1
  78. mouse_left_state=2
  79. Case 3
  80. 'check that only 1 tile away and not diag
  81. dx=Abs(t2x-t1x)
  82. dy=Abs(t2y-t1y)
  83. If dx=1 And dy=0
  84. ' Switch(t1x,t1y,t2x,t2y)
  85. selection_done=1
  86. ElseIf dx=0 And dy=1
  87. ' Switch(t1x,t1y,t2x,t2y)
  88. selection_done=1
  89. EndIf
  90. mouse_left_state=0
  91. Case 99
  92. mouse_left_state=0
  93. EndSelect
  94. EndIf
  95. EndFunction
  96. 'add tiles to array at the top
  97. Function FillGrid()
  98. Local x,y,tl,tlc
  99. For x=0 Until 8
  100. If map[x,0]=0
  101. map[x,0]=1+Rnd(1)*7'15
  102. EndIf
  103. Next
  104. 'Fall
  105. For y=12 To 0 Step -1
  106. For x=0 Until 8
  107. If map[x,y]>0
  108. If map[x,y+1]=0
  109. map[x,y+1]=map[x,y]
  110. map[x,y]=0
  111. EndIf
  112. EndIf
  113. Next
  114. Next
  115. For y=0 Until 14
  116. For x=0 Until 8
  117. tl=map[x,y]
  118. If tl>0
  119. If Counttile(x,y,tl,0,tlc)
  120. KillTiles(x,y,tlc,0)
  121. ElseIf Counttile(x,y,tl,1,tlc)
  122. KillTiles(x,y,tlc,1)
  123. ElseIf Counttile(x,y,tl,2,tlc)
  124. KillTiles(x,y,tlc,2)
  125. ElseIf Counttile(x,y,tl,3,tlc)
  126. KillTiles(x,y,tlc,3)
  127. EndIf
  128. EndIf
  129. Next
  130. Next
  131. EndFunction
  132. Function KillTiles(x,y,c,dir)
  133. Local d
  134. For d=0 Until c
  135. Select dir
  136. Case 0
  137. map[x,y]=0
  138. y:-1
  139. Case 1
  140. map[x,y]=0
  141. x:+1
  142. Case 2
  143. map[x,y]=0
  144. y:+1
  145. Case 3
  146. map[x,y]=0
  147. x:-1
  148. EndSelect
  149. Next
  150. EndFunction
  151. Function CountTile(x,y,ty,dir, cn Var)
  152. cn = 0
  153. Select dir
  154. Case 0 ' Up
  155. While y>0
  156. If map[x,y]=ty
  157. cn:+1
  158. Else
  159. Return cn>2
  160. EndIf
  161. y=y-1
  162. Wend
  163. Return cn>2
  164. Case 1 ' Right
  165. While x<8
  166. If map[x,y]=ty
  167. cn:+1
  168. Else
  169. Return cn>2
  170. EndIf
  171. x:+1
  172. Wend
  173. Return cn>2
  174. Case 2 ' Down
  175. While y<14
  176. If map[x,y]=ty
  177. cn:+1
  178. Else
  179. Return cn>2
  180. EndIf
  181. y:+1
  182. Wend
  183. Return cn>2
  184. Case 3 ' Left
  185. While x>0
  186. If map[x,y]=ty
  187. cn:+1
  188. Else
  189. Return cn>2
  190. EndIf
  191. x:-1
  192. Wend
  193. Return cn>2
  194. EndSelect
  195. cn = 0
  196. Return 0
  197. EndFunction
  198. Function DrawGrid()
  199. SetColor 255,255,255
  200. Local x,y
  201. For y=0 Until 14
  202. For x=0 Until 8
  203. If selection_done
  204. If map[x,y]>0
  205. If (x=t1x And y=t1y) Or (x=t2x And y=t2y)
  206. Else
  207. SetBlend MASKBLEND
  208. DrawImage blocks,208+x*32,32+y*32,map[x,y]-1
  209. EndIf
  210. EndIf
  211. Else
  212. If map[x,y]>0
  213. DrawImage blocks,208+x*32,32+y*32,map[x,y]-1
  214. EndIf
  215. EndIf
  216. Next
  217. Next
  218. SetViewport 0,0,640,480
  219. EndFunction
  220. Function DrawLayout()
  221. Cls
  222. If Scn_Flash<0
  223. Scn_Flash:+0.2
  224. Else
  225. Scn_Flash=0
  226. EndIf
  227. SetBlend SOLIDBLEND
  228. SetColor 255,255,255
  229. TileImage backIm,0,0
  230. SetColor 128,128,128
  231. SetViewport 192,24,256,448
  232. Cls
  233. TileImage backIm,0,0
  234. If shine_pos#=0 SetViewport 0,0,640,480
  235. EndFunction
  236. Function CausesPop()
  237. Local x,y,tl,tlc
  238. For y=0 Until 14
  239. For x=0 Until 8
  240. tl=map[x,y]
  241. If tl>0
  242. If Counttile(x,y,tl,0,tlc)
  243. Return 1
  244. ElseIf Counttile(x,y,tl,1,tlc)
  245. Return 1
  246. ElseIf Counttile(x,y,tl,2,tlc)
  247. Return 1
  248. ElseIf Counttile(x,y,tl,3,tlc)
  249. Return 1
  250. EndIf
  251. EndIf
  252. Next
  253. Next
  254. Return 0
  255. EndFunction
  256. Function Switch(x0,y0,x1,y1)
  257. Local a
  258. a=map[x0,y0]
  259. map[x0,y0]=map[x1,y1]
  260. map[x1,y1]=a
  261. If CausesPop() Return 1
  262. a=map[x0,y0]
  263. map[x0,y0]=map[x1,y1]
  264. map[x1,y1]=a
  265. Return 0
  266. EndFunction
  267. Function Do_Swap_Tiles()
  268. Local x1,x2,y1,y2
  269. Local size1#,size2#
  270. Select selection_done
  271. Case 1
  272. 'setup tile swap
  273. rotation=180
  274. selection_done = 2
  275. 'calculate center
  276. x1 = 208+t1x*32
  277. x2 = 208+t2x*32
  278. y1 = 32+t1y*32
  279. y2 = 32+t2y*32
  280. Center_X = ((x2-x1)/2)+x1
  281. Center_Y = ((y2-y1)/2)+y1
  282. Tile_Rad = 16
  283. 'check side
  284. If t1y=t2y
  285. If t1x>t2x
  286. mt1 = map[t2x,t2y]
  287. mt2 = map[t1x,t1y]
  288. Else
  289. mt1 = map[t1x,t1y]
  290. mt2 = map[t2x,t2y]
  291. EndIf
  292. Else
  293. If t1y>t2y
  294. mt1 = map[t2x,t2y]
  295. mt2 = map[t1x,t1y]
  296. Else
  297. mt1 = map[t1x,t1y]
  298. mt2 = map[t2x,t2y]
  299. EndIf
  300. EndIf
  301. THE_Axis = 1
  302. If t1y=t2y THE_Axis = 0
  303. Case 2
  304. If rotation<0
  305. If Switch(t1x,t1y,t2x,t2y)
  306. selection_done = 0
  307. Else
  308. selection_done = 3
  309. EndIf
  310. Else
  311. rotation:-FLIPSPEED
  312. size2# = 0.80+0.20*Sin(rotation)
  313. size1# = 0.80+0.20*Sin(-rotation)
  314. Select THE_Axis
  315. Case 0 'x
  316. SetScale size1,size1
  317. DrawImage blocks, Center_X+Tile_Rad*Cos(rotation), Center_Y, mt1-1
  318. SetScale size2,size2
  319. DrawImage blocks, Center_X-Tile_Rad*Cos(rotation), Center_Y, mt2-1
  320. SetScale 1,1
  321. Case 1 ' y
  322. SetScale size1,size1
  323. DrawImage blocks, Center_X, Center_Y+Tile_Rad*Cos(rotation), mt1-1
  324. SetScale size2,size2
  325. DrawImage blocks, Center_X, Center_Y-Tile_Rad*Cos(rotation), mt2-1
  326. SetScale 1,1
  327. EndSelect
  328. ' DrawImage blocks,192+t2x*32,24+t2y*32,mt2
  329. EndIf
  330. Case 3 '
  331. If rotation>=180
  332. selection_done = 0
  333. Else
  334. rotation:+FLIPSPEED
  335. size2# = 0.80+0.20*Sin(rotation)
  336. size1# = 0.80+0.20*Sin(-rotation)
  337. Select THE_Axis
  338. Case 0 'x
  339. SetScale size1,size1
  340. DrawImage blocks, Center_X+Tile_Rad*Cos(rotation), Center_Y, mt1-1
  341. SetScale size2,size2
  342. DrawImage blocks, Center_X-Tile_Rad*Cos(rotation), Center_Y, mt2-1
  343. SetScale 1,1
  344. Case 1 ' y
  345. SetScale size1,size1
  346. DrawImage blocks, Center_X, Center_Y+Tile_Rad*Cos(rotation), mt1-1
  347. SetScale size2,size2
  348. DrawImage blocks, Center_X, Center_Y-Tile_Rad*Cos(rotation), mt2-1
  349. SetScale 1,1
  350. EndSelect
  351. EndIf
  352. EndSelect
  353. EndFunction