Converted the three "collision" functions to four differently-named ones.

The fourth is simply a duplicate of ship_collision for bullets. A
bit redundant, but I figure it's clearer of a definition. Besides,
this opens up the door to possibly making bullets a different struct
type in the future, if that turns out to be desirable.
This commit is contained in:
onpon4 2015-09-25 16:28:09 -04:00
parent a07b2bc576
commit 0f10c6d5c7
14 changed files with 105 additions and 86 deletions

View File

@ -1133,9 +1133,7 @@ bool alien_place(object *alien)
{ {
if ((aliens[i].owner != alien) && (aliens[i].shield > 0)) if ((aliens[i].owner != alien) && (aliens[i].shield > 0))
{ {
if (collision(alien->x, alien->y, alien->image[0]->w, if (ship_collision(alien, &aliens[i]))
alien->image[0]->h, aliens[i].x, aliens[i].y,
aliens[i].image[0]->w, aliens[i].image[0]->h))
return false; return false;
} }
} }
@ -1485,7 +1483,7 @@ void alien_move(object *alien)
continue; continue;
} }
if (collision(alien, &aliens[i])) if (ship_collision(alien, &aliens[i]))
{ {
if ((aliens[i].classDef == CD_BARRIER) && if ((aliens[i].classDef == CD_BARRIER) &&
(aliens[i].owner != alien)) (aliens[i].owner != alien))
@ -1503,7 +1501,7 @@ void alien_move(object *alien)
// Handle a collision with the player // Handle a collision with the player
if ((player.shield > 0) && (alien->shield > 0) && (checkCollisions)) if ((player.shield > 0) && (alien->shield > 0) && (checkCollisions))
{ {
if (collision(alien, &player)) if (ship_collision(alien, &player))
{ {
if (alien->classDef == CD_ASTEROID) if (alien->classDef == CD_ASTEROID)
{ {

View File

@ -181,3 +181,24 @@ object *bullet_getTarget(object *bullet)
return &aliens[i]; return &aliens[i];
} }
bool bullet_collision(object *bullet, object *ship)
{
float x0 = bullet->x;
float y0 = bullet->y;
float w0 = bullet->image[0]->w;
float h0 = bullet->image[0]->h;
float x2 = ship->x;
float y2 = ship->y;
float w1 = ship->image[0]->w;
float h1 = ship->image[0]->h;
float x1 = x0 + w0;
float y1 = y0 + h0;
float x3 = x2 + w1;
float y3 = y2 + h1;
return !(x1<x2 || x3<x0 || y1<y2 || y3<y0);
}

View File

@ -22,5 +22,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
void bullet_add(object *theWeapon, object *attacker, int y, int dy); void bullet_add(object *theWeapon, object *attacker, int y, int dy);
object *bullet_getTarget(object *bullet); object *bullet_getTarget(object *bullet);
bool bullet_collision(object *bullet, object *ship);
#endif #endif

View File

@ -206,6 +206,27 @@ void collectable_add(float x, float y, int type, int value, int life)
engine.collectableTail = collectable; engine.collectableTail = collectable;
} }
bool collectable_collision(collectables *collectable, object *ship)
{
float x0 = collectable->x;
float y0 = collectable->y;
float w0 = collectable->image->w;
float h0 = collectable->image->h;
float x2 = ship->x;
float y2 = ship->y;
float w1 = ship->image[0]->w;
float h1 = ship->image[0]->h;
float x1 = x0 + w0;
float y1 = y0 + h0;
float x3 = x2 + w1;
float y3 = y2 + h1;
return !(x1<x2 || x3<x0 || y1<y2 || y3<y0);
}
void collectable_explode(collectables *collectable) void collectable_explode(collectables *collectable)
{ {
if ((collectable->x >= 0) && (collectable->x <= screen->w) && if ((collectable->x >= 0) && (collectable->x <= screen->w) &&

View File

@ -21,6 +21,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define COLLECTABLE_H #define COLLECTABLE_H
void collectable_add(float x, float y, int type, int value, int life); void collectable_add(float x, float y, int type, int value, int life);
bool collectable_collision(collectables *collectable, object *ship);
void collectable_explode(collectables *collectable); void collectable_explode(collectables *collectable);
#endif #endif

View File

@ -253,7 +253,7 @@ static void game_doCollectables()
collectable->life--; collectable->life--;
if ((player.shield > 0) && (collision(collectable, &player))) if ((player.shield > 0) && (collectable_collision(collectable, &player)))
{ {
switch(collectable->type) switch(collectable->type)
{ {
@ -597,7 +597,7 @@ static void game_doBullets()
if (okayToHit) if (okayToHit)
{ {
if ((bullet->active) && (collision(bullet, &aliens[i]))) if ((bullet->active) && (bullet_collision(bullet, &aliens[i])))
{ {
old_shield = aliens[i].shield; old_shield = aliens[i].shield;
@ -657,7 +657,7 @@ static void game_doBullets()
(bullet->id == WT_LASER) || (bullet->id == WT_CHARGER)) (bullet->id == WT_LASER) || (bullet->id == WT_CHARGER))
{ {
if (bullet->active && (player.shield > 0) && if (bullet->active && (player.shield > 0) &&
(bullet->owner != &player) && collision(bullet, &player)) (bullet->owner != &player) && bullet_collision(bullet, &player))
{ {
old_shield = player.shield; old_shield = player.shield;
@ -712,7 +712,7 @@ static void game_doBullets()
{ {
if (cargo[j].active) if (cargo[j].active)
{ {
if (collision(bullet, &cargo[j])) if (bullet_collision(bullet, &cargo[j]))
{ {
bullet->active = false; bullet->active = false;
explosion_add(bullet->x, bullet->y, E_SMALL_EXPLOSION); explosion_add(bullet->x, bullet->y, E_SMALL_EXPLOSION);
@ -743,7 +743,7 @@ static void game_doBullets()
if (collectable->type == P_MINE) if (collectable->type == P_MINE)
{ {
if (collision(collectable, bullet)) if (collectable_collision(collectable, bullet))
{ {
collectable->active = false; collectable->active = false;
@ -1932,6 +1932,17 @@ static bool game_checkPauseRequest()
return false; return false;
} }
bool game_collision(float x0, float y0, int w0, int h0, float x2, float y2, int w1, int h1)
{
float x1 = x0 + w0;
float y1 = y0 + h0;
float x3 = x2 + w1;
float y3 = y2 + h1;
return !(x1<x2 || x3<x0 || y1<y2 || y3<y0);
}
int game_mainLoop() int game_mainLoop()
{ {
resetLists(); resetLists();

View File

@ -25,6 +25,7 @@ extern Game game;
void game_init(); void game_init();
void game_doStars(); void game_doStars();
void game_doExplosions(); void game_doExplosions();
bool game_collision(float x0, float y0, int w0, int h0, float x2, float y2, int w1, int h1);
int game_mainLoop(); int game_mainLoop();
#endif #endif

View File

@ -37,59 +37,6 @@ bRect *bufferTail;
textObject textShape[MAX_TEXTSHAPES]; textObject textShape[MAX_TEXTSHAPES];
SDL_Surface *messageBox; SDL_Surface *messageBox;
bool collision(float x0, float y0, int w0, int h0, float x2, float y2, int w1, int h1)
{
float x1 = x0 + w0;
float y1 = y0 + h0;
float x3 = x2 + w1;
float y3 = y2 + h1;
return !(x1<x2 || x3<x0 || y1<y2 || y3<y0);
}
bool collision(object *object1, object *object2)
{
float x0 = object1->x;
float y0 = object1->y;
float w0 = object1->image[0]->w;
float h0 = object1->image[0]->h;
float x2 = object2->x;
float y2 = object2->y;
float w1 = object2->image[0]->w;
float h1 = object2->image[0]->h;
float x1 = x0 + w0;
float y1 = y0 + h0;
float x3 = x2 + w1;
float y3 = y2 + h1;
return !(x1<x2 || x3<x0 || y1<y2 || y3<y0);
}
bool collision(collectables *object1, object *object2)
{
float x0 = object1->x;
float y0 = object1->y;
float w0 = object1->image->w;
float h0 = object1->image->h;
float x2 = object2->x;
float y2 = object2->y;
float w1 = object2->image[0]->w;
float h1 = object2->image[0]->h;
float x1 = x0 + w0;
float y1 = y0 + h0;
float x3 = x2 + w1;
float y3 = y2 + h1;
return !(x1<x2 || x3<x0 || y1<y2 || y3<y0);
}
void initGraphics() void initGraphics()
{ {
bufferHead = new bRect; bufferHead = new bRect;

View File

@ -34,10 +34,6 @@ extern textObject textShape[MAX_TEXTSHAPES];
extern SDL_Surface *messageBox; extern SDL_Surface *messageBox;
extern bool collision(float x0, float y0, int w0, int h0, float x2, float y2, int w1, int h1);
extern bool collision(object *object1, object *object2);
extern bool collision(collectables *object1, object *object2);
extern void initGraphics(); extern void initGraphics();
extern SDL_Surface *setTransparent(SDL_Surface *sprite); extern SDL_Surface *setTransparent(SDL_Surface *sprite);
extern void addBuffer(int x, int y, int w, int h); extern void addBuffer(int x, int y, int w, int h);

View File

@ -266,7 +266,7 @@ static bool intermission_showSystem(float x, float y, bool selectable)
blit(systemPlanet[planet].image, r.x, r.y); blit(systemPlanet[planet].image, r.x, r.y);
if (selectable && if (selectable &&
collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, game_collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6,
r.x, r.y, systemPlanet[planet].image->w, r.x, r.y, systemPlanet[planet].image->w,
systemPlanet[planet].image->h)) systemPlanet[planet].image->h))
{ {
@ -454,7 +454,7 @@ static void intermission_doComms(SDL_Surface *comms)
{ {
for (int i = 0 ; i < 4 ; i++) for (int i = 0 ; i < 4 ; i++)
{ {
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 170, 180 + (i * 60), 430, 50)) if (game_collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 170, 180 + (i * 60), 430, 50))
{ {
intermission_createMissionDetailSurface(comms, i); intermission_createMissionDetailSurface(comms, i);
engine.keyState[KEY_FIRE] = 0; engine.keyState[KEY_FIRE] = 0;
@ -463,7 +463,7 @@ static void intermission_doComms(SDL_Surface *comms)
} }
else else
{ {
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 170, 440, 160, 20)) if (game_collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 170, 440, 160, 20))
{ {
intermission_createCommsSurface(comms); intermission_createCommsSurface(comms);
engine.keyState[KEY_FIRE] = 0; engine.keyState[KEY_FIRE] = 0;
@ -518,26 +518,26 @@ static void intermission_doOptions(SDL_Surface *optionsSurface)
{ {
if ((engine.keyState[KEY_FIRE])) if ((engine.keyState[KEY_FIRE]))
{ {
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 417, 172, 45, 22)) if (game_collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 417, 172, 45, 22))
engine.useSound = true; engine.useSound = true;
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 478, 172, 45, 22)) if (game_collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 478, 172, 45, 22))
engine.useSound = false; engine.useSound = false;
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 417, 222, 45, 22)) if (game_collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 417, 222, 45, 22))
{ {
engine.useMusic = true; engine.useMusic = true;
audio_playMusic("music/through_space.ogg", -1); audio_playMusic("music/through_space.ogg", -1);
} }
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 478, 222, 45, 22)) if (game_collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 478, 222, 45, 22))
{ {
engine.useMusic = false; engine.useMusic = false;
audio_haltMusic(); audio_haltMusic();
} }
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 417, 272, 45, 22)) if (game_collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 417, 272, 45, 22))
{ {
if (!engine.fullScreen) if (!engine.fullScreen)
{ {
@ -546,7 +546,7 @@ static void intermission_doOptions(SDL_Surface *optionsSurface)
} }
} }
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 478, 272, 45, 22)) if (game_collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 478, 272, 45, 22))
{ {
if (engine.fullScreen) if (engine.fullScreen)
{ {
@ -920,7 +920,7 @@ int intermission()
blit(shape[i + 1], 80 + (i * 90), 500); blit(shape[i + 1], 80 + (i * 90), 500);
} }
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 80 + (i * 90), 500, 32, 32)) if (game_collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 80 + (i * 90), 500, 32, 32))
{ {
if (i != 0) if (i != 0)
{ {

View File

@ -238,7 +238,7 @@ int showSaveSlots(SDL_Surface *savesSurface, signed char saveSlot)
{ {
for (int i = 1 ; i <= 5 ; i++) for (int i = 1 ; i <= 5 ; i++)
{ {
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, if (game_collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6,
r.x, r.y, r.w, r.h)) r.x, r.y, r.w, r.h))
{ {
clickedSlot = i; clickedSlot = i;
@ -247,18 +247,18 @@ int showSaveSlots(SDL_Surface *savesSurface, signed char saveSlot)
r.y += 30; r.y += 30;
} }
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 215, if (game_collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 215,
365, 100, 25)) 365, 100, 25))
{ {
saveGame(saveSlot); saveGame(saveSlot);
createSavesSurface(savesSurface, -10); createSavesSurface(savesSurface, -10);
} }
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 335, if (game_collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 335,
365, 100, 25)) 365, 100, 25))
createSavesSurface(savesSurface, -1); createSavesSurface(savesSurface, -1);
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 453, if (game_collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 453,
365, 100, 25)) 365, 100, 25))
{ {
char filename[PATH_MAX]; char filename[PATH_MAX];

View File

@ -19,6 +19,27 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "Starfighter.h" #include "Starfighter.h"
bool ship_collision(object *ship, object *otherShip)
{
float x0 = ship->x;
float y0 = ship->y;
float w0 = ship->image[0]->w;
float h0 = ship->image[0]->h;
float x2 = otherShip->x;
float y2 = otherShip->y;
float w1 = otherShip->image[0]->w;
float h1 = otherShip->image[0]->h;
float x1 = x0 + w0;
float y1 = y0 + h0;
float x3 = x2 + w1;
float y3 = y2 + h1;
return !(x1<x2 || x3<x0 || y1<y2 || y3<y0);
}
/* /*
Fill in later... Fill in later...
*/ */
@ -142,7 +163,7 @@ void ship_fireRay(object *ship)
{ {
if (player.shield > 0) if (player.shield > 0)
{ {
if (collision(player.x, player.y, player.image[0]->w, if (game_collision(player.x, player.y, player.image[0]->w,
player.image[0]->h, ray.x, ray.y, ray.w, ray.h) && player.image[0]->h, ray.x, ray.y, ray.w, ray.h) &&
(!engine.cheatShield)) (!engine.cheatShield))
{ {
@ -174,7 +195,7 @@ void ship_fireRay(object *ship)
if ((aliens[i].shield > 0) && (ship != &aliens[i]) && if ((aliens[i].shield > 0) && (ship != &aliens[i]) &&
(ship->classDef != aliens[i].classDef)) (ship->classDef != aliens[i].classDef))
{ {
if (collision(aliens[i].x, aliens[i].y, aliens[i].image[0]->w, if (game_collision(aliens[i].x, aliens[i].y, aliens[i].image[0]->w,
aliens[i].image[0]->h, ray.x, ray.y, ray.w, ray.h)) aliens[i].image[0]->h, ray.x, ray.y, ray.w, ray.h))
{ {
alien_hurt(&aliens[i], ship->owner, 1, false); alien_hurt(&aliens[i], ship->owner, 1, false);

View File

@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef SHIP_H #ifndef SHIP_H
#define SHIP_H #define SHIP_H
bool ship_collision(object *ship, object *otherShip);
void ship_fireBullet(object *ship, int weaponType); void ship_fireBullet(object *ship, int weaponType);
void ship_fireRay(object *ship); void ship_fireRay(object *ship);

View File

@ -1044,7 +1044,7 @@ void showShop()
{ {
for (int i = 0 ; i < icons ; i++) for (int i = 0 ; i < icons ; i++)
{ {
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, if (game_collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6,
shopItems[i].x, shopItems[i].y, 32, 25)) shopItems[i].x, shopItems[i].y, 32, 25))
{ {
shopSelectedItem = i; shopSelectedItem = i;
@ -1055,14 +1055,14 @@ void showShop()
if (shopSelectedItem > -1) if (shopSelectedItem > -1)
{ {
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 60, 350, 24, 16)) if (game_collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 60, 350, 24, 16))
{ {
buy(shopSelectedItem); buy(shopSelectedItem);
engine.keyState[KEY_FIRE] = 0; engine.keyState[KEY_FIRE] = 0;
drawShop(); drawShop();
} }
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 700, 350, 24, 16)) if (game_collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 700, 350, 24, 16))
{ {
sell(shopSelectedItem); sell(shopSelectedItem);
engine.keyState[KEY_FIRE] = 0; engine.keyState[KEY_FIRE] = 0;