?login_element?

Subversion Repositories NedoOS

Rev

Rev 957 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #ifndef ENGINE_C
  2. #define ENGINE_C
  3. //#include <evo.h>
  4. //#include "sprite_pool.c"
  5.  
  6. #define STATE_PLAY 0
  7. #define STATE_TALK 1
  8. #define STATE_GAMEOVER 2
  9. #define STATE_WIN 3
  10. #define STATE_MENU 4
  11.  
  12. static u8 state = STATE_MENU;
  13.  
  14. #define GHOST 16
  15. #define FAIRY 32
  16. #define BAT 48
  17. #define INJAN 260
  18. #define BOOK 276
  19. #define KNIFES 292
  20. #define ICE 528
  21. #define FIARY2 784
  22. #define FROG 800
  23. #define ICEBALL 816
  24. #define FIREBALL 544
  25. #define EYE 560
  26. #define FIARY3 1040
  27. #define SHARP_ICE 1056
  28. #define KNIFE_LEFT 1552
  29.  
  30. //#define DEFAULT_BULLET 0x102
  31. //#define MARISA_BULLET 0x143
  32. //#define CIRNO_BULLET 0x100
  33. //#define SAKUYA_BULLET 0x140
  34. //#define FLAN_BULLET 0x101
  35. //#define BULLET2 0x141
  36. #define DEFAULT_BULLET 0x510
  37. #define MARISA_BULLET 0x51a
  38. #define CIRNO_BULLET 0x50c
  39. #define SAKUYA_BULLET 0x514
  40. #define FLAN_BULLET 0x50e
  41. #define BULLET2 0x516
  42.  
  43.  
  44. #define CIRNO 512
  45. #define FLANDRE 768
  46. #define MARISA 1024
  47. #define MAID 1536
  48. #define PATCHULY 1792
  49. #define FAIRY_MB 2048
  50. static u8 keys[40];
  51. static u8 is_npc_right_type(Npc *n, u8 type);
  52. //static u16 score;
  53. static u8 hp;
  54. static i16 scroll_pos;
  55. static i16 hor_scroll_pos = 0;
  56. static u32 level_loop;
  57. static i8 scroll_speed;
  58. static i8 hor_scroll_speed = 0;
  59. static i8 immortality = -1;
  60. static u8 schedule = 0;
  61. static u8 add_cnt = 0;
  62. static u8 push_counter = 0;
  63. static void (*process_level)();
  64. static u16 bullet_sprite = DEFAULT_BULLET;
  65.  
  66. void wait_for_space()
  67. {
  68.     while (1)
  69.     {
  70.         keyboard(keys);
  71.         if (keys[KEY_SPACE])
  72.             break;
  73.         swap_screen();
  74.     }
  75. }
  76.  
  77. static void push_npc(i16 x, i16 y, i8 dx, i8 dy, u16 tile, u8 type, u16 hp, u16 bonus)
  78. {
  79.     i8 npc = find_free_npc();
  80.  
  81.     if (npc != -1)
  82.     {
  83.         npcs[npc].sprite.tile = tile;
  84.        if (x < 16) x = 16;
  85.        if (x > 296) x = 296;
  86.         npcs[npc].sprite.x = x;
  87.         npcs[npc].sprite.y = y;
  88.         npcs[npc].sprite.dx = dx;
  89.         npcs[npc].sprite.dy = dy;
  90.         npcs[npc].hp = hp;
  91.         npcs[npc].type = type;
  92.         npcs[npc].take_bonus = bonus;
  93.     }
  94. }
  95.  
  96. static void push_bonus(u16 type, u16 x, u16 y, i8 dx, i8 dy)
  97. {
  98.     i8 b = find_free_bonus();
  99.     if (b >= 0)
  100.     {
  101.         bonuses[b].x = x;
  102.         bonuses[b].y = y;
  103.         bonuses[b].tile = type;
  104.         bonuses[b].dx = dx;
  105.         bonuses[b].dy = dy;
  106.     }
  107. }
  108.  
  109. static void push_bullet(u16 x, u16 y, i8 dx, i8 dy)
  110. {
  111.     i8 b = find_free_bullet();
  112.     if (b != -1)
  113.     {
  114.         bullets[b].x = x;
  115.         bullets[b].y = y;
  116.         bullets[b].dx = dx;
  117.         bullets[b].dy = dy;
  118.         bullets[b].tile = bullet_sprite;
  119.     }
  120. }
  121.  
  122. static void push_star(u16 x, u16 y)
  123. {
  124.     push_bullet(x + 2, y + 3, 2, 3);
  125.     push_bullet(x + 3, y + 0, 3, 0);
  126.     push_bullet(x + 3, y - 2, 3, -2);
  127.     push_bullet(x + 1, y - 4, 1, -4);
  128.     push_bullet(x - 1, y - 4, -1, -4);
  129.     push_bullet(x - 4, y - 3, -4, -3);
  130.     push_bullet(x - 4, y - 1, -4, -1);
  131.     push_bullet(x - 4, y + 2, -4, 2);
  132.     push_bullet(x - 2, y + 3, -2, 3);
  133. }
  134.  
  135. static void npc_type_shoot(u8 type, i8 dx, i8 dy)
  136. {
  137.     static u8 npc;
  138.     for (npc = 0; npc < NPC_count; npc++)
  139.     {
  140.         if (is_npc_right_type(&npcs[npc], type))
  141.         {
  142.             push_bullet(npcs[npc].sprite.x + 16, npcs[npc].sprite.y + 16, dx, dy);
  143.         }
  144.     }
  145. }
  146.  
  147. static i16 abs(i16 v)
  148. {
  149.     if (v < 0)
  150.         return -v;
  151.  
  152.     return v;
  153. }
  154.  
  155. static i16 sign(i16 v)
  156. {
  157.     if (v < 0)
  158.         return -1;
  159.  
  160.     return 1;
  161. }
  162.  
  163. static i16 min(i16 x, i16 y)
  164. {
  165.     return x < y ? x : y;
  166. }
  167.  
  168. static i16 max(i16 x, i16 y)
  169. {
  170.     return x > y ? x : y;
  171. }
  172.  
  173. static void npc_aimed_shoot(u8 type)
  174. {
  175.     i16 dx;
  176.     i16 dy;
  177.     static u8 npc;
  178.     for (npc = 0; npc < NPC_count; npc++)
  179.     {
  180.         if (is_npc_right_type(&npcs[npc], type))
  181.         {
  182.             dx = player.x - npcs[npc].sprite.x;
  183.             dy = player.y - npcs[npc].sprite.y;
  184.             dx = sign(dx) * min(abs(dx) / 8, 5);
  185.             dy = sign(dy) * min(abs(dy) / 8, 5);
  186.  
  187.             push_bullet(npcs[npc].sprite.x + 16, npcs[npc].sprite.y + 16, dx, dy);
  188.         }
  189.     }
  190. }
  191.  
  192. static void npc_shoot(i8 dx, u8 dy)
  193. {
  194.     static u8 npc;
  195.     for (npc = 0; npc < NPC_count; npc++)
  196.     {
  197.         if (is_npc_active(&npcs[npc]))
  198.         {
  199.             push_bullet(npcs[npc].sprite.x + 16, npcs[npc].sprite.y + 16, dx, dy);
  200.         }
  201.     }
  202. }
  203.  
  204. static void npc_type_star(u8 type)
  205. {
  206.     static u8 npc;
  207.     for (npc = 0; npc < NPC_count; npc++)
  208.     {
  209.         if (is_npc_right_type(&npcs[npc], type))
  210.         {
  211.             push_star(npcs[npc].sprite.x + 16, npcs[npc].sprite.y + 16);
  212.         }
  213.     }
  214. }
  215.  
  216. static void npc_by_type_set_speed(u8 type, i8 dx, i8 dy)
  217. {
  218.     static u8 npc;
  219.     for (npc = 0; npc < NPC_count; npc++)
  220.     {
  221.         if (is_npc_right_type(&npcs[npc], type))
  222.         {
  223.             npcs[npc].sprite.dx = dx;
  224.             npcs[npc].sprite.dy = dy;
  225.         }
  226.     }
  227. }
  228.  
  229. static void user_fire()
  230. {
  231.     i8 bullet = find_free_my_bullet();
  232.     if (bullet > -1)
  233.     {
  234.         my_bullets[bullet].tile = MY_BULLET;
  235.         my_bullets[bullet].x = player.x + 8;
  236.         my_bullets[bullet].y = player.y - 8;
  237.         my_bullets[bullet].dx = 0;
  238.         my_bullets[bullet].dy = -16;
  239.     }
  240. }
  241.  
  242. static u8 is_npc_right_type(Npc *n, u8 type)
  243. {
  244.     return n->type == type && is_npc_active(n);
  245. }
  246.  
  247. static u8 is_player_collide_with_bullet(MovableSprite *bullet)
  248. {
  249.     return bullet->x < player.x + 28 &&
  250.            bullet->x + 8 > player.x + 4 &&
  251.            bullet->y < player.y + 28 &&
  252.            bullet->y + 8 > player.y + 4;
  253. }
  254.  
  255. static u8 is_player_collide_with_enemy(MovableSprite *enemy)
  256. {
  257.     return enemy->x + 4 < player.x + 28 &&
  258.            enemy->x + 28 > player.x &&
  259.            enemy->y + 4 < player.y + 28 &&
  260.            enemy->y + 28 > player.y;
  261. }
  262.  
  263. static u8 is_enemy_collide_with_bullet(MovableSprite *enemy, MovableSprite *bullet)
  264. {
  265.     return enemy->x < bullet->x + 8 &&
  266.            enemy->x + 28 > bullet->x &&
  267.            enemy->y + 4 < bullet->y + 16 &&
  268.            enemy->y + 28 > bullet->y;
  269. }
  270.  
  271. static MovableSprite *is_player_collide_with_pups()
  272. {
  273.     static u8 cnt;
  274.     for (cnt = 0; cnt < MAX_BONUS_COUNT; cnt++)
  275.     {
  276.         if (bonuses[cnt].y < MAX_Y_RES &&
  277.             bonuses[cnt].x < player.x + 28 &&
  278.             bonuses[cnt].x + 8 > player.x + 4 &&
  279.             bonuses[cnt].y < player.y + 28 &&
  280.             bonuses[cnt].y + 8 > player.y + 4)
  281.         {
  282.             bonuses[cnt].y = MAX_Y_RES + 1;
  283.             return &bonuses[cnt];
  284.         }
  285.     }
  286.  
  287.     return 0;
  288. }
  289.  
  290. static u8 is_player_collide()
  291. {
  292. #ifdef CHEAT
  293. if (cheat_on) return 0;
  294. #endif
  295.     for (cnt = 0; cnt < BULLETS_count; cnt++)
  296.         if (is_bullet_active(&bullets[cnt]) && is_player_collide_with_bullet(&bullets[cnt]))
  297.         {
  298.             return 1;
  299.         }
  300.  
  301.     for (cnt = 0; cnt < NPC_count; cnt++)
  302.         if (is_npc_active(&npcs[cnt]) && is_player_collide_with_enemy(&npcs[cnt].sprite))
  303.         {
  304.             npcs[cnt].hp--;
  305.             return 1;
  306.         }
  307.  
  308.     return 0;
  309. }
  310.  
  311. static void enemy_collision()
  312. {
  313.     static u8 ec, cnt;
  314.     u8 is_played = 0;
  315.     for (cnt = 0; cnt < MY_BULLETS_max; cnt++)
  316.         if (is_bullet_active(&my_bullets[cnt]))
  317.             for (ec = 0; ec < NPC_count; ec++)
  318.                 if (is_npc_active(&npcs[ec]) && is_enemy_collide_with_bullet(&npcs[ec].sprite, &my_bullets[cnt]))
  319.                 {
  320.                     if (my_bullets[cnt].tile == MY_BULLET)
  321.                         my_bullets[cnt].tile = MY_BULLET + 2;
  322.  
  323.                     if (!is_played)
  324.                     {
  325.                         sfx_play(0, -3);
  326.                         is_played = 1;
  327.                     }
  328.                     npcs[ec].hp--;
  329.                     score ++;
  330.                 }
  331. }
  332.  
  333. static Npc *find_npc_by_type(u8 type)
  334. {
  335.     u8 i;
  336.     for (i = 0; i < NPC_count; i++)
  337.     {
  338.         if (is_npc_right_type(&npcs[i], type))
  339.         {
  340.             return &npcs[i];
  341.         }
  342.     }
  343.  
  344.     return 0;
  345. }
  346. #endif