Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "jellyfish.h"
  2. #include "../enemy.h"
  3. #include "../game.h"
  4. #include "../hero.h"
  5. #include <stdlib.h>
  6. #include <math.h>
  7.  
  8. void jellyfishStep(Jellyfish* j);
  9. void jellyfishDraw(Jellyfish* j);
  10.  
  11. void createJellyfish(int x, int y)
  12. {
  13.         int i;
  14.         for (i = 0; i < MAX_ENEMIES; i++) {
  15.                 if (enemies[i] == NULL) {
  16.                         Enemy* e = malloc(sizeof *e);
  17.                         Jellyfish* j = malloc(sizeof *j);
  18.                         j->id = i;
  19.                        
  20.                         j->x = x;
  21.                         j->y = j->ystart = y;
  22.                         j->ystart += 20;
  23.                        
  24.                         j->spd = 0;
  25.                         j->angle = 0;
  26.                        
  27.                         j->state = 0;
  28.                         j->imageIndex = 0;
  29.                        
  30.                         e->data = j;
  31.                         e->enemyStep = jellyfishStep;
  32.                         e->enemyDraw = jellyfishDraw;
  33.                         e->type = 20;
  34.                        
  35.                         enemies[i] = e;
  36.                         i = MAX_ENEMIES;
  37.                 }
  38.         }
  39. }
  40.  
  41. void jellyfishStep(Jellyfish* j)
  42. {
  43.         Mask mask;
  44.         mask.unused = mask.circle = 0;
  45.         mask.w = mask.h = 30;
  46.         mask.x = j->x + 20 - (mask.w / 2);
  47.         mask.y = j->y + 20 - (mask.h / 2);
  48.        
  49.         //Idle float
  50.         if (j->state == 0)
  51.         {
  52.                 //Animate
  53.                 j->imageIndex += 0.06;
  54.                 if (j->imageIndex >= 4) {
  55.                         j->imageIndex -= 4;
  56.                 }
  57.                
  58.                 //Movement
  59.                 j->angle += 2.5;
  60.                 if (j->angle >= 360) { j->angle -= 360; }
  61.                 j->y = j->ystart + (20 * sin(j->angle * 3.14159 / 180));
  62.  
  63.                 //Update mask
  64.                 mask.y = j->y + 20 - (mask.h / 2);
  65.                
  66.                 //if player is close enough
  67.                 Mask area;
  68.                 area.unused = area.circle = 0;
  69.                 area.w = area.h = 160;
  70.                 area.x = j->x - 60;
  71.                 area.y = j->y - 60;
  72.                
  73.                 if (checkCollision(area, getHeroMask()) == 1) {
  74.                         j->state = 1;
  75.                         j->spd = 0;
  76.                 }
  77.         }
  78.         //Attack
  79.         if (j->state == 1)
  80.         {
  81.                 //Setup
  82.                 if (j->spd == 0) {
  83.                         PHL_PlaySound(sounds[sndPi02], CHN_ENEMIES);
  84.                         j->spd = 3;
  85.                        
  86.                         //Move Right
  87.                         if (herox > j->x + 20) {
  88.                                 //Move Up
  89.                                 if (heroy < j->y) {
  90.                                         j->angle = 135;
  91.                                 }
  92.                                 //Move Down
  93.                                 else {
  94.                                         j->angle = 45;
  95.                                 }
  96.                         }
  97.                         //Move Left
  98.                         else{
  99.                                 //Move Up
  100.                                 if (heroy < j->y) {
  101.                                         j->angle = 225;
  102.                                 }
  103.                                 //Move Down
  104.                                 else {
  105.                                         j->angle = 315;
  106.                                 }
  107.                         }
  108.                 }
  109.                
  110.                 //Movement
  111.                 j->x += (j->spd) * sin(j->angle * 3.14159 / 180);
  112.                 j->y += (j->spd) * cos(j->angle * 3.14159 / 180);
  113.                
  114.                 //Slow down
  115.                 j->spd -= 0.075;
  116.                 if (j->spd <= 0) {
  117.                         j->spd = 0;
  118.                         j->state = 2;
  119.                 }
  120.         }
  121.         //Stablize
  122.         if (j->state == 2)
  123.         {
  124.                 //Setup
  125.                 if (j->spd == 0) {
  126.                         j->spd = 1;
  127.                         j->ystart = j->y - 20;
  128.                         j->angle = 80;
  129.                 }
  130.                
  131.                 //Movement
  132.                 j->angle += 2.5;
  133.                 if (j->angle >= 360) { j->angle -= 360; }
  134.                 j->y = j->ystart + (20 * sin(j->angle * 3.14159 / 180));
  135.                
  136.                
  137.                 if (j->angle >= 180) {
  138.                         j->state = 0;
  139.                         j->ystart = j->y - 20;
  140.                         j->angle = 100;
  141.                 }
  142.         }
  143.        
  144.         //Update Mask
  145.         mask.x = j->x + 20 - (mask.w / 2);
  146.         mask.y = j->y + 20 - (mask.h / 2);
  147.        
  148.         //Collide with hero
  149.         if (checkCollision(mask, getHeroMask())) {
  150.                 heroHit(15, j->x + 20);
  151.         }
  152.        
  153.         //Sword collision
  154.         int i;
  155.         for (i = 0; i < MAX_WEAPONS; i++) {
  156.                 if (weapons[i] != NULL) {
  157.                         if (checkCollision(mask, weapons[i]->weaponMask)) {
  158.                                 spawnCollectable(j->x + 20, j->y);                             
  159.                                 weaponHit(weapons[i]);
  160.                                
  161.                                 createEffect(2, j->x - 12, j->y - 12);
  162.                                 enemyDestroy(j->id);
  163.                                
  164.                                 i = MAX_WEAPONS;
  165.                         }
  166.                 }      
  167.         }
  168. }
  169.  
  170. void jellyfishDraw(Jellyfish* j)
  171. {
  172.         int frame = 0;
  173.        
  174.         //if (j->state == 0) {
  175.                 int animation[4] = { 0, 1, 0, 2};
  176.                 frame = animation[(int)j->imageIndex];
  177.         //}
  178.        
  179.         if (j->state == 1) {
  180.                 if (j->angle == 135) {
  181.                         frame = 3;
  182.                 }
  183.                 else if (j->angle == 225) {
  184.                         frame = 4;
  185.                 }
  186.                 else if (j->angle == 315) {
  187.                         frame = 5;
  188.                 }
  189.                 else {
  190.                         frame = 6;
  191.                 }
  192.         }
  193.        
  194.         PHL_DrawSurfacePart(j->x, j->y, frame * 40, 520, 40, 40, images[imgEnemies]);
  195. }