aboutsummaryrefslogtreecommitdiff
path: root/engines/pegasus/energymonitor.cpp
blob: 7a9ca7878a1ad34a5bcb345b68cf9712fd885c38 (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
/* 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.
 *
 * Additional copyright for this file:
 * Copyright (C) 1995-1997 Presto Studios, Inc.
 *
 * 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 "pegasus/energymonitor.h"
#include "pegasus/pegasus.h"
#include "pegasus/surface.h"

namespace Pegasus {

Blinker::Blinker() {
	_sprite = 0;
	_frame1 = -1;
	_frame2 = -1;
	_blinkDuration = 0;
}

void Blinker::startBlinking(Sprite *sprite, int32 frame1, int32 frame2, uint32 numBlinks, TimeValue blinkDuration, TimeScale blinkScale) {
	stopBlinking();
	_sprite = sprite;
	_frame1 = frame1;
	_frame2 = frame2;
	_blinkDuration = blinkDuration;
	setScale(blinkScale);
	setSegment(0, blinkDuration * numBlinks * 2, blinkScale);
	setTime(0);
	start();
}

void Blinker::stopBlinking() {
	if (_sprite) {
		_sprite->setCurrentFrameIndex(_frame2);
		_sprite = 0;
		stop();
	}
}

void Blinker::timeChanged(const TimeValue time) {
	if (_sprite && _blinkDuration != 0) {
		if (((time / _blinkDuration) & 1) != 0 || time == getDuration()) {
			_sprite->setCurrentFrameIndex(_frame2);
			if (!isRunning())
				stopBlinking();
		} else {
			_sprite->setCurrentFrameIndex(_frame1);
		}
	}
}

static const NotificationFlags kEnergyExpiredFlag = 1;

EnergyMonitor *g_energyMonitor = 0;

EnergyMonitor::EnergyMonitor() : IdlerAnimation(kEnergyBarID), _energyLight(kWarningLightID) {
	PegasusEngine *vm = (PegasusEngine *)g_engine;

	_stage = kStageNoStage;

	_calibrating = false;
	_dontFlash = false;

	setBounds(338, 48, 434, 54);

	setDisplayOrder(kEnergyBarOrder);
	startDisplaying();

	SpriteFrame *frame = new SpriteFrame();
	frame->initFromPICTResource(vm->_resFork, kLightOffID);
	_energyLight.addFrame(frame, 0, 0);

	frame = new SpriteFrame();
	frame->initFromPICTResource(vm->_resFork, kLightYellowID);
	_energyLight.addFrame(frame, 0, 0);

	frame = new SpriteFrame();
	frame->initFromPICTResource(vm->_resFork, kLightOrangeID);
	_energyLight.addFrame(frame, 0, 0);

	frame = new SpriteFrame();
	frame->initFromPICTResource(vm->_resFork, kLightRedID);
	_energyLight.addFrame(frame, 0, 0);

	_energyLight.setBounds(540, 35, 600, 59);
	_energyLight.setDisplayOrder(kEnergyLightOrder);
	_energyLight.startDisplaying();

	setScale(1);
	setSegment(0, kMaxJMPEnergy);

	setEnergyValue(kCasualEnergy);

	g_energyMonitor = this;
}

EnergyMonitor::~EnergyMonitor() {
	g_energyMonitor = 0;
}

void EnergyMonitor::setEnergyValue(const uint32 value) {
	if (isRunning()) {
		stop();
		setTime(getStop() - value);
		start();
	} else {
		setTime(getStop() - value);
	}
}

void EnergyMonitor::startEnergyDraining() {
	if (!isRunning()) {
		_energyLight.show();
		start();
		show();
	}
}

void EnergyMonitor::setEnergyDrainRate(Common::Rational rate) {
	setRate(rate);
}

Common::Rational EnergyMonitor::getEnergyDrainRate() {
	return getRate();
}

void EnergyMonitor::stopEnergyDraining() {
	if (isRunning()) {
		stop();
		_energyLight.hide();
		hide();
	}
}

void EnergyMonitor::drainEnergy(const int32 delta) {
	setTime(getTime() + delta);
}

int32 EnergyMonitor::getCurrentEnergy() {
	return kMaxJMPEnergy - getTime();
}

void EnergyMonitor::timeChanged(const TimeValue currentTime) {
	if (currentTime == getStop()) {
		PegasusEngine *vm = (PegasusEngine *)g_engine;
		if (vm->getEnergyDeathReason() != -1)
			vm->die(vm->getEnergyDeathReason());
	} else {
		uint32 currentEnergy = kMaxJMPEnergy - currentTime;

		EnergyStage newStage;
		if (currentEnergy > kWorriedEnergy)
			newStage = kStageCasual;
		else if (currentEnergy > kNervousEnergy)
			newStage = kStageWorried;
		else if (currentEnergy > kPanicStrickenEnergy)
			newStage = kStageNervous;
		else
			newStage = kStagePanicStricken;

		if (_stage != newStage) {
			uint32 newFrame;

			switch (newStage) {
			case kStageCasual:
				_barColor = g_system->getScreenFormat().RGBToColor(0x48, 0xB0, 0xD8);
				newFrame = kFrameLightOff;
				break;
			case kStageWorried:
				_barColor = g_system->getScreenFormat().RGBToColor(0xD8, 0xC0, 0x30);
				newFrame = kFrameLightYellow;
				break;
			case kStageNervous:
				_barColor = g_system->getScreenFormat().RGBToColor(0xD8, 0x78, 0x38);
				newFrame = kFrameLightOrange;
				break;
			case kStagePanicStricken:
				_barColor = g_system->getScreenFormat().RGBToColor(0xD8, 0x40, 0x38);
				newFrame = kFrameLightRed;
				break;
			default:
				error("no stage in energy monitor?");
				break;
			}

			_stage = newStage;
			uint32 oldFrame = _energyLight.getCurrentFrameIndex();

			if (!_calibrating) {
				if (oldFrame > newFrame || oldFrame == 0xffffffff || _dontFlash) {
					_energyLight.setCurrentFrameIndex(newFrame);
					_dontFlash = false;
				} else {
					_lightBlinker.startBlinking(&_energyLight, oldFrame, newFrame, 4, 1, 3);
					triggerRedraw();
				}
			}
		}

		Common::Rect r;
		calcLevelRect(r);
		if (r != _levelRect) {
			_levelRect = r;
			triggerRedraw();
		}
	}
}

void EnergyMonitor::calcLevelRect(Common::Rect &r) {
	if (getStop() == 0) {
		r = Common::Rect();
	} else {
		getBounds(r);
		r.left = r.right - r.width() * (kMaxJMPEnergy - getTime()) / getStop();
	}
}

void EnergyMonitor::draw(const Common::Rect &r) {
	Common::Rect r2 = r.findIntersectingRect(_levelRect);

	if (!r2.isEmpty()) {
		Graphics::Surface *screen = ((PegasusEngine *)g_engine)->_gfx->getWorkArea();
		screen->fillRect(r2, _barColor);
	}
}

void EnergyMonitor::calibrateEnergyBar() {
	PegasusEngine *vm = (PegasusEngine *)g_engine;
	
	_calibrating = true;

	vm->setEnergyDeathReason(-1);

	uint32 numFrames = _energyLight.getNumFrames();
	for (uint32 i = 1; i < numFrames; i++) {
		_energyLight.setCurrentFrameIndex(i);
		_energyLight.show();
		vm->delayShell(1, 3);
		_energyLight.hide();
		vm->delayShell(1, 3);
	}

	_energyLight.setCurrentFrameIndex(0);
	_energyLight.hide();

	show();
	setEnergyValue(0);
	setEnergyDrainRate(-(int32)kMaxJMPEnergy / 2);

	// Make sure warning light is hidden...
	_energyLight.hide();
	while (getCurrentEnergy() != (int32)kMaxJMPEnergy) {
		vm->checkCallBacks();
		vm->refreshDisplay();
		g_system->delayMillis(10);
	}

	vm->refreshDisplay();
	setEnergyDrainRate(0);
	hide();

	_calibrating = false;
}

void EnergyMonitor::restoreLastEnergyValue() {
	PegasusEngine *vm = (PegasusEngine *)g_engine;

	_dontFlash = true;
	setEnergyValue(vm->getSavedEnergyValue());
	vm->resetEnergyDeathReason();
}

void EnergyMonitor::saveCurrentEnergyValue() {
	((PegasusEngine *)g_engine)->setLastEnergyValue(getCurrentEnergy());
}

} // End of namespace Pegasus