Blob Blame Raw

#include "private.h"

#include "sprite.h"
#include "world.h"
#include "group.h"
#include "drawing.h"
#include "animation.h"


struct _Sprite {
	double x;
	double y;
	double rotation;
	double width;
	double height;
	double scale;
	
	double vx;
	double vy;
	double rotationSpeed;
	
	HeliCollider collider;
	double bounciness;
	
	int rotateToDirection;
	int mirrorX;
	int mirrorY;
	double depth;
	double lifeTime;
	int visible;
	int tag;
	int debug;
	
	double shapeColor[4];
	double tintColor[4];
	
	HeliArray groups;
	
	Animation animation;
};

static HeliArray sprites;
static HeliArray spritesSorted;


static void prepareCollider(Sprite sprite, HeliCollider *collider) {
	collider->type = sprite->collider.type;
	collider->x = sprite->collider.x + sprite->x;
	collider->y = sprite->collider.y + sprite->y;
	collider->width = sprite->collider.width;
	collider->height = sprite->collider.height;
	collider->radius = sprite->collider.radius;
	collider->rotation = sprite->collider.rotation + sprite->rotation;
	
	// auto-size
	if (collider->type == COLLIDER_RECTANGLE) {
		if (collider->width < -HELI_PRECISION) collider->width = sprite->width;
		if (collider->height < -HELI_PRECISION) collider->height = sprite->height;
		if (collider->radius < -HELI_PRECISION) collider->radius = 0;
	} else
	if (collider->type == COLLIDER_CIRCLE) {
		collider->width = 0;
		collider->height = 0;
		if (collider->radius < -HELI_PRECISION) collider->radius = 0.5*sprite->width;
	}
	
	// scale
	collider->width *= sprite->scale;
	collider->height *= sprite->scale;
	collider->radius *= sprite->scale;
	
	// fix small values
	if (collider->width  < HELI_PRECISION) collider->width = HELI_PRECISION;
	if (collider->height < HELI_PRECISION) collider->height = HELI_PRECISION;
	if (collider->radius < HELI_PRECISION) collider->radius = HELI_PRECISION;
	
	// fix round corners
	if (collider->type == COLLIDER_RECTANGLE) {
		double rmax = 0.5*( collider->width < collider->height
						  ? collider->width : collider->height );
		if (collider->radius >= rmax - HELI_PRECISION)
			collider->radius = rmax;
		collider->width  -= 2*collider->radius;
		collider->height -= 2*collider->radius;
	}
}


Sprite createSpriteEx(double x, double y, double width, double height) {
	if (!heliInitialized) return NULL;

	Sprite s = calloc(1, sizeof(*s));
	s->x = x;
	s->y = y;
	s->width = width;
	s->height = height;
	
	s->scale = 1;
	s->collider.type = COLLIDER_RECTANGLE;
	s->collider.width = s->collider.height = -1;
	s->bounciness = 1;
	
	s->mirrorX = 1;
	s->mirrorY = 1;
	s->lifeTime = -1;
	s->visible = TRUE;
	
	s->shapeColor[0] = s->shapeColor[1] = s->shapeColor[2] = 0.5;
	s->shapeColor[3] = 1;
	s->tintColor[0] = s->tintColor[1] = s->tintColor[2] = s->tintColor[3] = 1;
	
	heliArrayInsert(&sprites, -1, s, NULL);
	heliArrayInsert(&spritesSorted, -1, s, NULL);
	
	heliObjectRegister(s, (HeliFreeCallback)&spriteDestroy);
	return s;
}

Sprite createSprite(double x, double y)
	{ return createSpriteEx(x, y, 100, 100); }

void spriteDestroy(Sprite sprite) {
	spriteSetNoAnimation(sprite);
	while(sprite->groups.count > 0)
		groupRemove((Group)sprite->groups.items[0].value, sprite);
	for(int i = sprites.count - 1; i >= 0; --i)
		if (sprites.items[i].value == sprite)
			heliArrayRemove(&sprites, i);
	for(int i = spritesSorted.count - 1; i >= 0; --i)
		if (spritesSorted.items[i].value == sprite)
			heliArrayRemove(&spritesSorted, i);
	heliArrayDestroy(&sprite->groups);
	heliObjectUnregister(sprite);
	free(sprite);
}

void spriteDestroyTimer(Sprite sprite, double lifeTime) {
	if (lifeTime <= 0) { spriteDestroy(sprite); return; }
	sprite->lifeTime = lifeTime;
}

double spriteGetX(Sprite sprite) { return sprite->x; }
void spriteSetX(Sprite sprite, double x) { sprite->x = x; }

