Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "pumpkin.h"
  2. #include "../game.h"
  3. #include "../hero.h"
  4. #include <stdlib.h>
  5.  
  6. void pumpkinenemyStep(Pumpkinenemy* p);
  7. void pumpkinenemyDraw(Pumpkinenemy* p);
  8.  
  9. void pumpkinheadStep(Pumpkinhead* p);
  10. void pumpkinheadDraw(Pumpkinhead* p);
  11.  
  12. void createPumpkinenemy(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.                         Pumpkinenemy* p = malloc(sizeof *p);
  19.                        
  20.                         p->id = i;
  21.                        
  22.                         p->hp = 3;
  23.                         p->blink = 0;
  24.                        
  25.                         p->x = x;
  26.                         p->y = y;
  27.                        
  28.                         p->dir = 1;
  29.                         if (herox < p->x + 20) {
  30.                                 p->dir = -1;
  31.                         }
  32.                        
  33.                         p->imageIndex = 0;
  34.                        
  35.                         p->state = 0;
  36.                         p->timer = 0;
  37.                        
  38.                         e->data = p;
  39.                         e->enemyStep = pumpkinenemyStep;
  40.                         e->enemyDraw = pumpkinenemyDraw;
  41.                         e->type = 32;
  42.                        
  43.                         enemies[i] = e;
  44.                         i = MAX_ENEMIES;
  45.                 }
  46.         }
  47. }
  48.  
  49. void pumpkinenemyStep(Pumpkinenemy* p)
  50. {
  51.         //Setup Mask
  52.         Mask mask;
  53.         {
  54.                 mask.unused = mask.circle = 0;
  55.                 mask.w = 20;
  56.                 mask.h = 38;
  57.                 mask.x = p->x + ((40 - mask.w) / 2);
  58.                 mask.y = p->y + (40 - mask.h);
  59.         }
  60.        
  61.         //Animate
  62.         {
  63.                 p->imageIndex += 0.1;
  64.                 if (p->imageIndex >= 2) {
  65.                         p->imageIndex -= 2;
  66.                 }
  67.                
  68.                 if (p->blink > 0) {
  69.                         p->blink -= 1;
  70.                 }
  71.         }
  72.        
  73.         //Walking
  74.         if (p->state == 0)
  75.         {
  76.                 double hsp = 0.5;              
  77.                 p->x += hsp * p->dir;
  78.                 mask.x = p->x + ((40 - mask.w) / 2);
  79.                
  80.                 //Hit wall
  81.                 {                      
  82.                         if (checkTileCollision(1, mask) == 1 || mask.x > 640 || mask.x + mask.w < 0) {
  83.                                 p->dir *= -1;
  84.                         }
  85.                 }
  86.                
  87.                 //On edge
  88.                 {
  89.                         mask.x += mask.w * p->dir;
  90.                         mask.y += 20;
  91.                        
  92.                         PHL_Rect collide = getTileCollision(1, mask);
  93.                         if (collide.x == -1) {
  94.                                 collide = getTileCollision(3, mask);
  95.                         }
  96.                        
  97.                         if (collide.x == -1) {
  98.                                 p->dir *= -1;
  99.                         }
  100.                 }
  101.                
  102.                 //Player is close
  103.                 {
  104.                         if (p->timer <= 0) {
  105.                                 Mask area;
  106.                                 {
  107.                                         area.circle = area.unused = 0;
  108.                                         area.w = 240;
  109.                                         area.h = 80;
  110.                                         area.x = p->x - 100;
  111.                                         area.y = p->y - 40;
  112.                                 }
  113.                                 if (checkCollision(area, getHeroMask()) == 1) {
  114.                                         p->state = 1;
  115.                                         p->timer = 0;
  116.                                         p->dir = 1;
  117.                                         if (herox < p->x + 20) {
  118.                                                 p->dir = -1;
  119.                                         }
  120.                                 }
  121.                                
  122.                         }else{
  123.                                 p->timer -= 1;
  124.                         }
  125.                 }
  126.                
  127.         }
  128.        
  129.         //Deheaded
  130.         else if (p->state == 1) {
  131.                 //Animate
  132.                 {
  133.                         p->imageIndex = 0;
  134.                         if (p->timer >= 15) {
  135.                                 p->imageIndex = 2;
  136.                         }
  137.                 }
  138.                
  139.                 p->timer += 1;
  140.                 if (p->timer == 15) {
  141.                         createPumpkinhead(p->x, p->y - 6, p->dir);
  142.                         PHL_PlaySound(sounds[sndPi05], CHN_ENEMIES);
  143.                 }
  144.                
  145.                 if (p->timer >= 40) {
  146.                         p->state = 0;
  147.                         p->imageIndex = 0;
  148.                         p->timer = 300;
  149.                 }
  150.         }
  151.        
  152.         //Update Mask
  153.         mask.x = p->x + ((40 - mask.w) / 2);
  154.         mask.y = p->y + (40 - mask.h);
  155.        
  156.         //Hero Collision
  157.         {
  158.                 if (checkCollision(mask, getHeroMask()) == 1) {
  159.                         heroHit(15, mask.x + (mask.w / 2));
  160.                 }
  161.         }
  162.        
  163.         //Weapon Collision
  164.         {
  165.                 int i;
  166.                 for (i = 0; i < MAX_WEAPONS; i++) {
  167.                         if (weapons[i] != NULL) {
  168.                                 if (weapons[i]->cooldown == 0) {
  169.                                         if (checkCollision(mask, weapons[i]->weaponMask)) {
  170.                                                 weaponHit(weapons[i]);
  171.                                                
  172.                                                 p->hp -= 1;
  173.                                                 p->blink = 15;
  174.                                                
  175.                                                 //Death
  176.                                                 if (p->hp <= 0) {                                      
  177.                                                         createEffect(2, p->x - 12, p->y - 6);
  178.                                                         spawnCollectable(p->x + 20, p->y);
  179.                                                         enemyDestroy(p->id);
  180.                                                 }
  181.  
  182.                                                 i = MAX_WEAPONS;
  183.                                         }
  184.                                 }
  185.                         }      
  186.                 }
  187.         }
  188.        
  189. }
  190.  
  191. void pumpkinenemyDraw(Pumpkinenemy* p)
  192. {
  193.         if (p->blink % 2 == 0) {
  194.                 int cropX = (int)p->imageIndex * 40;
  195.                
  196.                 if (p->dir == -1) {
  197.                         cropX += 120;
  198.                 }
  199.                
  200.                 PHL_DrawSurfacePart(p->x, p->y, cropX, 560, 40, 40, images[imgEnemies]);
  201.         }
  202. }
  203.  
  204.  
  205. //Pumpkin bomb head
  206. void createPumpkinhead(int x, int y, int dir)
  207. {
  208.         int i;
  209.         for (i = 0; i < MAX_ENEMIES; i++) {
  210.                 if (enemies[i] == NULL) {
  211.                         Enemy* e = malloc(sizeof *e);
  212.                         Pumpkinhead* p = malloc(sizeof *p);
  213.                         p->id = i;
  214.                        
  215.                         p->dir = dir;
  216.                        
  217.                         p->x = x;
  218.                         p->y = y;
  219.                        
  220.                         p->vsp = -2;
  221.                        
  222.                         p->imageIndex = 0;
  223.  
  224.                         p->state = 0;
  225.                         p->timer = 0;
  226.                        
  227.                         e->data = p;
  228.                         e->enemyStep = pumpkinheadStep;
  229.                         e->enemyDraw = pumpkinheadDraw;
  230.                         e->type = -1;
  231.                        
  232.                         enemies[i] = e;
  233.                         i = MAX_ENEMIES;
  234.                 }
  235.         }
  236. }
  237.  
  238. void pumpkinheadStep(Pumpkinhead* p)
  239. {
  240.         char dead = 0;
  241.        
  242.         //Setup Mask
  243.         Mask mask;
  244.         {
  245.                 mask.circle = mask.unused = 0;
  246.                 mask.w = 20;
  247.                 mask.h = 22;
  248.                 mask.x = p->x + ((40 - mask.w) / 2);
  249.                 mask.y = p->y + ((40 - mask.h) / 2);
  250.         }
  251.        
  252.         //Pumpkin head
  253.         if (p->state == 0)
  254.         {
  255.                 char explode = 0;
  256.                
  257.                 //Animate
  258.                 {
  259.                         p->imageIndex += 0.1;
  260.                         if (p->imageIndex >= 2) {
  261.                                 p->imageIndex -= 2;
  262.                         }
  263.                 }
  264.                        
  265.                 //Movement
  266.                 {
  267.                         int hsp = 3;
  268.                         p->x += hsp * p->dir;
  269.                         mask.x = p->x + ((40 - mask.w) / 2);
  270.                        
  271.                         PHL_Rect collide = getTileCollision(1, mask);
  272.                         if (collide.x != -1) {
  273.                                 p->x = collide.x + 20 - ((20 + (mask.w / 2)) * p->dir) - 20;
  274.                                 mask.x = p->x + ((40 - mask.w) / 2);
  275.                                 p->dir *= -1;
  276.                         }
  277.                        
  278.                         double grav = 0.15;
  279.                         p->y += p->vsp;
  280.                         p->vsp += grav;
  281.                         mask.y = p->y + ((40 - mask.h) / 2);
  282.                        
  283.                         collide = getTileCollision(1, mask);
  284.                         if (collide.x == -1) {
  285.                                 collide = getTileCollision(3, mask);
  286.                         }
  287.                        
  288.                         if (collide.x != -1) {
  289.                                 p->y = collide.y - 40;
  290.                                 explode = 1;
  291.                         }
  292.                 }
  293.                
  294.                 //Update Mask
  295.                 mask.x = p->x + ((40 - mask.w) / 2);
  296.                 mask.y = p->y + ((40 - mask.h) / 2);
  297.        
  298.                 //Explode
  299.                 {
  300.                         if (explode == 1) {
  301.                                 PHL_PlaySound(sounds[sndBom03], CHN_ENEMIES);
  302.                                 p->state = 1;
  303.                                 p->imageIndex = 0;
  304.                                 p->timer = 0;
  305.                         }
  306.                 }
  307.                
  308.                 //Outside of room
  309.                 {
  310.                         if (mask.y > 480 || mask.x > 640 || mask.x + mask.w < 0) {
  311.                                 dead = 1;
  312.                         }
  313.                 }
  314.                
  315.         }
  316.        
  317.         //Explosion
  318.         else if (p->state == 1)
  319.         {
  320.                 //Update Mask
  321.                 {
  322.                         mask.w = 68;
  323.                         mask.h = 66;
  324.                         mask.x = p->x - 44 + 64 - (mask.w / 2);
  325.                         mask.y = p->y - 44 + (84 - mask.h);
  326.                 }
  327.                
  328.                 //Animate
  329.                 {
  330.                         p->imageIndex += 0.33;
  331.                         if (p->imageIndex >= 12) {
  332.                                 dead = 1;
  333.                         }
  334.                 }
  335.                
  336.                 //Hero Collision
  337.                 {
  338.                         if (checkCollision(mask, getHeroMask()) == 1) {
  339.                                 heroHit(40, mask.x + (mask.w / 2));
  340.                         }
  341.                 }
  342.         }
  343.        
  344.         //Destroy object
  345.         {
  346.                 if (dead == 1) {
  347.                         enemyDestroy(p->id);
  348.                 }
  349.         }
  350. }
  351.  
  352. void pumpkinheadDraw(Pumpkinhead* p)
  353. {
  354.         if (p->state == 0) {
  355.                 int cropX = (int)p->imageIndex * 40;
  356.                        
  357.                 if (p->dir == -1) {
  358.                         cropX += 80;
  359.                 }
  360.                
  361.                 PHL_DrawSurfacePart(p->x, p->y, cropX, 240, 40, 40, images[imgEnemies]);
  362.         }
  363.        
  364.         if (p->state == 1) {
  365.                 int cropX = (int)p->imageIndex * 128;
  366.                 int cropY = 0;
  367.                        
  368.                 while (cropX >= 640) {
  369.                         cropX -= 640;
  370.                         cropY += 96;
  371.                 }
  372.                
  373.                 PHL_DrawSurfacePart(p->x - 44, p->y - 44, cropX, cropY, 128, 96, images[imgExplosion]);
  374.         }
  375. }