From 8e7f53ea54d7c6f61324531a45883c5bc5a72aa9 Mon Sep 17 00:00:00 2001 From: Steve Date: Sat, 27 Jan 2018 08:03:26 +0000 Subject: [PATCH] Cannon. --- common.mk | 3 +- src/defs.h | 5 + src/structs.h | 3 + src/world/entities/cannons/cannon.c | 191 ++++++++++++++++++++++++ src/world/entities/cannons/cannon.h | 36 +++++ src/world/entities/items/keycard.h | 5 - src/world/entities/items/weaponPickup.h | 4 - 7 files changed, 237 insertions(+), 10 deletions(-) create mode 100644 src/world/entities/cannons/cannon.c create mode 100644 src/world/entities/cannons/cannon.h diff --git a/common.mk b/common.mk index bd2660b..e81af85 100644 --- a/common.mk +++ b/common.mk @@ -13,6 +13,7 @@ SEARCHPATH += src/world SEARCHPATH += src/world/entities SEARCHPATH += src/world/entities/blobs SEARCHPATH += src/world/entities/boss +SEARCHPATH += src/world/entities/cannons SEARCHPATH += src/world/entities/items vpath %.c $(SEARCHPATH) @@ -22,7 +23,7 @@ DEPS += defs.h structs.h OBJS += atlas.o OBJS += battery.o blaze.o bob.o boss.o blobBoss.o -OBJS += camera.o cell.o cherry.o combat.o consumable.o +OBJS += camera.o cannon.o cell.o cherry.o combat.o consumable.o OBJS += draw.o OBJS += effects.o entities.o explosions.o eyeDroidCommander.o OBJS += frost.o diff --git a/src/defs.h b/src/defs.h index 13920e1..6bec99c 100644 --- a/src/defs.h +++ b/src/defs.h @@ -192,6 +192,11 @@ enum MS_COMPLETE }; +enum +{ + WS_IN_PROGRESS +}; + enum { PLANE_BACKGROUND, diff --git a/src/structs.h b/src/structs.h index 054a5be..7d16a1a 100644 --- a/src/structs.h +++ b/src/structs.h @@ -113,6 +113,7 @@ struct Entity { int damage; int weaponType; int shotsToFire; + int maxShotsToFire; int isSolid; int environment; int isStatic; @@ -147,6 +148,7 @@ struct Entity { void (*touch)(Entity *other); void (*tick)(void); void (*die)(void); + int (*canFire)(Entity *target); void (*reset)(void); void (*activate)(int active); void (*changeEnvironment)(void); @@ -331,6 +333,7 @@ struct Particle { typedef struct { char id[MAX_NAME_LENGTH]; + int state; Entity *bob, *boss; Map map; Entity entityHead, *entityTail; diff --git a/src/world/entities/cannons/cannon.c b/src/world/entities/cannons/cannon.c new file mode 100644 index 0000000..e4d39ac --- /dev/null +++ b/src/world/entities/cannons/cannon.c @@ -0,0 +1,191 @@ +/* +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 "cannon.h" + +static void applyDamage(int damage); +static void walk(void); +static void die(void); +static void animate(void); +static SDL_Rect getBounds(void); +static int canFire(Entity *target); +static void preFire(void); + +void initCannon(Entity *e) +{ + initUnit(e); + + e->sprite[FACING_LEFT] = getSpriteIndex("CannonLeft"); + e->sprite[FACING_RIGHT] = getSpriteIndex("CannonRight"); + e->sprite[FACING_DIE] = getSpriteIndex("CannonLeft"); + + e->weaponType = WPN_MISSILE; + + e->maxShotsToFire = 4; + + e->reload = 0; + + e->canCarryItem = 1; + + e->health = e->healthMax = 50; + + e->flags |= EF_EXPLODES; + + e->animate = animate; + e->applyDamage = applyDamage; + e->walk = walk; + e->die = die; + e->getBounds= getBounds; + e->canFire = canFire; +} + +static void applyDamage(int damage) +{ + if (self->health > 0) + { + self->health -= damage; + + self->facing = self->x < world.bob->x ? FACING_RIGHT : FACING_LEFT; + + self->thinkTime = 0; + } +} + +static void die(void) +{ + int mx, my; + + self->health--; + + if (self->health % 3 == 0) + { + mx = (int) ((self->x + (self->w / 2)) / MAP_TILE_SIZE); + my = (int) ((self->y + self->h) / MAP_TILE_SIZE); + addScorchDecal(mx, my); + + addExplosion(self->x, self->y, 50, self); + } + + if (self->health <= -50) + { + updateObjective(self->name); + updateObjective("ENEMY"); + fireTriggers(self->name); + + dropCarriedItem(); + + self->alive = ALIVE_DEAD; + } +} + +static void patrol(void) +{ + self->facing = rand() % 2 ? FACING_LEFT : FACING_RIGHT; + + self->thinkTime = rrnd(FPS / 2, FPS); +} + +static void lookForPlayer(void) +{ + int r; + + self->thinkTime = rrnd(FPS / 2, FPS); + + if (world.state != WS_IN_PROGRESS || dev.cheatBlind) + { + patrol(); + return; + } + + if ((self->facing == FACING_LEFT && world.bob->x > self->x) || (self->facing == FACING_RIGHT && world.bob->x < self->x)) + { + patrol(); + return; + } + + if (getDistance(world.bob->x, world.bob->y, self->x, self->y) > 650) + { + patrol(); + return; + } + + if (!enemyCanSeePlayer(self)) + { + patrol(); + return; + } + + r = rand() % 100; + + if (self->isMissionTarget) + { + r = rand() % 20; + } + + if (r < 15) + { + self->shotsToFire = rrnd(1, self->maxShotsToFire); + self->action = preFire; + } +} + +static void preFire(void) +{ + self->facing = (world.bob->x < self->x) ? FACING_LEFT : FACING_RIGHT; + + if (self->reload > 0) + { + return; + } + + self->attack(); + + self->shotsToFire--; + + if (self->shotsToFire == 0) + { + self->walk(); + } +} + +static void walk(void) +{ + self->action = lookForPlayer; +} + +static void animate(void) +{ + +} + +static SDL_Rect getBounds(void) +{ + self->bounds.x = self->x + 36; + self->bounds.y = self->y; + self->bounds.w = 36; + self->bounds.h = self->h; + + return self->bounds; +} + +static int canFire(Entity *target) +{ + return abs(target->y - self->y) <= MAP_TILE_SIZE; +} diff --git a/src/world/entities/cannons/cannon.h b/src/world/entities/cannons/cannon.h new file mode 100644 index 0000000..220d1e2 --- /dev/null +++ b/src/world/entities/cannons/cannon.h @@ -0,0 +1,36 @@ +/* +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 initUnit(Entity *e); +extern void addExplosion(float x, float y, int radius, Entity *owner); +extern int rrnd(int low, int high); +extern int getSpriteIndex(char *name); +extern int getDistance(int x1, int y1, int x2, int y2); +extern int enemyCanSeePlayer(Entity *e); +extern void updateObjective(char *targetName); +extern void fireTriggers(char *targetName); +extern void dropCarriedItem(void); +extern void addScorchDecal(int x, int y); + +extern Dev dev; +extern Entity *self; +extern World world; diff --git a/src/world/entities/items/keycard.h b/src/world/entities/items/keycard.h index ed850a8..a52f7b7 100644 --- a/src/world/entities/items/keycard.h +++ b/src/world/entities/items/keycard.h @@ -20,14 +20,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "../../../common.h" -extern void playSound(int snd, int ch); -extern void setGameplayMessage(int type, char *format, ...); extern void initItem(Entity *e); -extern int getSpriteIndex(char *name); extern void updateObjective(char *targetName); -extern int rrnd(int low, int high); extern void teekaExitMission(void); extern Entity *self; -extern Game game; extern World world; diff --git a/src/world/entities/items/weaponPickup.h b/src/world/entities/items/weaponPickup.h index 22efe30..8df66d9 100644 --- a/src/world/entities/items/weaponPickup.h +++ b/src/world/entities/items/weaponPickup.h @@ -24,13 +24,9 @@ extern void playSound(int snd, int ch); extern void setGameplayMessage(int type, char *format, ...); extern void initConsumable(Entity *e); extern int getSpriteIndex(char *name); -extern void updateObjective(char *targetName); -extern int rrnd(int low, int high); extern void pickupItem(void); -extern void teekaExitMission(void); extern void setEntitySize(Entity *e); extern int touchedPlayer(Entity *e); extern Entity *self; -extern Game game; extern World world;