Subversion Repositories Kolibri OS

Rev

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

  1. /* Rocket Forces
  2.  * Filename: rforces.cpp
  3.  * Version 0.1
  4.  * Copyright (c) Serial 2007
  5.  */
  6.  
  7. /* Version 0.2
  8.  * Copyright (c) Leency 2018
  9.  */
  10.  
  11.  
  12. #include <stdlib.h>
  13.  
  14. #include "kosSyst.h"
  15. #include "kosFile.h"
  16. #include "mymath.h"
  17. #include "properties.h"
  18. #include "objects.h"
  19. #include "rforces.h"
  20.  
  21.  
  22. const char header[] = GAME_NAME;
  23. cCursor *cursor;
  24. cGun *gun = new cGun;
  25. cCross **crosses = new cCross*[R_COUNT];
  26. cRocket **rockets = new cRocket*[R_COUNT];
  27. cBomb **bombs = new cBomb*[B_COUNT];
  28. cExplode **explodes = new cExplode*[R_COUNT + B_COUNT];
  29. cBuilding *house = new cBuilding();
  30. Dword *cur_handle;
  31. int score, health;
  32.  
  33. bool game_over;
  34.  
  35. struct MouseState
  36. {
  37.         int x, y, lbclick;     
  38.         Dword buttons;
  39. } ms;
  40.  
  41.  
  42. void kos_Main()
  43. {
  44.         Dword frame_start, frame_end;
  45.         OnStart();
  46.         Menu();
  47.         kos_SetMaskForEvents(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER);
  48.         for (;;)
  49.         {
  50.                 frame_start = kos_GetTime();
  51.                 switch (kos_CheckForEvent())
  52.                 {
  53.                 case EM_KEY_PRESS:
  54.                         Byte keyCode;
  55.                         kos_GetKey(keyCode);
  56.                         if (keyCode == 27)
  57.                         {
  58.                                 kos_ExitApp();
  59.                         }
  60.                         if (keyCode == 51)
  61.                         {
  62.                                 OnStart();
  63.                         }
  64.                         break;
  65.                 case EM_BUTTON_CLICK:
  66.                         Dword btn_id;
  67.                         if (kos_GetButtonID(btn_id)) {
  68.                                 if (btn_id == 1) kos_ExitApp();
  69.                         }
  70.                         break;
  71.                 case EM_MOUSE_EVENT:
  72.                         OnMouseMove();
  73.                         if (ms.lbclick == 1)
  74.                         {
  75.                                 OnLMBClick();
  76.                         }
  77.                         break;
  78.                 case EM_WINDOW_REDRAW:
  79.                         DrawWindow();
  80.                         break;
  81.                 default:
  82.                         if (game_over) break;
  83.                         DrawBombs();
  84.                         DrawRocketsAndCrosses();
  85.                         DrawExplodes();
  86.                         DrawStats();
  87.                         frame_end = kos_GetTime();
  88.                         if (frame_end - frame_start < FRAME_TIME) {
  89.                                 kos_Pause(FRAME_TIME - (frame_end - frame_start));
  90.                         }
  91.                         if (health <= 0) GameOver();
  92.                 }
  93.         }
  94. }
  95.  
  96. void DrawWindow()
  97. {
  98.         kos_WindowRedrawStatus(1);
  99.         kos_DefineAndDrawWindow(10, 40, WINDOW_WIDTH + 12,
  100.                 WINDOW_HEIGHT + kos_GetSkinHeight() + 12, 0x34,
  101.                 BG_COLOR, 0, 0, (Dword)header);
  102.         kos_WindowRedrawStatus(2);
  103.  
  104.         DrawStats();
  105.         if (health <= 0) GameOver();
  106.  
  107.         // Draw buildings
  108.         for (int i = 20; i < 5 * 50; i += 50)
  109.         {
  110.                 house->Draw(i, 467, H_COLOR);
  111.         }
  112.         for (int i = 8 * 50; i < 13 * 50; i += 50)
  113.         {
  114.                 house->Draw(i, 467, H_COLOR);
  115.         }
  116.  
  117. }
  118.  
  119. void DrawBombs()
  120. {
  121.         for (int i = 0; i < B_COUNT; i++)
  122.         {
  123.                 if (bombs[i]->IsEnabled() == 0)
  124.                 {
  125.                         int rnd;
  126.                         rnd = rtlRand() % B_POSSIBILITY;
  127.                         if (rnd == 1)
  128.                         {
  129.                                 rnd = 10 + rtlRand() % 620;
  130.                                 bombs[i]->Enable(rnd, 0, 4, 9, rnd + 2, 0);
  131.                         }
  132.                 }
  133.                 else
  134.                 {
  135.                         if (bombs[i]->cy > gun->cy + 5)
  136.                         {
  137.                                 health -= 5;
  138.                                 if (explodes[R_COUNT + i]->IsEnabled() == 1)
  139.                                 {
  140.                                         explodes[R_COUNT + i]->Disable(BG_COLOR);
  141.                                 }
  142.                                 explodes[R_COUNT + i]->Enable(bombs[i]->cx, bombs[i]->cy);
  143.                                 bombs[i]->Disable(BG_COLOR);
  144.                         }
  145.                         else
  146.                         {
  147.                                 bombs[i]->cy += B_SPEED;
  148.                                 bombs[i]->DrawAngle(bombs[i]->cx, 639, B_COLOR);
  149.                         }
  150.                 }
  151.         }
  152. }
  153.  
  154. void DrawRocketsAndCrosses()
  155. {
  156.         double a;
  157.         for (int i = 0; i < R_COUNT; i++)
  158.         {
  159.                 if (crosses[i]->IsEnabled() == 1)
  160.                 {
  161.                         if (sqrt(((long int) (crosses[i]->x - rockets[i]->cx) * (crosses[i]->x - rockets[i]->cx)) + ((long int) (crosses[i]->y - rockets[i]->cy) * (crosses[i]->y - rockets[i]->cy))) < 5)
  162.                         {
  163.                                 if (explodes[i]->IsEnabled() == 1)
  164.                                 {
  165.                                         explodes[i]->Disable(BG_COLOR);
  166.                                 }
  167.                                 explodes[i]->Enable(crosses[i]->x, crosses[i]->y);
  168.                                 crosses[i]->Disable(BG_COLOR);
  169.                                 rockets[i]->Disable(BG_COLOR);
  170.                         }
  171.                         else
  172.                         {
  173.                                 crosses[i]->Draw(CROSS_COLOR);
  174.                                 if (rockets[i]->cx - crosses[i]->x == 0)
  175.                                 {
  176.                                         a = M_PI / 2;
  177.                                 }
  178.                                 else
  179.                                 {
  180.                                         a = atan((double)(rockets[i]->cy - crosses[i]->y) / (double)(rockets[i]->cx - crosses[i]->x));
  181.                                         if (rockets[i]->cx - crosses[i]->x < 0) a += M_PI;
  182.                                 }
  183.                                 rockets[i]->cx = round_int(rockets[i]->cx - R_SPEED * cos(a));
  184.                                 rockets[i]->cy = round_int(rockets[i]->cy - R_SPEED * sin(a));
  185.                                 rockets[i]->DrawAngle(crosses[i]->x, crosses[i]->y, R_COLOR);
  186.                         }
  187.                 }
  188.         }
  189. }
  190.  
  191. void DrawExplodes()
  192. {
  193.         for (int i = 0; i < R_COUNT + B_COUNT; i++)
  194.         {
  195.                 if (explodes[i]->IsEnabled() == 1)
  196.                 {
  197.                         explodes[i]->DrawNext(EXP_COLOR);
  198.                         for (int j = 0; j < B_COUNT; j++)
  199.                         {
  200.                                 if ( bombs[j]->IsEnabled() == 1 &&
  201.                                          bombs[j]->cx > explodes[i]->cx - explodes[i]->step - 1 && bombs[j]->cx < explodes[i]->cx + explodes[i]->step + 1 &&
  202.                                          bombs[j]->cy + 5 > explodes[i]->cy - explodes[i]->step - 1 && bombs[j]->cy + 5 < explodes[i]->cy + explodes[i]->step + 1
  203.                                         )
  204.                                 {
  205.                                         score += B_COUNT + 2;
  206.                                         if (explodes[R_COUNT + j]->IsEnabled() == 1)
  207.                                         {
  208.                                                 explodes[R_COUNT + j]->Disable(BG_COLOR);
  209.                                         }
  210.                                         explodes[R_COUNT + j]->Enable(bombs[j]->cx, bombs[j]->cy);
  211.                                         bombs[j]->Disable(BG_COLOR);
  212.                                 }
  213.                         }
  214.                 }
  215.         }
  216. }
  217.  
  218. void OnMouseMove()
  219. {
  220.         Dword old_buttons = ms.buttons;
  221.         kos_GetMouseWindowXY(ms.x, ms.y);
  222.         kos_GetMouseButtonsState(ms.buttons);
  223.  
  224.         if (health <= 0) return;
  225.  
  226.         //restore mouse cursor when it over Window Header
  227.         if (ms.y > 5000) RestoreSystemCursor(); else SetGameCursor();
  228.  
  229.         if ((old_buttons & 0x00000001) == 0 && (ms.buttons & 0x00000001) == 1)
  230.         {
  231.                 ms.lbclick = 1;
  232.         }
  233.         else
  234.         {
  235.                 ms.lbclick = 0;
  236.         }
  237.  
  238.         if (ms.x >= 0 && ms.x < WINDOW_WIDTH &&  ms.y >= 0 && ms.y < WINDOW_HEIGHT)
  239.         {
  240.                 gun->DrawAngle(ms.x, ms.y, G_COLOR);
  241.         }
  242.        
  243.         if (HARDWARE_CURSOR == 0)
  244.         {
  245.                 cursor->Draw(ms.x, ms.y, CUR_COLOR);
  246.         }
  247.  
  248.         /*if (DEBUG == 1)
  249.         {
  250.                 kos_DisplayNumberToWindowBg(ms.x, 3, WINDOW_WIDTH - 30, 10, TEXT_COLOR, BG_COLOR, nbDecimal, false);
  251.                 kos_DisplayNumberToWindowBg(ms.y, 3, WINDOW_WIDTH - 30, 22, TEXT_COLOR, BG_COLOR, nbDecimal, false);
  252.                 kos_DisplayNumberToWindowBg(ms.buttons, 1, WINDOW_WIDTH - 30, 34, TEXT_COLOR, BG_COLOR, nbDecimal, false);
  253.         }*/
  254. }
  255.  
  256. void DrawStats()
  257. {
  258.         kos_WriteTextWithBg(8, 10, 0xC0, TEXT_COLOR, 0, "Population:", 11);
  259.         kos_WriteTextWithBg(8+15*6, 9, 0xC0, TEXT_COLOR, 0, "%", 1);
  260.         kos_WriteTextWithBg(8, 22, 0xC0, TEXT_COLOR, 0, "Score:", 6);
  261.         kos_DisplayNumberToWindowBg(health, 3, 79, 10, TEXT_COLOR, BG_COLOR, nbDecimal, false);
  262.         kos_DisplayNumberToWindowBg(score, 4, 49, 22, TEXT_COLOR, BG_COLOR, nbDecimal, false);
  263. }
  264.  
  265. void OnLMBClick()
  266. {
  267.         if (ms.y < gun->cy - 10)
  268.         {
  269.                 double a;
  270.                 int j = -1;
  271.                 for (int i = 0; i < R_COUNT; i++)
  272.                 {
  273.                         if (crosses[i]->IsEnabled() == 0)
  274.                         {
  275.                                 if (j >= -1) j = i;
  276.                         }
  277.                         else if (ms.x > crosses[i]->x - 10 && ms.x < crosses[i]->x + 10 && ms.y > crosses[i]->y - 10 && ms.y < crosses[i]->y + 10)
  278.                         {
  279.                                 j = -2;
  280.                                 break;
  281.                         }
  282.                 }
  283.                 if (j >= 0)
  284.                 {
  285.                         if (score > 0) score -= 1;
  286.                         crosses[j]->Enable(ms.x, ms.y);
  287.                         if (gun->cx - ms.x == 0)
  288.                         {
  289.                                 a = M_PI/2;
  290.                         }
  291.                         else
  292.                         {
  293.                                 a = atan((double)gun->cy - ms.y / (double) gun->cx - ms.x);
  294.                                 if (gun->cx - ms.x < 0) a += M_PI;
  295.                         }
  296.                         rockets[j]->Enable(round_int(gun->cx - 15 * cos(a)) - 2, round_int(gun->cy - 15 * sin(a)) - 5, 3, 6, round_int(gun->cx - 15 * cos(a)), round_int(gun->cy - 15 * sin(a)));
  297.                 }
  298.         }
  299. }
  300.  
  301. void SetGameCursor()
  302. {
  303.         Dword *cur = new Dword[1024];
  304.         for (int i = 0; i < 1024; i++)
  305.         {
  306.                 cur[i] = 0x00000000;
  307.         }
  308.         if (HARDWARE_CURSOR == 1)
  309.         {
  310.                 Dword cur_color = 0xFF000000 | CUR_COLOR;
  311.                 cur[0 * 32 + 5] = cur_color;
  312.                 cur[1 * 32 + 5] = cur_color;
  313.                 cur[2 * 32 + 5] = cur_color;
  314.                 cur[2 * 32 + 3] = cur_color;
  315.                 cur[2 * 32 + 4] = cur_color;
  316.                 cur[2 * 32 + 6] = cur_color;
  317.                 cur[3 * 32 + 2] = cur_color;
  318.                 cur[4 * 32 + 2] = cur_color;
  319.                 cur[5 * 32 + 2] = cur_color;
  320.                 cur[5 * 32 + 1] = cur_color;
  321.                 cur[5 * 32 + 0] = cur_color;
  322.  
  323.                 cur[5 * 32 + 5] = cur_color;
  324.  
  325.                 cur[8 * 32 + 4] = cur_color;
  326.                 cur[8 * 32 + 5] = cur_color;
  327.                 cur[8 * 32 + 6] = cur_color;
  328.                 cur[8 * 32 + 7] = cur_color;
  329.                 cur[9 * 32 + 5] = cur_color;
  330.                 cur[10 * 32 + 5] = cur_color;
  331.                 cur[7 * 32 + 8] = cur_color;
  332.                 cur[6 * 32 + 8] = cur_color;
  333.                 cur[5 * 32 + 8] = cur_color;
  334.                 cur[5 * 32 + 9] = cur_color;
  335.                 cur[5 * 32 + 10] = cur_color;
  336.         }
  337.         cur_handle = kos_LoadMouseCursor(cur, 0x05050002);
  338.         delete[] cur;
  339.         kos_SetMouseCursor(cur_handle);
  340. }
  341.  
  342. void RestoreSystemCursor()
  343. {
  344.         if (cur_handle) kos_SetMouseCursor(0);
  345.         cur_handle=0;
  346. }
  347.  
  348. void Menu()
  349. {
  350.         NewGame();
  351. }
  352.  
  353. void NewGame()
  354. {
  355.         gun->DrawAngle((WINDOW_WIDTH / 2) - 5, WINDOW_HEIGHT - 20, G_COLOR);
  356. }
  357.  
  358. void OnStart()
  359. {
  360.         if (HARDWARE_CURSOR == 0)
  361.         {
  362.                 cursor = new cCursor();
  363.         }
  364.         SetGameCursor();
  365.  
  366.         gun->Enable((WINDOW_WIDTH / 2) - 10, WINDOW_HEIGHT - 30, 10, 20, (WINDOW_WIDTH / 2) - 5, WINDOW_HEIGHT - 20);
  367.  
  368.         for (int i = 0; i < R_COUNT; i++)
  369.         {
  370.                 crosses[i] = new cCross();
  371.                 rockets[i] = new cRocket();
  372.         }
  373.         for (int i = 0; i < B_COUNT; i++)
  374.         {
  375.                 bombs[i] = new cBomb();
  376.         }
  377.         for (int i = 0; i < R_COUNT + B_COUNT; i++)
  378.         {
  379.                 explodes[i] = new cExplode();
  380.         }
  381.  
  382.         health = 100;
  383.         score = 0;
  384.  
  385.         game_over = false;
  386.  
  387.         rtlSrand(kos_GetTime());
  388.  
  389.         DrawWindow();
  390. }
  391.  
  392. void GameOver()
  393. {
  394.         int xcenter = WINDOW_WIDTH / 2;
  395.         int y = WINDOW_HEIGHT/ 2 - 40;
  396.         kos_WriteTextToWindow(xcenter-50, y, 0x81, TEXT_COLOR, "Game Over", 9);
  397.         kos_WriteTextToWindow(xcenter-43, y+36, 0x80, TEXT_COLOR, "[F2] - New game", 0);
  398.         kos_WriteTextToWindow(xcenter-43, y+53, 0x80, TEXT_COLOR, "[Ecs] - Exit", 0);
  399.         //
  400.         RestoreSystemCursor();
  401.         game_over = true;
  402. }
  403.