1 /*=============================================================================
2 Blobby Volley 2
3 Copyright (C) 2006 Jonathan Sieber (jonathan_sieber@yahoo.de)
4 Copyright (C) 2006 Daniel Knobe (daniel-knobe@web.de)
5 
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 =============================================================================*/
21 
22 /* header include */
23 #include "GameState.h"
24 
25 /* includes */
26 #include <boost/make_shared.hpp>
27 
28 #include "ReplayRecorder.h"
29 #include "DuelMatch.h"
30 #include "SoundManager.h"
31 #include "IMGUI.h"
32 #include "TextManager.h"
33 #include "Blood.h"
34 #include "MatchEvents.h"
35 #include "PhysicWorld.h"
36 #include "FileWrite.h"
37 
38 /* implementation */
39 
40 
GameState(DuelMatch * match)41 GameState::GameState(DuelMatch* match) : mMatch(match), mSaveReplay(false), mErrorMessage("")
42 {
43 }
44 
~GameState()45 GameState::~GameState()
46 {
47 	// disable game drawing
48 	RenderManager::getSingleton().drawGame(false);
49 }
50 
presentGame()51 void GameState::presentGame()
52 {
53 	// enable game drawing
54 	RenderManager::getSingleton().drawGame(true);
55 
56 	RenderManager& rmanager = RenderManager::getSingleton();
57 	SoundManager& smanager = SoundManager::getSingleton();
58 
59 
60 
61 	rmanager.setBlob(LEFT_PLAYER, mMatch->getBlobPosition(LEFT_PLAYER), mMatch->getWorld().getBlobState(LEFT_PLAYER));
62 	rmanager.setBlob(RIGHT_PLAYER, mMatch->getBlobPosition(RIGHT_PLAYER),	mMatch->getWorld().getBlobState(RIGHT_PLAYER));
63 
64 	if(mMatch->getPlayer(LEFT_PLAYER).getOscillating())
65 	{
66 		rmanager.setBlobColor(LEFT_PLAYER, rmanager.getOscillationColor());
67 	}
68 	 else
69 	{
70 		rmanager.setBlobColor(LEFT_PLAYER, mMatch->getPlayer(LEFT_PLAYER).getStaticColor());
71 	}
72 
73 	if(mMatch->getPlayer(RIGHT_PLAYER).getOscillating())
74 	{
75 		rmanager.setBlobColor(RIGHT_PLAYER, rmanager.getOscillationColor());
76 	}
77 	 else
78 	{
79 		rmanager.setBlobColor(RIGHT_PLAYER, mMatch->getPlayer(RIGHT_PLAYER).getStaticColor());
80 	}
81 
82 	rmanager.setBall(mMatch->getBallPosition(), mMatch->getWorld().getBallRotation());
83 
84 	int events = mMatch->getEvents();
85 	if(events & EVENT_LEFT_BLOBBY_HIT)
86 	{
87 		smanager.playSound("sounds/bums.wav", mMatch->getWorld().getLastHitIntensity() + BALL_HIT_PLAYER_SOUND_VOLUME);
88 		Vector2 hitPos = mMatch->getBallPosition() +
89 				(mMatch->getBlobPosition(LEFT_PLAYER) - mMatch->getBallPosition()).normalise().scale(31.5);
90 		BloodManager::getSingleton().spillBlood(hitPos, mMatch->getWorld().getLastHitIntensity(), 0);
91 	}
92 
93 	if (events & EVENT_RIGHT_BLOBBY_HIT)
94 	{
95 		smanager.playSound("sounds/bums.wav", mMatch->getWorld().getLastHitIntensity() + BALL_HIT_PLAYER_SOUND_VOLUME);
96 		Vector2 hitPos = mMatch->getBallPosition() +
97 				(mMatch->getBlobPosition(RIGHT_PLAYER) - mMatch->getBallPosition()).normalise().scale(31.5);
98 		BloodManager::getSingleton().spillBlood(hitPos, mMatch->getWorld().getLastHitIntensity(), 1);
99 	}
100 
101 	if (events & EVENT_ERROR)
102 		smanager.playSound("sounds/pfiff.wav", ROUND_START_SOUND_VOLUME);
103 }
104 
presentGameUI()105 void GameState::presentGameUI()
106 {
107 	auto& imgui = IMGUI::getSingleton();
108 
109 	// Scores
110 	char textBuffer[64];
111 	snprintf(textBuffer, 8, mMatch->getServingPlayer() == LEFT_PLAYER ? "%02d!" : "%02d ", mMatch->getScore(LEFT_PLAYER));
112 	imgui.doText(GEN_ID, Vector2(24, 24), textBuffer);
113 	snprintf(textBuffer, 8, mMatch->getServingPlayer() == RIGHT_PLAYER ? "%02d!" : "%02d ", mMatch->getScore(RIGHT_PLAYER));
114 	imgui.doText(GEN_ID, Vector2(800-24, 24), textBuffer, TF_ALIGN_RIGHT);
115 
116 	// blob name / time textfields
117 	imgui.doText(GEN_ID, Vector2(12, 550), mMatch->getPlayer(LEFT_PLAYER).getName());
118 	imgui.doText(GEN_ID, Vector2(788, 550), mMatch->getPlayer(RIGHT_PLAYER).getName(), TF_ALIGN_RIGHT);
119 	imgui.doText(GEN_ID, Vector2(400, 24), mMatch->getClock().getTimeString(), TF_ALIGN_CENTER);
120 }
121 
displaySaveReplayPrompt()122 bool GameState::displaySaveReplayPrompt()
123 {
124 	auto& imgui = IMGUI::getSingleton();
125 
126 	imgui.doCursor();
127 
128 	imgui.doOverlay(GEN_ID, Vector2(150, 200), Vector2(650, 400));
129 	imgui.doText(GEN_ID, Vector2(190, 220), TextManager::RP_SAVE_NAME);
130 	static unsigned cpos;
131 	imgui.doEditbox(GEN_ID, Vector2(180, 270), 18, mFilename, cpos);
132 
133 	bool doSave = false;
134 
135 	if(imgui.doButton(GEN_ID, Vector2(220, 330), TextManager::LBL_OK))
136 	{
137 		if(mFilename != "")
138 		{
139 			imgui.resetSelection();
140 			doSave = true;
141 		}
142 	}
143 
144 	if (imgui.doButton(GEN_ID, Vector2(440, 330), TextManager::LBL_CANCEL))
145 	{
146 		mSaveReplay = false;
147 		imgui.resetSelection();
148 		doSave = false;
149 	}
150 	return doSave;
151 }
152 
displayErrorMessageBox()153 bool GameState::displayErrorMessageBox()
154 {
155 	auto& imgui = IMGUI::getSingleton();
156 
157 	imgui.doCursor();
158 
159 	imgui.doOverlay(GEN_ID, Vector2(100, 200), Vector2(700, 360));
160 	size_t split = mErrorMessage.find(':');
161 	std::string mProblem = mErrorMessage.substr(0, split);
162 	std::string mInfo = mErrorMessage.substr(split+1);
163 	imgui.doText(GEN_ID, Vector2(120, 220), mProblem);
164 	imgui.doText(GEN_ID, Vector2(120, 260), mInfo);
165 	if(imgui.doButton(GEN_ID, Vector2(330, 320), TextManager::LBL_OK))
166 	{
167 		mErrorMessage = "";
168 		return true;
169 	}
170 	return false;
171 }
172 
displayWinningPlayerScreen(PlayerSide winner)173 bool GameState::displayWinningPlayerScreen(PlayerSide winner)
174 {
175 	auto& imgui = IMGUI::getSingleton();
176 
177 	std::string tmp = mMatch->getPlayer(winner).getName();
178 	imgui.doOverlay(GEN_ID, Vector2(200, 150), Vector2(700, 450));
179 	imgui.doImage(GEN_ID, Vector2(200, 250), "gfx/pokal.bmp");
180 	imgui.doText(GEN_ID, Vector2(274, 240), tmp);
181 	imgui.doText(GEN_ID, Vector2(274, 300), TextManager::GAME_WIN);
182 	imgui.doCursor();
183 
184 	return false;
185 }
186 
setDefaultReplayName(const std::string & left,const std::string & right)187 void GameState::setDefaultReplayName(const std::string& left, const std::string& right)
188 {
189 	mFilename = left;
190 	if(mFilename.size() > 7)
191 		mFilename.resize(7);
192 	mFilename += " vs ";
193 
194 	std::string opp = right;
195 	if(right.size() > 7)
196 		opp.resize(7);
197 	mFilename += opp;
198 }
199 
saveReplay(ReplayRecorder & recorder)200 void GameState::saveReplay(ReplayRecorder& recorder)
201 {
202 	try
203 	{
204 		if (mFilename != "")
205 		{
206 			std::string repFileName = std::string("replays/") + mFilename + std::string(".bvr");
207 
208 			boost::shared_ptr<FileWrite> savetarget = boost::make_shared<FileWrite>(repFileName);
209 			/// \todo add a check whether we overwrite a file
210 			recorder.save(savetarget);
211 			savetarget->close();
212 			mSaveReplay = false;
213 		}
214 	}
215 	catch( FileLoadException& ex)
216 	{
217 		mErrorMessage = std::string("Unable to create file:" + ex.getFileName());
218 		mSaveReplay = true;	// try again
219 	}
220 	catch( FileAlreadyExistsException& ex)
221 	{
222 		mErrorMessage = std::string("File already exists!:"+ ex.getFileName());
223 		mSaveReplay = true;	// try again
224 	}
225 	 catch( std::exception& ex)
226 	{
227 		mErrorMessage = std::string("Could not save replay: ");
228 		mSaveReplay = true;	// try again
229 	}
230 }
231