double spriteGetY(Sprite sprite) { return sprite->y; }
void spriteSetY(Sprite sprite, double y) { sprite->y = y; }

double spriteGetVelocityX(Sprite sprite) { return sprite->vx; }
void spriteSetVelocityX(Sprite sprite, double x)
	{ spriteSetVelocityXY(sprite, x, sprite->vy); }

double spriteGetVelocityY(Sprite sprite) { return sprite->vy; }
void spriteSetVelocityY(Sprite sprite, double y)
	{ spriteSetVelocityXY(sprite, sprite->vx, y); }

double spriteGetScale(Sprite sprite) { return sprite->scale; }
void spriteSetScale(Sprite sprite, double scale) { sprite->scale = scale; }

double spriteGetWidth(Sprite sprite) { return sprite->width; }
void spriteSetWidth(Sprite sprite, double width)
	{ sprite->width = width > 0 ? width : 0; }

double spriteGetHeight(Sprite sprite) { return sprite->height; }
void spriteSetHeight(Sprite sprite, double height)
	{ sprite->height = height > 0 ? height : 0; }

int spriteGetRotateToDirection(Sprite sprite) { return sprite->rotateToDirection; }
void spriteSetRotateToDirection(Sprite sprite, int rotateToDirection) {
	if (rotateToDirection) {
		sprite->rotateToDirection = TRUE;
		sprite->rotation = spriteGetDirection(sprite);
	} else {
		sprite->rotateToDirection = FALSE;
	}
}

double spriteGetRotation(Sprite sprite) { return sprite->rotation; }
void spriteSetRotation(Sprite sprite, double rotation)
	{ if (!sprite->rotateToDirection) sprite->rotation = rotation; }

double spriteGetRotationSpeed(Sprite sprite) { return sprite->rotationSpeed; }
void spriteSetRotationSpeed(Sprite sprite, double rotationSpeed)
	{ sprite->rotationSpeed = rotationSpeed; }

int spriteGetMirrorX(Sprite sprite) { return sprite->mirrorX; }
void spriteSetMirrorX(Sprite sprite, int mirrorX)
	{ sprite->mirrorX = mirrorX < 0 ? -1 : 1; }

int spriteGetMirrorY(Sprite sprite) { return sprite->mirrorY; }
void spriteSetMirrorY(Sprite sprite, int mirrorY)
	{ sprite->mirrorY = mirrorY < 0 ? -1 : 1; }

double spriteGetDepth(Sprite sprite) { return sprite->depth; }
void spriteSetDepth(Sprite sprite, double depth)
	{ sprite->depth = depth; }

int spriteGetVisible(Sprite sprite) { return sprite->visible; }
void spriteSetVisible(Sprite sprite, int visible)
	{ sprite->visible = visible != FALSE; }

int spriteGetTag(Sprite sprite) { return sprite->tag; }
void spriteSetTag(Sprite sprite, int tag)
	{ sprite->tag = tag; }

int spriteGetDebug(Sprite sprite) { return sprite->debug; }
void spriteSetDebug(Sprite sprite, int debug)
	{ sprite->debug = debug != FALSE; }

int spriteOverlap(Sprite a, Sprite b)
	{ return spriteCollideEx(a, b, TRUE, TRUE, 0); }
int spriteCollide(Sprite a, Sprite b, double bounciness)
	{ return spriteCollideEx(a, b, FALSE, FALSE, bounciness); }
int spriteBounceOff(Sprite sprite, Sprite other, double bounciness)
	{ return spriteCollideEx(sprite, other, FALSE, TRUE, bounciness); }
int spritePush(Sprite sprite, Sprite other, double bounciness)
	{ return spriteCollideEx(sprite, other, TRUE, FALSE, bounciness); }

int spriteCollideEx(Sprite a, Sprite b, int keepVelocityA, int keepVelocityB, double bounciness) {
	if (a == b) return FALSE;
	HeliCollider ca, cb;
	prepareCollider(a, &ca);
	prepareCollider(b, &cb);
	double dx = 0, dy = 0;
	double vx = a->vx - b->vx;
	double vy = a->vy - b->vy;
	bounciness = fabs(bounciness * a->bounciness * b->bounciness);
	if (!heliCheckCollision(&ca, &cb, &dx, &dy, &vx, &vy, bounciness))
		return FALSE;
	
	if ( keepVelocityA && !keepVelocityB) {
		b->x -= dx;
		b->y -= dy;
		spriteSetVelocityXY(b, a->vx - vx, a->vy - vy);
	} else
	if (!keepVelocityA &&  keepVelocityB) {
		a->x += dx;
		a->y += dy;
		spriteSetVelocityXY(a, b->vx + vx, b->vy + vy);
	} else
	if (!keepVelocityA && !keepVelocityB) {
		double vxAvg = 0.5*(a->vx + b->vx);
		double vyAvg = 0.5*(a->vy + b->vy);
		
		a->x += 0.5*dx;
		a->y += 0.5*dy;
		spriteSetVelocityXY(a, vxAvg + 0.5*vx, vyAvg + 0.5*vy);
		
		b->x -= 0.5*dx;
		b->y -= 0.5*dy;
		spriteSetVelocityXY(b, vxAvg - 0.5*vx, vyAvg - 0.5*vy);
	}
	
	return TRUE;
}


