Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "fish.h"
  2. #include "../game.h"
  3. #include "../enemy.h"
  4. #include "../PHL.h"
  5. #include "../collision.h"
  6. #include "../hero.h"
  7. #include <stdlib.h>
  8.  
  9. void createFish(int x, int y, int dir)
  10. {
  11.         int i;
  12.         for (i = 0; i < MAX_ENEMIES; i++) {
  13.                 if (enemies[i] == NULL) {
  14.                         Enemy* e = malloc(sizeof *e);
  15.                         Fish* f = malloc(sizeof *f);
  16.                         f->id = i;
  17.                        
  18.                         f->x = f->xstart = x;
  19.                         f->y = y;
  20.                        
  21.                         f->imageIndex = 0;
  22.                        
  23.                         f->spd = 1;
  24.                        
  25.                         f->turning = 0;
  26.                         f->dir = 1;
  27.                         if (dir == 1) {
  28.                                 f->dir = -1;
  29.                                 f->spd = -1;
  30.                         }
  31.                        
  32.                         f->mask.circle = f->mask.unused = 0;
  33.                         f->mask.x = x + 3;
  34.                         f->mask.y = y + 6;
  35.                         f->mask.w = 34;
  36.                         f->mask.h = 32;
  37.                        
  38.                         e->data = f;
  39.                         e->enemyStep = fishStep;
  40.                         e->enemyDraw = fishDraw;
  41.                         e->type = 13;
  42.                        
  43.                         enemies[i] = e;
  44.                         i = MAX_ENEMIES;
  45.                 }      
  46.         }
  47. }
  48.  
  49. void fishStep(Fish* f)
  50. {
  51.         double fric = 0.02;
  52.        
  53.         f->x += f->spd;
  54.         f->mask.x = f->x + 3;
  55.        
  56.         if (f->turning == 0) {
  57.                 f->imageIndex += 0.1;
  58.                 if (f->imageIndex >= 2) {
  59.                         f->imageIndex -= 2;
  60.                 }
  61.         }else{
  62.                 f->imageIndex += 0.25;
  63.                 if (f->imageIndex >= 3) {
  64.                         f->turning = 0;
  65.                 }
  66.         }
  67.        
  68.         if (f->dir == 1) {             
  69.                 if (f->x > f->xstart + 25) {
  70.                         f->spd -= fric;
  71.                        
  72.                         if (f->spd < 0) {
  73.                                 f->dir = -1;
  74.                                 f->turning = 1;
  75.                                 f->imageIndex = 0;
  76.                         }
  77.                 }else{
  78.                         f->spd += fric;
  79.                         if (f->spd > 1) {
  80.                                 f->spd = 1;
  81.                         }
  82.                 }
  83.         }else if (f->dir == -1) {              
  84.                 if (f->x < f->xstart - 25) {
  85.                         f->spd += fric;
  86.                        
  87.                         if (f->spd > 0) {
  88.                                 f->dir = 1;
  89.                                 f->turning = 1;
  90.                                 f->imageIndex = 0;
  91.                         }
  92.                 }else{
  93.                         f->spd -= fric;
  94.                         if (f->spd < -1) {
  95.                                 f->spd = -1;
  96.                         }
  97.                 }
  98.         }
  99.        
  100.         if (checkCollision(f->mask, getHeroMask())) {
  101.                 heroHit(15, f->x + 20);
  102.         }
  103.        
  104.         //Weapon collision
  105.         int i;
  106.         for (i = 0; i < MAX_WEAPONS; i++) {
  107.                 if (weapons[i] != NULL) {
  108.                         if (checkCollision(f->mask, weapons[i]->weaponMask)) {
  109.                                 weaponHit(weapons[i]);
  110.                                 createEffect(2, f->x - 12, f->y - 12);
  111.                                 spawnCollectable(f->x + 20, f->y);
  112.                                 enemyDestroy(f->id);
  113.                                
  114.                                 i = MAX_WEAPONS;
  115.                         }
  116.                 }      
  117.         }
  118. }
  119.  
  120. void fishDraw(Fish* f)
  121. {
  122.         int thisImage = 0;
  123.         if (f->turning == 1) {
  124.                 if (f->dir == -1) {
  125.                         int animation[3] = {4, 6, 5};
  126.                         thisImage = animation[(int)f->imageIndex];
  127.                 }else{
  128.                         int animation[3] = {5, 6, 4};
  129.                         thisImage = animation[(int)f->imageIndex];
  130.                 }
  131.         }else{
  132.                 thisImage = f->imageIndex;
  133.                 if (f->spd < 0) {
  134.                         thisImage += 2;
  135.                 }
  136.         }
  137.        
  138.         PHL_DrawSurfacePart(f->x, f->y, 360 + (thisImage * 40), 360, 40, 40, images[imgEnemies]);
  139. }