Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "bee.h"
  2. #include "../enemy.h"
  3. #include "../game.h"
  4. #include "../hero.h"
  5. #include <stdlib.h>
  6. #include <math.h>
  7.  
  8. void beeStep(Bee* b);
  9. void beeDraw(Bee* b);
  10.  
  11. void createBee(int x, int y, int dir)
  12. {
  13.         int i;
  14.         for (i = 0; i < MAX_ENEMIES; i++) {
  15.                 if (enemies[i] == NULL) {
  16.                         Enemy* e = /*(Enemy*)*/malloc(sizeof *e);
  17.                         Bee* b = /*(Bee*)*/malloc(sizeof *b);
  18.                         b->id = i;                     
  19.                        
  20.                         b->x = x;
  21.                         b->y = y;
  22.                         b->xstart = b->x;
  23.                         b->ystart = b->y;
  24.                        
  25.                         b->hsp = 0;
  26.                         b->vsp = 0;
  27.                        
  28.                         b->timer = 0;
  29.                         b->imageIndex = 0;
  30.                         b->dir = 1;
  31.                         b->state = 0;
  32.                        
  33.                         b->hoverdir = 180;
  34.                        
  35.                         if (dir == 1) {
  36.                                 b->hoverdir = 0;
  37.                                 b->dir = -1;
  38.                         }              
  39.                        
  40.                         e->data = b;
  41.                         e->enemyStep = beeStep;
  42.                         e->enemyDraw = beeDraw;
  43.                         e->type = 24;
  44.                        
  45.                         enemies[i] = e;
  46.                         i = MAX_ENEMIES;
  47.                 }
  48.         }
  49. }
  50.  
  51. void beeStep(Bee* b)
  52. {
  53.         //Animate
  54.         {
  55.                 b->imageIndex += 0.33;
  56.                 if (b->imageIndex >= 3) {
  57.                         b->imageIndex -= 3;
  58.                 }
  59.         }
  60.        
  61.         //Mindless hovering
  62.         if (b->state == 0)
  63.         {
  64.                 b->hoverdir += 2.6;
  65.                 if (b->hoverdir >= 360) {
  66.                         b->hoverdir -= 360;
  67.                 }
  68.                
  69.                 b->dir = 1;
  70.                 if (b->hoverdir <= 180) {
  71.                         b->dir = -1;
  72.                 }
  73.                
  74.                 b->x = b->xstart + (20 * cos(b->hoverdir * 3.14159 /180));
  75.                
  76.                 //If player is within range
  77.                 Mask area;
  78.                 area.unused = area.circle = 0;
  79.                 area.x = b->x - 80;
  80.                 area.y = b->y;
  81.                 area.w = 200;
  82.                 area.h = 100;
  83.                
  84.                 if (checkCollision(area, getHeroMask())) {
  85.                         b->state = 1;
  86.                        
  87.                         b->dir = 1;
  88.                         if (b->x + 20 > herox) {
  89.                                 b->dir = -1;
  90.                         }
  91.                        
  92.                         b->hsp = -5.5 * b->dir;
  93.                        
  94.                         PHL_PlaySound(sounds[sndBee01], CHN_ENEMIES);
  95.                 }
  96.         }
  97.         //Fly backwards
  98.         else if (b->state == 1)
  99.         {
  100.                 b->hsp += 0.25 * b->dir;
  101.                
  102.                 if ((b->dir == 1 && b->hsp >= 0) || (b->dir == -1 && b->hsp <= 0)) {
  103.                         b->hsp = 0;
  104.                         b->state = 2;
  105.                         b->vsp = 3.75;
  106.                 }
  107.         }
  108.         //Fly downwards
  109.         else if (b->state == 2)
  110.         {
  111.                 b->vsp -= 0.1;
  112.                 if (b->vsp <= 0) {
  113.                         b->state = 3;
  114.                         b->vsp = 0;
  115.                        
  116.                         b->dir = 1;
  117.                         if (b->x + 20 > herox) {
  118.                                 b->dir = -1;
  119.                         }
  120.                         b->hsp = 3 * b->dir;
  121.                 }
  122.         }
  123.         //Fly diaganal
  124.         else if (b->state == 3)
  125.         {
  126.                 b->vsp -= 0.1;
  127.                
  128.                 if (b->vsp < -3) {
  129.                         b->vsp = -3;
  130.                 }
  131.  
  132.                 if (b->y <= b->ystart) {
  133.                         b->state = 4;
  134.                        
  135.                         b->vsp = 0;
  136.                         b->y = b->ystart;
  137.                        
  138.                         if (b->x < b->xstart) {
  139.                                 b->dir = 1;
  140.                         }else{
  141.                                 b->dir = -1;
  142.                         }
  143.                         b->hsp = b->dir;
  144.                 }
  145.         }
  146.         //Fly back to start
  147.         else if (b->state == 4)
  148.         {
  149.                 if ((b->dir == 1 && b->x >= b->xstart) || (b->dir == -1 && b->x <= b->xstart)) {
  150.                         b->state = 0;
  151.                         b->hsp = 0;
  152.                        
  153.                         b->hoverdir = 0;
  154.                 }
  155.         }
  156.        
  157.         //Movement
  158.         {
  159.                 b->x += b->hsp;
  160.                 b->y += b->vsp;
  161.         }
  162.        
  163.         //Setup Mask
  164.         Mask mask;
  165.         {
  166.                 mask.circle = mask.unused = 0;
  167.                 mask.w = 24;
  168.                 mask.h = 32;
  169.                 mask.y = b->y + 6;
  170.                 mask.x = b->x + 14;
  171.                 if (b->dir == -1) {
  172.                         mask.x = b->x + 2;
  173.                 }
  174.         }
  175.        
  176.         //Hit Player
  177.         {
  178.                 if (checkCollision(mask, getHeroMask())) {
  179.                         heroHit(15, mask.x + (mask.w / 2));
  180.                 }
  181.         }
  182.        
  183.         //Weapon Collision
  184.         {
  185.                 int i;
  186.                 for (i = 0; i < MAX_WEAPONS; i++) {
  187.                         if (weapons[i] != NULL) {
  188.                                 if (checkCollision(mask, weapons[i]->weaponMask)) {
  189.                                         weaponHit(weapons[i]);
  190.                                        
  191.                                         createEffect(2, b->x - 12, b->y - 6);
  192.                                         spawnCollectable(b->x + 20, b->y);
  193.                                         enemyDestroy(b->id);
  194.                                        
  195.                                         i = MAX_WEAPONS;
  196.                                 }
  197.                         }      
  198.                 }
  199.         }
  200.        
  201. }
  202.  
  203. void beeDraw(Bee* b)
  204. {
  205.         int cropx = 280;
  206.        
  207.         if (b->dir == -1) {
  208.                 cropx += 120;
  209.         }
  210.        
  211.         cropx += (int)b->imageIndex * 40;
  212.        
  213.         PHL_DrawSurfacePart(b->x, b->y, cropx, 480, 40, 40, images[imgEnemies]);
  214. }