Blob Blame Raw

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

struct _Group {
	HeliArray sprites;
};


Group createGroup() {
	Group g = calloc(1, sizeof(*g));
	return g;
}

Group createEdgesGroupEx(double x1, double y1, double x2, double y2, double borderWidth) {
	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, y1, w, b));
	groupAdd(g, createSpriteEx(x1, y2, w, b));
	groupAdd(g, createSpriteEx(x1, y1, b, h));
	groupAdd(g, createSpriteEx(x2, y1, b, h));
	return g;
}

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


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

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

void groupAdd(Group group, Sprite sprite) {
	if (groupContains(group, sprite)) return;
	heliArrayInsert(&group->sprites, -1, sprite, NULL);
	heliArrayInsert(heliSpriteGetGroups(sprite), -1, sprite, 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);
	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); }

int groupOverlap(Group group, Sprite sprite)
	{ return groupCollideEx(group, sprite, TRUE, TRUE, 0); }
int groupCollide(Group group, Sprite sprite, double bounciness)
	{ return groupCollideEx(group, sprite, FALSE, FALSE, bounciness); }
int groupBounceOff(Group group, Sprite sprite, double bounciness)
	{ return groupCollideEx(group, sprite, FALSE, TRUE, bounciness); }
int groupPush(Group group, Sprite sprite, double bounciness)
	{ return groupCollideEx(group, sprite, TRUE, FALSE, bounciness); }
int groupCollideEx(Group group, Sprite sprite, int keepVelocityGroup, int keepVelocitySprite, double bounciness) {
	int result = FALSE;
	for(int i = 0; i < groupGetCount(group); ++i)
		if (spriteCollideEx(groupGet(group, i), sprite, keepVelocityGroup, keepVelocitySprite, bounciness))
			result = TRUE;
	return result;
}

int groupOverlapGroup(Group a, Group b)
	{ return groupCollideGroupEx(a, b, TRUE, TRUE, 0); }
int groupCollideGroup(Group a, Group b, double bounciness)
	{ return groupCollideGroupEx(a, b, FALSE, FALSE, bounciness); }
int groupBounceOffGroup(Group group, Group other, double bounciness)
	{ return groupCollideGroupEx(group, other, FALSE, TRUE, bounciness); }
int groupPushGroup(Group group, Group other, double bounciness)
	{ return groupCollideGroupEx(group, other, TRUE, FALSE, bounciness); }
int groupCollideGroupEx(Group a, Group b, int keepVelocityA, int keepVelocityB, double bounciness) {
	int result = FALSE;
	for(int i = 0; i < groupGetCount(b); ++i)
		if (groupCollideEx(a, groupGet(b, i), keepVelocityA, keepVelocityB, bounciness))
			result = TRUE;
	return result;
}

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 foreachDouble(Group group, double value, HeliSpriteEashDouble func)
	{ for(int i = 0; i < groupGetCount(group); ++i) func(groupGet(group, i), value); }
static void foreachString(Group group, const char *value, HeliSpriteEashString func)
	{ for(int i = 0; i < groupGetCount(group); ++i) func(groupGet(group, i), value); }


void groupDestroyTimerEach(Group group, double lifetime)
	{ foreachDouble(group, lifetime, &spriteDestroyTimer); }

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 groupSetVelocityXEach(Group group, double x)
	{ foreachDouble(group, x, &spriteSetVelocityX); }
void groupSetVelocityYEach(Group group, double y)
	{ foreachDouble(group, y, &spriteSetVelocityY); }
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 groupSetAnimationEach(Group group, const char *path)
	{ foreachString(group, path, &spriteSetAnimation); }
void groupSetShapeColorEach(Group group, const char *color)
	{ foreachString(group, color, &spriteSetShapeColor); }
void groupSetTintColorEach(Group group, const char *color)
	{ foreachString(group, color, &spriteSetTintColor); }

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 groupSetVelocityEach(Group group, double x, double y) {
	for(int i = 0; i < groupGetCount(group); ++i)
		spriteSetVelocityXY(groupGet(group, i), x, y);
}

void groupSetColliderEachEx(Group group, Collider type, double xOffset, double yOffset,
							double widthOrRadius, double height, double rotationOffset)
{
	for(int i = 0; i < groupGetCount(group); ++i)
		spriteSetColliderEx(groupGet(group, i), type, xOffset, yOffset,
							widthOrRadius, height, rotationOffset);
}

void groupSetColliderEach(Group group, Collider type, double xOffset, double yOffset)
	{ groupSetColliderEachEx(group, type, xOffset, yOffset, -1, -1, 0); }