diff --git a/include/dawn/dawn.h b/include/dawn/dawn.h index b30d82b4..6464348f 100644 --- a/include/dawn/dawn.h +++ b/include/dawn/dawn.h @@ -6,10 +6,9 @@ #pragma once #include "libs.h" -// Animation -#include "animation/easing.h" - // Display / Rendering +#include "display/animation/easing.h" +#include "display/animation/timeline.h" #include "display/debug/grid.h" #include "display/gui/bitmapfont.h" diff --git a/include/dawn/animation/easing.h b/include/dawn/display/animation/easing.h similarity index 98% rename from include/dawn/animation/easing.h rename to include/dawn/display/animation/easing.h index b5df18cf..53b89901 100644 --- a/include/dawn/animation/easing.h +++ b/include/dawn/display/animation/easing.h @@ -5,7 +5,6 @@ * https://opensource.org/licenses/MIT */ #pragma once -#include "../libs.h" /** * Returns the ease time for a given real time duration span. diff --git a/include/dawn/display/animation/timeline.h b/include/dawn/display/animation/timeline.h new file mode 100644 index 00000000..dce4ad0d --- /dev/null +++ b/include/dawn/display/animation/timeline.h @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "../../libs.h" + +/** Type forwarder for timeline_t */ +typedef struct _timeline_t timeline_t; + +/** Callback for when a timeline event occurs */ +typedef void timelinecallback_t(timeline_t*); + +typedef struct test_t { + /** + * The time that this action should occur within the timeline + * set to 0 or less to start immediately. + */ + float start; + + /** + * The duration of the action, this will decide for how long onDuration will + * be called for, and when onEnd should be called. + * Set to a negative number to have this continue forever. + * Set to 0 to only fire the onStart. + * onStart can fire with either onDuration and onEnd on the same frame, but + * onEnd and onDuration cannot fire the same frame. + */ + float duration; + + timelinecallback_t *onStart; + timelinecallback_t *onDuration; + timelinecallback_t *onEnd; +} timelineaction_t; + +typedef struct _timeline_t { + /** The current time as far as the timeline is concerned */ + float current; + + /** The time of the last "frame" as far as the timeline is concerned */ + float previous; + + /** The frame time diff, essentially current = previous + diff */ + float diff; + + /** User pointer, allows you to point to some other data */ + void *user; + + /** Actions within the timeline */ + timelineaction_t actions[128]; + uint8_t actionCount; +} timeline_t; \ No newline at end of file diff --git a/src/display/animation/timeline.c b/src/display/animation/timeline.c new file mode 100644 index 00000000..95771b40 --- /dev/null +++ b/src/display/animation/timeline.c @@ -0,0 +1,79 @@ +/** + * Copyright (c) 2021 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "timeline.h" + +void onDone(timeline_t *test) { +} + +void timelineInit(timeline_t *timeline) { + timeline->current = 0; + timeline->diff = 0; + timeline->previous = 0; + timeline->user = 0; + timeline->actionCount = 0; + + timeline->actions[0].onStart = &onDone; +} + +void timelineUpdate(timeline_t *timeline, float delta) { + uint8_t i; + timelineaction_t *action; + float full; + + timeline->diff = delta; + timeline->previous = timeline->current; + timeline->current = timeline->current + delta; + + // Find all actions that would have started or ended in this timespan. + for(i = 0; i < timeline->actionCount; i++) { + action = timeline->actions +i; + + // Has the action started yet? + if(action->start > timeline->current) continue; + + // Did we start this frame? + if(action->start > timeline->previous && action->onStart != NULL) { + action->onStart(timeline); + } + + // Durations of 0 only fire starts, never ends or durations. + if(action->duration == 0) continue; + + // Is the end still in the future? Durations in negatives go forever + full = action->start+action->duration; + if(action->duration < 0 || full > timeline->current) { + if(action->onDuration != NULL) action->onDuration(timeline); + } else if(full > timeline->previous) {// Did we end this frame? + if(action->onEnd != NULL) action->onEnd(timeline); + } + } +} + +bool timelineIsFinished(timeline_t *timeline) { + uint8_t i; + timelineaction_t *action; + + for(i = 0; i < timeline->actionCount; i++) { + action = timeline->actions +i; + if(action->start > timeline->current) return false; + if(action->duration < 0) return false; + if(action->duration == 0) continue; + if(action->start+action->duration > timeline->current) return false; + } + + return true; +} + +timelineaction_t * timelineAddAction(timeline_t *timeline, float start, + float duration +) { + timelineaction_t *action = timeline->actions + (timeline->actionCount++); + action->start = start, action->duration = duration; + action->onStart = action->onEnd = action->onDuration = NULL; + return action; +} \ No newline at end of file diff --git a/src/display/animation/timeline.h b/src/display/animation/timeline.h new file mode 100644 index 00000000..a355bcdc --- /dev/null +++ b/src/display/animation/timeline.h @@ -0,0 +1,15 @@ +// Copyright (c) 2021 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include + +void timelineInit(timeline_t *timeline); +void timelineUpdate(timeline_t *timeline, float delta); +bool timelineIsFinished(timeline_t *timeline); + +timelineaction_t * timelineAddAction(timeline_t *timeline, float start, + float duration +); \ No newline at end of file diff --git a/src/game/game.c b/src/game/game.c index 2fd2575c..b2ce24e2 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -7,15 +7,35 @@ #include "game.h" +timeline_t TIMELINE_TEST; + +void onStart(timeline_t *tl) { + printf("Action started %f\n", tl->current); +} + +void onDuration(timeline_t *tl) { + printf("Action duration %f\n", tl->current); +} + +void onEnd(timeline_t *tl) { + printf("Action ended %f\n", tl->current); +} + bool gameInit(game_t *game) { // Init the game game->name = GAME_NAME; // Init the engine and the rendering pipeline engineInit(&game->engine, game); + + timelineInit(&TIMELINE_TEST); + timelineaction_t *action = timelineAddAction(&TIMELINE_TEST, 1, 1); + action->onStart = &onStart; + action->onDuration = &onDuration; + action->onEnd = &onEnd; // Hand off to the poker logic. - pokerInit(&game->poker, &game->engine); + // pokerInit(&game->poker, &game->engine); return true; } @@ -25,7 +45,14 @@ bool gameUpdate(game_t *game, float platformDelta) { engineUpdateStart(&game->engine, game, platformDelta); // Hand off to the poker logic - pokerUpdate(&game->poker, &game->engine); + // pokerUpdate(&game->poker, &game->engine); + + timelineUpdate(&TIMELINE_TEST, platformDelta); + if(timelineIsFinished(&TIMELINE_TEST)) { + printf("Timeline finished\n"); + } else { + printf("Timeline not finished\n"); + } // Hand back to the engine. return engineUpdateEnd(&game->engine, game); diff --git a/src/game/game.h b/src/game/game.h index 631f1101..f8597832 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -9,7 +9,7 @@ #include "../poker/poker.h" #include "../poker/card.h" -#include "../util/array.h" +#include "../display/animation/timeline.h" /** * Initialize the game context. diff --git a/src/poker/round/match.c b/src/poker/round/match.c index 2923c2c9..f4678149 100644 --- a/src/poker/round/match.c +++ b/src/poker/round/match.c @@ -27,8 +27,13 @@ void pokerMatchInit(poker_t *poker, engine_t *engine) { } void pokerMatchUpdate(poker_t *poker, engine_t *engine) { + // Ease into the game. float t = easeTimeToEase(poker->roundMatch.time, engine->time.current, 5); - pokerLookAtPlayer( - &poker->cameraWorld, POKER_SEAT_PLAYER0, 1 - easeOutQuint(t) - ); + pokerLookAtPlayer(&poker->cameraWorld, POKER_SEAT_PLAYER0, ( + t < 1 ? 1 - easeOutQuart(t) : 0 + )); + + if(t > 0.75) { + pokerTalk(poker, "Hello World"); + } } \ No newline at end of file