Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "seal.h"
  2. #include "../game.h"
  3. #include "../enemy.h"
  4. #include "../collision.h"
  5. #include "../hero.h"
  6. #include <stdlib.h>
  7.  
  8. void sealStep(Seal* s);
  9. void sealDraw(Seal* s);
  10.  
  11. void createSeal(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.                         Seal* s = malloc(sizeof *s);
  18.                         s->id = i;
  19.                         s->hp = 2;
  20.                        
  21.                         s->x = x;
  22.                         s->y = y;
  23.                        
  24.                         s->imageIndex = 0;
  25.                        
  26.                         s->dir = 1;
  27.                         if (x + 20 > herox) {
  28.                                 s->dir = -1;
  29.                         }
  30.                        
  31.                         s->state = 0;
  32.                         s->timer = 0;
  33.                        
  34.                         s->invincible = 0;
  35.                        
  36.                         e->data = s;
  37.                         e->enemyStep = sealStep;
  38.                         e->enemyDraw = sealDraw;
  39.                         e->type = 19;
  40.                        
  41.                         enemies[i] = e;
  42.                         i = MAX_ENEMIES;
  43.                 }
  44.         }
  45. }
  46.  
  47. void sealStep(Seal* s)
  48. {
  49.         if (s->invincible > 0) {
  50.                 s->invincible -= 1;
  51.         }
  52.        
  53.         Mask mask;
  54.         mask.unused = mask.circle = 0;
  55.         mask.w = mask.h = 28;
  56.         mask.x = s->x + ((40 - mask.w) / 2);
  57.         mask.y = s->y + (40 - mask.h);
  58.        
  59.         //Walk
  60.         if (s->state == 0)
  61.         {
  62.                 //Animate
  63.                 s->imageIndex += 0.1;
  64.                 if (s->imageIndex >= 2) {
  65.                         s->imageIndex -= 2;
  66.                 }              
  67.                
  68.                 //Check if hit a wall
  69.                 if (checkTileCollision(1, mask) == 1) {
  70.                         s->dir *= -1;
  71.                 }else{
  72.                         //Check if on edge
  73.                         mask.x += mask.w * s->dir;
  74.                         mask.y += mask.h;
  75.                        
  76.                         PHL_Rect collide = getTileCollision(1, mask);  
  77.                         if (collide.x == -1) {
  78.                                 collide = getTileCollision(3, mask);
  79.                         }
  80.                         if (collide.x == -1) {
  81.                                 s->dir *= -1;
  82.                         }
  83.                        
  84.                         mask.x = s->x + ((40 - mask.w) / 2);
  85.                         mask.y = s->y + (40 - mask.h);
  86.                 }
  87.                
  88.                 //Movement
  89.                 s->x += 0.5 * s->dir;
  90.                
  91.                 if (s->timer <= 0) {
  92.                         //Check if player is close enough
  93.                         Mask area;
  94.                         area.unused = area.circle = 0;
  95.                         area.x = s->x - 40;
  96.                         area.y = s->y;
  97.                         area.w = 120;
  98.                         area.h = 120;
  99.                        
  100.                         if (checkCollision(area, getHeroMask()) == 1) {
  101.                                 s->state = 1;
  102.                                 s->timer = -1;
  103.                         }
  104.                 }else{
  105.                         s->timer -= 1;
  106.                 }
  107.         }
  108.        
  109.         //Rear back
  110.         else if (s->state == 1)
  111.         {
  112.                 //Setup
  113.                 if (s->timer == -1) {
  114.                         s->imageIndex = 4;
  115.                         s->timer = 20;
  116.                 }
  117.                
  118.                 s->timer -= 1;
  119.                
  120.                 if (s->timer <= 0) {
  121.                         s->state = 2;
  122.                         s->timer = -1;
  123.                         s->imageIndex = 0;
  124.                 }
  125.         }
  126.        
  127.         //Tounge attack
  128.         else if (s->state == 2)
  129.         {
  130.                 //Setup
  131.                 if (s->timer == -1) {
  132.                         s->timer = 0;
  133.                         PHL_PlaySound(sounds[sndGet01], CHN_ENEMIES);
  134.                 }
  135.                
  136.                 //Animate
  137.                 int animation[41] = {0, 1, 1, 2, 2, 3, 3, 4, 4, 4,
  138.                                                      4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  139.                                                      4, 4, 4, 4, 4, 4, 4, 4, 3, 3,
  140.                                                      2, 2, 1, 1, 0, 0, 5, 5, 5, 5, 5 };
  141.                                                          
  142.                 s->imageIndex = animation[(int)s->timer];
  143.                
  144.                 //Update mask height to fit tounge
  145.                 int len[6] = { 18, 38, 58, 64, 66, 0};
  146.                 mask.h += len[(int)s->imageIndex];
  147.                
  148.                 s->timer += 1;
  149.                
  150.                 if (s->timer >= 41) {
  151.                         s->state = 0;
  152.                         s->timer = 120;
  153.                         s->imageIndex = 0;
  154.                 }
  155.         }
  156.        
  157.         //Hit Player
  158.         if (checkCollision(mask, getHeroMask())) {
  159.                 heroHit(10, s->x + 20);
  160.         }
  161.        
  162.         int i;
  163.         for (i = 0; i < MAX_WEAPONS; i++) {
  164.                 if (weapons[i] != NULL) {
  165.                         if (weapons[i]->cooldown == 0) {
  166.                                 if (checkCollision(mask, weapons[i]->weaponMask)) {
  167.                                         s->hp -= 1;
  168.                                         s->invincible = 15;
  169.                                         weaponHit(weapons[i]);
  170.                                         if (s->hp <= 0) {
  171.                                                 enemyDestroy(s->id);
  172.                                                 createEffect(2, s->x - 12, s->y - 6);
  173.                                                 spawnCollectable(s->x + 20, s->y);
  174.                                         }
  175.                                         i = MAX_WEAPONS;
  176.                                 }
  177.                         }
  178.                 }      
  179.         }
  180. }
  181.  
  182. void sealDraw(Seal* s)
  183. {
  184.         if (s->invincible % 2 == 0) {
  185.                 int cx = 400 + ((int)s->imageIndex * 40);
  186.                
  187.                 if (s->state == 0) {
  188.                         if (s->dir == -1) {
  189.                                 cx += 80;
  190.                         }
  191.                 }
  192.                
  193.                 if (s->state == 2) {
  194.                         cx = 600;
  195.                 }
  196.                
  197.                 PHL_DrawSurfacePart(s->x, s->y, cx, 200, 40, 40, images[imgEnemies]);
  198.                
  199.                 //Draw tounge
  200.                 if (s->state == 2) {
  201.                         PHL_DrawSurfacePart(s->x, s->y + 28, 200 + ((int)s->imageIndex * 40), 0, 40, 80, images[imgMisc2040]);
  202.                 }
  203.         }
  204. }