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 ax;
	double ay;
	double rotationSpeed;
	
	HeliCollider collider;
	double colliderSensitiveDistance;
	int massLevel;
	
	double wx;
	double wy;
	double friction;
	double airFriction;
	double touchFriction;
	
	int rotateToDirection;
	int mirrorX;
	int mirrorY;
	double depth;
	double lifeTime;
	int visible;
	int debug;
	
	double shapeColor[4];
	double tintColor[4];
	
	HeliArray groups;
	
	Animation animation;
	
	int userTag;
	char *userText;
	void *userData;
	SpriteCallback destroyCallback;
};

static HeliArray sprites;
static HeliArray spritesSorted;


static void prepareCollider(Sprite sprite, HeliCollider *collider) {
	memcpy(collider, &sprite->collider, sizeof(*collider));
	collider->x += sprite->x;
	collider->y += sprite->y;
	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;
	}
}


static void autoRotate(Sprite sprite) {
	if (sprite->rotateToDirection)
		sprite->rotation = spriteGetDirection(sprite);
}


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->colliderSensitiveDistance = 0.001;
	s->collider.bounciness = 1;
	s->friction = 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;
	
	s->userText = heliStringCopy("");
	
	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) {
	if (sprite->destroyCallback) sprite->destroyCallback(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->userText);
	free(sprite);
}

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

Sprite spriteClone(Sprite sprite) {
	if (!heliInitialized) return NULL;
	Sprite s = calloc(1, sizeof(*s));
	memcpy(s, sprite, sizeof(*s));
	s->lifeTime = -1;
	memset(&s->groups, 0, sizeof(s->groups));
	s->userText = heliStringCopy(sprite->userText);
	return s;
}


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; }
void spriteSetXY(Sprite sprite, double x, double y)
	{ spriteSetX(sprite, x); spriteSetY(sprite, 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); }
void spriteSetVelocityXY(Sprite sprite, double x, double y) {
	spriteResetTouch(sprite);
	sprite->vx = x; sprite->vy = y;
	autoRotate(sprite);
}

double spriteGetAccelerationX(Sprite sprite) { return sprite->ax; }
void spriteSetAccelerationX(Sprite sprite, double x) { sprite->ax = x; }
double spriteGetAccelerationY(Sprite sprite) { return sprite->ay; }
void spriteSetAccelerationY(Sprite sprite, double y) { sprite->ay = y; }
void spriteSetAccelerationXY(Sprite sprite, double x, double y)
	{ spriteSetAccelerationX(sprite, x); spriteSetAccelerationY(sprite, 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 spriteGetDebug(Sprite sprite) { return sprite->debug; }
void spriteSetDebug(Sprite sprite, int debug)
	{ sprite->debug = debug != FALSE; }


void heliSpriteCollisionApply(Sprite a, Sprite b, HeliCollisionInfo *info) {
	if ( a->massLevel > b->massLevel) {
		b->x -= info->dx;
		b->y -= info->dy;
		b->vx = a->vx - info->vx;
		b->vy = a->vy - info->vy;
		autoRotate(b);
		if (b->wx*b->wx + b->wy*b->wy < info->ax*info->ax + info->ay*info->ay) {
			b->wx = info->ax;
			b->wy = info->ay;
			b->touchFriction = a->friction;
		}
	} else
	if ( b->massLevel > a->massLevel ) {
		a->x += info->dx;
		a->y += info->dy;
		a->vx = b->vx + info->vx;
		a->vy = b->vy + info->vy;
		autoRotate(a);
		if (a->wx*a->wx + a->wy*a->wy < info->ax*info->ax + info->ay*info->ay) {
			a->wx = -info->ax;
			a->wy = -info->ay;
			a->touchFriction = b->friction;
		}
	} else {
		double vxAvg = 0.5*(a->vx + b->vx);
		double vyAvg = 0.5*(a->vy + b->vy);
		double wx = info->ax;
		double wy = info->ay;
		double w = wx*wx + wy*wy;
		
		a->x += 0.5*info->dx;
		a->y += 0.5*info->dy;
		a->vx = vxAvg + 0.5*info->vx;
		a->vy = vyAvg + 0.5*info->vy;
		autoRotate(a);
		if (a->wx*a->wx + a->wy*a->wy < w) {
			a->wx = -wx;
			a->wy = -wy;
			a->touchFriction = b->friction;
		}
		
		b->x -= 0.5*info->dx;
		b->y -= 0.5*info->dy;
		b->vx = vxAvg - 0.5*info->vx;
		b->vy = vyAvg - 0.5*info->vy;
		autoRotate(b);
		if (b->wx*b->wx + b->wy*b->wy < w) {
			b->wx = wx;
			b->wy = wy;
			b->touchFriction = a->friction;
		}
	}
}

int heliSpriteCollisionCheck(Sprite a, Sprite b, HeliCollisionInfo *info) {
	if (a == b) return FALSE;
	HeliCollider ca = {}, cb = {};
	prepareCollider(a, &ca);
	prepareCollider(b, &cb);
	memset(info, 0, sizeof(*info));
	info->vx = a->vx - b->vx;
	info->vy = a->vy - b->vy;
	info->ax = a->ax - b->ax;
	info->ay = a->ay - b->ay;
	return heliCheckCollision(&ca, &cb, info, a->colliderSensitiveDistance + b->colliderSensitiveDistance);
}

int spriteOverlap(Sprite a, Sprite b) {
	HeliCollisionInfo info = {};
	heliSpriteCollisionCheck(a, b, &info);
	return info.actualCollision;
}

int spriteCollide(Sprite a, Sprite b) {
	HeliCollisionInfo info = {};
	if (heliSpriteCollisionCheck(a, b, &info)) {
		heliSpriteCollisionApply(a, b, &info);
		return info.actualCollision;
	}
	return FALSE;
}


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

double spriteGetFriction(Sprite sprite)
	{ return sprite->friction; }
void spriteSetFriction(Sprite sprite, double friction)
	{ sprite->friction = friction > 0 ? friction : 0; }
double spriteGetAirFriction(Sprite sprite)
	{ return sprite->airFriction; }
void spriteSetAirFriction(Sprite sprite, double friction)
	{ sprite->airFriction = friction > 0 ? friction : 0; }

int spriteGetMassLevel(Sprite sprite)
	{ return sprite->massLevel; }
void spriteSetMassLevel(Sprite sprite, int massLevel)
	{ sprite->massLevel = massLevel; }

double spriteGetTouchWeight(Sprite sprite)
	{ return sqrt(sprite->wx*sprite->wx + sprite->wy*sprite->wy); }
double spriteGetTouchWeightX(Sprite sprite)
	{ return sprite->wx; }
double spriteGetTouchWeightY(Sprite sprite)
	{ return sprite->wy; }
double spriteGetTouchFriction(Sprite sprite)
	{ return sprite->touchFriction; }
void spriteResetTouch(Sprite sprite) {
	sprite->wx = 0;
	sprite->wy = 0;
	sprite->touchFriction = 0;
}

double spriteGetColliderSensitiveDistance(Sprite sprite)
	{ return sprite->colliderSensitiveDistance; }
void spriteSetColliderSensitiveDistance(Sprite sprite, double distance)
	{ sprite->colliderSensitiveDistance = distance > 10*HELI_PRECISION ? distance : 10*HELI_PRECISION; }

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;
	}
}

