From 1fbf7971b823a8412f18988e792860643f481b52 Mon Sep 17 00:00:00 2001 From: Steve Date: Sun, 4 Feb 2018 11:00:14 +0000 Subject: [PATCH] Moved bullets into entities/bullets. --- common.mk | 3 +- src/combat/weapons.c | 102 ----------------- src/combat/weapons.h | 7 +- src/entities/bullets/bullet.c | 123 +++++++++++++++++++++ src/entities/bullets/bullet.h | 32 ++++++ src/{combat => entities/bullets}/grenade.c | 0 src/{combat => entities/bullets}/grenade.h | 2 +- src/{combat => entities/bullets}/laser.c | 0 src/{combat => entities/bullets}/laser.h | 2 +- src/{combat => entities/bullets}/missile.c | 0 src/{combat => entities/bullets}/missile.h | 2 +- 11 files changed, 161 insertions(+), 112 deletions(-) create mode 100644 src/entities/bullets/bullet.c create mode 100644 src/entities/bullets/bullet.h rename src/{combat => entities/bullets}/grenade.c (100%) rename src/{combat => entities/bullets}/grenade.h (97%) rename src/{combat => entities/bullets}/laser.c (100%) rename src/{combat => entities/bullets}/laser.h (97%) rename src/{combat => entities/bullets}/missile.c (100%) rename src/{combat => entities/bullets}/missile.h (97%) diff --git a/common.mk b/common.mk index 070b3ff..1d9ec3e 100644 --- a/common.mk +++ b/common.mk @@ -7,6 +7,7 @@ SEARCHPATH += src/combat SEARCHPATH += src/entities SEARCHPATH += src/entities/blobs SEARCHPATH += src/entities/boss +SEARCHPATH += src/entities/bullets SEARCHPATH += src/entities/cannons SEARCHPATH += src/entities/decoration SEARCHPATH += src/entities/evilBlobs @@ -30,7 +31,7 @@ vpath %.h $(SEARCHPATH) DEPS += defs.h structs.h OBJS += atlas.o atlasTest.o aquaBlob.o -OBJS += battery.o blaze.o bob.o boss.o blobBoss.o +OBJS += battery.o blaze.o bob.o boss.o blobBoss.o bullet.o OBJS += camera.o cannon.o cardReader.o cell.o cherry.o combat.o consumable.o OBJS += debris.o destructable.o door.o draw.o OBJS += effects.o ending.o entities.o entity.o entityFactory.o exit.o explosions.o eyeDroid.o eyeDroidCommander.o evilBlob.o diff --git a/src/combat/weapons.c b/src/combat/weapons.c index 6220ab3..8e90f41 100644 --- a/src/combat/weapons.c +++ b/src/combat/weapons.c @@ -20,10 +20,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "weapons.h" -Bullet *createBaseBullet(Unit *owner); -static void tick(void); -static void touch(Entity *other); - static Sprite *bulletSprite[2]; static Sprite *plasmaSprite[2]; static Sprite *aimedSprite; @@ -243,104 +239,6 @@ void fireMissile(Unit *owner) playSound(SND_MISSILE, CH_WEAPON); } -Bullet *createBaseBullet(Unit *owner) -{ - Bullet *bullet; - - bullet = malloc(sizeof(Bullet)); - memset(bullet, 0, sizeof(Bullet)); - - initEntity((Entity*)bullet); - - bullet->x = (owner->x + owner->w / 2); - bullet->y = (owner->y + owner->h / 2) - 3; - bullet->dx = owner->facing == FACING_RIGHT ? 15 : -15; - bullet->facing = owner->facing; - bullet->damage = 1; - bullet->owner = (Entity*)owner; - bullet->health = FPS * 3; - bullet->flags |= EF_WEIGHTLESS | EF_IGNORE_BULLETS | EF_NO_ENVIRONMENT | EF_KILL_OFFSCREEN | EF_NO_TELEPORT; - - bullet->tick = tick; - bullet->touch = touch; - - return bullet; -} - -static void tick(void) -{ - Bullet *b; - - b = (Bullet*)self; - - b->health--; - - if (b->x <= 0 || b->y <= 0 || b->x >= (MAP_WIDTH * MAP_TILE_SIZE) - b->w || b->y >= (MAP_HEIGHT * MAP_TILE_SIZE) - b->h) - { - b->alive = ALIVE_DEAD; - } - - // don't allow the player to kill everything on the map by firing - // constantly - if (b->owner->type == ET_BOB) - { - if (b->x < camera.x || b->y < camera.y || b->x > camera.x + SCREEN_WIDTH || b->y > camera.y + SCREEN_HEIGHT) - { - b->alive = ALIVE_DEAD; - } - } -} - -static void touch(Entity *other) -{ - Bullet *b; - - b = (Bullet*)self; - - if (b->alive == ALIVE_ALIVE) - { - if (other == NULL) - { - addSparkParticles(b->x, b->y); - - b->alive = ALIVE_DEAD; - - if (rand() % 2) - { - playSound(SND_RICO_1, CH_ANY); - } - else - { - playSound(SND_RICO_2, CH_ANY); - } - } - else if (other != b->owner && (!(other->flags & EF_IGNORE_BULLETS)) && b->owner->type != other->type) - { - other->applyDamage(b->damage); - - if (other->flags & EF_EXPLODES) - { - playSound(SND_METAL_HIT, CH_ANY); - - addSparkParticles(b->x, b->y); - } - else - { - playSound(SND_FLESH_HIT, CH_ANY); - - addSmallFleshChunk(b->x, b->y); - } - - b->alive = ALIVE_DEAD; - - if (b->owner->type == world.bob->type) - { - game.statShotsHit[b->weaponType]++; - } - } - } -} - int getRandomPlayerWeapon(int excludeGrenades) { if (excludeGrenades) diff --git a/src/combat/weapons.h b/src/combat/weapons.h index 5d6f5e9..d949a18 100644 --- a/src/combat/weapons.h +++ b/src/combat/weapons.h @@ -27,11 +27,6 @@ extern void getSlope(int x1, int y1, int x2, int y2, float *dx, float *dy); extern void initLaser(Bullet *b); extern void initGrenade(Bullet *b); extern void initMissile(Bullet *b); -extern void initEntity(Entity *e); -extern void addSmallFleshChunk(float x, float y); -extern void addSparkParticles(float x, float y); +extern Bullet *createBaseBullet(Unit *owner); -extern Camera camera; -extern Entity *self; -extern Game game; extern World world; diff --git a/src/entities/bullets/bullet.c b/src/entities/bullets/bullet.c new file mode 100644 index 0000000..56bc1b7 --- /dev/null +++ b/src/entities/bullets/bullet.c @@ -0,0 +1,123 @@ +/* +Copyright (C) 2018 Parallel Realities + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#include "bullet.h" + +static void tick(void); +static void touch(Entity *other); + +Bullet *createBaseBullet(Unit *owner) +{ + Bullet *bullet; + + bullet = malloc(sizeof(Bullet)); + memset(bullet, 0, sizeof(Bullet)); + + initEntity((Entity*)bullet); + + bullet->x = (owner->x + owner->w / 2); + bullet->y = (owner->y + owner->h / 2) - 3; + bullet->dx = owner->facing == FACING_RIGHT ? 15 : -15; + bullet->facing = owner->facing; + bullet->damage = 1; + bullet->owner = (Entity*)owner; + bullet->health = FPS * 3; + bullet->flags |= EF_WEIGHTLESS | EF_IGNORE_BULLETS | EF_NO_ENVIRONMENT | EF_KILL_OFFSCREEN | EF_NO_TELEPORT; + + bullet->tick = tick; + bullet->touch = touch; + + bullet->spriteFrame = 0; + + return bullet; +} + +static void tick(void) +{ + Bullet *b; + + b = (Bullet*)self; + + b->health--; + + if (b->x <= 0 || b->y <= 0 || b->x >= (MAP_WIDTH * MAP_TILE_SIZE) - b->w || b->y >= (MAP_HEIGHT * MAP_TILE_SIZE) - b->h) + { + b->alive = ALIVE_DEAD; + } + + /* don't allow the player to kill everything on the map by firing constantly */ + if (b->owner->type == ET_BOB) + { + if (b->x < camera.x || b->y < camera.y || b->x > camera.x + SCREEN_WIDTH || b->y > camera.y + SCREEN_HEIGHT) + { + b->alive = ALIVE_DEAD; + } + } +} + +static void touch(Entity *other) +{ + Bullet *b; + + b = (Bullet*)self; + + if (b->alive == ALIVE_ALIVE) + { + if (other == NULL) + { + addSparkParticles(b->x, b->y); + + b->alive = ALIVE_DEAD; + + if (rand() % 2) + { + playSound(SND_RICO_1, CH_ANY); + } + else + { + playSound(SND_RICO_2, CH_ANY); + } + } + else if (other != b->owner && (!(other->flags & EF_IGNORE_BULLETS)) && b->owner->type != other->type) + { + other->applyDamage(b->damage); + + if (other->flags & EF_EXPLODES) + { + playSound(SND_METAL_HIT, CH_ANY); + + addSparkParticles(b->x, b->y); + } + else + { + playSound(SND_FLESH_HIT, CH_ANY); + + addSmallFleshChunk(b->x, b->y); + } + + b->alive = ALIVE_DEAD; + + if (b->owner->type == world.bob->type) + { + game.statShotsHit[b->weaponType]++; + } + } + } +} diff --git a/src/entities/bullets/bullet.h b/src/entities/bullets/bullet.h new file mode 100644 index 0000000..9987d0f --- /dev/null +++ b/src/entities/bullets/bullet.h @@ -0,0 +1,32 @@ +/* +Copyright (C) 2018 Parallel Realities + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#include "../../common.h" + +extern void playSound(int snd, int ch); +extern void initEntity(Entity *e); +extern void addSmallFleshChunk(float x, float y); +extern void addSparkParticles(float x, float y); +extern Bullet *createBaseBullet(Unit *owner); + +extern Camera camera; +extern Entity *self; +extern Game game; +extern World world; diff --git a/src/combat/grenade.c b/src/entities/bullets/grenade.c similarity index 100% rename from src/combat/grenade.c rename to src/entities/bullets/grenade.c diff --git a/src/combat/grenade.h b/src/entities/bullets/grenade.h similarity index 97% rename from src/combat/grenade.h rename to src/entities/bullets/grenade.h index ecb707d..aa210b9 100644 --- a/src/combat/grenade.h +++ b/src/entities/bullets/grenade.h @@ -18,7 +18,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "../common.h" +#include "../../common.h" extern void playSound(int snd, int ch); extern void initGrenade(Bullet *b); diff --git a/src/combat/laser.c b/src/entities/bullets/laser.c similarity index 100% rename from src/combat/laser.c rename to src/entities/bullets/laser.c diff --git a/src/combat/laser.h b/src/entities/bullets/laser.h similarity index 97% rename from src/combat/laser.h rename to src/entities/bullets/laser.h index 32f3c93..ec55d74 100644 --- a/src/combat/laser.h +++ b/src/entities/bullets/laser.h @@ -18,7 +18,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "../common.h" +#include "../../common.h" extern void playSound(int snd, int ch); extern int rrnd(int low, int high); diff --git a/src/combat/missile.c b/src/entities/bullets/missile.c similarity index 100% rename from src/combat/missile.c rename to src/entities/bullets/missile.c diff --git a/src/combat/missile.h b/src/entities/bullets/missile.h similarity index 97% rename from src/combat/missile.h rename to src/entities/bullets/missile.h index d2b83e4..fc3e5d1 100644 --- a/src/combat/missile.h +++ b/src/entities/bullets/missile.h @@ -18,7 +18,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "../common.h" +#include "../../common.h" extern int rrnd(int low, int high); extern void initMissile(Bullet *b);