Subversion Repositories Kolibri OS

Rev

Rev 7484 | Rev 7493 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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