Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "boar.h"
  2. #include "../game.h"
  3. #include "../hero.h"
  4. #include <stdlib.h>
  5.  
  6. void boarStep(Boar* b);
  7. void boarDraw(Boar* b);
  8.  
  9. void createBoar(int x, int y)
  10. {
  11.         int i;
  12.         for (i = 0; i < MAX_ENEMIES; i++) {
  13.                 if (enemies[i] == NULL) {
  14.                         Enemy* e = /*(Enemy*)*/malloc(sizeof *e);
  15.                         Boar* b = /*(Boar*)*/malloc(sizeof *b);
  16.                        
  17.                         b->id = i;
  18.                        
  19.                         b->hp = 3;
  20.                        
  21.                         b->x = x;
  22.                         b->y = y;
  23.                        
  24.                         b->hsp = 0;
  25.                        
  26.                         b->imageIndex = 0;
  27.                         b->dir = 1;
  28.                        
  29.                         b->blink = 0;
  30.                        
  31.                         b->state = 0;
  32.                         b->timer = 0;
  33.                        
  34.                         e->data = b;
  35.                         e->enemyStep = boarStep;
  36.                         e->enemyDraw = boarDraw;
  37.                         e->type = 26;
  38.                        
  39.                         enemies[i] = e;
  40.                         i = MAX_ENEMIES;
  41.                 }
  42.         }
  43. }
  44.  
  45. void boarStep(Boar* b)
  46. {
  47.         //Setup mask
  48.         Mask mask;
  49.         {
  50.                 mask.unused = mask.circle = 0;
  51.                 mask.w = 32;
  52.                 mask.h = 28;
  53.                 mask.x = b->x + ((40 - mask.w) / 2);
  54.                 mask.y = b->y + 40 - mask.h;
  55.         }
  56.        
  57.         //Blink animation
  58.         {
  59.                 if (b->blink > 0) {
  60.                         b->blink -= 1;
  61.                 }
  62.         }
  63.        
  64.         //Patterns
  65.         {
  66.                 //Dance
  67.                 if (b->state == 0)
  68.                 {
  69.                         //Animate
  70.                         b->imageIndex += 0.15;
  71.                         if (b->imageIndex >= 8) {
  72.                                 b->imageIndex -= 8;
  73.                         }
  74.                        
  75.                         //if player gets near
  76.                         Mask area;
  77.                         area.unused = area.circle = 0;
  78.                         area.x = b->x - 80;
  79.                         area.y = b->y - 40;
  80.                         area.w = 200;
  81.                         area.h = 80;
  82.                        
  83.                         if (checkCollision(area, getHeroMask()) == 1) {
  84.                                 b->state = 1;
  85.                                 b->timer = -1;
  86.                                 b->imageIndex = 0;
  87.                                 b->dir = 1;
  88.                                 if (herox < b->x + 20) {
  89.                                         b->dir = -1;
  90.                                 }
  91.                         }
  92.                 }
  93.                 //Rev up
  94.                 else if (b->state == 1)
  95.                 {
  96.                         b->timer += 1;
  97.                        
  98.                         //Play sound
  99.                         if (b->timer % 10 == 0) {
  100.                                 PHL_PlaySound(sounds[sndShot01], CHN_ENEMIES);
  101.                         }
  102.                        
  103.                         //Create effect
  104.                         if (b->timer % 16 == 0) {
  105.                                 if (b->dir == 1) {
  106.                                         createEffectExtra(3, b->x + 20 - 30, b->y + 8, -1, 0, 0);
  107.                                 }
  108.                                 if (b->dir == -1) {
  109.                                         createEffectExtra(3, b->x + 20 - 10, b->y + 8, 1, 0, 0);
  110.                                 }
  111.                         }                      
  112.                        
  113.                         if (b->timer >= 60) {
  114.                                 b->state = 2;
  115.                                 b->hsp = 3;
  116.                         }
  117.                 }
  118.                 //Running
  119.                 else if (b->state == 2)
  120.                 {
  121.                         b->x += b->hsp * b->dir;
  122.                         mask.x = b->x + ((40 - mask.w) / 2);
  123.                        
  124.                         //Collide with wall
  125.                         if (checkTileCollision(1, mask) == 1) {
  126.                                 b->x -= b->hsp * b->dir;
  127.                                 b->dir *= -1;
  128.                         }
  129.                        
  130.                         //On edge
  131.                         {
  132.                                 mask.x = b->x + ((40 - mask.w) / 2);
  133.                                
  134.                                 mask.x += mask.w * b->dir;
  135.                                 mask.y += 1;
  136.                                
  137.                                 PHL_Rect collide = getTileCollision(1, mask);
  138.                                 if (collide.x == -1) {
  139.                                         collide = getTileCollision(3, mask);
  140.                                 }
  141.                                 if (collide.x == -1) {
  142.                                         b->dir *= -1;
  143.                                 }
  144.                                
  145.                                 mask.y -= 1;
  146.                                 mask.x = b->x + ((40 - mask.w) / 2);
  147.                         }
  148.                        
  149.                         b->hsp -= 0.05;
  150.                         if (b->hsp <= 0) {
  151.                                 b->state = 0;
  152.                         }
  153.                 }
  154.                
  155.                 //Running animation
  156.                 if (b->state == 1 || b->state == 2) {
  157.                         //Animate
  158.                         b->imageIndex += 0.2;
  159.                         if (b->imageIndex >= 2) {
  160.                                 b->imageIndex -= 2;
  161.                         }
  162.                 }
  163.         }
  164.        
  165.         //Collide with hero
  166.         {
  167.                 if (checkCollision(mask, getHeroMask())) {
  168.                         heroHit(30, mask.x + (mask.w / 2));
  169.                 }
  170.         }
  171.        
  172.         //Weapon collision
  173.         {
  174.                 int i;
  175.                 for (i = 0; i < MAX_WEAPONS; i++) {
  176.                         if (weapons[i] != NULL) {
  177.                                 if (weapons[i]->cooldown == 0) {
  178.                                         if (checkCollision(mask, weapons[i]->weaponMask)) {
  179.                                                 weaponHit(weapons[i]);
  180.                                                 b->hp -= 1;
  181.                                                 b->blink = 15;
  182.                                                
  183.                                                 //Death
  184.                                                 if (b->hp <= 0) {
  185.                                                         createEffect(2, b->x - 12, b->y - 12);
  186.                                                         spawnCollectable(b->x + 20, b->y);
  187.                                                         enemyDestroy(b->id);
  188.                                                 }
  189.                                                
  190.                                                 i = MAX_WEAPONS;
  191.                                         }
  192.                                 }
  193.                         }      
  194.                 }
  195.         }
  196. }
  197.  
  198. void boarDraw(Boar* b)
  199. {
  200.         if (b->blink % 2 == 0)
  201.         {
  202.                 int cropx = 0, cropy = 360;
  203.                 int drawx = b->x, drawy = b->y;
  204.                
  205.                 //Dance
  206.                 if (b->state == 0) {
  207.                         int animation[8] = {0, 1, 2, 1, 0, 3, 4, 3};
  208.                         cropx = 160 + (animation[(int)b->imageIndex] * 40);
  209.                 }
  210.                 //Charge
  211.                 else{
  212.                         cropx = (int)b->imageIndex * 40;
  213.                        
  214.                         if (b->dir == -1) {
  215.                                 cropx += 80;
  216.                         }
  217.                 }
  218.                
  219.                 PHL_DrawSurfacePart(drawx, drawy, cropx, cropy, 40, 40, images[imgEnemies]);
  220.         }
  221. }