Subversion Repositories Kolibri OS

Rev

Rev 7481 | 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.         kos_WriteTextToWindow(8, 10, 0, TEXT_COLOR, "Population:    %", 16);
  97.         kos_WriteTextToWindow(8, 22, 0, TEXT_COLOR, "Score:", 6);
  98.  
  99.         OnMouseMove();
  100.  
  101.         // Draw buildings
  102.         for (int i = 20; i < 5 * 50; i += 50)
  103.         {
  104.                 house->Draw(i, 467, H_COLOR);
  105.         }
  106.         for (int i = 8 * 50; i < 13 * 50; i += 50)
  107.         {
  108.                 house->Draw(i, 467, H_COLOR);
  109.         }
  110.  
  111. }
  112.  
  113. void DrawBombs()
  114. {
  115.         for (int i = 0; i < B_COUNT; i++)
  116.         {
  117.                 if (bombs[i]->IsEnabled() == 0)
  118.                 {
  119.                         int rnd;
  120.                         rnd = rtlRand() % B_POSSIBILITY;
  121.                         if (rnd == 1)
  122.                         {
  123.                                 rnd = 10 + rtlRand() % 620;
  124.                                 bombs[i]->Enable(rnd, 0, 4, 9, rnd + 2, 0);
  125.                         }
  126.                 }
  127.                 else
  128.                 {
  129.                         if (bombs[i]->cy > gun->cy + 5)
  130.                         {
  131.                                 health -= 5;
  132.                                 if (explodes[R_COUNT + i]->IsEnabled() == 1)
  133.                                 {
  134.                                         explodes[R_COUNT + i]->Disable(BG_COLOR);
  135.                                 }
  136.                                 explodes[R_COUNT + i]->Enable(bombs[i]->cx, bombs[i]->cy);
  137.                                 bombs[i]->Disable(BG_COLOR);
  138.                         }
  139.                         else
  140.                         {
  141.                                 bombs[i]->cy += B_SPEED;
  142.                                 bombs[i]->DrawAngle(bombs[i]->cx, 639, B_COLOR);
  143.                         }
  144.                 }
  145.         }
  146. }
  147.  
  148. void DrawRocketsAndCrosses()
  149. {
  150.         double a;
  151.         for (int i = 0; i < R_COUNT; i++)
  152.         {
  153.                 if (crosses[i]->IsEnabled() == 1)
  154.                 {
  155.                         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)
  156.                         {
  157.                                 if (explodes[i]->IsEnabled() == 1)
  158.                                 {
  159.                                         explodes[i]->Disable(BG_COLOR);
  160.                                 }
  161.                                 explodes[i]->Enable(crosses[i]->x, crosses[i]->y);
  162.                                 crosses[i]->Disable(BG_COLOR);
  163.                                 rockets[i]->Disable(BG_COLOR);
  164.                         }
  165.                         else
  166.                         {
  167.                                 crosses[i]->Draw(CROSS_COLOR);
  168.                                 if (rockets[i]->cx - crosses[i]->x == 0)
  169.                                 {
  170.                                         a = M_PI / 2;
  171.                                 }
  172.                                 else
  173.                                 {
  174.                                         a = atan((double)(rockets[i]->cy - crosses[i]->y) / (double)(rockets[i]->cx - crosses[i]->x));
  175.                                         if (rockets[i]->cx - crosses[i]->x < 0) a += M_PI;
  176.                                 }
  177.                                 rockets[i]->cx = round_int(rockets[i]->cx - R_SPEED * cos(a));
  178.                                 rockets[i]->cy = round_int(rockets[i]->cy - R_SPEED * sin(a));
  179.                                 rockets[i]->DrawAngle(crosses[i]->x, crosses[i]->y, R_COLOR);
  180.                         }
  181.                 }
  182.         }
  183. }
  184.  
  185. void DrawExplodes()
  186. {
  187.         for (int i = 0; i < R_COUNT + B_COUNT; i++)
  188.         {
  189.                 if (explodes[i]->IsEnabled() == 1)
  190.                 {
  191.                         explodes[i]->DrawNext(EXP_COLOR);
  192.                         for (int j = 0; j < B_COUNT; j++)
  193.                         {
  194.                                 if ( bombs[j]->IsEnabled() == 1 &&
  195.                                          bombs[j]->cx > explodes[i]->cx - explodes[i]->step - 1 && bombs[j]->cx < explodes[i]->cx + explodes[i]->step + 1 &&
  196.                                          bombs[j]->cy + 5 > explodes[i]->cy - explodes[i]->step - 1 && bombs[j]->cy + 5 < explodes[i]->cy + explodes[i]->step + 1
  197.                                         )
  198.                                 {
  199.                                         score += B_COUNT + 2;
  200.                                         if (explodes[R_COUNT + j]->IsEnabled() == 1)
  201.                                         {
  202.                                                 explodes[R_COUNT + j]->Disable(BG_COLOR);
  203.                                         }
  204.                                         explodes[R_COUNT + j]->Enable(bombs[j]->cx, bombs[j]->cy);
  205.                                         bombs[j]->Disable(BG_COLOR);
  206.                                 }
  207.                         }
  208.                 }
  209.         }
  210. }
  211.  
  212. void OnMouseMove()
  213. {
  214.         Dword old_buttons = ms.buttons;
  215.         kos_GetMouseWindowXY(ms.x, ms.y);
  216.         kos_GetMouseButtonsState(ms.buttons);
  217.         if ((old_buttons & 0x00000001) == 0 && (ms.buttons & 0x00000001) == 1)
  218.         {
  219.                 ms.lbclick = 1;
  220.         }
  221.         else
  222.         {
  223.                 ms.lbclick = 0;
  224.         }
  225.  
  226.         kos_DisplayNumberToWindowBg(health, 3, 79, 10, TEXT_COLOR, BG_COLOR, nbDecimal, false);
  227.         kos_DisplayNumberToWindowBg(score, 4, 49, 22, TEXT_COLOR, BG_COLOR, nbDecimal, false);
  228.  
  229.         if (ms.x >= 0 && ms.x < WINDOW_WIDTH &&  ms.y >= 0 && ms.y < WINDOW_HEIGHT)
  230.         {
  231.                 gun->DrawAngle(ms.x, ms.y, G_COLOR);
  232.         }
  233.        
  234.         if (HARDWARE_CURSOR == 0)
  235.         {
  236.                 cursor->Draw(ms.x, ms.y, CUR_COLOR);
  237.         }
  238.  
  239.         /*if (DEBUG == 1)
  240.         {
  241.                 kos_DisplayNumberToWindowBg(ms.x, 3, WINDOW_WIDTH - 30, 10, TEXT_COLOR, BG_COLOR, nbDecimal, false);
  242.                 kos_DisplayNumberToWindowBg(ms.y, 3, WINDOW_WIDTH - 30, 22, TEXT_COLOR, BG_COLOR, nbDecimal, false);
  243.                 kos_DisplayNumberToWindowBg(ms.buttons, 1, WINDOW_WIDTH - 30, 34, TEXT_COLOR, BG_COLOR, nbDecimal, false);
  244.         }*/
  245.  
  246. }
  247.  
  248. void OnLMBClick()
  249. {
  250.         if (ms.y < gun->cy - 10)
  251.         {
  252.                 double a;
  253.                 int j = -1;
  254.                 for (int i = 0; i < R_COUNT; i++)
  255.                 {
  256.                         if (crosses[i]->IsEnabled() == 0)
  257.                         {
  258.                                 if (j >= -1) j = i;
  259.                         }
  260.                         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)
  261.                         {
  262.                                 j = -2;
  263.                                 break;
  264.                         }
  265.                 }
  266.                 if (j >= 0)
  267.                 {
  268.                         if (score > 0) score -= 1;
  269.                         crosses[j]->Enable(ms.x, ms.y);
  270.                         if (gun->cx - ms.x == 0)
  271.                         {
  272.                                 a = M_PI/2;
  273.                         }
  274.                         else
  275.                         {
  276.                                 a = atan((double)gun->cy - ms.y / (double) gun->cx - ms.x);
  277.                                 if (gun->cx - ms.x < 0) a += M_PI;
  278.                         }
  279.                         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)));
  280.                 }
  281.         }
  282. }
  283.  
  284. void OnRMBClick()
  285. {
  286. }
  287.  
  288. void ChangeCursor()
  289. {
  290.         Dword *cur = new Dword[1024];
  291.         for (int i = 0; i < 1024; i++)
  292.         {
  293.                 cur[i] = 0x00000000;
  294.         }
  295.         if (HARDWARE_CURSOR == 1)
  296.         {
  297.                 Dword cur_color = 0xFF000000 | CUR_COLOR;
  298.                 cur[0 * 32 + 5] = cur_color;
  299.                 cur[1 * 32 + 5] = cur_color;
  300.                 cur[2 * 32 + 5] = cur_color;
  301.                 cur[2 * 32 + 3] = cur_color;
  302.                 cur[2 * 32 + 4] = cur_color;
  303.                 cur[2 * 32 + 6] = cur_color;
  304.                 cur[3 * 32 + 2] = cur_color;
  305.                 cur[4 * 32 + 2] = cur_color;
  306.                 cur[5 * 32 + 2] = cur_color;
  307.                 cur[5 * 32 + 1] = cur_color;
  308.                 cur[5 * 32 + 0] = cur_color;
  309.  
  310.                 cur[5 * 32 + 5] = cur_color;
  311.  
  312.                 cur[8 * 32 + 4] = cur_color;
  313.                 cur[8 * 32 + 5] = cur_color;
  314.                 cur[8 * 32 + 6] = cur_color;
  315.                 cur[8 * 32 + 7] = cur_color;
  316.                 cur[9 * 32 + 5] = cur_color;
  317.                 cur[10 * 32 + 5] = cur_color;
  318.                 cur[7 * 32 + 8] = cur_color;
  319.                 cur[6 * 32 + 8] = cur_color;
  320.                 cur[5 * 32 + 8] = cur_color;
  321.                 cur[5 * 32 + 9] = cur_color;
  322.                 cur[5 * 32 + 10] = cur_color;
  323.         }
  324.         cur_handle = kos_LoadMouseCursor(cur, 0x05050002);
  325.         delete[] cur;
  326.         kos_SetMouseCursor(cur_handle);
  327. }
  328.  
  329. void Menu()
  330. {
  331.         NewGame();
  332. }
  333.  
  334. void NewGame()
  335. {
  336.         gun->DrawAngle((WINDOW_WIDTH / 2) - 5, WINDOW_HEIGHT - 20, G_COLOR);
  337. }
  338.  
  339. void OnStart()
  340. {
  341.         if (HARDWARE_CURSOR == 0)
  342.         {
  343.                 cursor = new cCursor();
  344.         }
  345.         ChangeCursor();
  346.  
  347.         gun->Enable((WINDOW_WIDTH / 2) - 10, WINDOW_HEIGHT - 30, 10, 20, (WINDOW_WIDTH / 2) - 5, WINDOW_HEIGHT - 20);
  348.  
  349.         for (int i = 0; i < R_COUNT; i++)
  350.         {
  351.                 crosses[i] = new cCross();
  352.                 rockets[i] = new cRocket();
  353.         }
  354.         for (int i = 0; i < B_COUNT; i++)
  355.         {
  356.                 bombs[i] = new cBomb();
  357.         }
  358.         for (int i = 0; i < R_COUNT + B_COUNT; i++)
  359.         {
  360.                 explodes[i] = new cExplode();
  361.         }
  362.  
  363.         health = 100;
  364.         score = 0;
  365.  
  366.         rtlSrand(kos_GetTime());
  367.  
  368.         DrawWindow();
  369. }
  370.  
  371. void OnExit()
  372. {
  373.         kos_WriteTextToWindow(WINDOW_WIDTH / 2 - 35, WINDOW_HEIGHT / 2 - 10, 0, TEXT_COLOR, "Game Over", 9);
  374.  
  375.         kos_Pause(200);
  376.  
  377.         kos_ExitApp();
  378. }
  379.