player.dd 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. (include "player.ddh")
  2. (def extern int CARD_ROSE)
  3. (class_function Player void create (group)
  4. (group
  5. (= this.isUser 0)
  6. # player itself
  7. (this.mesh.load (asset "assets/character.asset" DD_PLY))
  8. (this.mesh.loadTexture (asset "assets/character_1.asset" DD_PLY))
  9. # individual cards
  10. (= this.cardsTotal 0)
  11. # font
  12. (this.font.setAlign DD_STRING3D_ALIGN_CENTER)
  13. (this.font.setColorFront 0.9 0.9 0.9)
  14. (this.font.setColorBack 0.3 0.3 0.3)
  15. # selection
  16. (= this.highlightCard -1)
  17. (= this.selectedCard -1)
  18. (= this.selectedCard2 0)
  19. (dd_matrix_identity this.selectedMatrix)
  20. (dd_matrix_translate this.selectedMatrix 0 2.01 2.6)
  21. (dd_matrix_rotate this.selectedMatrix 90 1 0 0)
  22. #(dd_matrix_rotate this.selectedMatrix (- (dd_math_rand 20) 10) 0 0 1)
  23. (= this.userLookingAtCard -1)
  24. )
  25. )
  26. (class_function Player void draw (group)
  27. (group
  28. (if this.isUser
  29. (this.drawUser)
  30. (this.drawAI)
  31. )
  32. # draw cards
  33. (for (def int i 0) (< i this.cardsTotal) (= i (+ i 1))
  34. (group
  35. # highlighted card
  36. (if (== i this.highlightCard)
  37. (group
  38. (dd_matrix_push)
  39. (this.cards[i].applyMatrix)
  40. (dd_scalef 1.15 1.15 1.15)
  41. (this.cards[i].drawRaw)
  42. (dd_translatef 0 0 0.01)
  43. (dd_scalef 0.1 0.1 0.1)
  44. (this.font.draw "confirm")
  45. (dd_matrix_pop)
  46. )
  47. # looking at card
  48. (&& (== i this.userLookingAtCard) (== this.highlightCard -1))
  49. (group
  50. (dd_matrix_push)
  51. (this.cards[i].applyMatrix)
  52. (dd_scalef 1.1 1.1 1.1)
  53. (this.cards[i].drawRaw)
  54. (dd_matrix_pop)
  55. )
  56. # else
  57. (this.cards[i].draw)
  58. )
  59. )
  60. )
  61. # draw selected card
  62. (if this.selectedCard2
  63. (this.selectedCard2.draw)
  64. )
  65. )
  66. )
  67. (class_function Player void drawUser (group)
  68. (group
  69. )
  70. )
  71. (class_function Player void drawLookingAtText (group)
  72. (group
  73. # (if (>= this.userLookingAtCard 0)
  74. # (group
  75. # (dd_matrix_push)
  76. # (dd_translatef 0 0 -5)
  77. # (this.cards[this.userLookingAtCard].drawText)
  78. # (dd_matrix_pop)
  79. # )
  80. # )
  81. )
  82. )
  83. (class_function Player void drawAI (group)
  84. (group
  85. # draw player
  86. (dd_matrix_push)
  87. (dd_multMatrixf this)
  88. (this.mesh.draw)
  89. (dd_translatef 0 2.2 0.3)
  90. (dd_scalef 0.5 0.5 0.5)
  91. (this.font.drawInt this.cardsTotal)
  92. (dd_matrix_pop)
  93. )
  94. )
  95. (class_function Player void clean (group)
  96. (group
  97. )
  98. )
  99. # add new card to the hand
  100. (def ref dd_meshColour newCardMesh)
  101. (def ref Card newCard)
  102. (class_function Player void addCard (group Card card)
  103. (group
  104. # apply limit
  105. (if (>= this.cardsTotal 10)
  106. (echo "CANNOT GIVE MORE CARDS")
  107. (group
  108. (= this.cards[this.cardsTotal] card)
  109. (= this.cardsTotal (+ this.cardsTotal 1))
  110. (if this.isUser
  111. (this.calculateCardPositionsUser)
  112. (this.calculateCardPositions)
  113. )
  114. )
  115. )
  116. )
  117. )
  118. (class_function Player void removeCard (group int index)
  119. (group
  120. # no cards to remove
  121. (if (<= this.cardsTotal 0)
  122. (echo "NO CARDS TO REMOVE")
  123. # index out of bounds
  124. (|| (< index 0) (>= index this.cardsTotal))
  125. (echo "INDEX OUT OF BOUNDS TO REMOVE")
  126. # remove given card
  127. (group
  128. (def int temp)
  129. # shuffle all cards in the array down one step
  130. (for (def int i index) (< i (- this.cardsTotal 1)) (= i (+ i 1))
  131. (group
  132. (= temp (+ i 1))
  133. (= this.cards[i] this.cards[temp])
  134. )
  135. )
  136. (= temp (- this.cardsTotal 1))
  137. (= this.cards[temp] 0)
  138. # update new number of cards
  139. (= this.cardsTotal (- this.cardsTotal 1))
  140. # re-position cards
  141. (if this.isUser
  142. (this.calculateCardPositionsUser)
  143. (this.calculateCardPositions)
  144. )
  145. )
  146. )
  147. )
  148. )
  149. (class_function Player void calculateCardPositions (group)
  150. (group
  151. (for (def int i 0) (< i this.cardsTotal) (= i (+ i 1))
  152. (group
  153. (dd_matrix_identity this.cards[i].target)
  154. (dd_matrix_mult this.cards[i].target this)
  155. (dd_matrix_translate this.cards[i].target 0 2.5 0.2)
  156. (dd_matrix_translate this.cards[i].target (- (* i 0.42) (/ (* (- this.cardsTotal 1) 0.42) 2)) 0 0)
  157. (dd_matrix_rotate this.cards[i].target 180 0 1 0)
  158. (if (== this.cards[i].id CARD_ROSE)
  159. (dd_matrix_rotate this.cards[i].target 180 0 1 0)
  160. )
  161. )
  162. )
  163. )
  164. )
  165. (class_function Player void calculateCardPositionsUser (group)
  166. (group
  167. (for (def int i 0) (< i this.cardsTotal) (= i (+ i 1))
  168. (group
  169. (dd_matrix_identity this.cards[i].target)
  170. (dd_matrix_mult this.cards[i].target this)
  171. (dd_matrix_translate this.cards[i].target 0 3.7 0)
  172. (dd_matrix_rotate this.cards[i].target (- (* -30 i) (* (/ (- this.cardsTotal 1) 2.0) -30)) 0 1 0)
  173. (dd_matrix_rotate this.cards[i].target -43 1 0 0)
  174. (dd_matrix_translate this.cards[i].target 0 0 -1.2)
  175. (dd_matrix_rotate this.cards[i].target 25 1 0 0)
  176. )
  177. )
  178. )
  179. )
  180. (class_function Player void highlightLookingCard (group)
  181. (group
  182. # player has cards
  183. (if (<= this.cardsTotal 0)
  184. (group
  185. (echo "cannot select card, no cards left")
  186. )
  187. # card is highlighted, either select it or remove highlight
  188. (>= this.highlightCard 0)
  189. (group
  190. # looking at highlighted card - select it
  191. (if (== this.highlightCard this.userLookingAtCard)
  192. (group
  193. (= this.selectedCard2 this.cards[this.highlightCard])
  194. (dd_matrix_identity this.selectedCard2.target)
  195. (dd_matrix_mult this.selectedCard2.target this.actual)
  196. (dd_matrix_mult this.selectedCard2.target this.selectedMatrix)
  197. # remove card from array
  198. (for (def int i (+ this.highlightCard 1)) (< i this.cardsTotal) (= i (+ i 1))
  199. (group
  200. (def int prevIndex (- i 1))
  201. (= this.cards[prevIndex] this.cards[i])
  202. )
  203. )
  204. (= this.cardsTotal (- this.cardsTotal 1))
  205. (if this.isUser
  206. (this.calculateCardPositionsUser)
  207. (this.calculateCardPositions)
  208. )
  209. )
  210. # looking elsewhere - de-highlight card
  211. (group
  212. (= this.highlightCard -1)
  213. )
  214. )
  215. )
  216. (&& (>= this.userLookingAtCard 0) (< this.userLookingAtCard this.cardsTotal))
  217. (group
  218. (= this.highlightCard this.userLookingAtCard)
  219. )
  220. )
  221. )
  222. )
  223. (class_function Player void planCard (group int index)
  224. (group
  225. (if (<= this.cardsTotal 0)
  226. (group
  227. (echo "cannot select card, no cards left")
  228. )
  229. (&& (< index 0) (>= index this.cardsTotal))
  230. (group
  231. (echo "cannot select card, out of bounds")
  232. )
  233. # else
  234. (group
  235. (= this.selectedCard2 this.cards[index])
  236. (dd_matrix_identity this.selectedCard2.target)
  237. (dd_matrix_mult this.selectedCard2.target this.actual)
  238. (dd_matrix_mult this.selectedCard2.target this.selectedMatrix)
  239. # remove card from array
  240. (for (def int i (+ index 1)) (< i this.cardsTotal) (= i (+ i 1))
  241. (group
  242. (def int prevIndex (- i 1))
  243. (= this.cards[prevIndex] this.cards[i])
  244. )
  245. )
  246. (= this.cardsTotal (- this.cardsTotal 1))
  247. (if this.isUser
  248. (this.calculateCardPositionsUser)
  249. (this.calculateCardPositions)
  250. )
  251. ) # selected card
  252. )
  253. )
  254. )
  255. (class_function Player void selectCard (group)
  256. (group
  257. (if (<= this.cardsTotal 0)
  258. (group
  259. (echo "cannot select card, no cards left")
  260. )
  261. (&& (< this.userLookingAtCard 0) (>= this.userLookingAtCard this.cardsTotal))
  262. (group
  263. (echo "cannot select card, out of bounds")
  264. )
  265. # else
  266. (group
  267. # get index, either by card being looked at, or random for AI
  268. (def int index)
  269. (if this.isUser
  270. (group
  271. (= index this.highlightCard)
  272. (= this.highlightCard -1)
  273. )
  274. (group
  275. (= index (dd_math_rand this.cardsTotal))
  276. )
  277. )
  278. (= this.selectedCard (dd_math_rand this.cardsTotal))
  279. (= this.selectedCard2 this.cards[index])
  280. (dd_matrix_identity this.selectedCard2.target)
  281. (dd_matrix_mult this.selectedCard2.target this.actual)
  282. (dd_matrix_mult this.selectedCard2.target this.selectedMatrix)
  283. # remove card from array
  284. (for (def int i (+ index 1)) (< i this.cardsTotal) (= i (+ i 1))
  285. (group
  286. (def int prevIndex (- i 1))
  287. (= this.cards[prevIndex] this.cards[i])
  288. )
  289. )
  290. (= this.cardsTotal (- this.cardsTotal 1))
  291. (if this.isUser
  292. (this.calculateCardPositionsUser)
  293. (this.calculateCardPositions)
  294. )
  295. ) # selected card
  296. )
  297. )
  298. )
  299. (class_function Player void deselectCard (group)
  300. (group
  301. (= this.selectedCard -1)
  302. (= this.selectedCard2 0)
  303. )
  304. )
  305. (class_function Player void lookAtCard (group float rotX float rotY)
  306. (group
  307. (def int lookingAtCard -1)
  308. # looking at the hand's layer (Y-axis)
  309. (if (&& (< rotY -40) (> rotY -60))
  310. (group
  311. # looking at the hand - figure out which card
  312. (if (&& (> rotX (* (/ (- this.cardsTotal 0.0) 2.0) -30)) (< rotX (* (/ (- this.cardsTotal 0) 2.0) 30)))
  313. (group
  314. (= lookingAtCard (/ (+ (/ (* this.cardsTotal 30) 2.0) rotX) 30))
  315. )
  316. )
  317. )
  318. ) # looking at hand's layer
  319. (= this.userLookingAtCard lookingAtCard)
  320. )
  321. ) # look at card