double spriteGetBounciness(Sprite sprite) { return sprite->bounciness; }
void spriteSetBounciness(Sprite sprite, double bounciness)
	{ sprite->bounciness = bounciness > 0 ? bounciness : 0; }

void spriteSetCollider(Sprite sprite, Collider type, double xOffset, double yOffset, double rotationOffset)
	{ spriteSetColliderEx(sprite, type, xOffset, yOffset, rotationOffset, -1, -1, -1); }
void spriteSetColliderCircle(Sprite sprite, double xOffset, double yOffset, double radius)
	{ spriteSetColliderEx(sprite, COLLIDER_CIRCLE, xOffset, yOffset, 0, 0, 0, radius); }
void spriteSetColliderRectangle(
	Sprite sprite, double xOffset, double yOffset, double rotationOffset,
	double width, double height, double cornersRadius )
{
	spriteSetColliderEx(
		sprite, COLLIDER_RECTANGLE,
		xOffset, yOffset, rotationOffset,
		width, height, cornersRadius);
}

void spriteSetColliderEx(
	Sprite sprite, Collider type,
	double xOffset, double yOffset, double rotationOffset,
	double width, double height, double radius )
{
	sprite->collider.type = type;
	sprite->collider.x = xOffset;
	sprite->collider.y = yOffset;
	sprite->collider.rotation = rotationOffset;
	if (type == COLLIDER_CIRCLE) {
		sprite->collider.width = 0;
		sprite->collider.height = 0;
		sprite->collider.radius = radius;
	} else
	if (type == COLLIDER_RECTANGLE) {
		sprite->collider.width = width;
		sprite->collider.height = height;
		sprite->collider.radius = radius;
	} else {
		sprite->collider.width = 0;
		sprite->collider.height = 0;
		sprite->collider.radius = 0;
	}
}

void spriteSetAnimation(Sprite sprite, Animation animation)
	{ sprite->animation = animation; }

void spriteSetNoAnimation(Sprite sprite)
	{ spriteSetAnimation(sprite, NULL); }

void spriteSetShapeColor(Sprite sprite, unsigned int colorCode)
	{ heliColorToDouble(colorCode, sprite->shapeColor); }
void spriteSetTintColor(Sprite sprite, unsigned int colorCode)
	{ heliColorToDouble(colorCode, sprite->tintColor); }

void spriteSetVelocityXY(Sprite sprite, double x, double y) {
	sprite->vx = x;
	sprite->vy = y;
	if (sprite->rotateToDirection)
		sprite->rotation = spriteGetDirection(sprite);
}

void spriteSetSpeedAndDirection(Sprite sprite, double speed, double angle) {
	double a = angle*(PI/180);
	spriteSetVelocityXY(sprite, cos(a)*speed, sin(a)*speed);
}

double spriteGetSpeed(Sprite sprite)
	{ return sqrt(sprite->vx*sprite->vx + sprite->vy*sprite->vy); }
double spriteGetDirection(Sprite sprite) {
	return fabs(sprite->vx) > HELI_PRECISION || fabs(sprite->vy) > HELI_PRECISION
	     ? atan2(sprite->vy, sprite->vx)*(180/PI) : 0;
}
void spritePointTo(Sprite sprite, double x, double y)
	{ spriteSetRotation( sprite, atan2(y - sprite->y, x - sprite->x)*(180/PI) ); }

double spriteGetScaledWidth(Sprite sprite)
	{ return sprite->width * sprite->scale; }
double spriteGetScaledHeight(Sprite sprite)
	{ return sprite->height * sprite->scale; }



int worldGetSpriteCount()
	{ return sprites.count; }

Sprite worldGetSprite(int i)
	{ return (Sprite)heliArrayGetValue(&sprites, i); }


