aboutsummaryrefslogtreecommitdiff
path: root/engines/bladerunner/items.cpp
blob: e5b894a0d2e6da759061cf5b80accc3d4e87c4b6 (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
/* 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 "bladerunner/items.h"

#include "bladerunner/game_constants.h"
#include "bladerunner/savefile.h"
#include "bladerunner/scene.h"
#include "bladerunner/scene_objects.h"
#include "bladerunner/zbuffer.h"

namespace BladeRunner {

Items::Items(BladeRunnerEngine *vm) {
	_vm = vm;
}

Items::~Items() {
	reset();
}

void Items::reset() {
	for (int i = _items.size() - 1; i >= 0; i--) {
		delete _items.remove_at(i);
	}
}

void Items::getXYZ(int itemId, float *x, float *y, float *z) const {
	int itemIndex = findItem(itemId);
	assert(itemIndex != -1);

	_items[itemIndex]->getXYZ(x, y, z);
}

void Items::setXYZ(int itemId, Vector3 position) {
	int itemIndex = findItem(itemId);
	assert(itemIndex != -1);

	_items[itemIndex]->setXYZ(position);
}

void Items::getWidthHeight(int itemId, int *width, int *height) const {
	int itemIndex = findItem(itemId);
	assert(itemIndex != -1);

	_items[itemIndex]->getWidthHeight(width, height);
}

void Items::getAnimationId(int itemId, int *animationId) const {
	int itemIndex = findItem(itemId);
	assert(itemIndex != -1);

	_items[itemIndex]->getAnimationId(animationId);
}

void Items::tick() {
	int setId = _vm->_scene->getSetId();
	for (int i = 0; i < (int)_items.size(); i++) {
		if (_items[i]->_setId != setId) {
			continue;
		}
		bool notPoliceMazeTarget = setId == kSetPS10_PS11_PS12_PS13 && !_items[i]->isTarget();
		Common::Rect screenRect;
		if (_items[i]->tick(&screenRect, notPoliceMazeTarget)) {
			_vm->_zbuffer->mark(screenRect);
		}
	}
}

bool Items::addToWorld(int itemId, int animationId, int setId, Vector3 position, int facing, int height, int width, bool isTargetFlag, bool isVisibleFlag, bool isPoliceMazeEnemyFlag, bool addToSetFlag) {
	if (_items.size() >= 100) {
		return false;
	}
	int itemIndex = findItem(itemId);
	if (itemIndex == -1) {
		itemIndex = _items.size();
		_items.push_back(new Item(_vm));
	}

	Item *item = _items[itemIndex];
	item->setup(itemId, setId, animationId, position, facing, height, width, isTargetFlag, isVisibleFlag, isPoliceMazeEnemyFlag);

	if (addToSetFlag && setId == _vm->_scene->getSetId()) {
		return _vm->_sceneObjects->addItem(itemId + kSceneObjectOffsetItems, item->_boundingBox, item->_screenRectangle, isTargetFlag, isVisibleFlag);
	}
	return true;
}

bool Items::addToSet(int setId) {
	int itemCount = _items.size();
	if (itemCount == 0) {
		return true;
	}
	for (int i = 0; i < itemCount; i++) {
		Item *item = _items[i];
		if (item->_setId == setId) {
			_vm->_sceneObjects->addItem(item->_itemId + kSceneObjectOffsetItems, item->_boundingBox, item->_screenRectangle, item->isTarget(), item->_isVisible);
		}
	}
	return true;
}

bool Items::remove(int itemId) {
	if (_items.size() == 0) {
		return false;
	}
	int itemIndex = findItem(itemId);
	if (itemIndex == -1) {
		return false;
	}

	if (_items[itemIndex]->_setId == _vm->_scene->getSetId()) {
		_vm->_sceneObjects->remove(itemId + kSceneObjectOffsetItems);
	}

	delete _items.remove_at(itemIndex);

	return true;
}

void Items::setIsTarget(int itemId, bool val) {
	int itemIndex = findItem(itemId);
	if (itemIndex == -1) {
		return;
	}
	_items[itemIndex]->setIsTarget(val);
	_vm->_sceneObjects->setIsTarget(itemId + kSceneObjectOffsetItems, val);
}

bool Items::isTarget(int itemId) const {
	int itemIndex = findItem(itemId);
	if (itemIndex == -1) {
		return false;
	}
	return _items[itemIndex]->isTarget();
}

bool Items::isSpinning(int itemId) const {
	int itemIndex = findItem(itemId);
	if (itemIndex == -1) {
		return false;
	}
	return _items[itemIndex]->isSpinning();
}

bool Items::isVisible(int itemId) const {
	int itemIndex = findItem(itemId);
	if (itemIndex == -1) {
		return false;
	}
	return _items[itemIndex]->isVisible();
}

bool Items::isPoliceMazeEnemy(int itemId) const {
	int itemIndex = findItem(itemId);
	if (itemIndex == -1) {
		return false;
	}
	return _items[itemIndex]->isPoliceMazeEnemy();
}

void Items::setPoliceMazeEnemy(int itemId, bool val) {
	int itemIndex = findItem(itemId);
	if (itemIndex == -1) {
		return;
	}
	_items[itemIndex]->setPoliceMazeEnemy(val);
}

void Items::setIsObstacle(int itemId, bool val) {
	int itemIndex = findItem(itemId);
	if (itemIndex == -1) {
		return;
	}
	_items[itemIndex]->setVisible(val);
	_vm->_sceneObjects->setIsClickable(itemId + kSceneObjectOffsetItems, val);
}

const BoundingBox &Items::getBoundingBox(int itemId) {
	int itemIndex = findItem(itemId);
	// if (itemIndex == -1) {
	// 	return nullptr;
	// }
	return _items[itemIndex]->getBoundingBox();
}

const Common::Rect &Items::getScreenRectangle(int itemId) {
	int itemIndex = findItem(itemId);
	// if (itemIndex == -1) {
	// 	return nullptr;
	// }
	return _items[itemIndex]->getScreenRectangle();
}

int Items::getFacing(int itemId) const {
	int itemIndex = findItem(itemId);
	if (itemIndex == -1) {
		return 0;
	}
	return _items[itemIndex]->getFacing();
}

void Items::setFacing(int itemId, int facing) {
	int itemIndex = findItem(itemId);
	if (itemIndex == -1) {
		return;
	}
	_items[itemIndex]->setFacing(facing);
}

void Items::spinInWorld(int itemId) {
	int itemIndex = findItem(itemId);
	if (itemIndex == -1) {
		return;
	}
	_items[itemIndex]->spinInWorld();
}

int Items::findTargetUnderMouse(int mouseX, int mouseY) const {
	int setId = _vm->_scene->getSetId();
	for (int i = 0 ; i < (int)_items.size(); ++i) {
		if (_items[i]->_setId == setId && _items[i]->isTarget() && _items[i]->isUnderMouse(mouseX, mouseY)) {
			return _items[i]->_itemId;
		}
	}
	return -1;
}

int Items::findItem(int itemId) const {
	for (int i = 0; i < (int)_items.size(); i++) {
		if (_items[i]->_itemId == itemId) {
			return i;
		}
	}
	return -1;
}

void Items::save(SaveFileWriteStream &f) {
	int size = (int)_items.size();

	f.writeInt(size);
	int i;
	for (i = 0; i != size; ++i) {
		_items[i]->save(f);
	}

	// Always write out 100 items
	for (; i != 100; ++i) {
		f.padBytes(0x174); // bbox + rect + 18 float fields
	}
}

void Items::load(SaveFileReadStream &f) {
	for (int i = _items.size() - 1; i >= 0; i--) {
		delete _items.remove_at(i);
	}
	_items.resize(f.readInt());

	int size = (int)_items.size();

	int i;
	for (i = 0; i != size; ++i) {
		_items[i] = new Item(_vm);
		_items[i]->load(f);
	}

	// Always read out 100 items
	for (; i != 100; ++i) {
		f.skip(0x174); // bbox + rect + 18 float fields
	}
}

} // End of namespace BladeRunner