new_project.dd 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # struct that handles rewards
  2. struct game_reward {
  3. # sprite, of what the reward looks like
  4. struct dd_sprite sprite;
  5. # interval is how often the reward appears
  6. # counter is how close it is to appearing
  7. # when counter >= interval, reward is active
  8. int counter;
  9. int interval;
  10. # init the reward as deactivated
  11. void init() {
  12. this.sprite.load("dd_logo.png");
  13. this.sprite.w = 50;
  14. this.sprite.h = 50;
  15. this.interval = 100;
  16. this.counter = 0;
  17. };
  18. # if deactivated, advance counter until it activates
  19. # in which case it moves to a random position on the screen
  20. void update() {
  21. if (this.counter < this.interval) {
  22. this.counter = this.counter +1;
  23. if (this.counter == this.interval) {
  24. this.sprite.x = dd_math_rand(DD_GAME_WIDTH -this.sprite.w);
  25. this.sprite.y = dd_math_rand(DD_GAME_HEIGHT -this.sprite.h);
  26. }
  27. }
  28. };
  29. # only draw reward if activated
  30. void draw() {
  31. if (this.counter >= this.interval) {
  32. this.sprite.draw();
  33. }
  34. };
  35. }; # reward struct
  36. # main world
  37. struct world_main : dd_world {
  38. # test sprite
  39. struct dd_sprite my_sprite;
  40. # bezier curve points
  41. struct dd_vector2d bezier1;
  42. struct dd_vector2d bezier2;
  43. struct dd_vector2d bezier3;
  44. # bezier counter
  45. int counter;
  46. # current point ( 0 = top left, 1 = top right, 2 = bottom right, 3 = bottom left )
  47. int point;
  48. # rewards
  49. struct game_reward reward1;
  50. # punishments
  51. struct dd_sprite punishment1;
  52. int punishment1_counter;
  53. int punishment_interval;
  54. # init sprite
  55. void init() {
  56. # start from point 0
  57. this.point = 0;
  58. this.counter = 100;
  59. # first bezier curve has random values
  60. this.bezier1.x = 0;
  61. this.bezier1.y = 0;
  62. this.bezier2.x = DD_GAME_WIDTH -10;
  63. this.bezier2.y = 0;
  64. this.bezier3.x = DD_GAME_WIDTH;
  65. this.bezier3.y = DD_GAME_HEIGHT;
  66. # first sprite is at the top
  67. this.my_sprite.load("dd_logo.png");
  68. this.punishment_interval = 150;
  69. this.punishment1.load("dd_logo2.png");
  70. this.punishment1.w = 75;
  71. this.punishment1.h = 75;
  72. this.punishment1_counter = 0;
  73. };
  74. # update sprite's position
  75. override void update() {
  76. # update counter
  77. this.counter = this.counter +1;
  78. # counter finished, moved to next curve
  79. if (this.counter > 100) {
  80. # reset counter and move to next point
  81. this.counter = 0;
  82. this.point = this.point +1;
  83. if (this.point >= 4) {
  84. this.point = 0;
  85. }
  86. # calculate the first two points of a bezier curve
  87. this.bezier1.x = this.my_sprite.x +this.my_sprite.w/2;
  88. this.bezier1.y = this.my_sprite.y +this.my_sprite.h/2;
  89. # the third point depends on the active point
  90. if (this.point == 0) {
  91. this.bezier3.x = 0;
  92. this.bezier3.y = 0;
  93. }
  94. if (this.point == 1) {
  95. this.bezier3.x = DD_GAME_WIDTH;
  96. this.bezier3.y = 0;
  97. }
  98. if (this.point == 2) {
  99. this.bezier3.x = DD_GAME_WIDTH;
  100. this.bezier3.y = DD_GAME_HEIGHT;
  101. }
  102. if (this.point == 3) {
  103. this.bezier3.x = 0;
  104. this.bezier3.y = DD_GAME_HEIGHT;
  105. }
  106. int halfNewx;
  107. int halfNewy;
  108. halfNewx = this.bezier1.x +(this.bezier3.x -this.bezier1.x) /2;
  109. halfNewy = this.bezier1.y +(this.bezier3.y -this.bezier1.y) /2;
  110. this.bezier2.x = halfNewx +(this.bezier2.x -halfNewx) /2;
  111. this.bezier2.y = halfNewy +(this.bezier2.y -halfNewy) /2;
  112. } # counter finished
  113. # calculate the interpolation of those points
  114. int finalx;
  115. finalx = dd_curves_bezier(this.bezier1.x, this.bezier2.x, this.bezier3.x, this.counter);
  116. int finaly;
  117. finaly = dd_curves_bezier(this.bezier1.y, this.bezier2.y, this.bezier3.y, this.counter);
  118. # apply result to sprite
  119. this.my_sprite.x = finalx -this.my_sprite.w/2;
  120. this.my_sprite.y = finaly -this.my_sprite.h/2;
  121. # reward
  122. this.reward1.update();
  123. # if reward is active, handle collision
  124. if (this.reward1.counter == this.reward1.interval) {
  125. if (this.my_sprite.collide(this.reward1.sprite)) {
  126. this.reward1.counter = 0;
  127. }
  128. }
  129. # punishment
  130. if (this.punishment1_counter < this.punishment_interval) {
  131. this.punishment1_counter = this.punishment1_counter +1;
  132. if (this.punishment1_counter == this.punishment_interval) {
  133. this.punishment1.x = dd_math_rand(DD_GAME_WIDTH -this.punishment1.w);
  134. this.punishment1.y = dd_math_rand(DD_GAME_HEIGHT -this.punishment1.h);
  135. }
  136. }
  137. # if punishment is active, handle collision
  138. if (this.punishment1_counter == this.punishment_interval) {
  139. if (this.my_sprite.collide(this.punishment1)) {
  140. this.punishment1_counter = 0;
  141. dd_open_world(world_main_create);
  142. #dd_game_exit();
  143. }
  144. }
  145. }; # update
  146. # draw sprite
  147. override void draw() {
  148. this.my_sprite.draw();
  149. this.reward1.draw();
  150. if (this.punishment1_counter == this.punishment_interval) {
  151. this.punishment1.draw();
  152. }
  153. };
  154. override void click(int x, int y) {
  155. if (x >= (DD_GAME_WIDTH/2)) {
  156. this.counter = 100;
  157. }
  158. };
  159. }; # world_main