#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 { struct mobj_s* prev; struct mobj_s* next; 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(player_t player); void MovePlayer(player_t player); void DebugInfo(player_t player); void MoveMobj(mobj_t mobj); void ShootBullet(player_t player); void InitPlayer(player_t player); int main(void) { player_t player; PA_Init(); PA_InitVBL(); PA_InitText(1,0); // On the top screen InitPlayer(player); // 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(player); MovePlayer(player); DebugInfo(player); for(i = 0; i < MAXMOBJS; i++) if(mobjlist[i].alive) MoveMobj(mobjlist[i]); PA_WaitForVBL(); } return 0; } void InitPlayer(player_t player) { 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(player_t player) { if(Pad.Newpress.A) ShootBullet(player); } void MovePlayer(player_t player) { 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 && newyy) dist = sqrt((((x-newx)^2)+((newy-y)^2))); else if(newx 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(player_t player) { 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); } void ShootBullet(player_t player) { PA_CreateSprite(0, 0,(void*)bullet_Sprite, OBJ_SIZE_8X8,1, 0, 64-16, 64-16); /* mobj->x = player.mo->x; mobj->y = player.mo->y; mobj->angle = player.mo->angle; mobj->radius = 1; mobj->momx = PA_Cos(mobj->angle); mobj->momy = PA_Sin(mobj->angle); mobj->type = MT_BULLET; */ } void MoveMobj(mobj_t mobj) { mobj.x += PA_Cos(mobj.angle)/-mobj.momx; mobj.y -= PA_Sin(mobj.angle)/-mobj.momy; // 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)-16, (mobj.y>>8)-16); // Sprite position converted to normal... }