00001 #include <vector>
00002 #include <iostream>
00003
00004 #include "game.h"
00005 #include "sound.h"
00006
00007 Game::Game(Graphics &G, Input &I, Sound &S) : GD(G), ID(I), SD(S)
00008 {
00009 Background *background;
00010
00011 exit = false;
00012
00013
00014 currentSubWorld = new SubWorld(GD);
00015 OM.SetSubWorld (currentSubWorld);
00016
00017 background = &(currentSubWorld->GetBackground());
00018
00019
00020 OM.AddObject (OT_CROSSHAIR);
00021 rapidfire = false;
00022
00023 OM.AddObject (OT_MAINCHAR);
00024 OM.GetObject (OT_MAINCHAR)->SetPosition(background->GetWidth()/2, background->GetHeight()/2);
00025 OM.GetMainChar ()->SetWalkingBounds(GD.getWidth()/2, GD.getHeight()/2, background->GetMaxX(), background->GetMaxY());
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 OM.AddObject(OT_BEAR)->SetPosition(400,400);
00039
00040
00041 Sound::PlayTheme(-1);
00042
00043 rotation = 0;
00044 }
00045
00046 Game::~Game()
00047 {
00048 if (currentSubWorld)
00049 delete currentSubWorld;
00050 }
00051
00052 bool Game::step()
00053 {
00054 if(ID.IsPressed(Input::ROT_UP)) {
00055 rotation += 0.05;
00056 }
00057 if(ID.IsPressed(Input::ROT_DOWN)) {
00058 rotation -= 0.05;
00059 }
00060
00061 if(ID.IsPressed(Input::MUSIC_TOGGLE)) {
00062 if(Sound::ThemeIsPlaying())
00063 Sound::StopTheme();
00064 else
00065 Sound::PlayTheme(-1);
00066 ID.ResetKey(Input::MUSIC_TOGGLE);
00067 }
00068 if(ID.IsPressed(Input::SWITCH_MUSIC)) {
00069 static int choice = 1;
00070 if (choice) {
00071 Sound::LoadTheme(ALTERNATE_THEME);
00072 choice = 0;
00073 }
00074 else {
00075 Sound::LoadTheme(THEME_MUSIC);
00076 choice = 1;
00077 }
00078 ID.ResetKey(Input::SWITCH_MUSIC);
00079 }
00080 if(ID.IsPressed(Input::FULLSCREEN_TOGGLE)) {
00081 GD.toggleFullscreen();
00082 ID.ResetKey(Input::FULLSCREEN_TOGGLE);
00083 }
00084
00085 if(ID.IsPressed(Input::LOCKMOUSE_TOGGLE)) {
00086 GD.lockMouse(!GD.isMouseLocked());
00087 ID.ResetKey(Input::LOCKMOUSE_TOGGLE);
00088 }
00089
00090 if(ID.IsPressed(Input::ADD_UFO)) {
00091 if(ID.IsShift()) {
00092 Sound::PlaySample(UFO_NOISE);
00093 for(int k = 0; k < 100; k++)
00094 OM.AddObject(OT_UFO);
00095 } else
00096 Sound::PlaySample(UFO_NOISE);
00097 OM.AddObject(OT_UFO);
00098
00099
00100 ID.ResetKey(Input::ADD_UFO);
00101 }
00102 if(ID.IsPressed(Input::RAPID_FIRE)) {
00103 rapidfire = !rapidfire;
00104 ID.ResetKey(Input::RAPID_FIRE);
00105 }
00106
00107
00108 if(this->GameLoop())
00109 this->RenderScreen();
00110
00111 return false;
00112 }
00113
00114
00115
00116
00117 int Game::GameLoop()
00118 {
00119 int x, y, buttons;
00120
00121 buttons = ID.GetMouse (x, y);
00122 OM.GetCrosshair()->SetPosition(x, y);
00123 OM.GetMainChar()->setLook ( OM.GetCrosshair()->GetXint(),
00124 OM.GetCrosshair()->GetYint());
00125
00126
00127 if(buttons & Input::MOUSE_BUTTON1) {
00128
00129 OM.GetMainChar()->FireWeapon();
00130
00131
00132 Sound::PlaySample(GUNSHOT);
00133
00134
00135 if(!rapidfire)
00136 ID.ResetMouseButton(Input::MOUSE_BUTTON1);
00137 }
00138
00139
00140 if (OM.GetMainChar()->isWalking() &&
00141 !Sound::SampleIsPlaying(WALKING)) {
00142 Sound::PlaySample(WALKING);
00143 }
00144
00145 OM.GetMainChar()->setWalking(ID.IsPressed(Input::WALK_EAST),
00146 ID.IsPressed(Input::WALK_SOUTH),
00147 ID.IsPressed(Input::WALK_WEST),
00148 ID.IsPressed(Input::WALK_NORTH));
00149
00150 if (currentSubWorld->GetBackground().SetPosition((float)OM.GetMainChar()->GetX()-GD.getWidth()/2,
00151 (float)OM.GetMainChar()->GetY()-GD.getHeight()/2))
00152 OM.recenter();
00153
00154 OM.update();
00155 OM.think();
00156 OM.animate();
00157
00158 return 1;
00159 }
00160
00161
00162
00163 int Game::RenderScreen()
00164 {
00165 if(!GD.isHWSurface()) {
00166 vector<Rect> &rects = OM.GetDrawList();
00167 int n = (int)rects.size();
00168
00169 bool drewBkg = false;
00170
00171
00172 if (currentSubWorld->GetBackground().Draw()) {
00173 rects.push_back (Util_BuildRect (0, 0, GD.getWidth(), GD.getHeight()));
00174 drewBkg = true;
00175 }
00176
00177
00178 if (!drewBkg) {
00179 for(int i = 0; i < n; i++) {
00180 Rect tar;
00181
00182 tar.x = rects[i].x;
00183 tar.y = rects[i].y;
00184 tar.w = rects[i].w;
00185 tar.h = rects[i].h;
00186
00187 currentSubWorld->GetBackground().DrawRect (tar);
00188 }
00189 }
00190
00191
00192 rects = OM.draw(GD);
00193
00194
00195 int n2 = (int)rects.size();
00196
00197 if (!drewBkg) {
00198 for(int i = 0; i < n2; i++) {
00199 GD.update(rects[i]);
00200 }
00201 } else
00202 GD.update (0, 0, GD.getWidth(), GD.getHeight());
00203
00204 rects.erase(rects.begin(), rects.begin()+n);
00205
00206 } else {
00207 GD.RotateZ (rotation);
00208
00209 currentSubWorld->GetBackground().Draw();
00210
00211 OM.draw(GD);
00212
00213 GD.flip();
00214 }
00215
00216 return 1;
00217 }