Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "dog.h"
  2. #include "../game.h"
  3. #include "../hero.h"
  4. #include <stdlib.h>
  5.  
  6. void dogStep(Dog* d);
  7. void dogDraw(Dog* d);
  8.  
  9. int hitWall(Dog* d, Mask mask);
  10.  
  11. void createDog(int x, int y)
  12. {
  13.         int i;
  14.         for (i = 0; i < MAX_ENEMIES; i++) {
  15.                 if (enemies[i] == NULL) {
  16.                         Enemy* e = malloc(sizeof *e);
  17.                         Dog* d = malloc(sizeof *d);
  18.                         d->id = i;
  19.                         d->hp = 3;
  20.                         d->blink = 0;
  21.                        
  22.                         d->x = x;
  23.                         d->y = y;
  24.                        
  25.                         d->hsp = 0;
  26.                         d->vsp = 0;
  27.                        
  28.                         d->imageIndex = 0;
  29.                        
  30.                         d->dir = 1;
  31.                         if (herox < d->x) {
  32.                                 d->dir = -1;
  33.                         }
  34.                        
  35.                         d->state = 0;
  36.                         d->timer = 0;
  37.                         d->counter = 0;
  38.                        
  39.                         e->data = d;
  40.                         e->enemyStep = dogStep;
  41.                         e->enemyDraw = dogDraw;
  42.                         e->type = 30;
  43.                        
  44.                         enemies[i] = e;
  45.                         i = MAX_ENEMIES;
  46.                 }
  47.         }
  48. }
  49.  
  50. void dogStep(Dog* d)
  51. {
  52.         double grav = 0.175;
  53.        
  54.         char onground = 0;
  55.         char wallhit = 0;
  56.        
  57.         //Setup Mask
  58.         Mask mask;
  59.         {
  60.                 mask.circle = mask.unused = 0;
  61.                 mask.w = 32;
  62.                 mask.h = 32;
  63.                 mask.x = d->x + ((40 - mask.w) / 2);
  64.                 mask.y = d->y + (40 - mask.h);
  65.         }
  66.        
  67.         //Blink animation
  68.         {
  69.                 if (d->blink > 0) {
  70.                         d->blink -= 1;
  71.                 }
  72.         }
  73.        
  74.         //Horizontal movement
  75.         {
  76.                 d->x += d->hsp;
  77.                 mask.x = d->x + ((40 - mask.w) / 2);
  78.                
  79.                 //Wall collision
  80.                 if (hitWall(d, mask) == 1) {
  81.                         wallhit = 1;
  82.                         mask.x = d->x + ((40 - mask.w) / 2);
  83.                 }
  84.         }
  85.        
  86.         //Vertical Movement
  87.         {
  88.                 d->vsp += grav;
  89.                 d->y += d->vsp;        
  90.                 mask.y = d->y + (40 - mask.h);         
  91.                                
  92.                 PHL_Rect collide = getTileCollision(1, mask);
  93.                 if (collide.x == -1) {
  94.                         collide = getTileCollision(3, mask);
  95.                 }
  96.                 if (collide.x != -1) {
  97.                         //Floor
  98.                         if (d->vsp >= 0) {
  99.                                 onground = 1;
  100.                                 d->vsp = 0;
  101.                                 d->y = collide.y - 40;
  102.                         }
  103.                         //Ceiling
  104.                         if (d->vsp < 0) {
  105.                                 d->y = collide.y + 40 - (40 - mask.h);
  106.                         }
  107.                         mask.y = d->y + (40 - mask.h);
  108.                 }
  109.         }
  110.        
  111.         //Wait
  112.         if (d->state == 0)
  113.         {
  114.                 double fric = 0.1;
  115.                
  116.                 //Animate
  117.                 {
  118.                         d->imageIndex += 0.1;
  119.                         if (d->imageIndex >= 2) {
  120.                                 d->imageIndex -= 2;
  121.                         }
  122.                 }              
  123.                
  124.                 //Collide with wall
  125.                 {
  126.                         if (wallhit == 1 && onground == 1) {
  127.                                 d->hsp *= -1;
  128.                         }
  129.                 }
  130.                
  131.                 //Slide to hault
  132.                 if (d->hsp > 0) {
  133.                         d->dir = 1;
  134.                        
  135.                         d->hsp -= fric;
  136.                         if (d->hsp <= 0) {
  137.                                 d->hsp = 0;
  138.                         }
  139.                 }
  140.                 if (d->hsp < 0) {
  141.                         d->dir = -1;
  142.                        
  143.                         d->hsp += fric;
  144.                         if (d->hsp >= 0) {
  145.                                 d->hsp = 0;
  146.                         }
  147.                 }
  148.                
  149.                 //Player is close
  150.                 {
  151.                         if (d->hsp == 0) {     
  152.                                 Mask area;
  153.                                 area.unused = area.circle = 0;
  154.                                 area.w = 220;
  155.                                 area.h = 60;
  156.                                 area.x = d->x - 90;
  157.                                 area.y = d->y - 20;
  158.                                
  159.                                 if (checkCollision(area, getHeroMask()) == 1) {
  160.                                         d->state = 1;
  161.                                         d->counter = 0;
  162.                                         d->vsp = 1;
  163.                                 }
  164.                         }
  165.                 }
  166.                
  167.         }
  168.        
  169.         //Hopping
  170.         else if (d->state == 1)
  171.         {
  172.                 int spd = 2;
  173.  
  174.                 d->hsp = spd * d->dir;
  175.                
  176.                 //Land on floor
  177.                 {
  178.                         if (onground == 1) {
  179.                                        
  180.                                 //Landed                                       
  181.                                 d->counter += 1;
  182.                                 d->vsp = -1.5;                         
  183.                                 if (d->counter == 3) {
  184.                                         d->vsp = -4;
  185.                                 }
  186.                                 if (d->counter == 4) {
  187.                                         d->state = 0;
  188.                                         d->counter = 0;
  189.                                         d->vsp = 0;
  190.                                         d->hsp = spd * d->dir;
  191.                                 }else{
  192.                                         PHL_PlaySound(sounds[sndPi05], CHN_ENEMIES);
  193.                                         d->dir = 1;
  194.                                         if (herox < d->x + 20) {
  195.                                                 d->dir = -1;
  196.                                         }
  197.                                 }
  198.                         }
  199.                 }
  200.                
  201.                 //Animate
  202.                 {
  203.                         d->imageIndex = 1;
  204.                         if (d->vsp < 0) {
  205.                                 d->imageIndex = 2;
  206.                         }
  207.                 }
  208.                
  209.         }
  210.        
  211.         //Update mask to be safe
  212.         mask.x = d->x + ((40 - mask.w) / 2);
  213.         mask.y = d->y + (40 - mask.h);
  214.        
  215.         //Hit Player
  216.         {
  217.                 if (checkCollision(mask, getHeroMask())) {
  218.                         if (heroHit(10, mask.x + (mask.w / 2)) == 1) {
  219.                                 heroStun();
  220.                         }
  221.                 }
  222.         }
  223.        
  224.         //Weapon collision
  225.         {
  226.                 int i;
  227.                 for (i = 0; i < MAX_WEAPONS; i++) {
  228.                         if (weapons[i] != NULL) {
  229.                                 if (weapons[i]->cooldown == 0) {
  230.                                         if (checkCollision(mask, weapons[i]->weaponMask)) {
  231.                                                 weaponHit(weapons[i]);                                                 
  232.                                                 //Hit
  233.                                                 d->blink = 15;
  234.                                                 d->hp -= 1;
  235.  
  236.                                                 //Death
  237.                                                 if (d->hp <= 0) {
  238.                                                         createEffect(2, d->x - 12, d->y - 6);
  239.                                                         spawnCollectable(d->x + 20, d->y);
  240.                                                         enemyDestroy(d->id);
  241.                                                 }
  242.  
  243.                                                 i = MAX_WEAPONS;
  244.                                         }
  245.                                 }
  246.                         }      
  247.                 }
  248.         }
  249.        
  250. }
  251.  
  252. void dogDraw(Dog* d)
  253. {
  254.         if (d->blink % 2 == 0) {
  255.                 int cropX = 240 + ((int)d->imageIndex * 40);
  256.                
  257.                 if (d->dir == -1) {
  258.                         cropX += 120;
  259.                 }
  260.                
  261.                 PHL_DrawSurfacePart(d->x, d->y, cropX, 40, 40, 40, images[imgEnemies]);
  262.         }
  263. }
  264.  
  265. int hitWall(Dog* d, Mask mask)
  266. {
  267.         PHL_Rect collide = getTileCollision(1, mask);
  268.        
  269.         if (collide.x == -1) {
  270.                 collide = getTileCollision(3, mask);
  271.         }
  272.        
  273.         if (collide.x != -1) {
  274.                 int dir = 1;
  275.                 if (d->hsp < 0) {
  276.                         dir = -1;
  277.                 }
  278.                 d->x = collide.x + 20 - ((20 + (mask.w / 2)) * dir) - 20;
  279.                
  280.                 return 1;
  281.         }else{
  282.                 if (d->x < -20) {
  283.                         d->x = -20;
  284.                         return 1;
  285.                 }
  286.                
  287.                 if (d->x > 620) {
  288.                         d->x = 620;
  289.                         return 1;
  290.                 }
  291.         }
  292.        
  293.         return 0;
  294. }