Blob Blame Raw

#include "private.h"
#include "group.h"
#include "world.h"

struct _Group {
	HeliArray sprites;
	HeliArray spritesSorted;
};


Group createGroup() {
	if (!heliInitialized) return NULL;
	Group g = calloc(1, sizeof(*g));
	heliObjectRegister(g, (HeliFreeCallback)&groupDestroy);
	return g;
}

Group createEdgesGroupEx(double x1, double y1, double x2, double y2, double borderWidth, int weightLevel) {
	if (!heliInitialized) return NULL;
	if (x2 < x1) { double x = x2; x2 = x1; x1 = x; }
	if (y2 < y1) { double y = y2; y2 = y1; y1 = y; }
	if (borderWidth < 0.1) borderWidth = 0.1;
	
	double b = borderWidth;
	double w = x2 - x1 + b*2;
	double h = y2 - y1 + b*2;
	x1 -= b;
	y1 -= b;
	
	Group g = createGroup();
	groupAdd(g, createSpriteEx(x1 + w/2, y1 + b/2, w, b));
	groupAdd(g, createSpriteEx(x1 + w/2, y2 + b/2, w, b));
	groupAdd(g, createSpriteEx(x1 + b/2, y1 + h/2, b, h));
	groupAdd(g, createSpriteEx(x2 + b/2, y1 + h/2, b, h));
	groupSetMassLevel(g, weightLevel);
	return g;
}

Group createEdgesGroup()
	{ return createEdgesGroupEx(0, 0, worldGetWidth(), worldGetHeight(), worldGetHeight(), 100); }


void groupDestroy(Group group) {
	groupClear(group);
	heliArrayDestroy(&group->sprites);
	heliArrayDestroy(&group->spritesSorted);
	heliObjectUnregister(group);
	free(group);
}

void groupDestroyEx(Group group, int destroySprites) {
	groupClearEx(group, destroySprites);
	groupDestroy(group);
}

void groupAdd(Group group, Sprite sprite) {
	if (!sprite) return;
	if (groupContains(group, sprite)) return;
	heliArrayInsert(&group->sprites, -1, sprite, NULL);
	heliArrayInsert(&group->spritesSorted, -1, sprite, NULL);
	heliArrayInsert(heliSpriteGetGroups(sprite), -1, group, NULL);
}

void groupRemove(Group group, Sprite sprite) {
	for(int i = group->sprites.count-1; i >= 0; --i)
		if (group->sprites.items[i].value == sprite)
			heliArrayRemove(&group->sprites, i);
	for(int i = group->spritesSorted.count-1; i >= 0; --i)
		if (group->spritesSorted.items[i].value == sprite)
			heliArrayRemove(&group->spritesSorted, i);
	HeliArray *groups = heliSpriteGetGroups(sprite);
	for(int i = groups->count-1; i >= 0; --i)
		if (groups->items[i].value == group)
			heliArrayRemove(groups, i);
}

void groupClearEx(Group group, int destroySprites) {
	while(groupGetCount(group) > 0) {
		Sprite s = groupGet(group, 0);
		groupRemove(group, s);
		if (destroySprites) spriteDestroy(s);
	}
}

void groupClear(Group group)
	{ groupClearEx(group, FALSE); }
void groupDestroyEach(Group group)
	{ groupClearEx(group, TRUE); }

int groupContains(Group group, Sprite sprite) {
	for(int i = group->sprites.count-1; i >= 0; --i)
		if (group->sprites.items[i].value == sprite)
			return TRUE;
	return FALSE;
}

int groupGetCount(Group group)
	{ return group->sprites.count; }
Sprite groupGet(Group group, int i)
	{ return (Sprite)heliArrayGetValue(&group->sprites, i); }


Sprite groupSpriteByPoint(Group group, double x, double y, int onlyVisible) {
	heliSpriteSort(&group->spritesSorted);
	for(int i = group->spritesSorted.count - 1; i >= 0; --i) {
		Sprite s = (Sprite)(group->spritesSorted.items[i].value);
		if ((!onlyVisible || spriteGetVisible(s)) && spriteIsPointInside(s, x, y)) return s;
	}
	return NULL;
}


int groupOverlap(Group group, Sprite sprite) {
	for(int i = 0; i < groupGetCount(group); ++i)
		if (spriteOverlap(groupGet(group, i), sprite))
			return TRUE;
	return FALSE;
}

