Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "knight.h"
  2. #include "../enemy.h"
  3. #include "../hero.h"
  4. #include "../PHL.h"
  5. #include "../game.h"
  6. #include <stdlib.h>
  7.  
  8. void knightDestroy(Knight* k);
  9.  
  10. void createKnight(int x, int y, int type)
  11. {
  12.         int i;
  13.         for (i = 0; i < MAX_ENEMIES; i++) {
  14.                 if (enemies[i] == NULL) {
  15.                         Enemy* e = malloc(sizeof *e);
  16.                         Knight* k = malloc(sizeof *k);
  17.                        
  18.                         k->id = i;
  19.                         k->type = type;
  20.                        
  21.                         k->x = x;
  22.                         k->y = y;
  23.                        
  24.                         //They face the player when they are spawned
  25.                         k->dir = -1;
  26.                         if (herox > x + 20) {
  27.                                 k->dir = 1;
  28.                         }
  29.                        
  30.                         k->vsp = 0;
  31.                         k->grav = 0.2;
  32.                        
  33.                         k->state = 0;
  34.                         k->timer = 60 + (((rand() % 5) + 1) * 60);
  35.                         k->imageIndex = 0;
  36.                        
  37.                         k->hp = 2;
  38.                         //Shield Knight
  39.                         if (k->type == 1) {
  40.                                 k->hp = 3;
  41.                         }
  42.                        
  43.                         k->invincible = 0;
  44.                         k->shieldhit = 0;
  45.                        
  46.                         k->mask.circle = 0;
  47.                         k->mask.unused = 0;
  48.                         k->mask.x = x + 4;
  49.                         k->mask.y = y + 8;
  50.                         k->mask.w = 32;
  51.                         k->mask.h = 32;
  52.                        
  53.                         e->data = k;
  54.                         e->enemyStep = knightStep;
  55.                         e->enemyDraw = knightDraw;
  56.                         e->type = 3;
  57.                        
  58.                         enemies[i] = e;
  59.                         i = MAX_ENEMIES;
  60.                 }
  61.         }
  62. }
  63.  
  64. void knightStep(Knight* k)
  65. {
  66.         if (k->shieldhit > 0) {
  67.                 k->shieldhit -= 1;
  68.         }
  69.        
  70.         if (k->invincible > 0) {
  71.                 k->invincible -= 1;
  72.         }
  73.        
  74.         if (k->state == 0) { //Walk
  75.                 k->imageIndex += 0.1;
  76.                 if (k->imageIndex >= 2) {
  77.                         k->imageIndex -= 2;
  78.                 }
  79.                
  80.                 double spd = 1;
  81.                 if (k->type == 1) {
  82.                         spd = 0.5;
  83.                 }
  84.                 spd *= k->dir;
  85.                
  86.                 k->x += spd;
  87.                
  88.                 k->mask.x = k->x + 4;
  89.                 k->mask.y = k->y + 8;
  90.                
  91.                 Mask emask;
  92.                 emask.circle = emask.unused = 0;
  93.                 emask.w = 16;
  94.                 emask.h = 32;
  95.                 emask.x = k->x + 12;
  96.                 emask.y = k->y + 8;
  97.                
  98.                 //Turn when colliding with a wall
  99.                 if (checkTileCollision(1, emask)) {
  100.                         k->dir *= -1;
  101.                 }else{
  102.                         //Turn when on an edge
  103.                         k->mask.x += k->mask.w * k->dir;
  104.                         k->mask.y += 1;
  105.                         PHL_Rect collide = getTileCollision(1, k->mask);
  106.                         if (collide.x == -1) {
  107.                                 collide = getTileCollision(3, k->mask);
  108.                         }
  109.                         if (collide.x == -1) {
  110.                                 k->dir *= -1;
  111.                         }
  112.                 }
  113.                
  114.                 if (k->x + 20 >= 640 || k->x + 20 <= 0) {
  115.                         k->dir *= -1;
  116.                 }
  117.                
  118.                 k->mask.x = k->x + 4;
  119.                 k->mask.y = k->y + 8;
  120.                
  121.                 k->timer -= 1;
  122.                 if (k->timer <= 0) {
  123.                         k->state = 1;
  124.                         k->timer = 120;
  125.                         k->imageIndex = 0;
  126.                 }
  127.         }
  128.         else if (k->state == 1) { //Wait
  129.                 k->timer -= 1;
  130.                 if (k->timer <= 0) {
  131.                         k->state = 0;
  132.                         k->dir = 1;
  133.                         if (herox < k->x + 20) {
  134.                                 k->dir = -1;
  135.                         }
  136.                         k->timer = 60 + (((rand() % 5) + 1) * 60);
  137.                 }              
  138.         }
  139.        
  140.         //Green Sword Knight
  141.         if (k->type == 0) {
  142.                 //Hit player
  143.                 Mask swordMask;
  144.                 swordMask.unused = 0;
  145.                 swordMask.circle = 0;
  146.                 swordMask.x = k->x + (24 * k->dir);
  147.                 swordMask.y = k->y + 20;
  148.                 swordMask.w = 40;
  149.                 swordMask.h = 10;
  150.                
  151.                 if (checkCollision(getHeroMask(), swordMask)) {
  152.                         heroHit(30, k->x + 20);
  153.                 }
  154.         }
  155.        
  156.         if (checkCollision(getHeroMask(), k->mask)) {
  157.                 heroHit(15, k->x + 20);
  158.         }
  159.        
  160.         //Weapon collision
  161.         int i;
  162.         for (i = 0; i < MAX_WEAPONS; i++) {
  163.                 if (weapons[i] != NULL) {
  164.                         if (weapons[i]->cooldown == 0) {
  165.                                 if (checkCollision(k->mask, weapons[i]->weaponMask)) {
  166.                                         char gotHit = 1;
  167.                                        
  168.                                         int weapondir = weapons[i]->dir;
  169.                                         weaponHit(weapons[i]);
  170.                                        
  171.                                         //Shield Collision
  172.                                         if (k->type == 1) {                                                    
  173.                                                 if (weapondir == k->dir * -1) {
  174.                                                         gotHit = 0;
  175.                                                         k->shieldhit = 15;
  176.                                                         PHL_PlaySound(sounds[sndHit03], CHN_WEAPONS);
  177.                                                 }
  178.                                         }                                      
  179.                                        
  180.                                         if (gotHit == 1) {
  181.                                                 k->hp -= 1;
  182.                                                 k->invincible = 15;
  183.                                                
  184.                                                 i = MAX_WEAPONS;
  185.                                         }                                      
  186.                                        
  187.                                         if (k->hp <= 0) {
  188.                                                 knightDestroy(k);
  189.                                         }                                      
  190.                                 }
  191.                         }
  192.                 }      
  193.         }
  194. }
  195.  
  196. void knightDraw(Knight* k)
  197. {
  198.         if (k->invincible % 2 == 0) {
  199.                 int cx = 0, cy = 200;
  200.                
  201.                 //Green Knight's Sword
  202.                 if (k->type == 0) {
  203.                         int swordimg = 0;
  204.                         if (k->dir == -1) {
  205.                                 swordimg = 1;
  206.                         }
  207.                         int posx = 24, posy = 8;
  208.                         if ((int)k->imageIndex == 1) {
  209.                                 posx -= 2;
  210.                                 posy -= 2;
  211.                         }
  212.                         PHL_DrawSurfacePart(k->x + (posx * k->dir), k->y + posy, 160 + (swordimg * 40), 200, 40, 40, images[imgEnemies]);
  213.                 }
  214.                
  215.                 //Shield Knight
  216.                 if (k->type == 1) {
  217.                         cx = 240;
  218.                 }
  219.  
  220.                 if (k->dir == -1) {
  221.                         cx += 80;
  222.                 }
  223.                 PHL_DrawSurfacePart(k->x, k->y, cx + ((int)k->imageIndex * 40), cy, 40, 40, images[imgEnemies]);
  224.         }
  225. }
  226.  
  227. void knightDestroy(Knight* k)
  228. {
  229.         createEffect(2, k->x - 12, k->y - 6);
  230.         spawnCollectable(k->x + 20, k->y);
  231.         enemyDestroy(k->id);
  232. }