From b862123c95e3463c48950155d5bb6196baeae8dc Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 7 Feb 2018 19:21:13 +0000 Subject: [PATCH] Focus camera on doors, etc., that are opened when offscreen. --- src/world/camera.c | 7 ++++++- src/world/entities.c | 2 ++ src/world/world.c | 46 +++++++++++++++++++++++++++++--------------- src/world/world.h | 1 + 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/src/world/camera.c b/src/world/camera.c index 2e3d4d4..fe93da4 100644 --- a/src/world/camera.c +++ b/src/world/camera.c @@ -90,5 +90,10 @@ void clipCamera(void) int isOnScreen(Entity *e) { - return (e->x >= camera.x && e->y >= camera.y && e->x < camera.x + SCREEN_WIDTH && e->y < camera.y + SCREEN_HEIGHT); + int x, y; + + x = (-camera.x + e->x); + y = (-camera.y + e->y); + + return (x >= 0 && y >= 0 && x - e->w < SCREEN_WIDTH && y - e->h < SCREEN_HEIGHT); } diff --git a/src/world/entities.c b/src/world/entities.c index 929c08d..4dbcc75 100644 --- a/src/world/entities.c +++ b/src/world/entities.c @@ -129,6 +129,8 @@ void doEntities(void) } self->tick(); + + self->observationTime = limit(self->observationTime - 1, 0, FPS * 5); self->isOnGround = 0; diff --git a/src/world/world.c b/src/world/world.c index 35eb9c9..df54409 100644 --- a/src/world/world.c +++ b/src/world/world.c @@ -36,6 +36,7 @@ static int canAdd(Unit *u, int mx, int my); void startMission(void); static Texture *background; +static int observationIndex; void initWorld(void) { @@ -61,6 +62,8 @@ void initWorld(void) world.state = WS_START; + observationIndex = 0; + if (world.isBossMission) { startMission(); @@ -79,6 +82,8 @@ void initWorld(void) app.delegate.draw = draw; startMission(); + + world.bob->y += MAP_TILE_SIZE * 4; } static void logic(void) @@ -225,9 +230,7 @@ static void doWorldInProgress(void) if (world.observationTimer > 0) { - world.observationTimer--; - - if (world.observationTimer == FPS * 1.5) + if (--world.observationTimer == FPS * 1.5) { world.entityToTrack = world.entitiesToObserve[0]; @@ -240,21 +243,32 @@ static void doWorldObserving(void) { int i; - world.observationTimer--; - - if (world.observationTimer == 0) + cameraTrack(world.entityToTrack); + + if (--world.observationTimer == 0) { - for (i = 0 ; i < MAX_ENTS_TO_OBSERVE ; i++) + if (++observationIndex < MAX_ENTS_TO_OBSERVE && world.entitiesToObserve[observationIndex] != NULL) { - if (world.entitiesToObserve[i]) - { - world.entitiesToObserve[i]->observationTime = SDL_GetTicks() + 5000; - } - } + world.entityToTrack = world.entitiesToObserve[observationIndex]; - memset(world.entitiesToObserve, 0, sizeof(Entity*) * MAX_ENTS_TO_OBSERVE); - world.entityToTrack = (Entity*)world.bob; - world.state = WS_IN_PROGRESS; + world.observationTimer = FPS * 1.5; + } + else + { + for (i = 0 ; i < MAX_ENTS_TO_OBSERVE ; i++) + { + if (world.entitiesToObserve[i] != NULL) + { + world.entitiesToObserve[i]->observationTime = FPS * 5; + } + } + + memset(world.entitiesToObserve, 0, sizeof(Entity*) * MAX_ENTS_TO_OBSERVE); + world.entityToTrack = (Entity*)world.bob; + world.state = WS_IN_PROGRESS; + + observationIndex = 0; + } } } @@ -497,7 +511,7 @@ void observeActivation(Entity *e) { int i; - if (e->observationTime < SDL_GetTicks() && !e->isVisible) + if (e->observationTime == 0 && !isOnScreen(e)) { for (i = 0 ; i < MAX_ENTS_TO_OBSERVE ; i++) { diff --git a/src/world/world.h b/src/world/world.h index 90775e1..c24cde2 100644 --- a/src/world/world.h +++ b/src/world/world.h @@ -60,6 +60,7 @@ extern void initEffects(void); extern void drawParticles(int plane); extern void initItems(void); extern void doPlayer(void); +extern int isOnScreen(Entity *e); extern App app; extern Dev dev;