game.dd 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. # main world
  2. # defines a cube that is constantly rotating,
  3. # simulating a "hello world" with a cube
  4. (class dd_world_game dd_world
  5. (group
  6. # map size
  7. (def int tiles_width)
  8. (def int tiles_height)
  9. # base tile that everything else copies
  10. (def dd_meshTexture baseTile)
  11. # tiles
  12. (def (array Tile 100) tile)
  13. (def int tile_amount)
  14. (def (array int 40) tile_mage)
  15. (def int tile_mage_amount)
  16. # camera
  17. (def dd_matrix camera)
  18. (def int current_tile)
  19. # type of mages
  20. (def dd_meshColour mageMesh)
  21. (def dd_meshColour mageMeshSelected)
  22. (def dd_meshColour mageDiamondMesh)
  23. (def dd_meshColour mageDiamondMeshSelected)
  24. (def dd_meshColour mageSelfMesh)
  25. (def dd_meshColour mageSelfMeshSelected)
  26. (def int stage)
  27. (def int isSolved)
  28. (def dd_matrix solvedCamera)
  29. # constructor, initialise the map, and the mages
  30. # initialise rotation
  31. (function create (group)
  32. (group
  33. # clear colour
  34. (dd_clearColour 0.1 0.1 0.1 1)
  35. # initialise the base tile
  36. (dd_meshTexture_create this.baseTile)
  37. (this.baseTile.set_primitive DD_PRIMITIVE_RECTANGLE)
  38. (this.baseTile.loadTexture "dd_logo.bmp")
  39. # init mages
  40. (dd_meshColour_create this.mageMesh)
  41. (this.mageMesh.load "mage.ply")
  42. (dd_meshColour_create this.mageMeshSelected)
  43. (this.mageMeshSelected.load "mage_selected.ply")
  44. (dd_meshColour_create this.mageDiamondMesh)
  45. (this.mageDiamondMesh.load "mage_diamond.ply")
  46. (dd_meshColour_create this.mageDiamondMeshSelected)
  47. (this.mageDiamondMeshSelected.load "mage_diamond_selected.ply")
  48. (dd_meshColour_create this.mageSelfMesh)
  49. (this.mageSelfMesh.load "mage_self.ply")
  50. (dd_meshColour_create this.mageSelfMeshSelected)
  51. (this.mageSelfMeshSelected.load "mage_self_selected.ply")
  52. # init first stage
  53. (this.initStage 0)
  54. # camera initialisation
  55. (dd_matrix_identity this.camera)
  56. (dd_matrix_translates this.camera 0 0 -10)
  57. (= this.stage 0)
  58. )
  59. )
  60. # update - add rotation
  61. (function update (group) override
  62. (group
  63. (if this.isSolved
  64. (dd_matrix_translatea this.camera
  65. (* (- (- 0 (dd_matrix_x this.solvedCamera)) (dd_matrix_x this.camera)) 0.3)
  66. (* (- (dd_matrix_y this.solvedCamera) (dd_matrix_y this.camera)) 0.3)
  67. 0
  68. )
  69. # camera moves towards active tile
  70. (dd_matrix_translatea this.camera
  71. (* (- (- 0 (dd_matrix_x this.tile[this.tile_mage[this.current_tile]])) (dd_matrix_x this.camera)) 0.3)
  72. (* (- (- 0 (dd_matrix_y this.tile[this.tile_mage[this.current_tile]])) (dd_matrix_y this.camera)) 0.3)
  73. 0
  74. )
  75. )
  76. # tiles rotate towards desired value
  77. (for (def int i 0) (< i this.tile_amount) (= i (+ i 1))
  78. (this.tile[i].update)
  79. )
  80. )
  81. )
  82. # draw
  83. (function draw (group) override
  84. (group
  85. # apply camera
  86. (glMultMatrixf this.camera)
  87. # draw tiles
  88. (for (def int i 0) (< i this.tile_amount) (= i (+ i 1))
  89. (group
  90. (this.tile[i].draw)
  91. )
  92. )
  93. )
  94. )
  95. # input, change tiles or apply
  96. (function key_input (group char key) override
  97. (group
  98. # if the puzzle is solved, change input
  99. (if this.isSolved
  100. (group
  101. (if (== key 'w')
  102. (dd_matrix_translatea this.camera 0 0 1)
  103. (== key 's')
  104. (dd_matrix_translatea this.camera 0 0 -1)
  105. (== key 'f')
  106. (this.initStage (+ this.stage 1))
  107. )
  108. )
  109. (== key 'd')
  110. (group
  111. (if (< this.current_tile (- this.tile_mage_amount 1))
  112. (this.select (+ this.current_tile 1))
  113. (this.select 0)
  114. )
  115. )
  116. (== key 'a')
  117. (group
  118. (if (> this.current_tile 0)
  119. (this.select (- this.current_tile 1))
  120. # else
  121. (this.select (- this.tile_mage_amount 1))
  122. )
  123. )
  124. (== key ' ')
  125. (group
  126. (dd_world_set dd_world_menu)
  127. )
  128. (== key 'f')
  129. (group
  130. (this.rotateTiles this.current_tile 0 0)
  131. )
  132. (== key 't') (this.initStage (+ this.stage 1))
  133. (== key 'v') (this.setVictory)
  134. )
  135. )
  136. ) # input
  137. (function rotateTiles (group int index int instant int invert)
  138. (group
  139. # on invert, pick the previous mage
  140. (def int side this.tile[this.tile_mage[index]].side)
  141. # (if invert
  142. # (group
  143. # (= side (- side 1))
  144. # (if (< side 0) (= side 3))
  145. # )
  146. # )
  147. # make box rotation
  148. (if (== this.tile[this.tile_mage[index]].isMageArray[side] 2)
  149. (group
  150. # i = row
  151. # j = column
  152. (def int tileIndex this.tile_mage[index])
  153. (for (def int i 0) (< i 3) (= i (+ i 1))
  154. (for (def int j 0) (< j 3) (= j (+ j 1))
  155. (group
  156. # get the index
  157. (def int tempIndex (+ (- tileIndex (+ this.tiles_width 1)) (* i this.tiles_width) j))
  158. # if out of bounds - don't rotate
  159. (if (|| (< tempIndex 0) (>= tempIndex this.tile_amount))
  160. (group)
  161. # else if block is in the correct line, rotate it
  162. (&& (>= (+ (- tileIndex this.tiles_width) (* i this.tiles_width)) 0) (== (/ tempIndex this.tiles_width) (/ (+ (- tileIndex this.tiles_width) (* i this.tiles_width)) this.tiles_width)))
  163. (group
  164. (this.tile[tempIndex].advanceSide instant)
  165. )
  166. )
  167. )
  168. )
  169. )
  170. )
  171. # self rotation
  172. (== this.tile[this.tile_mage[index]].isMageArray[side] 1)
  173. (group
  174. (this.tile[this.tile_mage[index]].advanceSide instant)
  175. )
  176. # diamond
  177. (== this.tile[this.tile_mage[index]].isMageArray[side] 3)
  178. (group
  179. # i = row
  180. # j = column
  181. (def int tileIndex this.tile_mage[index])
  182. (for (def int i 0) (< i 3) (= i (+ i 1))
  183. (for (def int j 0) (< j 3) (= j (+ j 1))
  184. (group
  185. (if (|| (&& (== j 0) (== i 1)) (== j 1) (&& (== j 2) (== i 1)))
  186. (group
  187. # get the index
  188. (def int tempIndex (+ (- tileIndex (+ this.tiles_width 1)) (* i this.tiles_width) j))
  189. # if out of bounds - don't rotate
  190. (if (|| (< tempIndex 0) (>= tempIndex this.tile_amount))
  191. (group)
  192. # else if block is in the correct line, rotate it
  193. (&& (>= (+ (- tileIndex this.tiles_width) (* i this.tiles_width)) 0) (== (/ tempIndex this.tiles_width) (/ (+ (- tileIndex this.tiles_width) (* i this.tiles_width)) this.tiles_width)))
  194. (group
  195. (this.tile[tempIndex].advanceSide instant)
  196. )
  197. )
  198. )
  199. )
  200. )
  201. )
  202. )
  203. )
  204. )
  205. # check victory condition
  206. (def int hasWon 1)
  207. (for (def int i 0) (< i this.tile_amount) (= i (+ i 1))
  208. (if (> this.tile[i].side 0) (= hasWon 0))
  209. )
  210. (if (&& hasWon (== instant 0)) (this.setVictory))
  211. )
  212. ) # rotateTiles
  213. (function initStage (group int index)
  214. (group
  215. # map index
  216. (= this.stage index)
  217. # map size
  218. # from 4x3 to 7x6
  219. (= this.tiles_width (+ 4 (* (% this.stage 6) 0.5) (% this.stage 2)))
  220. (= this.tiles_height (+ 3 (* (% this.stage 6) 0.5) (% this.stage 2)))
  221. # calculate total amount of tiles, making sure not to exceed the max
  222. (= this.tile_amount (* this.tiles_width this.tiles_height))
  223. (if (> this.tile_amount 100) (= this.tile_amount 100))
  224. # initialise each tile
  225. (for (def int i 0) (< i this.tile_amount) (= i (+ i 1))
  226. (group
  227. (Tile_create this.tile[i])
  228. (dd_matrix_translates this.tile[i]
  229. (* (% i this.tiles_width) 1.02)
  230. (* (/ i this.tiles_width) -1.02)
  231. 0
  232. )
  233. (this.tile[i].mesh[0].copy this.baseTile)
  234. (this.tile[i].mesh[0].set_primitive_texcoords
  235. (* (/ 1.0 this.tiles_width) (% i this.tiles_width))
  236. (* (/ 1.0 this.tiles_height) (- (- this.tiles_height 1) (/ i this.tiles_width)))
  237. (/ 1.0 this.tiles_width)
  238. (/ 1.0 this.tiles_height)
  239. )
  240. )
  241. )
  242. # mages on the map 0, `tile_amount`]
  243. # from 2 to 40
  244. (= this.tile_mage_amount (+ 2 (* (/ this.stage 6) 2) (% this.stage 6)))
  245. (for (def int i 0) (< i this.tile_mage_amount) (= i (+ i 1))
  246. (group
  247. (def int lowIndex)
  248. (= lowIndex (* (/ this.tile_amount this.tile_mage_amount) i))
  249. (def int highIndex)
  250. (= highIndex (+ (* (/ this.tile_amount this.tile_mage_amount) (+ i 1))))
  251. (= this.tile_mage[i] (+ lowIndex (% (* (+ i 1) this.stage) (- highIndex lowIndex))))
  252. #(= this.tile_mage[i] (+ (* (/ this.tile_amount this.tile_mage_amount) i) (dd_math_rand (/ this.tile_amount this.tile_mage_amount))))
  253. )
  254. )
  255. # configure tiles to be mages
  256. (for (def int i 0) (< i this.tile_mage_amount) (= i (+ i 1))
  257. (group
  258. # mage is different on each of the 4 different sides
  259. # they can all be the same, different shape, or nothing
  260. # mageSet(SQUARE, SQUARE, SELF, DIAMOND)
  261. (= this.tile[this.tile_mage[i]].isMageArray[0] 2)
  262. (= this.tile[this.tile_mage[i]].isMageArray[1] 2)
  263. (= this.tile[this.tile_mage[i]].isMageArray[2] 2)
  264. (= this.tile[this.tile_mage[i]].isMageArray[3] 2)
  265. # second world
  266. (if (>= this.stage 6)
  267. (group
  268. (if (== (% i 3) 1)
  269. (= this.tile[this.tile_mage[i]].isMageArray[0] 3)
  270. )
  271. )
  272. )
  273. # third world
  274. (if (>= this.stage 12)
  275. (group
  276. (if (== (% i 4) 1)
  277. (= this.tile[this.tile_mage[i]].isMageArray[2] 1)
  278. )
  279. )
  280. )
  281. (for (def int j 0) (< j 4) (= j (+ j 1))
  282. (group
  283. (if (== this.tile[this.tile_mage[i]].isMageArray[j] 1)
  284. (group
  285. (this.tile[this.tile_mage[i]].mageMesh[j].copy this.mageSelfMesh)
  286. (this.tile[this.tile_mage[i]].mageMeshSelected[j].copy this.mageSelfMeshSelected)
  287. )
  288. (== this.tile[this.tile_mage[i]].isMageArray[j] 2)
  289. (group
  290. (this.tile[this.tile_mage[i]].mageMesh[j].copy this.mageMesh)
  291. (this.tile[this.tile_mage[i]].mageMeshSelected[j].copy this.mageMeshSelected)
  292. )
  293. (== this.tile[this.tile_mage[i]].isMageArray[j] 3)
  294. (group
  295. (this.tile[this.tile_mage[i]].mageMesh[j].copy this.mageDiamondMesh)
  296. (this.tile[this.tile_mage[i]].mageMeshSelected[j].copy this.mageDiamondMeshSelected)
  297. )
  298. )
  299. )
  300. )
  301. )
  302. )
  303. # init selection and select first tile
  304. (= this.current_tile 0)
  305. (this.tile[this.tile_mage[0]].select)
  306. # randomly rotate the map
  307. (for (def int i 0) (< i 20) (= i (+ i 1))
  308. (this.rotateTiles (dd_math_rand this.tile_mage_amount) 1 1)
  309. )
  310. # set the solved camera
  311. (dd_matrix_identity this.solvedCamera)
  312. (dd_matrix_translates this.solvedCamera (- (/ this.tiles_width 2.0) 0.5) (- (/ this.tiles_height 2.0) 0.5) 0)
  313. # reset victory flag
  314. (= this.isSolved 0)
  315. )
  316. ) # init stage
  317. # select a new mage tile
  318. (function select (group int newMageIndex)
  319. (group
  320. # deselect current selection
  321. (this.tile[this.tile_mage[this.current_tile]].deselect)
  322. # select new one
  323. (= this.current_tile newMageIndex)
  324. (this.tile[this.tile_mage[this.current_tile]].select)
  325. )
  326. )
  327. # run when the game has won
  328. (function setVictory (group)
  329. (group
  330. # deselect current selection
  331. (this.tile[this.tile_mage[this.current_tile]].deselect)
  332. # set victory flag
  333. (= this.isSolved 1)
  334. )
  335. ) # set victory
  336. )
  337. ) # main world