static void drawSpriteDebug(Sprite s) {
	saveState();
	translate(s->x, s->y);
	rotate(s->rotation);
	
	strokeWidth(0.5);
	
	double hw = 0.5 * s->scale * s->width;
	double hh = 0.5 * s->scale * s->height;
	
	noFill();
	stroke(colorByRGBA(0, 0, 0, 0.75));

	// frame
	rect(-hw, -hh, 2*hw, 2*hh);
	
	// center cross
	line(-hw/4, 0, hw/4, 0);
	line(0, -hh/4, 0, hh/4);
	
	// depth
	char buf[1024];
	snprintf(buf, sizeof(buf)-1, "%lf", s->depth);
	double s1 = hw*0.25;
	double s2 = hh*0.5;
	double ss = s1 < s2 ? s1 : s2;
	textSize(ss);
	textAlign(HALIGN_LEFT, VALIGN_BOTTOM);
	text(buf, 0.1*ss - hw, hh - 0.1*ss);
	
	restoreState();
}

static void drawSprite(Sprite s, double aaBorder) {
	double w = 0.5*s->scale*s->width;
	double h = 0.5*s->scale*s->height;
	if (w < HELI_PRECISION || h < HELI_PRECISION) return;
	w *= s->mirrorX;
	h *= s->mirrorY;
	
	unsigned int texid = s->animation ? animationGetGLTexId(s->animation) : 0u;
	double color[4] = {
		(texid ? 1.0 : s->shapeColor[0])*s->tintColor[0],
		(texid ? 1.0 : s->shapeColor[1])*s->tintColor[1],
		(texid ? 1.0 : s->shapeColor[2])*s->tintColor[2],
		(texid ? 1.0 : s->shapeColor[3])*s->tintColor[3] };
	if (color[3] < HELI_PRECISION) return;

	saveState();
	translate(s->x, s->y);
	rotate(s->rotation);
	noStroke();
	fill(colorByRGBA(color[0], color[1], color[2], color[3]));
	fillTexture(s->animation, -w, -h, 2*w, 2*h, FALSE);
	rect(-w, -h, 2*w, 2*h);
	restoreState();
}

void drawSprites() {
	// sort
	int found = TRUE;
	while(found) {
		found = FALSE;
		// forward
		for(int i = 1; i < spritesSorted.count; ++i) {
			if ( ((Sprite)spritesSorted.items[i-1].value)->depth
			   < ((Sprite)spritesSorted.items[i  ].value)->depth )
			{
				void *x = spritesSorted.items[i].value;
				spritesSorted.items[i].value = spritesSorted.items[i-1].value;
				spritesSorted.items[i-1].value = x;
				found = TRUE;
			}
		}
		if (!found) break;
		// backward
		found = FALSE;
		for(int i = spritesSorted.count - 1; i > 0; --i) {
			if ( ((Sprite)spritesSorted.items[i-1].value)->depth
			   < ((Sprite)spritesSorted.items[i  ].value)->depth )
			{
				void *x = spritesSorted.items[i].value;
				spritesSorted.items[i].value = spritesSorted.items[i-1].value;
				spritesSorted.items[i-1].value = x;
				found = TRUE;
			}
		}
	}
	
	// draw
	const double aaBorder = heliGLGetAAResolution();
	for(int i = 0; i < spritesSorted.count; ++i) {
		Sprite s = (Sprite)(spritesSorted.items[i].value);
		if (s->visible) drawSprite(s, aaBorder);
	}
	
	// draw debug marks
	for(int i = 0; i < spritesSorted.count; ++i) {
		Sprite s = (Sprite)(spritesSorted.items[i].value);
		if (s->debug) drawSpriteDebug(s);
	}
}

int mouseIsOver(Sprite sprite) {
	HeliCollider collider;
	prepareCollider(sprite, &collider);
	return heliPointCollision(&collider, mouseX(), mouseY());
}

void heliSpriteUpdate(double dt) {
	// auto-remove
	for(int i = sprites.count - 1; i > 0; --i) {
		Sprite s = (Sprite)sprites.items[i].value;
		if (s->lifeTime >= -HELI_PRECISION) {
			s->lifeTime -= dt;
			if (s->lifeTime <= HELI_PRECISION)
				spriteDestroy(s);
		}
	}
	
	// update
	for(int i = 0; i < sprites.count; ++i) {
		Sprite s = (Sprite)sprites.items[i].value;
		s->x += s->vx*dt;
		s->y += s->vy*dt;
		if (!s->rotateToDirection)
			s->rotation += s->rotationSpeed*dt;
	}
}

HeliArray* heliSpriteGetGroups(Sprite sprite)
	{ return &sprite->groups; }

void heliSpriteFinish() {
	while(worldGetSpriteCount())
		{ spriteDestroy(worldGetSprite(0)); }
	heliArrayDestroy(&sprites);
	heliArrayDestroy(&spritesSorted);
}