All pastes #445641 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-04-18 00:12 UTC
rendered paste body
// Includes
#include <PA9.h>
#include <math.h>

// PAGfxConverter Include
#include "gfx/all_gfx.c"
#include "gfx/all_gfx.h"

#define MAXMOVE 25
#define UNIT 256
// Screen borders
#define BORDERLEFT -31*UNIT 
#define BORDERRIGHT 265*UNIT
#define BORDERTOP -31*UNIT
#define BORDERBOTTOM 200*UNIT
#define MAXMOBJS 256

typedef enum
{
   MT_BULLET,
   MT_LARGEASTEROID,
   MT_MEDIUMASTEROID,
   MT_SMALLASTEROID,
}type_t;

typedef struct mobj_s
{
   s32 x;
   s32 y;
   u16 angle;
   s32 momx;
   s32 momy;
   s32 radius;
   bool alive;
   
   type_t type;
}mobj_t;

typedef struct
{
	int score; // Player' score.
	s32 x;
   s32 y;
   u16 angle;
   s32 momx;
   s32 momy;
   s32 radius;
}player_t;

mobj_t mobjlist[MAXMOBJS];

void PlayerThink(void);
void MovePlayer(void);
void DebugInfo(void);
void MoveMobj(mobj_t* mobj);
void ShootBullet(void);
void InitPlayer(void);
int FindSlot(void);

player_t player;

int main(void)
{
	PA_Init();
	PA_InitVBL();
	
	PA_InitText(1,0); // On the top screen
 	InitPlayer(); // Init the player

	PA_LoadSpritePal(0, 1, (void*)vaisseau_Pal);
	PA_LoadSpritePal(0, 0, (void*)bullet_Pal);

	PA_CreateSprite(0, 1,(void*)vaisseau_Sprite, OBJ_SIZE_32X32,1, 1, 128-16, 96-16); // Create the ship in the center...
	PA_SetSpriteRotEnable(0,1,0);// Enable rotations and use Rotset 0... 
	
	while(1)
	{
	   int i;
	   
	   PlayerThink();
		MovePlayer();	
		DebugInfo();
		
//		for(i = 0; i < MAXMOBJS; i++)
//		  if(mobjlist[i].alive)
//				MoveMobj(&mobjlist[i]);			
			
		PA_WaitForVBL();
	}
	return 0;
}

void InitPlayer(void)
{
	player.x = 128*UNIT; // ship x position in 8bit fixed point
	player.y = 96*UNIT; // Y
	player.momx = 0;	// X momentum
	player.momy = 0;	// Y momentum
	player.angle = 0; // direction in which to move !
	player.radius = 32;
	player.score = 0; // Player' score.   
}
void PlayerThink(void)
{
   if(Pad.Newpress.A)
   	ShootBullet();
}
void MovePlayer(void)
{
   s32 newx = 0, newy = 0, dist = 0;
   s32 x, y, momx, momy;
   u16 moveangle = 0, angle = 0;
   
   x = player.x;
   y = player.y;
   momx = player.momx;
   momy = player.momy;
   angle = player.angle;
   
   if (Stylus.Held) // Move forward
		{ 
			angle = PA_GetAngle(x>>8, y>>8, Stylus.X, Stylus.Y);
			PA_SetRotsetNoZoom(0, 0, angle); // Turn the ship in the correct direction
			
			// Divide it so it's not instant acceleration
			momx += PA_Cos(angle)/40;
			momy -= PA_Sin(angle)/40;
		}

		newx = x + momx;
		newy = y + momy;
		
		if(newx>x && newy>y)
			dist = sqrt((((newx-x)^2)+((newy-y)^2)));
		else if(newx>x && newy<y)
			dist = sqrt((((newx-x)^2)+((y-newy)^2)));
		else if(newx<x && newy>y)
			dist = sqrt((((x-newx)^2)+((newy-y)^2)));
		else if(newx<x && newy<y)
			dist = sqrt((((x-newx)^2)+((y-newy)^2)));
			
		if(dist > MAXMOVE)
		{
		   moveangle = PA_GetAngle(newx>>8, newy>>8, x>>8, y>>8);
		   momx += PA_Cos(moveangle)/40;
		   momy -= PA_Sin(moveangle)/40;
		   x += momx;
		   y += momy;
		   
			dist = MAXMOVE;
		}					
		else
		{
			x = newx;
			y = newy;	
		}		
		
		// Teleport to the other side of the map.		
		if(x > BORDERRIGHT)
			x = BORDERLEFT;
		else if(x < BORDERLEFT)
			x = BORDERRIGHT;
			
		if(y < BORDERTOP)
			y = BORDERBOTTOM;
		else if(y > BORDERBOTTOM)
			y = BORDERTOP;
		
		player.x = x;
		player.y = y;
		player.momx = momx;
		player.momy = momy;
		player.angle = angle;
		
		PA_SetSpriteXY(0, 1, (x>>8)-16, (y>>8)-16); // Sprite position converted to normal...
}
void DebugInfo(void)
{
   	PA_OutputText(1, 5, 10, "Angle : %d  ", player.angle);
		PA_OutputText(1, 5, 11, "momx: %d ", player.momx);
		PA_OutputText(1, 5, 12, "momy: %d ", player.momy);
		PA_OutputText(1, 5, 13, "x: %d ", (player.x>>8)-16);
		PA_OutputText(1, 5, 14, "y: %d ", (player.y>>8)-16);
		
		PA_OutputText(1, 5, 16, "mobj x: %d ", (mobjlist[0].x>>8)-4);
		PA_OutputText(1, 5, 17, "mobj y: %d ", (mobjlist[0].y>>8)-4);
}
void ShootBullet(void)
{
   mobj_t* mobj = &mobjlist[FindSlot()];
   
	mobj->x = player.x;
	mobj->y = player.y;
	mobj->angle = player.angle;
	mobj->radius = 1;
	
	mobj->momx = PA_Cos(mobj->angle);
	mobj->momy = PA_Sin(mobj->angle);
	mobj->type = MT_BULLET;
	
	PA_CreateSprite(0, 0,(void*)bullet_Sprite, OBJ_SIZE_8X8,1, 0, mobj->x-4, mobj->y-4);
}
void MoveMobj(mobj_t* mobj)
{			
	mobj->x += PA_Cos(mobj->angle);
	mobj->y -= PA_Sin(mobj->angle);

	// Teleport to the other side of the map.		
	if(mobj->x > BORDERRIGHT)
		mobj->x = BORDERLEFT;
	else if(mobj->x < BORDERLEFT)
		mobj->x = BORDERRIGHT;
		
	if(mobj->y < BORDERTOP)
		mobj->y = BORDERBOTTOM;
	else if(mobj->y > BORDERBOTTOM)
		mobj->y = BORDERTOP;
	
	PA_SetSpriteXY(0, 0, (mobj->x>>8)-4, (mobj->y>>8)-4); // Sprite position converted to normal...  
}
int FindSlot(void)
{
   int i = 0;
	for(i=0;i<MAXMOBJS;i++)
	{
	   if(mobjlist[i].alive)
	   	continue;
	   else
			break;
	}	
	return i;
} 
void KillMobj(int slot)
{
	memset(&mobjlist[slot], 0, sizeof(mobj_t));	   
}