Here is the code I am using for my sprites
header:
#define ECOIII_ID (113)
#define ECOIII_SPEED (.100000000)
typedef struct ECOIII_
{
WORD id; // Enemy id, usually same as image id
WORD x; // x coord of enemy
WORD y; // y coord of enemy
float speed; // velocity if we are using dbMoveSprite
WORD Player_To_Follow; // Player Id to follow
}ECOIII;
void InitializeECOIII(ECOIII *ccc, WORD Id, WORD x, WORD y, float vel) // Initializes the starting points for the enemy
{
ccc->id = Id;
ccc->x = x;
ccc->y = y;
ccc->speed = vel;
ccc->Player_To_Follow = NONE;
dbSprite(ccc->id,ccc->x,ccc->y,ccc->id);
// Need to offset the sprite so that we can rotate at its center.
dbOffsetSprite(ccc->id,(dbSpriteWidth(ccc->id) / 2), (dbSpriteHeight(ccc->id) / 2));
}
void UpdateECOIIIPosition(ECOIII *ccc)
{
ccc->x = dbSpriteX(ccc->id);
ccc->y = dbSpriteY(ccc->id);
}
void FollowPlayer(ECOIII *ccc, WORD Player) // Sets the Pid of that player to follow
{
ccc->Player_To_Follow = Player;
}
void FreeECOIIIMemory(ECOIII *ccc)
{
dbDeleteSprite(ccc->id); // this deletes the image we allocated
dbDeleteSprite(PLAYER_ID); // We need to delete the player id sprite to free the memory
free(ccc); // this deletes the memory we allocated
}
// So there are a few ways to track a player //
// you can use the traditional way with dbSprite //
// Or you can use dbMoveSprite for better looking movement //
void MoveECOIII(ECOIII *ccc)
{
if(dbSpriteX(ccc->Player_To_Follow) > ccc->x)// so player 1 x coord is greater than the enemy's coord
{
// now we need to increase the x coord of the enemy to get to the player
// remember that we need to rotate the sprite in order to go right and left
ccc->x++;
dbRotateSprite(ccc->id, RIGHT_ANGLE);
dbSprite(ccc->id,ccc->x,ccc->y,ECOIII_ID);
dbRotateSprite(ccc->id, RESET_ANGLE);
}
if(dbSpriteX(ccc->Player_To_Follow) < ccc->x)// Player 1 coord is less than the enemy so we need to move the other way now
{
ccc->x--;
dbRotateSprite(ccc->id, LEFT_ANGLE);
dbSprite(ccc->id,ccc->x,ccc->y,ECOIII_ID);
dbRotateSprite(ccc->id, RESET_ANGLE);
}
if(dbSpriteY(ccc->Player_To_Follow) > ccc->y)
{
ccc->y++;
dbSprite(ccc->id, ccc->x, ccc->y, ECOIII_ID);
}
if(dbSpriteY(ccc->Player_To_Follow) < ccc->y)
{
ccc->y--;
dbSprite(ccc->id, ccc->x, ccc->y, ECOIII_ID);
}
}
// So in this function we will get the angle of the player for a more fluid movement
void MoveECOIIIWithAngle(ECOIII *ccc)
{
int PlayerX = dbSpriteX(ccc->Player_To_Follow);
int PlayerY = dbSpriteY(ccc->Player_To_Follow);
float Angle = dbWrapValue(dbAtanFull(PlayerX - ccc->x, ccc->y - PlayerY));
dbRotateSprite(ccc->id, Angle);
if(dbSpriteCollision(ccc->id, ccc->Player_To_Follow) == 1)
{
// if the enemy sprite collides with the player sprite we need to stop it
dbMoveSprite(ccc->id, 0);
}
else
{
// if the enemy isn't colliding with any sprites let's move!
dbMoveSprite(ccc->id, ccc->speed);
}
}
Main:
void DarkGDK ( void )
{
ECOIII *ccc = (ECOIII*) malloc(sizeof(ECOIII));
dbLoadImage("ECOIII.bmp", ECOIII_ID);
InitializeECOIII(ccc,ECOIII_ID,150,20,ECOIII_SPEED);
FollowPlayer (ccc,FBASEI_ID);
while ( LoopGDK( ) )
{
UpdateECOIIIPosition(ccc);// Updates enemy x and y coord
MoveECOIIIWithAngle(ccc); // Moves the enemy to the player
FreeECOIIIMemory(ccc);
Here is the code I am using for firing projectiles... I am sure there is a better way to do this but this is the first way I have found that is working for me....The projectiles are set up in the same manner as the unit sprites....
Main:
void DarkGDK ( void )
{
int kkkkk=4,DD = 5000,HHH =113;
int GGG = 5;
int q1 = 8;
int coll1 = 0;
while ( LoopGDK( ) )
{
{
if(GGG == 5)
{
dbSprite(DD, dbSpriteX(HHH),dbSpriteY(HHH), DD);
GGG = 0;
}
}
if(GGG < 5)
{
dbLoadImage("EP.bmp", 5000);
dbMoveSprite(DD, 20);//then make it move
GGG++;
}
if (dbSpriteCollision(EP1_ID,FCOI_ID))
{
q1 += -1;
dbLoadImage("EP.bmp", 449);
InitializeEP1(ep,EP1_ID,dbSpriteX(HHH),dbSpriteY(HHH),EP1_SPEED);
FollowPlayer (ep,FCOI_ID);
}
if(dbImageExist(FCOI_ID)) {
coll1 = dbSpriteCollision(EP1_ID,FCOI_ID);
if(q1==0){
dbDeleteSprite(FCOI_ID);
}
}
My code for the game is getting very lengthy and will be even more so... Eventually I will post it all .... or just upload the game... sprites and all...... Have a long ways to go.....