Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "bat.h"
  2. #include "../game.h"
  3. #include "../PHL.h"
  4. #include "../hero.h"
  5. #include <stdlib.h>
  6. #include <math.h>
  7.  
  8. void batStep(Bat* b);
  9. void batDraw(Bat* b);
  10.  
  11. void createBat(int x, int y, int type)
  12. {
  13.         int i;
  14.         for (i = 0; i < MAX_ENEMIES; i++) {
  15.                 if (enemies[i] == NULL) {
  16.                         Enemy* result = /*(Enemy*)*/malloc(sizeof *result);
  17.                         Bat* b = /*(Bat*)*/malloc(sizeof *b);
  18.                         b->id = i;
  19.                        
  20.                         b->x = b->xstart = x;
  21.                         b->y = b->ystart = y;
  22.                         b->type = type;
  23.                        
  24.                         b->imageIndex = 5;
  25.                         b->counter = 0;
  26.                         b->timer = 0;
  27.                         b->state = 0;
  28.                         b->dir = 1;
  29.  
  30.                         result->data = b;
  31.                         result->enemyStep = batStep;
  32.                         result->enemyDraw = batDraw;
  33.                         result->type = 1;
  34.                        
  35.                         enemies[i] = result;
  36.                         i = MAX_ENEMIES;
  37.                 }
  38.         }
  39. }
  40.  
  41. void batStep(Bat* b)
  42. {      
  43.         //Wait
  44.         if (b->state == 0)
  45.         {
  46.                 //Animate
  47.                 {
  48.                         b->imageIndex = 5;
  49.                 }
  50.                
  51.                 //wait for hero to get near
  52.                 {
  53.                         if (b->timer <= 0) {
  54.                                 Mask area;
  55.                                 area.circle = 0;
  56.                                 area.unused = 0;
  57.                                 area.x = b->xstart - 60;
  58.                                 area.y = b->ystart;
  59.                                 area.w = 160; area.h = 100;
  60.                                
  61.                                 if (checkCollisionXY(area, herox, heroy + 20)) {
  62.                                         PHL_PlaySound(sounds[sndPi07], CHN_ENEMIES);
  63.                                         b->state = 1;
  64.                                         b->timer = 270;
  65.                                         if (b->type == 1) {
  66.                                                 b->counter = 1;
  67.                                                 if (herox < b->x + 20) {
  68.                                                         b->dir = -1;
  69.                                                 }else{
  70.                                                         b->dir = 1;
  71.                                                 }
  72.                                         }
  73.                                 }
  74.                         }else{
  75.                                 b->timer -= 1;
  76.                         }
  77.                 }
  78.         }
  79.         //Fly
  80.         else if (b->state == 1)
  81.         {
  82.                 //Animate
  83.                 {
  84.                         b->imageIndex += 0.25;
  85.                         if (b->imageIndex >= 5) {
  86.                                 b->imageIndex -= 5;
  87.                         }
  88.                 }
  89.                
  90.                 //Rotation angle
  91.                 {
  92.                         b->timer += 4;
  93.                         if (b->timer >= 360) {
  94.                                 b->timer -= 360;
  95.                         }
  96.                 }
  97.                
  98.                 //Movement
  99.                 {
  100.                         b->y = b->ystart + 30 + (30 * sin(b->timer * 3.14159 / 180));
  101.                         //Red bat
  102.                         if (b->type == 1) {
  103.                                 b->x += 2 * b->dir;
  104.                         }
  105.                 }
  106.                
  107.                 //Return to perch
  108.                 {
  109.                         if (b->timer == 270) {
  110.                                 if (b->type == 1 && b->counter > 0) {
  111.                                         b->dir *= -1;
  112.                                         b->timer = 270;
  113.                                         b->counter -= 1;
  114.                                 }else{
  115.                                         b->state = 0;
  116.                                         b->timer = 70;
  117.                                 }
  118.                         }
  119.                 }
  120.         }
  121.        
  122.         //Setup Mask
  123.         Mask mask;
  124.         {
  125.                 mask.circle = mask.unused = 0;
  126.                 mask.w = 32;
  127.                 mask.h = 28;
  128.                 mask.x = b->x + ((40 - mask.w) / 2);
  129.                 mask.y = b->y;
  130.         }
  131.        
  132.         //Hit Player
  133.         {
  134.                 if (checkCollision(mask, heroMask)) {
  135.                         heroHit(10, mask.x + (mask.w / 2));
  136.                 }
  137.         }
  138.        
  139.         //Weapon collision
  140.         {
  141.                 int i;
  142.                 for (i = 0; i < MAX_WEAPONS; i++) {
  143.                         if (weapons[i] != NULL) {
  144.                                 if (checkCollision(mask, weapons[i]->weaponMask)) {
  145.                                         weaponHit(weapons[i]);
  146.                                         //Death
  147.                                         createEffect(2, b->x - 12, b->y - 6);
  148.                                         spawnCollectable(b->x + 20, b->y);
  149.                                         enemyDestroy(b->id);
  150.  
  151.                                         i = MAX_WEAPONS;
  152.                                 }
  153.                         }      
  154.                 }
  155.         }
  156.        
  157. }
  158.  
  159. void batDraw(Bat* b)
  160. {
  161.         int cropX = 0,
  162.                 cropY = 120;
  163.        
  164.         if (b->type == 1) {
  165.                 cropX = 400;
  166.                 cropY = 280;
  167.         }
  168.        
  169.         cropX += (int)b->imageIndex * 40;
  170.        
  171.         PHL_DrawSurfacePart(b->x, b->y - 4, cropX, cropY, 40, 40, images[imgEnemies]);
  172. }