aboutsummaryrefslogtreecommitdiff
path: root/src/cObjectHandler.cpp
blob: eca340bad08660f154a1ebeb87b1325cc7ebe327 (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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include "cObjectHandler.h"

cObjectHandler::cObjectHandler(cRender *_render, bool _enableInputMapping, bool _enableCollision) : cameraPosition ({0,0}), iActiveObject(0)
{
	render = _render;

	enableInputMapping = _enableInputMapping;
	enableCollision = enableInputMapping ? _enableCollision : false; // Collision requires input mapping

	objects.push_back(NULL); //Create first Object as Catcher for Events

	buildHitmap();
}

int cObjectHandler::createObject(cObject *_object)
{
	objects.push_back(_object);

	buildHitmap();
	return objects.size() - 1;
}

int cObjectHandler::moveObject(int _object, sPos _pos, int _mode)
{
	if (_object >= objects.size()) //prevent segmentation faults
		return 1;

	if (!objects[_object])
		return 1;

	sPos objPosition = objects[_object]->getPosition();
	sPos newPosition = {0,0};

	if (_mode == _MOVE_RELATIVE)
		newPosition = { objPosition.x + _pos.x, objPosition.y + _pos.y };
	else if (_mode == _MOVE_ABSOLUTE)
		newPosition = _pos;
	else if (_mode == _MOVE_FORCE_ABSOLUTE)
	{
		objects[_object]->setPosition(_pos);
		return 0;
	}

	sCollision coll = checkCollision(newPosition, objects[_object]->getSize());


	bool abort = false;

	if(coll.idc)
	{
		for(int i = 0; i < coll.idc; i++)
		{
			if(coll.idv[i] != _object)
				abort += objects[_object]->onCollisionActive(_pos, objects[coll.idv[i]]->onCollisionPassive(_pos));
		}
	}

	if(!abort)
		objects[_object]->setPosition(newPosition);

	if(coll.idv)
		free (coll.idv);
	if(coll.hitv)
		free (coll.hitv);

	buildHitmap();
	return abort;
}

int cObjectHandler::destroyObject(int _object)
{
	if(!objects[_object])
		return 1;

	delete objects[_object];
	objects[_object] = NULL;

	buildHitmap();
	return 0;
}

int cObjectHandler::write()
{
	render->clear();

	for (unsigned long int i = 0; i < meshes.size(); i++)
	{
		if(meshes[i])
		{
			moveWiremesh(i,{-cameraPosition.x, -cameraPosition.y, 0} ,_MOVE_RELATIVE);
			meshes[i]->write(render);
			moveWiremesh(i,{cameraPosition.x, cameraPosition.y, 0},_MOVE_RELATIVE);
		}
	}

	for (unsigned long int i = 0; i < objects.size(); i++)
	{
		if (objects[i]) // Check if objects[i] is existent
		{
			//Draw every Object
			sObject obj = objects[i]->getObject(); //get Object #i

			for (int o = 0; o < obj.sizeY; o++) { //y axis
				for (int p = 0; p < obj.sizeX; p++) { //x axis
					if (obj.cScreen[p][o]) { //Dont overwrite empty pixels
						sPos pos{ obj.pos.x + p - cameraPosition.x,
							 				obj.pos.y + o - cameraPosition.y };
						render->drawPoint(obj.cScreen[p][o], pos, true, obj.wColor[p][o]);
					}
				}
			}
		}
	}

	return 0;
}

int cObjectHandler::clickEvent(sPos _pos, unsigned int _button)
{
	if(_pos.x >= iHitMap.size())
		return 1;
	if(_pos.y >= iHitMap[_pos.x].size())
		return 1;


	if(objects[ iHitMap[_pos.x][_pos.y] ])
	{
		sPos rel_pos;
		sPos obj_pos = objects[ iHitMap[_pos.x][_pos.y] ]->getPosition();
		rel_pos.x = _pos.x - obj_pos.x + cameraPosition.x;
		rel_pos.y = _pos.y - obj_pos.y + cameraPosition.y;

		iActiveObject = iHitMap[_pos.x][_pos.y]; //Set active object
		objects[ iHitMap[_pos.x][_pos.y] ]->onClick(rel_pos, _button);
	}
	else
		return 1;

	return 0;
}

int cObjectHandler::charEvent(unsigned char _c)
{
	if(objects.size() > iActiveObject)
	{
		if(objects[iActiveObject])
		{
			objects[iActiveObject]->onChar(_c);
		}
		else
			return 1;
	}

	return 0;
}

void cObjectHandler::buildHitmap()
{
	if(!enableInputMapping)
		return;

	//Rebuild 2D vector
	sPos size = render->getSize();

	vector<unsigned int> cp;

	while(size.y > cp.size())
	{
		cp.push_back(0);
	}

	while (size.x > iHitMap.size())
	{
		iHitMap.push_back(cp);
	}

	while (size.x <= iHitMap.size())
	{
		iHitMap.pop_back();
	}
	for(unsigned int x = 0; x < iHitMap.size(); x++)
	{
		for(unsigned int y = 0; y < iHitMap[x].size(); y++)
		{
			iHitMap[x][y] = 0;
		}
	}
	//Write object IDs to iHitMap
	for(unsigned int i = 0; i < objects.size(); i++)
	{
		if(objects[i])
		{
			sPos oPos = objects[i]->getPosition();
			sPos oSize = objects[i]->getSize();

			oPos.x -= cameraPosition.x;
			oPos.y -= cameraPosition.y;

			for(int x = oPos.x; x < oPos.x + oSize.x; x++)
			{
				for(int y = oPos.y; y < oPos.y + oSize.y; y++)
				{
					if((x < size.x && y < size.y) && (x >= 0 && y >= 0)) //Objects can be outside the screen.
						iHitMap[x][y] = i;
				}//for
			}//for
		}//if
	}//for
}//buildHitmap

void cObjectHandler::focusNext()
{
	iActiveObject++;

	if(iActiveObject >= objects.size())
		iActiveObject = 0;
}

void cObjectHandler::focus(unsigned int _id)
{
	if(_id >= objects.size())
		iActiveObject = objects.size();
	else
		iActiveObject = _id;
}

int cObjectHandler::createWiremesh(cWiremesh *_mesh)
{
	meshes.push_back(_mesh);

	return meshes.size() - 1;
}

int cObjectHandler::moveWiremesh(int _mesh, sCoord3d _pos, int _mode)
{
	if (_mesh >= meshes.size()) //prevent segmentation faults
		return 1;

	if (!meshes[_mesh])
		return 1;

	sCoord3d meshPosition = meshes[_mesh]->getPosition();

	if (_mode == _MOVE_RELATIVE)
		meshes[_mesh]->setPosition(meshPosition + _pos);
	else if (_mode == _MOVE_ABSOLUTE)
		meshes[_mesh]->setPosition(_pos);

	return 0;
}

int cObjectHandler::destroyWiremesh(int _mesh)
{
	if(!meshes[_mesh])
		return 1;

	delete meshes[_mesh];
	meshes[_mesh] = NULL;

	return 0;
}

int cObjectHandler::rotateWiremesh(int _mesh, sCoord3d _angle)
{
	if (_mesh >= meshes.size()) //prevent segmentation faults
		return 1;

	if (!meshes[_mesh])
		return 1;

	meshes[_mesh]->rotate(_angle);

	return 0;
}

void cObjectHandler::setCameraPosition(sPos _pos, int _mode)
{
	if(_mode == _MOVE_ABSOLUTE)
		cameraPosition = _pos;
	else if(_mode == _MOVE_RELATIVE)
	{
		cameraPosition.x += _pos.x;
		cameraPosition.y += _pos.y;
	}

	buildHitmap();
}

sPos cObjectHandler::getCameraPosition()
{
	return cameraPosition;
}

sCollision cObjectHandler::checkCollision(sPos _pos, sPos _size)
{
	sCollision ret;
	vector<unsigned int> collisions;
	vector<int> hitTypes;
	ret.idc = 0;
	ret.idv = NULL;
	ret.hitv = NULL;

	if(!render)
		return ret;

	if(!enableCollision || !enableInputMapping)
		return ret;

	int sizeX, sizeY;

	sizeX = render->getSize().x;
	sizeY = render->getSize().y;

	//The mother of if-statements
	//No collision for offscreen objects
	if( (_pos.x < cameraPosition.x && _pos.x + _size.x + cameraPosition.x < 0) ||
			(_pos.x - cameraPosition.x >= iHitMap.size() && _pos.x + _size.x - cameraPosition.x >= iHitMap.size()) ||
			(_pos.y < cameraPosition.y && _pos.y + _size.y + cameraPosition.y < 0) ||
			(_pos.y - cameraPosition.y >= iHitMap[0].size() && _pos.y + _size.y - cameraPosition.y >= iHitMap[0].size()) )
		return ret;

	for(int x = _pos.x - cameraPosition.x; x < _pos.x + _size.x - cameraPosition.x; x++)
	{
		for(int y = _pos.y - cameraPosition.y; y < _pos.y + _size.y - cameraPosition.y; y++)
		{
			if(!(x >= sizeX || x < 0 || y >= sizeY || y < 0))
			{
				if(iHitMap[x][y])
					collisions.push_back(iHitMap[x][y]);
			}
		}
	}

	//Since Object can hit on multiple Pixels, duplications can occur.
	//Sort and set duplicates to zero
	//-> zeros are at front of vector
	for(unsigned int swaps = 1; swaps > 0;)
	{
		swaps = 0;

		for(long int i = 0; i  < (long int)collisions.size() - 1; i++)
		{
			if(collisions[i] > collisions[i + 1])
			{
				swaps ++;
				unsigned int tmp = 0;

				tmp = collisions[i];
				collisions[i] = collisions[i + 1];
				collisions[i + 1] = tmp;
			}
			if(collisions[i] == collisions[i + 1])
				collisions[i] = 0;
		}
	}


	//Since every empty entry is in front, pop them
	while( collisions.size() && !collisions.front())
		collisions.erase(collisions.begin());

	ret.idc = collisions.size();
	ret.idv = (unsigned int*) malloc( sizeof(*ret.idv) * ret.idc );

	for(unsigned int i = 0; i < ret.idc; i++)
	{
		ret.idv[i] = collisions[i];
	}

	return ret;
}