int spriteIsPointInside(Sprite sprite, double x, double y) {
	HeliCollider collider;
	prepareCollider(sprite, &collider);
	return heliPointCollision(&collider, x, y);
}

Animation spriteGetAnimation(Sprite sprite)
	{ return sprite->animation; }

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 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 spriteGetUserTag(Sprite sprite)
	{ return sprite->userTag; }
void spriteSetUserTag(Sprite sprite, int tag)
	{ sprite->userTag = tag; }
const char* spriteGetUserText(Sprite sprite)
	{ return sprite->userText; }
void spriteSetUserText(Sprite sprite, const char *text) {
	if (sprite->userText == text) return;
	free(sprite->userText);
	sprite->userText = heliStringCopy(text);
}
void* spriteGetUserData(Sprite sprite)
	{ return sprite->userData; }
void spriteSetUserData(Sprite sprite, void *data)
	{ sprite->userData = data; }

void spriteSetDestroy(Sprite sprite, SpriteCallback destroy)
	{ sprite->destroyCallback = destroy; }


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 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]));
	rectTextured(s->animation, -w, -h, 2*w, 2*h);
	restoreState();
}

void heliSpriteSort(HeliArray *sprites) {
	int found = TRUE;
	while(found) {
		found = FALSE;
		// forward
		for(int i = 1; i < sprites->count; ++i) {
			if ( ((Sprite)sprites->items[i-1].value)->depth
			   < ((Sprite)sprites->items[i  ].value)->depth )
			{
				void *x = sprites->items[i].value;
				sprites->items[i].value = sprites->items[i-1].value;
				sprites->items[i-1].value = x;
				found = TRUE;
			}
		}
		if (!found) break;
		// backward
		found = FALSE;
		for(int i = sprites->count - 1; i > 0; --i) {
			if ( ((Sprite)sprites->items[i-1].value)->depth
			   < ((Sprite)sprites->items[i  ].value)->depth )
			{
				void *x = sprites->items[i].value;
				sprites->items[i].value = sprites->items[i-1].value;
				sprites->items[i-1].value = x;
				found = TRUE;
			}
		}
	}
}

void drawSprites() {
	// sort
	heliSpriteSort(&spritesSorted);
	
	// draw
	for(int i = 0; i < spritesSorted.count; ++i) {
		Sprite s = (Sprite)(spritesSorted.items[i].value);
		if (s->visible) drawSprite(s);
	}
	
	// draw debug marks
	for(int i = 0; i < spritesSorted.count; ++i) {
		Sprite s = (Sprite)(spritesSorted.items[i].value);
		if (s->debug) drawSpriteDebug(s);
	}
}

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;
		double vx = s->vx + s->ax*dt;
		double vy = s->vy + s->ay*dt;
		
		double weight = spriteGetTouchWeight(s);
		double dvf = fabs(weight*s->friction*s->touchFriction)*dt;
		if (dvf > HELI_PRECISION) {
			double nx = s->wx/weight;
			double ny = s->wy/weight;
			double vb = nx*vx + ny*vy;
			double vs = ny*vx - nx*vy;
			if (vs >  dvf + HELI_PRECISION) vs -= dvf; else
			if (vs < -dvf - HELI_PRECISION) vs += dvf; else vs = 0;
			vx = nx*vb + ny*vs;
			vy = ny*vb - nx*vs;
		}
		
		double dvaf = fabs(s->airFriction)*dt;
		if (dvaf > HELI_PRECISION) {
			double v = sqrt(vx*vx + vy*vy), vn = v;
			if (v >  dvaf + HELI_PRECISION) vn -= dvaf; else
			if (v < -dvaf - HELI_PRECISION) vn += dvaf; else vn = 0;
			double k = vn > HELI_PRECISION ? vn/v : 0;
			vx *= k;
			vy *= k;
		}
		
		s->vx = vx;
		s->vy = vy;
		autoRotate(s);
		spriteResetTouch(s);
		
		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);
}