Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "skull.h"
  2. #include "../enemy.h"
  3. #include "../PHL.h"
  4. #include "../game.h"
  5. #include "../hero.h"
  6. #include <stdlib.h>
  7. #include <math.h>
  8.  
  9. void skullStep(Skull* s);
  10. void skullDraw(Skull* s);
  11.  
  12. void createSkull(int x, int y)
  13. {
  14.         int i;
  15.         for (i = 0; i < MAX_ENEMIES; i++) {
  16.                 if (enemies[i] == NULL) {
  17.                         Enemy* e = malloc(sizeof *e);
  18.                         Skull* s = malloc(sizeof *s);
  19.                         s->id = i;
  20.                        
  21.                         //X/Y in center of sprite
  22.                         s->x = x + 20;
  23.                         s->y = y + 20;
  24.                         s->yoffset = 0;
  25.                        
  26.                         s->rot = 0;
  27.                         s->state = 0;
  28.                         s->timer = 0;
  29.                         s->imageIndex = 0;
  30.                         s->dir = 0;
  31.  
  32.                         e->data = s;
  33.                         e->enemyStep = skullStep;
  34.                         e->enemyDraw = skullDraw;
  35.                         e->type = 12;
  36.                        
  37.                         enemies[i] = e;
  38.                         i = MAX_ENEMIES;
  39.                 }
  40.         }
  41. }
  42.  
  43. void skullStep(Skull* s)
  44. {
  45.         double imageSpeed = 0;
  46.        
  47.         //Wait
  48.         if (s->state == 0)
  49.         {
  50.                 imageSpeed = 0.2;
  51.                
  52.                 if (s->timer > 0) {
  53.                         s->timer -= 1;
  54.                 }else{         
  55.                         Mask tempmask;
  56.                        
  57.                         tempmask.unused = tempmask.circle = 0;
  58.                         tempmask.x = s->x - 100;
  59.                         tempmask.y = s->y - 100;
  60.                         tempmask.w = tempmask.h = 200;
  61.                        
  62.                         if (checkCollisionXY(tempmask, herox, heroy + 20)) {                   
  63.                        
  64.                         //Calculate distance
  65.                         //int dis = sqrt(pow(s->x - herox, 2) + pow(s->y - (heroy + 20), 2));
  66.                         //if (dis <= 100) {
  67.                                 s->state = 1;
  68.                                 //s->dir = (rand() % 8) * 45;
  69.                                 s->dir = (rand() % 360) + 1;
  70.                                 PHL_PlaySound(sounds[sndPi08], CHN_ENEMIES);
  71.                                 s->timer = 130;
  72.                         }
  73.                 }
  74.         }
  75.        
  76.         //Chase
  77.         else if (s->state == 1)
  78.         {
  79.                 imageSpeed = 0.3;              
  80.                
  81.                 int spd = 2;
  82.                 s->x += (spd * cos(s->dir * 3.14159 / 180));
  83.                 s->y += (spd * sin(s->dir * 3.14159 / 180));
  84.                
  85.                 double herodir = ((atan2((heroy + 20) - s->y, herox - s->x) * 180) / 3.14159);
  86.                 if (herodir >= 360) {
  87.                         herodir -= 360;
  88.                 }
  89.                 if (herodir < 0) {
  90.                         herodir += 360;
  91.                 }
  92.                
  93.                 double tempdir = s->dir - herodir;
  94.                 if (tempdir < 0) {
  95.                         tempdir += 360;
  96.                 }
  97.                
  98.                 if (tempdir < 180) {
  99.                         s->dir -= 2;
  100.                 }else{
  101.                         s->dir += 2;
  102.                 }
  103.                 if (s->dir >= 360) {
  104.                         s->dir -= 360;
  105.                 }
  106.                 if (s->dir < 0) {
  107.                         s->dir += 360;
  108.                 }
  109.                
  110.                 s->timer -= 1;
  111.                 if (s->timer <= 0) {
  112.                         s->state = 0;
  113.                         s->timer = 10;
  114.                 }
  115.         }
  116.        
  117.         //Animate
  118.         {
  119.                 s->imageIndex += imageSpeed;
  120.                 if (s->imageIndex >= 4) {
  121.                         s->imageIndex -= 4;
  122.                 }
  123.         }
  124.        
  125.         //Hover offset
  126.         {
  127.                 s->rot += 5;
  128.                 if (s->rot >= 360) {
  129.                         s->rot -= 360;
  130.                 }
  131.                 s->yoffset = (5 * sin(s->rot * 3.14159 / 180));
  132.         }
  133.        
  134.         //Setup Mask
  135.         Mask mask;
  136.         {
  137.                 mask.unused = 0;
  138.                 mask.circle = 1;
  139.                 mask.x = s->x;
  140.                 mask.y = s->y;
  141.                 mask.w = mask.h = 10;
  142.         }      
  143.        
  144.         //Hero collision
  145.         {
  146.                 if (checkCollision(mask, getHeroMask())) {
  147.                         heroHit(15, mask.x);
  148.                 }
  149.         }
  150.        
  151.         //Weapon collision
  152.         {
  153.                 int i;
  154.                 for (i = 0; i < MAX_WEAPONS; i++) {
  155.                         if (weapons[i] != NULL) {
  156.                                 if (checkCollision(mask, weapons[i]->weaponMask)) {
  157.                                         weaponHit(weapons[i]);
  158.                                        
  159.                                         createEffect(2, s->x - 32, s->y - 32);
  160.                                         spawnCollectable(s->x, s->y - 20);
  161.                                         enemyDestroy(s->id);
  162.                                        
  163.                                         i = MAX_WEAPONS;
  164.                                 }
  165.                         }      
  166.                 }
  167.         }
  168.        
  169. }
  170.  
  171. void skullDraw(Skull* s)
  172. {
  173.         PHL_DrawSurfacePart(s->x - 20, s->y + s->yoffset - 20, 480 + ((int)s->imageIndex * 40), 40, 40, 40, images[imgEnemies]);
  174. }