From 4a77cf6e4d8100ade5a3af9f09bf4f0222c5e7a9 Mon Sep 17 00:00:00 2001 From: Steve Date: Sat, 27 Jan 2018 10:21:45 +0000 Subject: [PATCH] Added doors. --- common.mk | 2 +- src/combat/weapons.h | 1 - src/defs.h | 7 + src/structs.h | 6 + src/world/entities/misc/infoPoint.h | 6 - src/world/entities/structures/door.c | 218 +++++++++++++++++++++++++++ src/world/entities/structures/door.h | 32 ++++ 7 files changed, 264 insertions(+), 8 deletions(-) create mode 100644 src/world/entities/structures/door.c create mode 100644 src/world/entities/structures/door.h diff --git a/common.mk b/common.mk index b63ba42..aa0b02e 100644 --- a/common.mk +++ b/common.mk @@ -27,7 +27,7 @@ DEPS += defs.h structs.h OBJS += atlas.o OBJS += battery.o blaze.o bob.o boss.o blobBoss.o OBJS += camera.o cannon.o cardReader.o cell.o cherry.o combat.o consumable.o -OBJS += debris.o destructable.o draw.o +OBJS += debris.o destructable.o door.o draw.o OBJS += effects.o entities.o explosions.o eyeDroidCommander.o OBJS += fleshChunk.o frost.o OBJS += game.o diff --git a/src/combat/weapons.h b/src/combat/weapons.h index 92bc6c5..88ad97d 100644 --- a/src/combat/weapons.h +++ b/src/combat/weapons.h @@ -20,7 +20,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "../common.h" -extern Entity *createEntity(void); extern int getSpriteIndex(char *name); extern void playSound(int snd, int ch); extern int rrnd(int low, int high); diff --git a/src/defs.h b/src/defs.h index aebb048..e57440a 100644 --- a/src/defs.h +++ b/src/defs.h @@ -143,6 +143,12 @@ enum FACING_DIE }; +enum +{ + DOOR_OPEN, + DOOR_CLOSED +}; + enum { WPN_PISTOL, @@ -280,6 +286,7 @@ enum CH_DEATH, CH_ITEM, CH_TOUCH, + CH_MECHANICAL, CH_MAX }; diff --git a/src/structs.h b/src/structs.h index 5136840..261faad 100644 --- a/src/structs.h +++ b/src/structs.h @@ -146,11 +146,17 @@ struct Entity { int messageTimer; char message[MAX_DESCRIPTION_LENGTH]; char requiredCard[MAX_NAME_LENGTH]; + char requiredKey[MAX_NAME_LENGTH]; long flags; SDL_Rect bounds; int sprite[3]; int spriteTime; int spriteFrame; + int isLocked; + int closedX; + int closedY; + int state; + int speed; Entity *carriedItem; Entity *owner; void (*action)(void); diff --git a/src/world/entities/misc/infoPoint.h b/src/world/entities/misc/infoPoint.h index 0018cb1..a078fdf 100644 --- a/src/world/entities/misc/infoPoint.h +++ b/src/world/entities/misc/infoPoint.h @@ -20,13 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "../../../common.h" -extern void activateEntities(char *names, int activate); -extern void dropCarriedItem(void); -extern int rrnd(int low, int high); -extern void addExplosion(float x, float y, int radius, Entity *owner); -extern void addScorchDecal(int x, int y); extern int getSpriteIndex(char *name); -extern void updateObjective(char *targetName); extern void initEntity(Entity *e); extern void showInfoMessage(char *message); diff --git a/src/world/entities/structures/door.c b/src/world/entities/structures/door.c new file mode 100644 index 0000000..c76eeb0 --- /dev/null +++ b/src/world/entities/structures/door.c @@ -0,0 +1,218 @@ +/* +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 "door.h" + +static void tick(void); +static void touch(Entity *other); +static void openWithKey(void); +static int isClosed(void); +static int isOpening(void); +static int isClosing(void); + +void initDoor(Entity *e) +{ + initEntity(e); + + e->isSolid = 1; + + e->flags |= EF_WEIGHTLESS | EF_NO_ENVIRONMENT | EF_NO_CLIP | EF_EXPLODES | EF_NO_TELEPORT; + + e->closedX = e->closedY = -1; + + e->state = DOOR_CLOSED; + + e->speed = 8; + + e->isLocked = 1; + + e->sprite[0] = e->sprite[1] = e->sprite[2] = getSpriteIndex("Door"); + + if (e->closedX == -1 && e->closedY == -1) + { + e->closedX = (int) e->x; + e->closedY = (int) e->y; + } + + e->tick = tick; + e->touch = touch; +} + +void initBronzeDoor(Entity *e) +{ + initDoor(e); + + STRNCPY(e->requiredKey, "Bronze Key", MAX_NAME_LENGTH); + + e->speed = 2; + + e->sprite[0] = e->sprite[1] = e->sprite[2] = getSpriteIndex("BronzeDoor"); +} + +void initSilverDoor(Entity *e) +{ + initDoor(e); + + STRNCPY(e->requiredKey, "Silver Key", MAX_NAME_LENGTH); + + e->speed = 2; + + e->sprite[0] = e->sprite[1] = e->sprite[2] = getSpriteIndex("SilverDoor"); +} + +void initGoldDoor(Entity *e) +{ + initDoor(e); + + STRNCPY(e->requiredKey, "Gold Key", MAX_NAME_LENGTH); + + e->speed = 2; + + e->sprite[0] = e->sprite[1] = e->sprite[2] = getSpriteIndex("GoldDoor"); +} + +static void tick(void) +{ + self->dx = self->dy = 0; + + if (isOpening()) + { + getSlope(self->tx, self->ty, self->x, self->y, &self->dx, &self->dy); + + self->dx *= self->speed; + self->dy *= self->speed; + + if (abs(self->x - self->tx) < self->speed && abs(self->y - self->ty) < self->speed) + { + self->x = self->tx; + self->y = self->ty; + } + + self->isStatic = 0; + } + else if (isClosing()) + { + getSlope(self->closedX, self->closedY, self->x, self->y, &self->dx, &self->dy); + + self->dx *= self->speed; + self->dy *= self->speed; + + if (abs(self->x - self->closedX) < self->speed && abs(self->y - self->closedY) < self->speed) + { + self->x = self->closedX; + self->y = self->closedY; + } + + self->isStatic = 0; + } + + if (self->dx == 0 && self->dy == 0 && !self->isStatic) + { + self->isStatic = 1; + + playSound(SND_DOOR_FINISH, CH_MECHANICAL); + } +} + +static void touch(Entity *other) +{ + if ((other->type != ET_BOB && self->isLocked) || (other->type != ET_BOB && other->type != ET_ENEMY)) + { + return; + } + + if (self->isLocked && !dev.cheatKeys) + { + if (isClosed()) + { + if (strlen(self->requiredKey) != 0) + { + openWithKey(); + } + else if (self->thinkTime == 0) + { + setGameplayMessage(MSG_GAMEPLAY, "Door is locked"); + + playSound(SND_DENIED, CH_MECHANICAL); + } + + self->thinkTime = 2; + + return; + } + else if (isClosing()) + { + return; + } + } + + if (self->state != DOOR_OPEN) + { + playSound(SND_DOOR_START, CH_MECHANICAL); + } + + self->state = DOOR_OPEN; +} + +static void openWithKey(void) +{ + if (hasItem(self->requiredKey)) + { + if (self->thinkTime <= 0) + { + setGameplayMessage(MSG_GAMEPLAY, "%s required", self->requiredKey); + + playSound(SND_DENIED, CH_MECHANICAL); + } + + self->thinkTime = 2; + + return; + } + + removeKey(self->requiredKey); + + setGameplayMessage(MSG_GAMEPLAY, "%s removed", self->requiredKey); + + STRNCPY(self->requiredKey, "", MAX_NAME_LENGTH); + self->isLocked = 0; + + if (self->state != DOOR_OPEN) + { + playSound(SND_DOOR_START, CH_MECHANICAL); + } + + self->state = DOOR_OPEN; +} + +static int isOpening(void) +{ + return (self->state == DOOR_OPEN && ((int) self->x != (int) self->tx || (int) self->y != (int) self->ty)); +} + +static int isClosing(void) +{ + return (self->state == DOOR_CLOSED && ((int) self->x != self->closedX || (int) self->y != self->closedY)); +} + +static int isClosed(void) +{ + return (self->state == DOOR_CLOSED && ((int) self->x == self->closedX && (int) self->y == self->closedY)); +} diff --git a/src/world/entities/structures/door.h b/src/world/entities/structures/door.h new file mode 100644 index 0000000..f9d148e --- /dev/null +++ b/src/world/entities/structures/door.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 initEntity(Entity *e); +extern int getSpriteIndex(char *name); +extern void playSound(int snd, int ch); +extern void getSlope(int x1, int y1, int x2, int y2, float *dx, float *dy); +extern void setGameplayMessage(int type, char *format, ...); +extern int hasItem(char *name); +extern void removeKey(char *name); + +extern Entity *self; +extern Dev dev;