Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "slug.h"
  2. #include "../enemy.h"
  3. #include "../game.h"
  4. #include "../PHL.h"
  5. #include "../hero.h"
  6. #include <stdlib.h>
  7.  
  8. void slugStep(Slug* s);
  9. void slugDraw(Slug* s);
  10.  
  11. void createSlug(int x, int y, int dir)
  12. {
  13.         int i;
  14.         for (i = 0; i < MAX_ENEMIES; i++) {
  15.                 if (enemies[i] == NULL) {
  16.                         Enemy* e = malloc(sizeof *e);
  17.                         Slug* s = malloc(sizeof *s);
  18.                         s->id = i;
  19.                        
  20.                         s->x = x;
  21.                         s->y = y;
  22.                        
  23.                         s->imageIndex = 0;                     
  24.                         s->vsp = 0;
  25.                        
  26.                         s->dir = 1;
  27.                         if (dir == 1) {
  28.                                 s->dir = -1;
  29.                         }                      
  30.  
  31.                         e->data = s;
  32.                         e->enemyStep = slugStep;
  33.                         e->enemyDraw = slugDraw;
  34.                         e->type = 2;
  35.                        
  36.                         enemies[i] = e;
  37.                         i = MAX_ENEMIES;
  38.                 }
  39.         }
  40. }
  41.  
  42. void slugStep(Slug* s)
  43. {
  44.         //Create Mask
  45.         Mask mask;
  46.         {
  47.                 mask.circle = 0;
  48.                 mask.unused = 0;
  49.                 mask.w = 32;
  50.                 mask.h = 24;
  51.                 mask.x = s->x + ((40 - mask.w) / 2);
  52.                 mask.y = s->y + (40 - mask.h);
  53.         }
  54.        
  55.         //Animate
  56.         {
  57.                 s->imageIndex += 0.1;
  58.                 if (s->imageIndex >= 4) {
  59.                         s->imageIndex -= 4;
  60.                 }
  61.         }
  62.        
  63.         //Check if on ground
  64.         int onground = 1;
  65.         {
  66.                 mask.y += 1;
  67.                 if (checkTileCollision(1, mask) == 0 && checkTileCollision(3, mask) == 0) {
  68.                         onground = 0;
  69.                 }
  70.                 mask.y -= 1;
  71.         }
  72.        
  73.         if (onground == 0) {
  74.                 double grav = 0.2;
  75.                
  76.                 //Fall
  77.                 {
  78.                         s->y += s->vsp;
  79.                         s->vsp += grav;
  80.                 }
  81.                
  82.                 //Land on ground
  83.                 {
  84.                         mask.y = mask.y + (40 - mask.h);
  85.                         PHL_Rect collide = getTileCollision(1, mask);
  86.                         if (collide.x == -1) {
  87.                                 collide = getTileCollision(3, mask);
  88.                         }
  89.                         if (collide.x != -1) {
  90.                                 s->y = collide.y - 40;
  91.                                 s->vsp = 0;
  92.                         }
  93.                 }
  94.         }else{
  95.                 //Check if on ledge
  96.                 {
  97.                         mask.x += mask.w * s->dir;
  98.                         mask.y += 1;
  99.                        
  100.                         PHL_Rect collide = getTileCollision(1, mask);
  101.                         if (collide.x == -1) {
  102.                                 collide = getTileCollision(3, mask);
  103.                         }
  104.                         if (collide.x == -1) {
  105.                                 s->dir *= -1;
  106.                         }
  107.                 }              
  108.         }
  109.        
  110.         //Horizontal movement
  111.         double hsp = 0.5;
  112.         {
  113.                 s->x += s->dir * hsp;
  114.         }
  115.        
  116.         //Check if hit a wall
  117.         {
  118.                 mask.x = s->x + ((40 - mask.w) / 2);
  119.                 mask.y = s->y + (40 - mask.h);
  120.                
  121.                 PHL_Rect collide = getTileCollision(1, mask);
  122.                 if (collide.x != -1) {
  123.                         s->dir *= -1;
  124.                 }
  125.         }
  126.        
  127.         //Hit Player
  128.         {
  129.                 if (checkCollision(mask, getHeroMask()) == 1) {
  130.                         heroHit(15, mask.x + (mask.w / 2));
  131.                 }
  132.         }
  133.        
  134.         //Weapon collision
  135.         {
  136.                 int i;
  137.                 for (i = 0; i < MAX_WEAPONS; i++) {
  138.                         if (weapons[i] != NULL) {
  139.                                 if (checkCollision(mask, weapons[i]->weaponMask) == 1) {                               
  140.                                         weaponHit(weapons[i]);
  141.                                        
  142.                                         createEffect(2, s->x - 12, s->y - 6);
  143.                                         spawnCollectable(s->x + 20, s->y);
  144.                                         enemyDestroy(s->id);
  145.                                        
  146.                                         i = MAX_WEAPONS;
  147.                                 }
  148.                         }      
  149.                 }
  150.         }
  151.        
  152. }
  153.  
  154. void slugDraw(Slug* s)
  155. {      
  156.         int anim[4] = { 1, 0, 2, 0 };
  157.        
  158.         int cropx = anim[(int)s->imageIndex] * 40;
  159.         if (s->dir == -1) {
  160.                 cropx += 120;
  161.         }
  162.        
  163.         PHL_DrawSurfacePart(s->x, s->y + 10, cropx, 40, 40, 40, images[imgEnemies]);
  164. }