Subversion Repositories Kolibri OS

Rev

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