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

#include "bladerunner/bladerunner.h"

#include "bladerunner/savefile.h"
#include "bladerunner/slice_renderer.h"
#include "bladerunner/zbuffer.h"

namespace BladeRunner {

Item::Item(BladeRunnerEngine *vm) {
	_vm = vm;

	_itemId = -1;
	_setId = -1;

	_animationId = -1;
	_position.x = 0;
	_position.y = 0;
	_position.z = 0;
	_facing = 0;
	_angle = 0.0f;
	_width = 0;
	_height = 0;
	_boundingBox.setXYZ(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
	_screenX = 0;
	_screenY = 0;
	_depth = 0.0f;
	_isTarget = false;
	_isSpinning = false;
	_facingChange = 0;
	_isVisible = true;
	_isPoliceMazeEnemy = false;
	_screenRectangle.bottom = -1;
	_screenRectangle.right = -1;
	_screenRectangle.top = -1;
	_screenRectangle.left = -1;
}

void Item::getXYZ(float *x, float *y, float *z) const {
	*x = _position.x;
	*y = _position.y;
	*z = _position.z;
}

void Item::getWidthHeight(int *width, int *height) const {
	*width = _width;
	*height = _height;
}

void Item::getAnimationId(int *animationId) const {
	*animationId = _animationId;
}

void Item::setFacing(int facing) {
	_facing = facing;
	_angle = _facing * (M_PI / 512.0f);
}

bool Item::tick(Common::Rect *screenRect, bool special) {
	if (!_isVisible) {
		*screenRect = Common::Rect();
		return false;
	}

	bool isVisibleFlag = false;

	Vector3 position(_position.x, -_position.z, _position.y);
	int animationId = _animationId + (special ? 1 : 0);
	_vm->_sliceRenderer->drawInWorld(animationId, 0, position, M_PI - _angle, 1.0f, _vm->_surfaceFront, _vm->_zbuffer->getData());
	_vm->_sliceRenderer->getScreenRectangle(&_screenRectangle, animationId, 0, position, M_PI - _angle, 1.0f);

	if (!_screenRectangle.isEmpty()) {
		*screenRect = _screenRectangle;
		isVisibleFlag = true;
	} else {
		*screenRect = Common::Rect();
	}

	if (_isSpinning) {
		_facing += _facingChange;

		if (_facing >= 1024) {
			_facing -= 1024;
		} else if (_facing < 0) {
			_facing += 1024;
		}
		_angle = _facing * (M_PI / 512.0f);

		if (_facingChange > 0) {
			_facingChange = _facingChange - 20;
			if (_facingChange < 0) {
				_facingChange = 0;
				_isSpinning = false;
			}
		} else if (_facingChange < 0) {
			_facingChange = _facingChange + 20;
			if (_facingChange > 0) {
				_facingChange = 0;
				_isSpinning = false;
			}
		} else {
			_isSpinning = false;
		}
	}

	return isVisibleFlag;
}

// setXYZ() recalculates the item's bounding box,
// but in addition to the item's (Vector3) position
// it takes into account the item's width and height
void Item::setXYZ(Vector3 position) {
	_position = position;
	int halfWidth = _width / 2;
	_boundingBox.setXYZ(_position.x - halfWidth, _position.y, _position.z - halfWidth,
	                    _position.x + halfWidth, _position.y + _height, _position.z + halfWidth);
	Vector3 screenPosition = _vm->_view->calculateScreenPosition(_position);
	_screenX = screenPosition.x;
	_screenY = screenPosition.y;
	_depth = screenPosition.z * 25.5f;
}

void Item::setup(int itemId, int setId, int animationId, Vector3 position, int facing, int height, int width, bool isTargetFlag, bool isVisibleFlag, bool isPoliceMazeEnemyFlag) {
	_itemId = itemId;
	_setId = setId;
	_animationId = animationId;
	_facing = facing;
	_angle = facing * (M_PI / 512.0f);
	_width = width;
	_height = height;
	_isTarget = isTargetFlag;
	_isVisible = isVisibleFlag;
	_isPoliceMazeEnemy = isPoliceMazeEnemyFlag;
	setXYZ(position);
	_screenRectangle.bottom = -1;
	_screenRectangle.right = -1;
	_screenRectangle.top = -1;
	_screenRectangle.left = -1;
}

void Item::spinInWorld() {
	_isSpinning = true;
	if (_vm->_rnd.getRandomNumberRng(1, 2) == 1) {
		_facingChange = -340;
	} else {
		_facingChange = 340;
	}
}

bool Item::isUnderMouse(int mouseX, int mouseY) const {
	return _isVisible
	    && mouseX >= _screenRectangle.left   - 10
	    && mouseX <= _screenRectangle.right  + 10
	    && mouseY >= _screenRectangle.top    - 10
	    && mouseY <= _screenRectangle.bottom + 10;
}

void Item::save(SaveFileWriteStream &f) {
	f.writeInt(_setId);
	f.writeInt(_itemId);
	f.writeBoundingBox(_boundingBox, false);
	f.writeRect(_screenRectangle);
	f.writeInt(_animationId);
	f.writeVector3(_position);
	f.writeInt(_facing);
	f.writeFloat(_angle);
	f.writeInt(_width);
	f.writeInt(_height);
	f.writeInt(_screenX);
	f.writeInt(_screenY);
	f.writeFloat(_depth);
	f.writeBool(_isTarget);
	f.writeBool(_isSpinning);
	f.writeInt(_facingChange);
	f.writeFloat(0.0f); // _viewAngle
	f.writeBool(_isVisible);
	f.writeBool(_isPoliceMazeEnemy);
}

void Item::load(SaveFileReadStream &f) {
	_setId = f.readInt();
	_itemId = f.readInt();
	_boundingBox = f.readBoundingBox(false);
	_screenRectangle = f.readRect();
	_animationId = f.readInt();
	_position = f.readVector3();
	_facing  = f.readInt();
	_angle = f.readFloat();
	_width = f.readInt();
	_height = f.readInt();
	_screenX = f.readInt();
	_screenY = f.readInt();
	_depth = f.readFloat();
	_isTarget = f.readBool();
	_isSpinning = f.readBool();
	_facingChange = f.readInt();
	f.skip(4);
	_isVisible = f.readBool();
	_isPoliceMazeEnemy = f.readBool();
}

} // End of namespace BladeRunner