Skip to content

Commit

Permalink
Improve bullet code
Browse files Browse the repository at this point in the history
  • Loading branch information
bl0ckeduser committed Dec 2, 2011
1 parent f0fd7ef commit f269a4a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 28 deletions.
49 changes: 24 additions & 25 deletions bullet.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ game_obj* newBullet(game_obj* list, float x, float y, float z, float angle)
game_obj* bul = newListNode(list);

bul->type = BULLET;
bul->data = (float *)malloc(7 * sizeof(float));
bul->data = (float *)malloc(9 * sizeof(float));
bul->data[BULLET_X] = x;
bul->data[BULLET_Y] = y;
bul->data[BULLET_Z] = z;
Expand All @@ -15,41 +15,40 @@ game_obj* newBullet(game_obj* list, float x, float y, float z, float angle)
bul->data[BULLET_TIMER] = 45;
bul->data[BULLET_EXPLODED] = 0;

#ifdef PC_TARGET
bul->data[BULLET_XDIR] = (float)sin(bul->data[BULLET_ANGLE] / 360.0 * (2*3.14));
bul->data[BULLET_ZDIR] = (float)cos(bul->data[BULLET_ANGLE] / 360.0 * (2*3.14));
#else
bul->data[BULLET_XDIR] = my_sin(bul->data[BULLET_ANGLE]);
bul->data[BULLET_ZDIR] = my_cos(bul->data[BULLET_ANGLE]);
#endif

/* collision system movement vector */
bul->box.move.x = (float)10*bul->data[BULLET_XDIR];
bul->box.move.y = (float)0;
bul->box.move.z = (float)10*bul->data[BULLET_ZDIR];

return bul;
}

void bulletTick(game_obj* bul)
{
float dirx, dirz, movey = 0;

if(bul->data[BULLET_EXPLODED]==1)
{
++bul->data[BULLET_EXPLODED];
deleteNode(bul);
}

#ifdef PC_TARGET
dirx = (float)sin(bul->data[BULLET_ANGLE] / 360.0 * (2*3.14));
dirz = (float)cos(bul->data[BULLET_ANGLE] / 360.0 * (2*3.14));
#else
dirx = my_sin(bul->data[BULLET_ANGLE]);
dirz = my_cos(bul->data[BULLET_ANGLE]);
#endif

bul->data[BULLET_X] += 20 * dirx * dtime;
bul->data[BULLET_Y] += movey;
bul->data[BULLET_Z] += 20 * dirz * dtime;
bul->data[BULLET_X] += 20 * bul->data[BULLET_XDIR] * dtime;
bul->data[BULLET_Z] += 20 * bul->data[BULLET_ZDIR] * dtime;

/* bullet collision box and movement vector */
bul->box.min.x = (float)(bul->data[BULLET_X] - 5);
bul->box.min.y = (float)(bul->data[BULLET_Y] - 5);
bul->box.min.z = (float)(bul->data[BULLET_Z] - 5);
bul->box.max.x = (float)(bul->data[BULLET_X] + 5);
bul->box.max.y = (float)(bul->data[BULLET_Y] + 5);
bul->box.max.z = (float)(bul->data[BULLET_Z] + 5);
bul->box.move.x = (float)10*dirx;
bul->box.move.y = (float)movey;
bul->box.move.z = (float)10*dirz;
/* bullet collision box */
bul->box.min.x = (float)(bul->data[BULLET_X] - 5);
bul->box.min.y = (float)(bul->data[BULLET_Y] - 5);
bul->box.min.z = (float)(bul->data[BULLET_Z] - 5);
bul->box.max.x = (float)(bul->data[BULLET_X] + 5);
bul->box.max.y = (float)(bul->data[BULLET_Y] + 5);
bul->box.max.z = (float)(bul->data[BULLET_Z] + 5);

if((bul->data[BULLET_TIMER] -= 3*dtime) < 0.0)
if(!bul->data[BULLET_EXPLODED])
Expand All @@ -58,7 +57,7 @@ void bulletTick(game_obj* bul)

void bulletCollide(game_obj* a, game_obj* b)
{
if(a->type==BULLET && b->type == SOLID || (b->type==TARGET && !b->data[TARGET_EXPLODED]))
if(a->type==BULLET || (b->type==TARGET && !b->data[TARGET_EXPLODED]))
if(!a->data[BULLET_EXPLODED])
a->data[BULLET_EXPLODED] = 1;

Expand Down
Binary file modified clown3d-DS.elf
Binary file not shown.
Binary file modified clown3d-DS.nds
Binary file not shown.
4 changes: 3 additions & 1 deletion headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ enum
BULLET_YVEL,
BULLET_ANGLE,
BULLET_TIMER,
BULLET_EXPLODED
BULLET_EXPLODED,
BULLET_XDIR,
BULLET_ZDIR
};

/* TARGET */
Expand Down
7 changes: 7 additions & 0 deletions key.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ void keyTick(game_obj* a)

if(a->rot>360)
a->rot = 0;
} else {
a->box.min.x = /* empty bounding box */
a->box.min.y =
a->box.min.z =
a->box.max.x =
a->box.max.y =
a->box.max.z = 0;
}
}

Expand Down
4 changes: 2 additions & 2 deletions player.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ void playerTick(game_obj* player)
if(shoot && player->data[PLAYER_BULLET_TIMER] <= 0.1f)
{
player->data[PLAYER_BULLET_TIMER] = 25.0;
newBullet(player, player->data[PLAYER_X],
newBullet(player, player->data[PLAYER_X] + player->data[PLAYER_DIRX] * 10,
player->data[PLAYER_Y],
player->data[PLAYER_Z],
player->data[PLAYER_Z] + player->data[PLAYER_DIRZ] * 10,
player->data[PLAYER_ANGLE]);
}

Expand Down

0 comments on commit f269a4a

Please sign in to comment.