int groupCollide(Group group, Sprite sprite) {
	int result = FALSE;
	for(int j = 0; j < 10; ++j) {
		int actualCollision = FALSE;
		Sprite bestSprite = NULL;
		HeliCollisionInfo bestInfo = {};
		for(int i = 0; i < groupGetCount(group); ++i) {
			Sprite s = groupGet(group, i);
			HeliCollisionInfo info = {};
			if (heliSpriteCollisionCheck(s, sprite, &info)) {
				if (info.actualCollision) actualCollision = TRUE;
				if (!bestSprite || bestInfo.distance <= info.distance) {
					bestSprite = s;
					memcpy(&bestInfo, &info, sizeof(bestInfo));
				}
			}
		}
		if (!bestSprite) break;
		heliSpriteCollisionApply(bestSprite, sprite, &bestInfo);
		if (!actualCollision) break;
		result = TRUE;
	}
	return result;
}

int groupOverlapGroup(Group a, Group b) {
	for(int i = 0; i < groupGetCount(b); ++i)
		if (groupOverlap(a, groupGet(b, i)))
			return TRUE;
	return FALSE;
}

int groupCollideGroup(Group a, Group b) {
	int result = FALSE;
	for(int i = 0; i < groupGetCount(b); ++i)
		if (groupCollide(a, groupGet(b, i)))
			result = TRUE;
	return result;
}

int groupOverlapBetween(Group group) {
	for(int i = 0; i < groupGetCount(group); ++i)
		for(int j = i+1; j < groupGetCount(group); ++j)
			if (spriteOverlap(groupGet(group, i), groupGet(group, j)))
				return TRUE;
	return FALSE;
}

int groupCollideBetween(Group group) {
	int result = FALSE;
	for(int i = 0; i < groupGetCount(group); ++i)
		for(int j = i+1; j < groupGetCount(group); ++j)
			if (spriteCollide(groupGet(group, i), groupGet(group, j)))
				result = TRUE;
	return result;
}

void groupResetTouch(Group group) {
	for(int i = 0; i < groupGetCount(group); ++i)
		spriteResetTouch(groupGet(group, i));
}


double groupGetMinDepth(Group group) {
	if (groupGetCount(group) <= 0) return 0;
	double md = spriteGetDepth(groupGet(group, 0));
	for(int i = 1; i < groupGetCount(group); ++i) {
		double d = spriteGetDepth(groupGet(group, i));
		if (d < md) md = d;
	}
	return md;
}

double groupGetMaxDepth(Group group) {
	if (groupGetCount(group) <= 0) return 0;
	double md = spriteGetDepth(groupGet(group, 0));
	for(int i = 1; i < groupGetCount(group); ++i) {
		double d = spriteGetDepth(groupGet(group, i));
		if (d > md) md = d;
	}
	return md;
}


static void foreachInt(Group group, int value, HeliSpriteEashInt func)
	{ for(int i = 0; i < groupGetCount(group); ++i) func(groupGet(group, i), value); }
static void foreachUInt(Group group, unsigned int value, HeliSpriteEashUInt func)
	{ for(int i = 0; i < groupGetCount(group); ++i) func(groupGet(group, i), value); }
static void foreachDouble(Group group, double value, HeliSpriteEashDouble func)
	{ for(int i = 0; i < groupGetCount(group); ++i) func(groupGet(group, i), value); }


void groupDestroyTimerEach(Group group, double lifetime)
	{ for(int i = groupGetCount(group) - 1; i >= 0; --i) spriteDestroyTimer(groupGet(group, i), lifetime); }

void groupSetVisibleEach(Group group, int visible)
	{ foreachInt(group, visible, &spriteSetVisible); }
void groupSetWidthEach(Group group, double width)
	{ foreachDouble(group, width, &spriteSetWidth); }
void groupSetHeightEach(Group group, double height)
	{ foreachDouble(group, height, &spriteSetHeight); }
void groupSetDepthEach(Group group, double depth)
	{ foreachDouble(group, depth, &spriteSetDepth); }
void groupSetXEach(Group group, double x)
	{ foreachDouble(group, x, &spriteSetX); }
void groupSetYEach(Group group, double y)
	{ foreachDouble(group, y, &spriteSetY); }
void groupSetVelocityXEach(Group group, double x)
	{ foreachDouble(group, x, &spriteSetVelocityX); }
void groupSetVelocityYEach(Group group, double y)
	{ foreachDouble(group, y, &spriteSetVelocityY); }
void groupSetAccelerationXEach(Group group, double x)
	{ foreachDouble(group, x, &spriteSetAccelerationX); }
void groupSetAccelerationYEach(Group group, double y)
	{ foreachDouble(group, y, &spriteSetAccelerationY); }
void groupSetRotateToDirectionEach(Group group, int rotateToDirection)
	{ foreachInt(group, rotateToDirection, &spriteSetRotateToDirection); }
void groupSetRotationEach(Group group, double rotation)
	{ foreachDouble(group, rotation, &spriteSetRotation); }
void groupSetRotationSpeedEach(Group group, double rotationSpeed)
	{ foreachDouble(group, rotationSpeed, &spriteSetRotationSpeed); }
