Blob Blame Raw

#include "private.h"

#include "sprite.h"
#include "world.h"

struct _Sprite {
	double x;
	double y;
	double rotation;
	double width;
	double height;
	double scale;
	
	double vx;
	double vy;
	double rotationSpeed;
	
	HeliCollider collider;
	int colliderId;
	double bounciness;
	
	int rotateToDirection;
	int mirrorX;
	int mirrorY;
	double depth;
	double lifeTime;
	int debug;
	
	double color[4];
	double tint[4];
	
	int playing;
	int frame;
	
	HeliArray images;
};

static HeliArray sprites;
static HeliArray sortedSprites;


static void prepareCollider(Sprite sprite, HeliCollider *collider) {
	#error
}



int worldGetSpriteCount()
	{ return sprites.count; }

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

void drawSprites() {
	// copy
	for(int i = 0; i < sprites.count; ++i)
		heliArrayAppend(&sortedSprites, sprites.items[i]);
	
	// sort
	int found = TRUE;
	while(found) {
		found = FALSE;
		for(int i = 1; i < sortedSprites.count; ++i) {
			if (((Sprite)sortedSprites.items[i-1])->depth < ((Sprite)sortedSprites.items[i])->depth) {
				void *x = sortedSprites.items[i];
				sortedSprites.items[i] = sortedSprites.items[i-1];
				sortedSprites.items[i-1] = x;
				found = TRUE;
			}
		}
	}
	
	// draw
	for(int i = 1; i < sortedSprites.count; ++i) {
		Sprite s = (Sprite)(sortedSprites.items[i]);
		#error
	}
	
	// clear
	heliArrayClear(sortedSprites, FALSE);
}

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

void heliUpdateSprites(double dt) {
	for(int i = 1; i < sprites.count; ++i) {
		Sprite s = (Sprite)(sprites.items[i]);
		s->x += s->vx*dt;
		s->y += s->vy*dt;
		s->rotation += s->rotationSpeed*dt;
		if (s->playing) {
			++s->frame;
			if (s->frames.count <= 0) {
				s->frame = 0;
			} else
			if (s->frame > s->frames.count) {
				s->frame %= s->frames.count;
			} else
			if (s->frame < 0) {
				s->frame = 0;
			}
		}
	}
}