Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "gyra.h"
  2. #include "../game.h"
  3. #include "../enemy.h"
  4. #include "../hero.h"
  5. #include <stdlib.h>
  6. #include <math.h>
  7.  
  8. void gyraStep(Gyra* g);
  9. void gyraDraw(Gyra* g);
  10. void gyraDestroy(Gyra* g);
  11.  
  12. int boss4flag = 21;
  13.  
  14. void createGyra(int x, int y)
  15. {
  16.         if (flags[boss4flag] == 0) { //have not yet beaten boss 4
  17.                 PHL_FreeSurface(images[imgBoss]);
  18.                 images[imgBoss] = PHL_LoadQDA("boss02.bmp");
  19.                
  20.                 int i;
  21.                 for (i = 0; i < MAX_ENEMIES; i++) {
  22.                         if (enemies[i] == NULL) {
  23.                                 //Boss start
  24.                                 setBossRoom();
  25.                                
  26.                                 Enemy* e = malloc(sizeof *e);
  27.                                 Gyra* g = malloc(sizeof *g);
  28.                                
  29.                                 g->id = i;                             
  30.                                 g->hp = 50;
  31.                                 //g->hp = 1;
  32.                                
  33.                                 g->x = x;
  34.                                 g->y = y;
  35.                                
  36.                                 g->targx = g->x;
  37.                                 g->targy = g->y;
  38.                                                                
  39.                                 g->state = 0;
  40.                                 g->timer = 260;
  41.                                 g->counter = 0;
  42.                                
  43.                                 g->invincible = 0;
  44.                                 g->dir = 0;            
  45.                                 g->imageIndex = 0;
  46.                                
  47.                                 //Setup
  48.                                 g->targx = g->x - 32;
  49.                                 g->targy = g->y + 64;
  50.                                 g->dir = 160;
  51.                                
  52.                                 g->x = g->targx + (80 * sin(g->dir * 3.14159 / 180));
  53.                                 g->y = g->targy + (80 * cos(g->dir * 3.14159 / 180));
  54.                                
  55.                                 int a;
  56.                                 for (a = 0; a < 144; a++) {
  57.                                         g->xrecord[a] = g->x;
  58.                                         g->yrecord[a] = g->y;
  59.                                 }
  60.                                
  61.                                 e->data = g;
  62.                                 e->enemyStep = gyraStep;
  63.                                 e->enemyDraw = gyraDraw;
  64.                                 e->type = 43;
  65.                                
  66.                                 enemies[i] = e;
  67.                                 i = MAX_ENEMIES;
  68.                                
  69.                         }
  70.                 }
  71.         }
  72. }
  73.  
  74. void gyraStep(Gyra* g)
  75. {      
  76.         //Animate
  77.         g->imageIndex += 0.1;
  78.         if (g->imageIndex >= 2) {
  79.                 g->imageIndex -= 2;
  80.         }
  81.        
  82.         int pattern[6] = {0, 1, 2, 1, 0, 2};
  83.        
  84.         //Move in a circle
  85.         if (g->state == 0)
  86.         {
  87.                 int len = 80;
  88.                
  89.                 //Setup
  90.                 if (g->timer == 0) {
  91.                         g->targx = g->x + (len * sin((g->dir + 90) * 3.14159 / 180));
  92.                         g->targy = g->y + (len * cos((g->dir + 90) * 3.14159 / 180));
  93.                         g->dir -= 90;
  94.                         g->timer = 250;
  95.                 }
  96.                
  97.                 g->dir += 1.5;
  98.                 if (g->dir >= 360) {
  99.                         g->dir -= 360;
  100.                 }
  101.                
  102.                 g->x = g->targx + (len * sin(g->dir * 3.14159 / 180));
  103.                 g->y = g->targy + (len * cos(g->dir * 3.14159 / 180));
  104.                
  105.                 g->timer -= 1;
  106.                 if (g->timer <= 0) {
  107.                         g->counter += 1;
  108.                         g->state = pattern[g->counter];
  109.                         /*     
  110.                         if (g->state != 1 && (g->x < 40 || g->x > 600 || g->y < 40 || g->y > 440)) {
  111.                                 g->state = 1;
  112.                         }*/
  113.                        
  114.                         g->timer = 0;
  115.                 }
  116.         }
  117.         //Attack
  118.         else if (g->state == 1)
  119.         {
  120.                 //Setup
  121.                 if (g->timer == 0) {
  122.                         g->targx = herox;
  123.                         g->targy = heroy + 20;
  124.                         g->dir += 90;
  125.                         g->timer = 320;
  126.                 }
  127.                
  128.                 double spd = 2;
  129.                 double diralt = 1.2;
  130.                
  131.                 double targdir = (atan2(g->targy - g->y, g->x - g->targx) * 180 / 3.14159) + 270;
  132.                
  133.                 targdir = g->dir - targdir;
  134.                 while (targdir >= 360) { targdir -= 360; }
  135.                 while (targdir < 0) { targdir += 360; }
  136.                
  137.                 if (targdir > 180) {
  138.                         g->dir += diralt;
  139.                 }
  140.                 if (targdir < 180) {
  141.                         g->dir -= diralt;
  142.                 }
  143.                
  144.                 //Movement
  145.                 g->x += spd * sin(g->dir * 3.14159 / 180);
  146.                 g->y += spd * cos(g->dir * 3.14159 / 180);
  147.                
  148.                 //Get (close) to targ coords
  149.                 g->timer -= 1;
  150.                 if (g->timer <= 0 || sqrt( pow(g->x - g->targx, 2) + pow(g->y - g->targy, 2) ) <= spd * 2) {
  151.                         g->counter += 1;
  152.                         if (g->counter >= 5) {
  153.                                 g->counter = 0;
  154.                         }
  155.                         g->state = pattern[g->counter];
  156.                         g->timer = 0;
  157.                 }
  158.         }
  159.         //Oval movement
  160.         else if (g->state == 2)
  161.         {
  162.                 int wlen = 120,
  163.                         hlen = 80;
  164.                        
  165.                 //Setup
  166.                 if (g->timer == 0) {
  167.                         g->targx = g->x + (wlen * sin((g->dir - 90) * 3.14159 / 180));
  168.                         g->targy = g->y + (hlen * cos((g->dir - 90) * 3.14159 / 180));
  169.                         g->dir += 90;
  170.                         g->timer = 200;
  171.                 }
  172.                
  173.                 g->dir -= 1.5;
  174.                 if (g->dir < 0) {
  175.                         g->dir += 360;
  176.                 }
  177.                
  178.                 g->x = g->targx + (wlen * sin(g->dir * 3.14159 / 180));
  179.                 g->y = g->targy + (hlen * cos(g->dir * 3.14159 / 180));
  180.                
  181.                 g->timer -= 1;
  182.                 if (g->timer <= 0) {
  183.                         g->counter += 1;
  184.                         if (g->counter >= 5) {
  185.                                 g->counter = 0;
  186.                         }
  187.                         g->state = pattern[g->counter];
  188.                         /*
  189.                         if (g->state != 1 && (g->x < 40 || g->x > 600 || g->y < 40 || g->y > 440)) {
  190.                                 g->state = 1;
  191.                                 g->timer = 0;
  192.                         }*/
  193.                 }
  194.         }
  195.        
  196.         //Death
  197.         if (g->state == 3)
  198.         {
  199.                 g->timer -= 1;
  200.                 if (g->timer <= 0) {
  201.                         g->timer = 12;
  202.                        
  203.                         int cx = g->xrecord[128 - (g->counter * 16)],
  204.                                 cy = g->yrecord[128 - (g->counter * 16)];
  205.                        
  206.                         createEffect(2, cx - 32, cy - 32);
  207.                        
  208.                         g->counter += 1;
  209.                         if (g->counter == 9) {
  210.                                 gyraDestroy(g);
  211.                         }
  212.                 }
  213.         }else{
  214.                 //Update tail record
  215.                 int i;
  216.                 for (i = 142; i >= 0; i--) {
  217.                         g->xrecord[i + 1] = g->xrecord[i];
  218.                         g->yrecord[i + 1] = g->yrecord[i];
  219.                 }
  220.                 g->xrecord[0] = g->x;
  221.                 g->yrecord[0] = g->y;
  222.                
  223.                 //for (i = 8; i >= 0; i--) {
  224.                 for (i = 0; i <= 8; i++) {
  225.                         int cx = g->x, cy = g->y;
  226.                        
  227.                         if (i != 0) {
  228.                                 cx = g->xrecord[i * 16];
  229.                                 cy = g->yrecord[i * 16];
  230.                         }
  231.                                
  232.                         Mask mask;
  233.                         mask.unused = 0;
  234.                         mask.circle = 1;
  235.                         mask.x = cx;
  236.                         mask.y = cy;
  237.                         mask.w = mask.h = 28;
  238.                        
  239.                         int a;
  240.                         for (a = 0; a < MAX_WEAPONS; a++) {
  241.                                 if (weapons[a] != NULL) {
  242.                                         if (weapons[a]->cooldown == 0) {
  243.                                                 if (checkCollision(mask, weapons[a]->weaponMask)) {
  244.                                                         g->invincible = -15;
  245.                                                         weaponHit(weapons[a]);
  246.                                                        
  247.                                                         if (i == 8) {
  248.                                                                 g->hp -= 1;
  249.                                                                 g->invincible = 15;
  250.                                                         }else{
  251.                                                                 PHL_PlaySound(sounds[sndHit03], CHN_WEAPONS);
  252.                                                         }
  253.                                                        
  254.                                                         a = MAX_WEAPONS;
  255.                                                 }
  256.                                         }
  257.                                 }      
  258.                         }
  259.                        
  260.                         //Hit player
  261.                         if (checkCollision(getHeroMask(), mask)) {
  262.                                 if (heroHit(30, mask.x) && i == 0) {
  263.                                         heroPoison();
  264.                                 }
  265.                         }
  266.                 }
  267.                                
  268.                 //Death
  269.                 if (g->hp <= 0) {
  270.                         g->state = 3;
  271.                         g->timer = 0;
  272.                         g->counter = 0;
  273.                         g->invincible = 200;
  274.                 }
  275.         }
  276.        
  277.         if (g->invincible > 0) {
  278.                 g->invincible -= 1;
  279.         }
  280.         if (g->invincible < 0) {
  281.                 g->invincible += 1;
  282.         }
  283.        
  284. }
  285.  
  286. void gyraDraw(Gyra* g)
  287. {
  288.         if (g->invincible <= 0 || g->invincible % 2 == 0) {
  289.                 //Draw Tail Tip
  290.                 if (g->state != 3 || g->counter <= 0) {
  291.                         PHL_DrawSurfacePart(g->xrecord[126] - 40, g->yrecord[126] - 40, 320 + ((int)g->imageIndex * 80), 0, 80, 80, images[imgBoss]);
  292.                 }
  293.                
  294.                 //Draw Tail
  295.                 int i;
  296.                 for (i = 7; i > 0; i--) {
  297.                         if (g->state != 3 || g->counter <= (7 - i) + 1) {
  298.                                 PHL_DrawSurfacePart(g->xrecord[i * 16] - 40, g->yrecord[i * 16] - 40, 160 + ((int)g->imageIndex * 80), 0, 80, 80, images[imgBoss]);
  299.                         }
  300.                 }
  301.                
  302.                 //Draw Head
  303.                 PHL_DrawSurfacePart(g->x - 40, g->y - 40, (int)g->imageIndex * 80, 0, 80, 80, images[imgBoss]);
  304.         }
  305.        
  306.         //PHL_DrawRect(g->targx, g->targy, 10, 10, PHL_NewRGB(255, 255, 255)); 
  307.         //heroAmmo = g->state;
  308.        
  309.         /*
  310.         int i;
  311.         for (i = 8; i >= 0; i--) {
  312.                 int cx = g->x, cy = g->y;
  313.                
  314.                 if (i != 0) {
  315.                         cx = g->xrecord[i * 16];
  316.                         cy = g->yrecord[i * 16];
  317.                 }
  318.                        
  319.                 PHL_DrawRect(cx, cy, 10, 10, PHL_NewRGB(255, 255, 255));
  320.         }
  321.         */
  322. }
  323.  
  324. void gyraDestroy(Gyra* g)
  325. {
  326.         enemyDestroy(g->id);
  327.         bossDefeatedFlag = 1;
  328.         roomSecret = 1;
  329.  
  330.         flags[boss4flag] = 1;
  331.         PHL_StopMusic();
  332. }