aboutsummaryrefslogtreecommitdiff
path: root/engines/titanic/game_manager.cpp
blob: e3365be108435cca6b3877a0a554bc9a39c281d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "titanic/game_manager.h"
#include "titanic/core/project_item.h"
#include "titanic/events.h"
#include "titanic/game_view.h"
#include "titanic/messages/messages.h"
#include "titanic/pet_control/pet_control.h"
#include "titanic/sound/background_sound_maker.h"
#include "titanic/support/files_manager.h"
#include "titanic/support/screen_manager.h"
#include "titanic/titanic.h"

namespace Titanic {

CGameManager::CGameManager(CProjectItem *project, CGameView *gameView, Audio::Mixer *mixer):
		_project(project), _gameView(gameView), _trueTalkManager(this),
		_inputHandler(this), _inputTranslator(&_inputHandler),
		_gameState(this), _sound(this, mixer), _musicRoom(this),
		_treeItem(nullptr), _soundMaker(nullptr), _movieRoom(nullptr),
		_dragItem(nullptr), _transitionCtr(0), _lastDiskTicksCount(0), _tickCount2(0) {

	CTimeEventInfo::_nextId = 0;
	_movie = nullptr;
	_movieSurface = CScreenManager::_screenManagerPtr->createSurface(600, 340);
	_project->setGameManager(this);
	g_vm->_filesManager->setGameManager(this);
}

CGameManager::~CGameManager() {
	delete _movie;
	delete _movieSurface;
	destroyTreeItem();

	_project->resetGameManager();
}

void CGameManager::destroyTreeItem() {
	if (_treeItem) {
		_treeItem->destroyAll();
		_treeItem = nullptr;
	}
}

void CGameManager::save(SimpleFile *file) {
	file->writeNumber(_lastDiskTicksCount);
	_gameState.save(file);
	_timers.save(file, 0);
	_trueTalkManager.save(file);
	_sound.save(file);
}

void CGameManager::load(SimpleFile *file) {
	file->readNumber();

	_gameState.load(file);
	_timers.load(file);
	_trueTalkManager.load(file);
	_sound.load(file);
}

void CGameManager::preLoad() {
	updateDiskTicksCount();
	_timers.destroyContents();
	_soundMaker = nullptr;

	_sound.preLoad();
	_trueTalkManager.preLoad();
}

void CGameManager::postLoad(CProjectItem *project) {
	if (_gameView) {
		_gameView->postLoad();

		if (!_gameView->_surface) {
			CViewItem *view = getView();
			if (view)
				_gameView->setView(view);
		}
	}

	// Signal to anything interested that the game has been loaded
	CLoadSuccessMsg msg(_lastDiskTicksCount - _tickCount2);
	msg.execute(project, nullptr, MSGFLAG_SCAN);

	// Signal to any registered timers
	_timers.postLoad(_lastDiskTicksCount, _project);

	// Signal the true talk manager and sound
	_trueTalkManager.postLoad();
	_sound.postLoad();
}

void CGameManager::preSave(CProjectItem *project) {
	// Generate a message that a save is being done
	updateDiskTicksCount();
	CPreSaveMsg msg(_lastDiskTicksCount);
	msg.execute(project, nullptr, MSGFLAG_SCAN);

	// Notify sub-objects of the save
	_timers.preSave(_lastDiskTicksCount);
	_trueTalkManager.preSave();
	_sound.preSave();
}

void CGameManager::postSave() {
	_timers.postSave();
	_trueTalkManager.postSave();
	_sound.postSave();
}

void CGameManager::roomTransition(CRoomItem *oldRoom, CRoomItem *newRoom) {
	delete _movie;
	_movie = nullptr;

	CResourceKey movieKey;
	if (newRoom == oldRoom) {
		movieKey = oldRoom->getTransitionMovieKey();
		_movieRoom = oldRoom;
	} else {
		movieKey = oldRoom->getExitMovieKey();
		_movieRoom = nullptr;
	}

	CString filename = movieKey.getFilename();
	if (g_vm->_filesManager->fileExists(filename)) {
		_movieSurface->freeSurface();
		_movie = g_vm->_movieManager.createMovie(filename, _movieSurface);
	}
}

void CGameManager::playClip(CMovieClip *clip, CRoomItem *oldRoom, CRoomItem *newRoom) {
	if (oldRoom != newRoom || newRoom != _movieRoom || !_movie)
		roomTransition(oldRoom, newRoom);

	if (clip && clip->_startFrame != clip->_endFrame && _movie) {
		// Clip details specifying a sub-section of movie to play
		Rect tempRect(20, 10, SCREEN_WIDTH - 20, 350);
		CMouseCursor &mouseCursor = *CScreenManager::_screenManagerPtr->_mouseCursor;

		lockInputHandler();
		mouseCursor.incHideCounter();
		_movie->playCutscene(tempRect, clip->_startFrame, clip->_endFrame);
		mouseCursor.decHideCounter();
		unlockInputHandler();
	}
}

void CGameManager::update() {
	updateMovies();
	frameMessage(getRoom());
	_timers.update(g_vm->_events->getTicksCount());
	_trueTalkManager.removeCompleted();

	CScreenManager::_screenManagerPtr->_mouseCursor->update();

	CViewItem *view = getView();
	if (view) {
		// Expand the game manager's bounds to encompass any modified
		// areas of any of the view's items
		for (CTreeItem *item = view; item; item = item->scan(view)) {
			Rect r = item->getBounds();
			if (!r.isEmpty())
				_bounds.combine(r);
		}

		// Also include any modified area of the PET control
		if (_project) {
			CPetControl *pet = _project->getPetControl();

			if (pet)
				_bounds.combine(pet->getBounds());
		}

		// And the text cursor
		CScreenManager *screenManager = CScreenManager::_screenManagerPtr;
		CTextCursor *textCursor = screenManager->_textCursor;
		if (textCursor && textCursor->_active)
			_bounds.combine(textCursor->getCursorBounds());

		// Set the screen's modified area bounds
		screenManager->setSurfaceBounds(SURFACE_PRIMARY, _bounds);

		// Handle redrawing the view if there is any changed area
		if (!_bounds.isEmpty()) {
			_gameView->draw(_bounds);
			_bounds = Rect();
		}

		_gameState.checkForViewChange();
	}
}

void CGameManager::updateMovies() {
	// Initial iteration to mark all the movies as not yet handled
	for (CMovieList::iterator i = CMovie::_playingMovies->begin();
			i != CMovie::_playingMovies->end(); ++i)
		(*i)->_handled = false;

	bool repeatFlag;
	do {
		repeatFlag = false;

		// Scan for a movie to process
		for (CMovieList::iterator i = CMovie::_playingMovies->begin();
				i != CMovie::_playingMovies->end(); ++i) {
			CMovie *movie = *i;
			if (movie->_handled)
				continue;

			// Flag the movie to have been handled
			movie->_handled = true;

			CMovieEventList eventsList;
			if (!movie->handleEvents(eventsList))
				movie->removeFromPlayingMovies();

			while (!eventsList.empty()) {
				CMovieEvent *movieEvent = eventsList.front();

				switch (movieEvent->_type) {
				case MET_MOVIE_END: {
					CMovieEndMsg endMsg(movieEvent->_startFrame, movieEvent->_endFrame);
					endMsg.execute(movieEvent->_gameObject);
					break;
				}

				case MET_FRAME: {
					CMovieFrameMsg frameMsg(movieEvent->_initialFrame, 0);
					frameMsg.execute(movieEvent->_gameObject);
					break;
				}

				default:
					break;
				}

				eventsList.remove(movieEvent);
				delete movieEvent;
			}

			repeatFlag = true;
			break;
		}
	} while (repeatFlag);
}

void CGameManager::updateDiskTicksCount() {
	_lastDiskTicksCount = g_vm->_events->getTicksCount();
}

void CGameManager::roomChange() {
	delete _movie;
	delete _movieSurface;

	_movie = nullptr;
	_movieSurface = CScreenManager::_screenManagerPtr->createSurface(600, 340);
	_trueTalkManager.clear();

	for (CTreeItem *treeItem = _project; treeItem; treeItem = treeItem->scan(_project))
		treeItem->freeSurface();

	markAllDirty();
}

void CGameManager::frameMessage(CRoomItem *room) {
	if (room) {
		// Signal the next frame
		CFrameMsg frameMsg(g_vm->_events->getTicksCount());
		frameMsg.execute(room, nullptr, MSGFLAG_SCAN);

		if (_gameState._soundMakerAllowed && !_soundMaker) {
			// Check for a sound maker in the room
			_soundMaker = dynamic_cast<CBackgroundSoundMaker *>(
				_project->findByName("zBackgroundSoundMaker"));
		}

		// If there's a sound maker, dispatch the event to it as well
		if (_soundMaker)
			frameMsg.execute(_soundMaker);
	}
}

void CGameManager::addDirtyRect(const Rect &r) {
	if (_bounds.isEmpty())
		_bounds = r;
	else
		_bounds.combine(r);
}

void CGameManager::markAllDirty() {
	_bounds = Rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}

CScreenManager *CGameManager::setScreenManager() const {
	return CScreenManager::setCurrent();
}

CString CGameManager::getFullViewName() {
	CViewItem *view = getView();
	CNodeItem *node = view->findNode();
	CRoomItem *room = node->findRoom();

	return CString::format("%s.%s.%s", room->getName().c_str(),
		node->getName().c_str(), view->getName().c_str());
}

} // End of namespace Titanic