Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "golem.h"
  2. #include "../PHL.h"
  3. #include "../hero.h"
  4. #include "../game.h"
  5. #include <stdlib.h>
  6.  
  7. void golemStep(Golem* g);
  8. void golemDraw(Golem* g);
  9.  
  10. void createGolem(int x, int y, int dir)
  11. {
  12.         int i;
  13.         for (i = 0; i < MAX_ENEMIES; i++) {
  14.                 if (enemies[i] == NULL) {
  15.                         Enemy* e = malloc(sizeof *e);
  16.                         Golem* g = malloc(sizeof *g);
  17.                         g->id = i;
  18.                        
  19.                         g->x = x;
  20.                         g->y = y;
  21.                        
  22.                         g->hp = 4;
  23.                        
  24.                         g->dir = 1;
  25.                         if (dir == 1) {
  26.                                 g->dir = -1;
  27.                         }
  28.                        
  29.                         g->imageIndex = 0;
  30.                         g->state = 0;
  31.                         g->blink = 0;                  
  32.                        
  33.                         e->data = g;
  34.                         e->enemyStep = golemStep;
  35.                         e->enemyDraw = golemDraw;
  36.                         e->type = 28;
  37.                        
  38.                         enemies[i] = e;
  39.                         i = MAX_ENEMIES;
  40.                 }
  41.         }
  42. }
  43.  
  44. void golemStep(Golem* g)
  45. {
  46.         double imageSpeed = 0.2;
  47.        
  48.         //Timers
  49.         {
  50.                 if (g->blink > 0) {
  51.                         g->blink -= 1;
  52.                 }
  53.         }
  54.        
  55.         //Setup Mask
  56.         Mask mask;
  57.         {
  58.                 mask.unused = mask.circle = 0;
  59.                 mask.w = 36;
  60.                 mask.h = 36;
  61.                 mask.x = g->x + ((40 - mask.w) / 2);
  62.                 mask.y = g->y + (40 - mask.h);
  63.         }
  64.        
  65.         //Rolling
  66.         if (g->state == 0)
  67.         {
  68.                 //Animate
  69.                 {
  70.                         g->imageIndex += imageSpeed * g->dir;
  71.                        
  72.                         if (g->imageIndex >= 8) {
  73.                                 g->imageIndex -= 8;
  74.                         }
  75.                        
  76.                         if (g->imageIndex < 0) {
  77.                                 g->imageIndex += 8;
  78.                         }
  79.                 }
  80.                
  81.                 //Movement
  82.                 double hsp = 1;
  83.                 {                      
  84.                         g->x += hsp * g->dir;
  85.                         mask.x = g->x + ((40 - mask.w) / 2);
  86.                 }
  87.                
  88.                 char nextState = 0;
  89.                
  90.                 //Check on ledge
  91.                 {
  92.                         mask.x += 30 * g->dir;
  93.                         mask.y += 10;
  94.                        
  95.                         if (checkTileCollision(1, mask) == 0 && checkTileCollision(3, mask) == 0) {
  96.                                 nextState = 1;
  97.                         }
  98.                        
  99.                         mask.x = g->x + ((40 - mask.w) / 2);
  100.                         mask.y = g->y + (40 - mask.h);
  101.                 }
  102.                
  103.                 //Collide with wall
  104.                 {
  105.                         mask.x += hsp * g->dir;
  106.                        
  107.                         if (checkTileCollision(1, mask) == 1) {
  108.                                 nextState = 1;
  109.                         }
  110.                        
  111.                         mask.x = g->x + ((40 - mask.w) / 2);
  112.                 }
  113.                
  114.                 if (nextState == 1) {
  115.                         PHL_PlaySound(sounds[sndPi10], CHN_ENEMIES);
  116.                         g->state = 1;
  117.                         g->imageIndex = 0;
  118.                 }
  119.         }
  120.        
  121.         //Forming
  122.         else if (g->state == 1)
  123.         {
  124.                 //Animate
  125.                 {
  126.                         g->imageIndex += imageSpeed;
  127.                        
  128.                         if (g->imageIndex >= 12) {
  129.                                 g->imageIndex = 0;
  130.                                 g->state = 0;
  131.                                 g->dir *= -1;
  132.                         }
  133.                 }
  134.                
  135.         }
  136.        
  137.         //Hero Collision
  138.         {
  139.                 if (checkCollision(mask, getHeroMask())) {
  140.                         heroHit(15, mask.x + (mask.w / 2));
  141.                 }
  142.         }
  143.        
  144.         //Weapon collision
  145.         {
  146.                 int i;
  147.                 for (i = 0; i < MAX_WEAPONS; i++) {
  148.                         if (weapons[i] != NULL) {
  149.                                 if (weapons[i]->cooldown == 0) {
  150.                                         if (checkCollision(mask, weapons[i]->weaponMask)) {
  151.                                                 weaponHit(weapons[i]);
  152.                                                
  153.                                                 //Tink
  154.                                                 if (g->state == 0) {
  155.                                                         PHL_PlaySound(sounds[sndHit03], CHN_WEAPONS);
  156.                                                 }else{
  157.                                                         g->hp -= 1;
  158.                                                         g->blink = 15;
  159.                                                 }
  160.                                                
  161.                                                 i = MAX_WEAPONS;
  162.                                         }
  163.                                 }
  164.                         }      
  165.                 }
  166.         }      
  167.  
  168.         //Death
  169.         {
  170.                 if (g->hp <= 0) {
  171.                         createRockSmash(mask.x + (mask.w / 2), mask.y + (mask.h / 2));
  172.                         spawnCollectable(g->x + 20, g->y);
  173.                         enemyDestroy(g->id);
  174.                 }
  175.         }
  176. }
  177.  
  178. void golemDraw(Golem* g)
  179. {
  180.         if (g->blink % 2 == 0) {
  181.                 int cropX = 320,
  182.                         cropY = 160;
  183.                        
  184.                 int drawY = g->y;
  185.                        
  186.                 if (g->state == 0) {
  187.                         cropX += (int)g->imageIndex * 40;
  188.                         drawY += 2;
  189.                 }else{
  190.                         cropY = 280;
  191.                         cropX = 240;
  192.                        
  193.                         int animation[12] = {0, 1, 2, 3, 3, 3, 3, 3, 3, 2, 1, 0};
  194.                         cropX += animation[(int)g->imageIndex] * 40;
  195.                 }
  196.                
  197.                 PHL_DrawSurfacePart(g->x, drawY, cropX, cropY, 40, 40, images[imgEnemies]);
  198.         }
  199. }