Subversion Repositories Kolibri OS

Rev

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