Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "slime.h"
  2. #include <stdlib.h>
  3. #include "../PHL.h"
  4. #include "../game.h"
  5. #include "../collision.h"
  6. #include "../hero.h"
  7. #include "../enemy.h"
  8. #include "../weapon.h"
  9.  
  10. void slimeStep(Slime* s);
  11. void slimeDraw(Slime* s);
  12.  
  13. void createSlime(int x, int y, int type, int offset)
  14. {
  15.         int i;
  16.         for (i = 0; i < MAX_ENEMIES; i++) {
  17.                 if (enemies[i] == NULL) {
  18.                         Enemy* result = malloc(sizeof *result);
  19.                         Slime* s = malloc(sizeof *s);
  20.                        
  21.                         s->id = i;
  22.                        
  23.                         s->x = x + 20;
  24.                         s->y = y;
  25.                         s->type = type;
  26.                         s->offset = offset;
  27.                        
  28.                         s->hp = 1;
  29.                         s->state = 0;
  30.                         s->counter = 0;
  31.                         s->timer = 0;
  32.                         s->grav = 0.125;
  33.                         s->vsp = 0;
  34.                         s->hsp = 0;
  35.                         s->imageIndex = 0;
  36.                        
  37.                         result->data = s;
  38.                         result->enemyStep = slimeStep;
  39.                         result->enemyDraw = slimeDraw;
  40.                         result->type = 0;
  41.                        
  42.                         enemies[i] = result;
  43.                         i = MAX_ENEMIES;
  44.                 }
  45.         }
  46. }
  47.  
  48. void slimeStep(Slime* s)
  49. {
  50.         char dead = 0;
  51.        
  52.         //Stay within room
  53.         {
  54.                 if (s->x > 640) {
  55.                         s->x = 640;
  56.                 }
  57.                
  58.                 if (s->x < 0) {
  59.                         s->x = 0;
  60.                 }
  61.         }
  62.        
  63.         //Setup Rectangle Mask
  64.         Mask mask;
  65.         {
  66.                 mask.unused = 0;
  67.                 mask.circle = 0;
  68.                 mask.w = 24;
  69.                 mask.h = 24;
  70.                 mask.x = s->x - (mask.w / 2);
  71.                 mask.y = s->y + 28 - (mask.h / 2);
  72.         }
  73.        
  74.         //Idle
  75.         if (s->state == 0)
  76.         {
  77.                 s->imageIndex += 0.25;
  78.                
  79.                 if (s->imageIndex >= 6) {
  80.                         s->imageIndex = 0;
  81.                        
  82.                         if (s->offset <= 0) {
  83.                                 s->state = 1;
  84.                                
  85.                                 //Red/Yellow Slime
  86.                                 if (s->type == 1 || s->type == 2)
  87.                                 {
  88.                                         s->hsp = 1;
  89.                                         if (s->type == 2) {
  90.                                                 s->hsp = 1.5;
  91.                                         }
  92.                                         if ((int)(rand() % 2) == 0) {
  93.                                                 s->hsp *= -1;
  94.                                         }
  95.                                 }
  96.                                
  97.                                 if (s->counter < 2) {
  98.                                         s->vsp = -2;
  99.                                         s->counter += 1;
  100.                                 }else{
  101.                                         s->vsp = -4;
  102.                                         s->counter = 0;
  103.                                 }
  104.                         }else{
  105.                                 s->offset -= 1;
  106.                         }
  107.                 }
  108.         }
  109.        
  110.         //Jump
  111.         else if (s->state == 1)
  112.         {
  113.                 //Red/Yellow Slime
  114.                 if (s->type == 1 || s->type == 2)
  115.                 {
  116.                         s->x += s->hsp;
  117.                         mask.x = s->x - (mask.w / 2);
  118.                        
  119.                         PHL_Rect collide = getTileCollision(1, mask);
  120.                         if (collide.x != -1) {
  121.                                 if (s->hsp > 0) {
  122.                                         s->x = collide.x - (mask.w / 2);
  123.                                 }else if (s->hsp < 0) {
  124.                                         s->x = collide.x + 40 + (mask.w / 2);
  125.                                 }
  126.                         }
  127.                        
  128.                         mask.x = s->x - (mask.w / 2);
  129.                 }
  130.                
  131.                 s->y += s->vsp;
  132.                 s->vsp += s->grav;
  133.                
  134.                 mask.y = s->y + 28 - (mask.h / 2);
  135.                
  136.                 PHL_Rect collide = getTileCollision(1, mask);
  137.                 if (collide.x == -1) {
  138.                         collide = getTileCollision(3, mask);
  139.                 }
  140.                 if (collide.x != -1) {
  141.                         if (s->vsp >= 0) {
  142.                                 s->state = 0;
  143.                                 s->hsp = 0;
  144.                                 s->y = collide.y - 40;
  145.                         }else{
  146.                                 s->y = collide.y + 40 - (40 - mask.h) + 1;
  147.                         }
  148.                 }
  149.         }
  150.        
  151.         //Setup Collision Mask
  152.         {
  153.                 mask.unused = 0;
  154.                 mask.circle = 1;
  155.                 mask.w = 12;
  156.                 mask.x = s->x;
  157.                 mask.y = s->y + 28;
  158.         }
  159.        
  160.         //Fell in a pit
  161.         {
  162.                 if (s->y > 480) {
  163.                         dead = 1;
  164.                 }
  165.         }
  166.        
  167.         //Collide with hero
  168.         {
  169.                 if (checkCollision(mask, heroMask)) {
  170.                         int dmg[3] = {10, 20, 20};
  171.                        
  172.                         if (heroHit(dmg[s->type], s->x) == 1 && s->type == 2) {
  173.                                 heroStun();
  174.                         }
  175.                 }
  176.         }
  177.        
  178.         //Sword collision
  179.         {
  180.                 int i;
  181.                 for (i = 0; i < MAX_WEAPONS; i++) {
  182.                         if (weapons[i] != NULL) {
  183.                                 if (checkCollision(mask, weapons[i]->weaponMask)) {
  184.                                         weaponHit(weapons[i]);
  185.                                        
  186.                                         spawnCollectable(s->x, s->y + 6);
  187.                                         createEffect(2, s->x - 32, s->y - 12);
  188.                                         dead = 1;      
  189.                                        
  190.                                         i = MAX_WEAPONS;
  191.                                 }
  192.                         }      
  193.                 }
  194.         }
  195.        
  196.         //Destroy object
  197.         {
  198.                 if (dead == 1) {
  199.                         enemyDestroy(s->id);
  200.                 }
  201.         }
  202. }
  203.  
  204. void slimeDraw(Slime* s)
  205. {      
  206.         int cropX = 0,
  207.                 cropY = 0;
  208.  
  209.         //Idle
  210.         if (s->state == 0) {
  211.                 int image[6] = { 0, 1, 2, 3, 4, 6 };
  212.                 cropX = image[(int)s->imageIndex] * 40;        
  213.         }
  214.        
  215.         //Jump
  216.         else if (s->state == 1) {
  217.                 cropX = 200;
  218.                 if (s->vsp >= 0) {
  219.                         cropX += 40;
  220.                 }
  221.         }
  222.        
  223.         //Color offsets
  224.         int addX[3] = {0, 280, 0};
  225.         int addY[3] = {0, 0, 480};
  226.        
  227.         PHL_DrawSurfacePart(s->x - 20, s->y + 12, cropX + addX[s->type], cropY + addY[s->type], 40, 40, images[imgEnemies]);
  228. }