tile.dd 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # tile from the grid
  2. # is assigned a side (front = 0, bottom = 1, back = 2, top = 3)
  3. (class Tile dd_matrix
  4. (group
  5. # current side
  6. (def int side)
  7. (def (array dd_matrix 4) matrix_side)
  8. # mesh of the tile
  9. (def (array dd_matrix 6) cubeMatrix)
  10. (def (array dd_meshTexture 6) mesh)
  11. (def (array dd_meshColour 4) mageMesh)
  12. (def (array dd_meshColour 4) mageMeshSelected)
  13. (def (array int 4) isMageArray)
  14. (def int isSelected)
  15. (def dd_matrix mageMatrix)
  16. # create tile facing front
  17. (function create (group)
  18. (group
  19. # initialise sides
  20. (for (def int i 0) (< i 4) (= i (+ i 1))
  21. (group
  22. (dd_matrix_identity this.matrix_side[i])
  23. (dd_matrix_rotate_x this.matrix_side[i] (dd_math_dec2rad (* i 90)))
  24. )
  25. )
  26. # initialise this tile as front side
  27. (= this.side 0)
  28. (dd_matrix_copy_rs this this.matrix_side[this.side])
  29. # initialise tile's mesh
  30. (for (def int i 0) (< i 6) (= i (+ i 1))
  31. (group
  32. (dd_matrix_identity this.cubeMatrix[i])
  33. (if (< i 4)
  34. (group
  35. (dd_matrix_rotate_x this.cubeMatrix[i] (dd_math_dec2rad (* 90 i)))
  36. (dd_matrix_translatem this.cubeMatrix[i] 0 0 0.5)
  37. )
  38. (group
  39. (dd_matrix_rotate_y this.cubeMatrix[i] (dd_math_dec2rad (+ -90 (* (- i 4) 180))))
  40. (dd_matrix_translatem this.cubeMatrix[i] 0 0 0.5)
  41. )
  42. )
  43. (dd_meshTexture_create this.mesh[i])
  44. (if (> i 0)
  45. (group
  46. (this.mesh[i].set_primitive DD_PRIMITIVE_RECTANGLE)
  47. (this.mesh[i].set_colour (* i 0.1) (* i 0.05) (* i 0.2))
  48. )
  49. )
  50. )
  51. )
  52. (for (def int i 0) (< i 4) (= i (+ i 1))
  53. (group
  54. (dd_meshColour_create this.mageMesh[i])
  55. (dd_meshColour_create this.mageMeshSelected[i])
  56. )
  57. )
  58. (= this.isSelected 0)
  59. (dd_matrix_identity this.mageMatrix)
  60. (for (def int i 0) (< i 4) (= i (+ i 1))
  61. (= this.isMageArray[i] 0)
  62. )
  63. )
  64. )
  65. # animate the tiles to the current side
  66. (function update (group)
  67. (group
  68. (dd_matrix_approach_rs this this.matrix_side[this.side] 0.1)
  69. (if this.isSelected (dd_matrix_rotate_z this.mageMatrix (dd_math_dec2rad -1)))
  70. )
  71. )
  72. # draw tile based on its side
  73. (function draw (group)
  74. (group
  75. (glPushMatrix)
  76. (glMultMatrixf this)
  77. (for (def int i 0) (< i 6) (= i (+ i 1))
  78. (group
  79. (glPushMatrix)
  80. (glMultMatrixf this.cubeMatrix[i])
  81. (this.mesh[i].draw)
  82. (if (&& (< i 4) this.isMageArray[i])
  83. (group
  84. (if this.isSelected
  85. (group
  86. (glMultMatrixf this.mageMatrix)
  87. (this.mageMeshSelected[i].draw)
  88. )
  89. (this.mageMesh[i].draw)
  90. )
  91. )
  92. )
  93. (glPopMatrix)
  94. )
  95. )
  96. (glPopMatrix)
  97. )
  98. )
  99. # advance to the next side, optionally skip animating
  100. (function advanceSide (group int instant)
  101. (group
  102. # advance side within limits
  103. (= this.side (+ this.side 1))
  104. (if (> this.side 3)
  105. (group
  106. (= this.side 0)
  107. )
  108. )
  109. # if instant, do not animate
  110. (if instant
  111. (dd_matrix_copy_rs this this.matrix_side[this.side])
  112. )
  113. )
  114. ) # advanceSide
  115. (function select (group)
  116. (group
  117. (= this.isSelected 1)
  118. (dd_matrix_identity this.mageMatrix)
  119. )
  120. )
  121. (function deselect (group)
  122. (group
  123. (= this.isSelected 0)
  124. )
  125. )
  126. )
  127. )