#include <nds.h>
#include <nds/registers_alt.h>
#include "sprite100.h"


#define TEX_FILEID 1928-0x83


extern void s100_something(void* actor);

int s100_collisionCB(ActivePhysics* a, ActivePhysics* b)
{
	//s100_something(a->owner);
	return 1;
}

u32 s100physics[] = 
{
	0x8000,		// X offset
	0x10000,	// Y offset
	0x8000,		// Width
	0x10000,	// Height
	//0xD30004,	// behavior?
	0x00D30004,
	0xFFFE,		// flags
	//0x02098C78	// player/sprite collision callback
	(u32)&s100_collisionCB
};

// behavior:
// bit2 = react to non-player collision (enemies, block hit from below)
// bit16 = react to player collision
// bit17 = ???
// bit20 = die when hit from below
// bit21 = 
// bit22 = ???
// bit23 = react to fireballs

// flags:
// bit1 = die when receiving fireball

void s100_loadtex(Sprite100* s100)
{
	u32 thefile = loadFileByExtId_3dModel(TEX_FILEID, 0); // fileID-overlaycount
	u32 thetex = NNS_G3dGetTex(thefile);
	
	getTextureParams(thetex, 0, &s100->mytex.texparam);
	getTexturePalBase(thetex, 0, &s100->mytex.palbase);
	
	s100->jumpheight = 0x40000;
	//s100->velocity.x = 0x2000;
	
	initActivePhysics(&s100->activephysics, s100, s100physics, 0);
	registerCollider(&s100->activephysics);
}

void s100_unloadtex(Sprite100* s100)
{
	unloadFileByExtId_cached(TEX_FILEID);
}


void s100_update_state0(Sprite100* s100)
{
	return;
	if (s100->jumpheight >= 0x40000)
	{
		if (checkCollisionUnder(s100->position.x + 0x8000, s100->position.y))
		{
			//s100->state = 1;
			s100->jumpheight = 0;
			s100->velocity.y = 0x8000;
		}
		else
		{
			s100->position.y -= s100->velocity.y;
			
			s100->velocity.y += 0x800;
			if (s100->velocity.y > 0x3000) s100->velocity.y = 0x3000;
		}
	}
	else
	{
		s100->position.y += s100->velocity.y;
		s100->jumpheight += s100->velocity.y;
		s100->velocity.y -= 0x800;
	}
}

void s100_update_state1(Sprite100* s100)
{
	// state 1: bouncing
	/*s100->position.y += 0x2000;
	s100->jumpheight += 0x2000;
	
	if (s100->jumpheight >= 0x40000)
	{
		s100->state = 0;
	}*/
	enemyActor_executeState1(s100);
}


inline void vec3(int x, int y, int z)
{
	REG_VERTEX16 = (y << 16) | (x & 0xFFFF);
	REG_VERTEX16 = (z & 0xFFFF);
}

inline void texcoord2(int x, int y)
{
	REG_TEXT_COORD = (y << 16) | (x & 0xFFFF);
}

void s100_draw(Sprite100* s100)
{
	G3_LoadMtx43(0x02085B20);
	
	// this+0x5C -> sprite pos (vec3)
	REG_MTX_TRANSLATE = s100->position.x;
	REG_MTX_TRANSLATE = s100->position.y;
	REG_MTX_TRANSLATE = s100->position.z;
	
	REG_MTX_SCALE = 0x10000;
	REG_MTX_SCALE = 0x10000;
	REG_MTX_SCALE = 0x10000;
	
	REG_MTX_CONTROL = 3;
	REG_MTX_IDENTITY = 0;
	REG_MTX_CONTROL = 2;
	
	REG_POLY_FORMAT = 0x001F3880;
	REG_TEXT_FORMAT = s100->mytex.texparam;
	(*(vu32*) 0x40004AC) = s100->mytex.palbase;
	
	/*if (s100->position.x & 0x10000)
		REG_COLOR = 0x7FE0;
	else
		REG_COLOR = 0x7C1F;*/
	REG_COLOR = 0x7FFF;
	
	REG_GFX_BEGIN = 1;
	
	texcoord2(0x100, 0);
	vec3(0x1000, 	0x2000, 	0);
	
	texcoord2(0, 0);
	vec3(0, 		0x2000, 	0);
	
	texcoord2(0, 0x100);
	vec3(0, 		0,			0);
	
	texcoord2(0x100, 0x100);
	vec3(0x1000,	0,			0);
	
	REG_GFX_END = 0;
}
