aboutsummaryrefslogtreecommitdiff
path: root/engines/pink/objects/sequences/sequencer.cpp
blob: b10d41437ec5609923441de88476e09fed9e0e82 (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
/* 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 "common/debug.h"

#include "pink/archive.h"
#include "pink/pink.h"
#include "pink/objects/actors/lead_actor.h"
#include "pink/objects/pages/game_page.h"
#include "pink/objects/sequences/sequencer.h"
#include "pink/objects/sequences/sequence.h"
#include "pink/objects/sequences/sequence_context.h"
#include "pink/objects/sequences/seq_timer.h"

namespace Pink {

Sequencer::Sequencer(GamePage *page)
	: _context(nullptr), _page(page), _time(0) {}

Sequencer::~Sequencer() {
	for (uint i = 0; i < _sequences.size(); ++i) {
		delete _sequences[i];
	}
	for (uint i = 0; i < _timers.size(); ++i) {
		delete _timers[i];
	}
	delete _context;
	for (uint i = 0; i < _parrallelContexts.size(); ++i) {
		delete _parrallelContexts[i];
	}
}

void Sequencer::deserialize(Archive &archive) {
	_sequences.deserialize(archive);
	_timers.deserialize(archive);
}

Sequence *Sequencer::findSequence(const Common::String &name) {
	for (uint i = 0; i < _sequences.size(); ++i) {
		if (_sequences[i]->getName() == name)
			return _sequences[i];
	}
	return nullptr;
}

void Sequencer::authorSequence(Sequence *sequence, bool loadingSave) {
 	if (_context)
		_context->getSequence()->forceEnd();

	if (sequence) {
		SequenceContext *context = new SequenceContext(sequence);

		SequenceContext *confilct;
		while(confilct = findConfilictingContextWith(context))
			confilct->getSequence()->forceEnd();

		_context = context;
		sequence->init(loadingSave);
	}
}

void Sequencer::authorParallelSequence(Sequence *seqeunce, bool loadingSave) {
	if (_context && _context->getSequence() == seqeunce)
		return;

	for (uint i = 0; i < _parrallelContexts.size(); ++i) {
		if (_parrallelContexts[i]->getSequence() == seqeunce)
			return;
	}

	const Common::String leadName = _page->getLeadActor()->getName();
	SequenceContext *context = new SequenceContext(seqeunce);

	if (!context->findState(leadName) && !findConfilictingContextWith(context)) {
		_parrallelContexts.push_back(context);
		seqeunce->init(loadingSave);
	} else
		delete context;
}


void Sequencer::toConsole() {
	debug("Sequencer:");
	for (uint i = 0; i < _sequences.size(); ++i) {
		_sequences[i]->toConsole();
	}
	for (uint i = 0; i < _timers.size(); ++i) {
		_timers[i]->toConsole();
	}
}

void Sequencer::update() {
	if (_context)
		_context->getSequence()->update();

	for (uint i = 0; i < _parrallelContexts.size(); ++i) {
		_parrallelContexts[i]->getSequence()->update();
	}

	uint time = _page->getGame()->getTotalPlayTime();
	if (time - _time > kTimersUpdateTime) {
		_time = time;
		for (uint i = 0; i < _timers.size(); ++i) {
			_timers[i]->update();
		}
	}
}

void Sequencer::removeContext(SequenceContext *context) {
	if (context == _context) {
		delete _context;
		_context = nullptr;
		return;
	}

	for (uint i = 0; i < _parrallelContexts.size(); ++i) {
		if (context == _parrallelContexts[i]) {
			delete _parrallelContexts[i];
			_parrallelContexts.remove_at(i);
			break;
		}
	}
}

void Sequencer::skipSubSequence() {
	if (_context)
		_context->getSequence()->skipSubSequence();
}

void Sequencer::restartSequence() {
	if (_context)
		_context->getSequence()->restart();
}

void Sequencer::skipSequence() {
	if (_context && _context->getSequence()->isSkippingAllowed())
		_context->getSequence()->skip();
}

void Sequencer::loadState(Archive &archive) {
	Sequence *sequence = findSequence(archive.readString());
	authorSequence(sequence, 1);
}

void Sequencer::saveState(Archive &archive) {
	Common::String sequenceName;
	if (_context)
		sequenceName = _context->getSequence()->getName();
	archive.writeString(sequenceName);
	// add pokus specific
}

SequenceContext *Sequencer::findConfilictingContextWith(SequenceContext *context) {
	if (_context && _context->isConflictsWith(context)) {
		return _context;
	}
	for (uint i = 0; i < _parrallelContexts.size(); ++i) {
		if (_parrallelContexts[i]->isConflictsWith(context))
			return _parrallelContexts[i];
	}
	return nullptr;
}

SequenceActorState *Sequencer::findState(const Common::String &name) {
	SequenceActorState *state = nullptr;
	if (_context && (state = _context->findState(name)))
		return state;

	for (uint i = 0; i < _parrallelContexts.size(); ++i) {
		state = _parrallelContexts[i]->findState(name);
		if (state)
			break;
	}
	return state;
}

} // End of namespace Pink