new_project-catmulrom.dd 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.x = 0;
  30. this.my_sprite.y = 0;
  31. this.my_sprite.load("dd_logo.png");
  32. };
  33. # update sprite's position
  34. override void update() {
  35. # update counter
  36. this.counter = this.counter +1;
  37. # counter finished, moved to next curve
  38. if (this.counter >= 100) {
  39. # reset counter and move to next point
  40. this.counter = 1;
  41. this.point = this.point +1;
  42. if (this.point >= 4) {
  43. this.point = 0;
  44. }
  45. # calculate the first three points of the curve
  46. this.p1.x = this.p2.x;
  47. this.p1.y = this.p2.y;
  48. this.p2.x = this.my_sprite.x +this.my_sprite.w/2;
  49. this.p2.y = this.my_sprite.y +this.my_sprite.h/2;
  50. if (this.point == 0) {
  51. this.p3.x = 0;
  52. this.p3.y = 0;
  53. this.p4.x = DD_GAME_WIDTH;
  54. this.p4.y = 0;
  55. }
  56. if (this.point == 1) {
  57. this.p3.x = DD_GAME_WIDTH;
  58. this.p3.y = 0;
  59. this.p4.x = DD_GAME_WIDTH;
  60. this.p4.y = DD_GAME_HEIGHT;
  61. }
  62. if (this.point == 2) {
  63. this.p3.x = DD_GAME_WIDTH;
  64. this.p3.y = DD_GAME_HEIGHT;
  65. this.p4.x = 0;
  66. this.p4.y = DD_GAME_HEIGHT;
  67. }
  68. if (this.point == 3) {
  69. this.p3.x = 0;
  70. this.p3.y = DD_GAME_HEIGHT;
  71. this.p4.x = 0;
  72. this.p4.y = 0;
  73. }
  74. } # counter finished
  75. # catmul-rom curve
  76. float counter_max;
  77. counter_max = 100;
  78. int tempx;
  79. tempx = ( (2 *this.p2.x) +(this.p3.x -this.p1.x) *(this.counter /counter_max)
  80. +(2 *this.p1.x -5 *this.p2.x +4 *this.p3.x -this.p4.x) *((this.counter /counter_max) *(this.counter /counter_max))
  81. +(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;
  82. int tempy;
  83. tempy = ( (2 *this.p2.y) +(this.p3.y -this.p1.y) *(this.counter /counter_max)
  84. +(2 *this.p1.y -5 *this.p2.y +4 *this.p3.y -this.p4.y) *((this.counter /counter_max) *(this.counter /counter_max))
  85. +(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;
  86. this.my_sprite.x = tempx -this.my_sprite.w/2;
  87. this.my_sprite.y = tempy -this.my_sprite.h/2;
  88. }; # update
  89. # draw sprite
  90. override void draw() {
  91. this.my_sprite.draw();
  92. };
  93. override void click() {
  94. this.counter = 100;
  95. };
  96. }; # world_main