void groupSetScaleEach(Group group, double scale)
	{ foreachDouble(group, scale, &spriteSetScale); }
void groupSetMirrorXEach(Group group, int mirrorX)
	{ foreachInt(group, mirrorX, &spriteSetMirrorX); }
void groupSetMirrorYEach(Group group, int mirrorY)
	{ foreachInt(group, mirrorY, &spriteSetMirrorY); }
void groupSetShapeColorEach(Group group, unsigned int colorCode)
	{ foreachUInt(group, colorCode, &spriteSetShapeColor); }
void groupSetTintColorEach(Group group, unsigned int colorCode)
	{ foreachUInt(group, colorCode, &spriteSetTintColor); }
void groupSetUserTagEach(Group group, int tag)
	{ foreachInt(group, tag, &spriteSetUserTag); }

void groupSetBounciness(Group group, double bounciness)
	{ foreachDouble(group, bounciness, &spriteSetBounciness); }
void groupSetBouncinessThreshold(Group group, double bouncinessThreshold)
	{ foreachDouble(group, bouncinessThreshold, &spriteSetBouncinessThreshold); }
void groupSetFriction(Group group, double friction)
	{ foreachDouble(group, friction, &spriteSetFriction); }
void groupSetAirFriction(Group group, double friction)
	{ foreachDouble(group, friction, &spriteSetAirFriction); }
void groupSetMassLevel(Group group, int massLevel)
	{ foreachInt(group, massLevel, &spriteSetMassLevel); }
void groupSetColliderSensitiveDistance(Group group, double distance)
	{ foreachDouble(group, distance, &spriteSetColliderSensitiveDistance); }


void groupSetAnimationEach(Group group, Animation animation) {
	for(int i = groupGetCount(group) - 1; i >= 0 ; --i)
		spriteSetAnimation(groupGet(group, i), animation);
}

void groupSetNoAnimationEach(Group group) {
	for(int i = groupGetCount(group) - 1; i >= 0 ; --i)
		spriteSetNoAnimation(groupGet(group, i));
}

void groupPointToEach(Group group, double x, double y) {
	for(int i = 0; i < groupGetCount(group); ++i)
		spritePointTo(groupGet(group, i), x, y);
}

void groupSetSpeedAndDirectionEach(Group group, double speed, double angle) {
	for(int i = 0; i < groupGetCount(group); ++i)
		spriteSetSpeedAndDirection(groupGet(group, i), speed, angle);
}

void groupSetXYEach(Group group, double x, double y) {
	for(int i = 0; i < groupGetCount(group); ++i)
		spriteSetXY(groupGet(group, i), x, y);
}
void groupSetVelocityEach(Group group, double x, double y) {
	for(int i = 0; i < groupGetCount(group); ++i)
		spriteSetVelocityXY(groupGet(group, i), x, y);
}
void groupSetAccelerationEach(Group group, double x, double y) {
	for(int i = 0; i < groupGetCount(group); ++i)
		spriteSetAccelerationXY(groupGet(group, i), x, y);
}


void groupSetUserTextEach(Group group, const char *text) {
	for(int i = groupGetCount(group) - 1; i >= 0 ; --i)
		spriteSetUserText(groupGet(group, i), text);
}

void groupSetUserDataEach(Group group, void *data) {
	for(int i = groupGetCount(group) - 1; i >= 0 ; --i)
		spriteSetUserData(groupGet(group, i), data);
}

void groupSetDestroyEach(Group group, SpriteCallback destroy) {
	for(int i = groupGetCount(group) - 1; i >= 0 ; --i)
		spriteSetDestroy(groupGet(group, i), destroy);
}


void groupSetColliderEach(Group group, Collider type, double xOffset, double yOffset, double rotationOffset)
	{ groupSetColliderEachEx(group, type, xOffset, yOffset, rotationOffset, -1, -1, -1); }
void groupSetColliderCircleEach(Group group, double xOffset, double yOffset, double radius)
	{ groupSetColliderEachEx(group, COLLIDER_CIRCLE, xOffset, yOffset, 0, 0, 0, radius); }
void groupSetColliderRectangleEach(
	Group group, double xOffset, double yOffset, double rotationOffset,
	double width, double height, double cornersRadius )
{
	groupSetColliderEachEx(
		group, COLLIDER_RECTANGLE,
		xOffset, yOffset, rotationOffset,
		width, height, cornersRadius);
}
void groupSetColliderEachEx(
	Group group, Collider type,
	double xOffset, double yOffset, double rotationOffset,
	double width, double height, double radius)
{
	for(int i = 0; i < groupGetCount(group); ++i)
		spriteSetColliderEx(
			groupGet(group, i), type,
			xOffset, yOffset, rotationOffset,
			width, height, radius );
}