Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "poisonknight.h"
  2. #include "../game.h"
  3. #include "../hero.h"
  4. #include <stdlib.h>
  5.  
  6. void poisonknightStep(Poisonknight* p);
  7. void poisonknightDraw(Poisonknight* p);
  8.  
  9. void goopStep(Goop* g);
  10. void goopDraw(Goop* g);
  11.  
  12. void createPoisonknight(int x, int y)
  13. {
  14.         int i;
  15.         for (i = 0; i < MAX_ENEMIES; i++) {
  16.                 if (enemies[i] == NULL) {
  17.                         Enemy* e = malloc(sizeof *e);
  18.                         Poisonknight* p = malloc(sizeof *p);
  19.                         p->id = i;
  20.                         p->hp = 2;
  21.                        
  22.                         p->x = x;
  23.                         p->y = y;
  24.                        
  25.                         p->imageIndex = 0;
  26.                        
  27.                         p->dir = 1;
  28.                         if (herox < p->x) {
  29.                                 p->dir = -1;
  30.                         }
  31.                        
  32.                         p->blink = 0;
  33.                         p->timer = 0;
  34.                         p->state = 0;
  35.                        
  36.                         e->data = p;
  37.                         e->enemyStep = poisonknightStep;
  38.                         e->enemyDraw = poisonknightDraw;
  39.                         e->type = 29;
  40.                        
  41.                         enemies[i] = e;
  42.                         i = MAX_ENEMIES;
  43.                 }
  44.         }
  45. }
  46.  
  47. void poisonknightStep(Poisonknight* p)
  48. {
  49.         char dead = 0;
  50.        
  51.         //Animate
  52.         {
  53.                 p->imageIndex += 0.1;
  54.                 if (p->imageIndex >= 2) {
  55.                         p->imageIndex -= 2;
  56.                 }
  57.                
  58.                 if (p->blink > 0) {
  59.                         p->blink -= 1;
  60.                 }
  61.         }
  62.        
  63.         //Setup Mask
  64.         Mask mask;
  65.         {
  66.                 mask.circle = mask.unused = 0;
  67.                 mask.w = 38;
  68.                 mask.h = 36;
  69.                 mask.x = p->x + ((40 - mask.w) / 2);
  70.                 mask.y = p->y + (40 - mask.h);
  71.         }
  72.        
  73.         //Walk
  74.         if (p->state == 0) {
  75.                 double hsp = 1;
  76.                
  77.                 p->x += hsp * p->dir;
  78.                 mask.x = p->x + ((40 - mask.w) / 2);
  79.                
  80.                 //Hit wall
  81.                 if (checkTileCollision(1, mask) == 1) {
  82.                         p->dir *= -1;
  83.                 }
  84.                
  85.                 //On wall edge
  86.                 else {
  87.                         mask.x += mask.w * p->dir;
  88.                         mask.y += 10;
  89.                        
  90.                         PHL_Rect collide = getTileCollision(1, mask);
  91.                         if (collide.x == -1) {
  92.                                 collide = getTileCollision(3, mask);
  93.                         }
  94.                         if (collide.x == -1) {
  95.                                 p->dir *= -1;
  96.                         }
  97.                 }
  98.                
  99.                 //Hero is close enough
  100.                 {
  101.                         if (p->timer <= 0) {
  102.                                 Mask area;
  103.                                 area.circle = area.unused = 0;
  104.                                 area.x = p->x - 110;
  105.                                 area.y = p->y;
  106.                                 area.w = 260;
  107.                                 area.h = 40;
  108.                                
  109.                                 if (checkCollision(area, getHeroMask()) == 1) {
  110.                                         p->dir = 1;
  111.                                         if (herox < p->x + 20) {
  112.                                                 p->dir = -1;
  113.                                         }
  114.                                         p->imageIndex = 1;
  115.                                         p->timer = 0;
  116.                                         p->state = 1;
  117.                                 }
  118.                                
  119.                         }else{
  120.                                 p->timer -= 1;
  121.                         }
  122.                 }
  123.         }
  124.        
  125.         //*beat*
  126.         else if (p->state == 1)
  127.         {
  128.                 //Animate
  129.                 p->imageIndex = 1;
  130.                
  131.                 p->timer += 1;
  132.                 if (p->timer >= 15) {
  133.                         p->state = 2;
  134.                         p->timer = 0;
  135.                         p->imageIndex = 2;
  136.                 }
  137.         }
  138.        
  139.         //Shoot goop
  140.         else if (p->state == 2)
  141.         {
  142.                 //Shoot goop
  143.                 if (p->timer == 0) {
  144.                         PHL_PlaySound(sounds[sndPi05], CHN_ENEMIES);
  145.                         createGoop(p->x + (20 * p->dir), p->y - 2, p->dir);
  146.                 }
  147.                
  148.                 //Animate
  149.                 p->imageIndex = 2;
  150.                
  151.                 p->timer += 1;
  152.                 if (p->timer >= 25) {
  153.                         p->state = 0;
  154.                         p->timer = 240;
  155.                 }
  156.         }
  157.        
  158.         //Update Mask
  159.         mask.x = p->x + ((40 - mask.w) / 2);
  160.         mask.y = p->y + (40 - mask.h);
  161.        
  162.        
  163.         //Collide with hero
  164.         {
  165.                 if (checkCollision(mask, getHeroMask()) == 1) {
  166.                         heroHit(15, mask.x + (mask.w / 2));
  167.                 }
  168.         }
  169.        
  170.         //Weapon collision
  171.         {
  172.                 int i;
  173.                 for (i = 0; i < MAX_WEAPONS; i++) {
  174.                         if (weapons[i] != NULL) {
  175.                                 if (weapons[i]->cooldown == 0) {
  176.                                         if (checkCollision(mask, weapons[i]->weaponMask)) {
  177.                                                 weaponHit(weapons[i]);
  178.                                                
  179.                                                 p->hp -= 1;
  180.                                                 p->blink = 15;
  181.  
  182.                                                 if (p->hp <= 0) {
  183.                                                         dead = 1;
  184.                                                         createEffect(2, p->x - 12, p->y - 6);
  185.                                                         spawnCollectable(p->x + 20, p->y);
  186.                                                 }
  187.                                         }
  188.                                 }
  189.                         }      
  190.                 }
  191.         }
  192.        
  193.         //Destroy object
  194.         {
  195.                 if (dead == 1) {
  196.                         enemyDestroy(p->id);
  197.                 }
  198.         }
  199. }
  200.  
  201. void poisonknightDraw(Poisonknight* p)
  202. {
  203.         if (p->blink % 2 == 0) {
  204.                 int cropX = (int)p->imageIndex * 40;
  205.                
  206.                 if (p->dir == -1) {
  207.                         cropX += 120;
  208.                 }
  209.                
  210.                 PHL_DrawSurfacePart(p->x, p->y, cropX, 280, 40, 40, images[imgEnemies]);
  211.         }
  212. }
  213.  
  214. //Poison Goop
  215. void createGoop(int x, int y, int dir)
  216. {
  217.         int i;
  218.         for (i = 0; i < MAX_ENEMIES; i++) {
  219.                 if (enemies[i] == NULL) {
  220.                         Enemy* e = malloc(sizeof *e);
  221.                         Goop* g = malloc(sizeof *g);
  222.                         g->id = i;
  223.                        
  224.                         g->x = x;
  225.                         g->y = y;
  226.                        
  227.                         g->dir = dir;
  228.                        
  229.                         g->imageIndex = 0;
  230.                        
  231.                         e->data = g;
  232.                         e->enemyStep = goopStep;
  233.                         e->enemyDraw = goopDraw;
  234.                         e->type = -1;
  235.                        
  236.                         enemies[i] = e;
  237.                         i = MAX_ENEMIES;
  238.                 }
  239.         }
  240. }
  241.  
  242. void goopStep(Goop* g)
  243. {
  244.         char dead = 0;
  245.        
  246.         //Animate
  247.         {
  248.                 g->imageIndex += 0.33;
  249.                 if (g->imageIndex >= 3) {
  250.                         g->imageIndex -= 3;
  251.                 }
  252.         }
  253.        
  254.         //Movement
  255.         {
  256.                 int hsp = 4;
  257.                 g->x += hsp * g->dir;
  258.         }
  259.        
  260.         //Setup Mask
  261.         Mask mask;
  262.         {
  263.                 mask.circle = mask.unused = 0;
  264.                 mask.w = 22;
  265.                 mask.h = 22;
  266.                 mask.x = g->x + ((40 - mask.w) / 2);
  267.                 mask.y = g->y + ((40 - mask.h) / 2);
  268.         }
  269.        
  270.         //Collide with hero
  271.         {
  272.                 //Collide with shield
  273.                 if (checkCollision(mask, shieldMask) == 1) {
  274.                         dead = 1;
  275.                         PHL_PlaySound(sounds[sndHit07], CHN_EFFECTS);
  276.                         createEffect(1, g->x, g->y);                   
  277.                 }
  278.                 //Collide with hero
  279.                 else if (checkCollision(mask, getHeroMask()) == 1) {
  280.                         if (heroHit(10, mask.x + (mask.w / 2)) == 1) {
  281.                                 heroPoison();
  282.                         }
  283.                 }
  284.         }
  285.        
  286.         //Collide with wall
  287.         {
  288.                 if (checkTileCollision(1, mask) == 1) {
  289.                         dead = 1;
  290.                         createEffect(1, g->x, g->y);
  291.                 }
  292.         }
  293.        
  294.         //Destroy if out of room
  295.         {
  296.                 if (g->x + 40 < 0 || g->x > 640) {
  297.                         dead = 1;
  298.                 }
  299.         }
  300.        
  301.         //Destroy object
  302.         {
  303.                 if (dead == 1) {
  304.                         enemyDestroy(g->id);
  305.                 }
  306.         }
  307. }
  308.  
  309. void goopDraw(Goop* g)
  310. {
  311.         int cropX = 400 + ((int)g->imageIndex * 40);
  312.        
  313.         if (g->dir == -1) {
  314.                 cropX += 120;
  315.         }
  316.        
  317.         PHL_DrawSurfacePart(g->x, g->y, cropX, 520, 40, 40, images[imgMisc20]);
  318. }