SpeedChange.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <graph/SpeedChange.h>
00022 #include <client/ScorchedClient.h>
00023 #include <client/ClientParams.h>
00024 #include <server/ServerShotFinishedState.h>
00025 #include <server/ScorchedServer.h>
00026 #include <common/Keyboard.h>
00027 #include <common/Defines.h>
00028 #include <client/ClientParams.h>
00029 #include <engine/ActionController.h>
00030 #include <graph/ParticleEngine.h>
00031 #include <GLW/GLWFont.h>
00032 #include <stdio.h>
00033 
00034 SpeedChange *SpeedChange::instance_ = 0;
00035 
00036 SpeedChange *SpeedChange::instance()
00037 {
00038         if (!instance_)
00039         {
00040                 instance_ = new SpeedChange;
00041         }
00042         return instance_;
00043 }
00044 
00045 SpeedChange::SpeedChange() :
00046         GameStateI("SpeedChange")
00047 {
00048 }
00049 
00050 SpeedChange::~SpeedChange()
00051 {
00052 }
00053 
00054 void SpeedChange::resetSpeed()
00055 {
00056         setSpeed(1);
00057 }
00058 
00059 void SpeedChange::draw(const unsigned state)
00060 {
00061         float speed = ScorchedClient::instance()->getActionController().getFast().asFloat();
00062         if (speed != 1.0f)
00063         {
00064                 GLState state(GLState::BLEND_ON | GLState::TEXTURE_OFF); 
00065                 static char buffer[10];
00066                 snprintf(buffer, 10, "%.1fX", speed);
00067 
00068                 static Vector fontColor(0.7f, 0.7f, 0.2f);
00069                 GLWFont::instance()->getGameFont()->draw(fontColor, 20, 10.0f, 10.0f, 0.0f, buffer);
00070         }
00071 }
00072 
00073 void SpeedChange::simulate(const unsigned state, float simTime)
00074 {
00075         fixed speed = ScorchedClient::instance()->getActionController().getFast();
00076         float timeTaken = frameClock_.getTimeDifference();
00077         if (timeTaken > 0.2f && speed > 1)
00078         {
00079                 setSpeed(speed / 2);
00080         }
00081 }
00082 
00083 void SpeedChange::keyboardCheck(const unsigned state, float frameTime, 
00084                                                            char *buffer, unsigned int keyState,
00085                                                            KeyboardHistory::HistoryElement *history, int hisCount, 
00086                                                            bool &skipRest)
00087 {
00088         if (ClientParams::instance()->getConnectedToServer()) return;
00089 
00090         KEYBOARDKEY("SIMULATION_SPEED_X1", x1Key);
00091         KEYBOARDKEY("SIMULATION_SPEED_X2", x2Key);
00092         KEYBOARDKEY("SIMULATION_SPEED_X3", x3Key);
00093         KEYBOARDKEY("SIMULATION_SPEED_X4", x4Key);
00094         KEYBOARDKEY("SIMULATION_SPEED_HALF", xHalfKey);
00095         KEYBOARDKEY("SIMULATION_SPEED_QUARTER", xQuarterKey);
00096         KEYBOARDKEY("SIMULATION_SPEED_EIGHTH", xEighthKey);
00097 
00098         if (x1Key->keyDown(buffer, keyState))
00099         {
00100                 resetSpeed();
00101         }
00102         else if (x2Key->keyDown(buffer, keyState))
00103         {
00104                 setSpeed(2);
00105         }
00106         else if (x3Key->keyDown(buffer, keyState))
00107         {
00108                 setSpeed(4);
00109         }
00110         else if (x4Key->keyDown(buffer, keyState))
00111         {
00112                 setSpeed(8);
00113         }
00114         else if (xHalfKey->keyDown(buffer, keyState))
00115         {
00116                 setSpeed(fixed(1) / fixed(2));
00117         }
00118         else if (xQuarterKey->keyDown(buffer, keyState))
00119         {
00120                 setSpeed(fixed(1) / fixed(4));
00121         }
00122         else if (xEighthKey->keyDown(buffer, keyState))
00123         {
00124                 setSpeed(fixed(1) / fixed(8));
00125         }
00126 }
00127 
00128 void SpeedChange::setSpeed(fixed speed)
00129 {
00130         ScorchedClient::instance()->getActionController().setFast(speed);
00131         ParticleEngine::setFast(speed.asFloat());
00132 
00133         if (!ClientParams::instance()->getConnectedToServer())
00134         {
00135                 ScorchedServer::instance()->getActionController().setFast(speed);
00136                 ServerShotFinishedState::setSpeed(speed.asFloat()); // Hack!!
00137         }
00138 }

Generated on Mon Feb 16 15:14:46 2009 for Scorched3D by  doxygen 1.5.3