Jump to content
  • Advertisement
Sign in to follow this  
lordimmortal2

Several Entity Questions

This topic is 2835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

While making a 2D game, just for my increased experience and knowledge, I have run into two problems that I've looked at for a couple days and can't seem to solve. I wanted to just build a fairly simple version of the enemy spawn, move, and despawn code that I could flesh out later - the enemies spawn randomly at 100 spawn points at the top of the screen, move down every frame since spawning, and then despawn when they're off the screen.

The problem with the spawn code is that it only spawns them at 0, 0, despite that it should be placing them at a random spawn point out of 100.

The problem with the despawn code is that it will despawn an enemy if there is only one enemy on the screen, but if there is more than 1, it will only despawn half of them.

And my final question is: I'm sure there are WAY less error-prone ways of dealing with entity counts, entity position, and definitely entity despawning. Is there anyway to deal with these things in a fairly non-advanced way?

The available spots for entities is just a data struct array with 256 slots. Each entity has a property called tag, that if it is -1, it isn't in play. Perhaps a better way of handling it would be to use dynamic memory?

206lr86.jpg

i2nl1x.jpg

When spawning just 5 enemies with spacing, as soon as the first enemy gets to the bottom of the screen, the last one in line stops moving and stops being rendered, yet keeps its tag.

Code
void InitGame(sEntity entity[])
{
srand(GetTickCount());

for(int loop = 0; loop < 256; loop++)
entity[loop].tag = -1;

entity[0].h = 32;
entity[0].w = 32;

pause = 0;

SpawnEntity(entity, NULL);
}

void Logic(sEntity entity[])
{
// Limit the framerate
static int time, start_time = GetTickCount();
time = GetTickCount() - start_time;
start_time = GetTickCount();

for(int ms = 0; ms < time; ms++)
{
// Check all input
if(InputData->LeftArrow && InputData->RightArrow)
{
// Do nothing, both held down
}
else if(InputData->LeftArrow)
{
// Move left
entity[0].x--;
}
else if(InputData->RightArrow)
{
// Move right
entity[0].x++;
}

if(InputData->UpArrow && InputData->DownArrow)
{
// Do nothing, both held down
}
else if(InputData->UpArrow)
{
// Move up
entity[0].y--;
}
else if(InputData->DownArrow)
{
// Move down
entity[0].y++;
}

if(InputData->Escape)
PostQuitMessage(0);

// Check for collision with the walls of the screen
if(entity[0].x < 0)
entity[0].x = 0;
else if(entity[0].x - entity[0].w > gwx->Width)
entity[0].x = gwx->Width - entity[0].w;

if(entity[0].y < 0)
entity[0].y = 0;
else if(entity[0].y - entity[0].h > gwx->Height)
entity[0].y = gwx->Height - entity[0].h;

/*
if(InputData->Enter && InputDataLastFrm ->Enter)
{
// Do nothing
}
*/
//else
if(InputData->Enter)
{
SpawnEntity(entity, MOB_NORMAL);
InputDataLastFrm->Enter = true;
}
else
InputDataLastFrm->Enter = false;

for(int loop = 1; loop <= entityCount; loop++)
{
//CheckEntityCollision(entity, loop, NULL, NULL);
MoveEntity(entity, loop);
}
}
}

void SpawnEntity(sEntity entity[], int type)
{
int random = rand() % 100 + 1;

// Make sure there is room in the array
if(entityCount < 256)
{
// Check type of mob
if(type == MOB_NORMAL)
{
for(int loop = 1; loop < 256; loop++)
{
if(entity[loop].tag == -1)
{
// Spawn position
entity[loop].x = ((random/100) * gwx->Width);
entity[loop].y = 0;

entity[loop].tag = loop;
break;
}
}
}

if(type == PLAYER)
{
entity[0].x = 50;
entity[0].y = 50;

entity[0].tag = 0;
}

CountEntities(entity);
}
}

void MoveEntity(sEntity entity[], int number)
{
if(entity[number].tag > -1)
entity[number].y++;

if(entity[number].y > gwx->Width)
{
entity[number].x = 0;
entity[number].y = 0;

entity[number].tag = -1;
}

CountEntities(entity);
}

void CountEntities(sEntity entity[])
{
int count = 0;

for(int loop = 0; loop < 256; loop++)
{
if(entity[loop].tag > -1)
count++;
}

entityCount = count;
}


I hope this made sense and as always all replies and help are greatly appreciated =).

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!