new_project-catmulrom.dd 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # main world
  2. struct world_main : dd_world {
  3. # test sprite
  4. struct dd_sprite my_sprite;
  5. # catmul-rom points
  6. struct dd_vector2d p1;
  7. struct dd_vector2d p2;
  8. struct dd_vector2d p3;
  9. struct dd_vector2d p4;
  10. # curve counter
  11. int counter;
  12. # current point ( 0 = top left, 1 = top right, 2 = bottom right, 3 = bottom left )
  13. int point;
  14. # init sprite
  15. void init() {
  16. # start from point 0
  17. this.point = 0;
  18. this.counter = 100;
  19. # first curve has random values
  20. this.p1.x = 0;
  21. this.p1.y = DD_GAME_HEIGHT;
  22. this.p2.x = DD_GAME_WIDTH /2;
  23. this.p2.y = DD_GAME_HEIGHT/2;
  24. this.p3.x = DD_GAME_WIDTH *2/3;
  25. this.p3.y = DD_GAME_HEIGHT*2/3;
  26. this.p4.x = DD_GAME_WIDTH;
  27. this.p4.y = DD_GAME_HEIGHT;
  28. # first sprite is at the top
  29. this.my_sprite.name = "dd_logo.png";
  30. this.my_sprite.x = 0;
  31. this.my_sprite.y = 0;
  32. this.my_sprite.load();
  33. };
  34. # update sprite's position
  35. override void update() {
  36. # update counter
  37. this.counter = this.counter +1;
  38. # counter finished, moved to next curve
  39. if (this.counter >= 100) {
  40. # reset counter and move to next point
  41. this.counter = 1;
  42. this.point = this.point +1;
  43. if (this.point >= 4) {
  44. this.point = 0;
  45. }
  46. # calculate the first three points of the curve
  47. this.p1.x = this.p2.x;
  48. this.p1.y = this.p2.y;
  49. this.p2.x = this.my_sprite.x +this.my_sprite.w/2;
  50. this.p2.y = this.my_sprite.y +this.my_sprite.h/2;
  51. if (this.point == 0) {
  52. this.p3.x = 0;
  53. this.p3.y = 0;
  54. this.p4.x = DD_GAME_WIDTH;
  55. this.p4.y = 0;
  56. }
  57. if (this.point == 1) {
  58. this.p3.x = DD_GAME_WIDTH;
  59. this.p3.y = 0;
  60. this.p4.x = DD_GAME_WIDTH;
  61. this.p4.y = DD_GAME_HEIGHT;
  62. }
  63. if (this.point == 2) {
  64. this.p3.x = DD_GAME_WIDTH;
  65. this.p3.y = DD_GAME_HEIGHT;
  66. this.p4.x = 0;
  67. this.p4.y = DD_GAME_HEIGHT;
  68. }
  69. if (this.point == 3) {
  70. this.p3.x = 0;
  71. this.p3.y = DD_GAME_HEIGHT;
  72. this.p4.x = 0;
  73. this.p4.y = 0;
  74. }
  75. } # counter finished
  76. # catmul-rom curve
  77. float counter_max;
  78. counter_max = 100;
  79. int tempx;
  80. tempx = ( (2 *this.p2.x) +(this.p3.x -this.p1.x) *(this.counter /counter_max)
  81. +(2 *this.p1.x -5 *this.p2.x +4 *this.p3.x -this.p4.x) *((this.counter /counter_max) *(this.counter /counter_max))
  82. +(3 *this.p2.x -this.p1.x -3 *this.p3.x +this.p4.x) *((this.counter /counter_max) *(this.counter /counter_max) *(this.counter /counter_max))) /2;
  83. int tempy;
  84. tempy = ( (2 *this.p2.y) +(this.p3.y -this.p1.y) *(this.counter /counter_max)
  85. +(2 *this.p1.y -5 *this.p2.y +4 *this.p3.y -this.p4.y) *((this.counter /counter_max) *(this.counter /counter_max))
  86. +(3 *this.p2.y -this.p1.y -3 *this.p3.y +this.p4.y) *((this.counter /counter_max) *(this.counter /counter_max) *(this.counter /counter_max))) /2;
  87. this.my_sprite.x = tempx -this.my_sprite.w/2;
  88. this.my_sprite.y = tempy -this.my_sprite.h/2;
  89. }; # update
  90. # draw sprite
  91. override void draw() {
  92. this.my_sprite.draw();
  93. };
  94. override void click() {
  95. this.counter = 100;
  96. };
  97. }; # world_main