123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- # main world
- struct world_main : dd_world {
- # test sprite
- struct dd_sprite my_sprite;
- # catmul-rom points
- struct dd_vector2d p1;
- struct dd_vector2d p2;
- struct dd_vector2d p3;
- struct dd_vector2d p4;
- # curve counter
- int counter;
- # current point ( 0 = top left, 1 = top right, 2 = bottom right, 3 = bottom left )
- int point;
- # init sprite
- void init() {
- # start from point 0
- this.point = 0;
- this.counter = 100;
- # first curve has random values
- this.p1.x = 0;
- this.p1.y = DD_GAME_HEIGHT;
- this.p2.x = DD_GAME_WIDTH /2;
- this.p2.y = DD_GAME_HEIGHT/2;
- this.p3.x = DD_GAME_WIDTH *2/3;
- this.p3.y = DD_GAME_HEIGHT*2/3;
- this.p4.x = DD_GAME_WIDTH;
- this.p4.y = DD_GAME_HEIGHT;
- # first sprite is at the top
- this.my_sprite.x = 0;
- this.my_sprite.y = 0;
- this.my_sprite.load("dd_logo.png");
- };
- # update sprite's position
- override void update() {
- # update counter
- this.counter = this.counter +1;
- # counter finished, moved to next curve
- if (this.counter >= 100) {
- # reset counter and move to next point
- this.counter = 1;
- this.point = this.point +1;
- if (this.point >= 4) {
- this.point = 0;
- }
- # calculate the first three points of the curve
- this.p1.x = this.p2.x;
- this.p1.y = this.p2.y;
- this.p2.x = this.my_sprite.x +this.my_sprite.w/2;
- this.p2.y = this.my_sprite.y +this.my_sprite.h/2;
- if (this.point == 0) {
- this.p3.x = 0;
- this.p3.y = 0;
- this.p4.x = DD_GAME_WIDTH;
- this.p4.y = 0;
- }
- if (this.point == 1) {
- this.p3.x = DD_GAME_WIDTH;
- this.p3.y = 0;
- this.p4.x = DD_GAME_WIDTH;
- this.p4.y = DD_GAME_HEIGHT;
- }
- if (this.point == 2) {
- this.p3.x = DD_GAME_WIDTH;
- this.p3.y = DD_GAME_HEIGHT;
- this.p4.x = 0;
- this.p4.y = DD_GAME_HEIGHT;
- }
- if (this.point == 3) {
- this.p3.x = 0;
- this.p3.y = DD_GAME_HEIGHT;
- this.p4.x = 0;
- this.p4.y = 0;
- }
- } # counter finished
- # catmul-rom curve
- float counter_max;
- counter_max = 100;
- int tempx;
- tempx = ( (2 *this.p2.x) +(this.p3.x -this.p1.x) *(this.counter /counter_max)
- +(2 *this.p1.x -5 *this.p2.x +4 *this.p3.x -this.p4.x) *((this.counter /counter_max) *(this.counter /counter_max))
- +(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;
- int tempy;
- tempy = ( (2 *this.p2.y) +(this.p3.y -this.p1.y) *(this.counter /counter_max)
- +(2 *this.p1.y -5 *this.p2.y +4 *this.p3.y -this.p4.y) *((this.counter /counter_max) *(this.counter /counter_max))
- +(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;
- this.my_sprite.x = tempx -this.my_sprite.w/2;
- this.my_sprite.y = tempy -this.my_sprite.h/2;
- }; # update
- # draw sprite
- override void draw() {
- this.my_sprite.draw();
- };
- override void click() {
- this.counter = 100;
- };
- }; # world_main
|