# HG changeset patch # User David Demelier # Date 1607771807 -3600 # Node ID a49ae1b6ea4f0c34ebf894cc92cef76e2f766af5 # Parent eadd3dbfa0afb85e43ca392241ec15a195e15073 adventure: don't use plural in directories diff -r eadd3dbfa0af -r a49ae1b6ea4f libmlk-adventure/CMakeLists.txt --- a/libmlk-adventure/CMakeLists.txt Sat Dec 12 12:14:44 2020 +0100 +++ b/libmlk-adventure/CMakeLists.txt Sat Dec 12 12:16:47 2020 +0100 @@ -20,12 +20,12 @@ set( SOURCES - ${libadventure_SOURCE_DIR}/adventure/actions/chest.c - ${libadventure_SOURCE_DIR}/adventure/actions/chest.h - ${libadventure_SOURCE_DIR}/adventure/actions/spawner.c - ${libadventure_SOURCE_DIR}/adventure/actions/spawner.h - ${libadventure_SOURCE_DIR}/adventure/actions/teleport.c - ${libadventure_SOURCE_DIR}/adventure/actions/teleport.h + ${libadventure_SOURCE_DIR}/adventure/action/chest.c + ${libadventure_SOURCE_DIR}/adventure/action/chest.h + ${libadventure_SOURCE_DIR}/adventure/action/spawner.c + ${libadventure_SOURCE_DIR}/adventure/action/spawner.h + ${libadventure_SOURCE_DIR}/adventure/action/teleport.c + ${libadventure_SOURCE_DIR}/adventure/action/teleport.h ${libadventure_SOURCE_DIR}/adventure/adventure_p.h ${libadventure_SOURCE_DIR}/adventure/assets.c ${libadventure_SOURCE_DIR}/adventure/assets.h diff -r eadd3dbfa0af -r a49ae1b6ea4f libmlk-adventure/adventure/action/chest.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libmlk-adventure/adventure/action/chest.c Sat Dec 12 12:16:47 2020 +0100 @@ -0,0 +1,153 @@ +/* + * chest.c -- chest object + * + * Copyright (c) 2020 David Demelier + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "chest.h" + +#define X(c) ((c)->x - ((c)->map)->view_x) +#define Y(c) ((c)->y - ((c)->map)->view_y) +#define W(c) ((c)->animation.sprite->cellw) +#define H(c) ((c)->animation.sprite->cellh) +#define TOLERANCE (10) + +static bool +is_near(const struct chest *c) +{ + const int x = c->x - c->map->player_sprite->cellw - TOLERANCE; + const int y = c->y - c->map->player_sprite->cellh - TOLERANCE; + const unsigned int w = W(c) + c->map->player_sprite->cellw + (TOLERANCE * 2); + const unsigned int h = H(c) + c->map->player_sprite->cellh + (TOLERANCE * 2); + + return maths_is_boxed(x, y, w, h, c->map->player_x, c->map->player_y); +} + +static void +invoke(struct chest *c) +{ + c->state = CHEST_STATE_ANIMATE; + + animation_start(&c->animation); + + if (c->sound) + sound_play(c->sound, -1, 0); +} + +static void +handle(struct action *act, const union event *ev) +{ + struct chest *c = act->data; + + if (!is_near(c) || c->state != CHEST_STATE_CLOSED) + return; + + switch (ev->type) { + case EVENT_KEYDOWN: + if (ev->key.key == KEY_ENTER) + invoke(c); + break; + case EVENT_CLICKDOWN: + if (maths_is_boxed(X(c), Y(c), W(c), H(c), ev->click.x, ev->click.y)) + invoke(c); + break; + default: + break; + } +} + +static bool +update(struct action *act, unsigned int ticks) +{ + struct chest *c = act->data; + + if (c->state != CHEST_STATE_ANIMATE) + return false; + + if (animation_update(&c->animation, ticks)) { + c->state = CHEST_STATE_OPEN; + + if (c->exec) + c->exec(c); + } + + return false; +} + +static void +draw(struct action *act) +{ + struct chest *c = act->data; + + switch (c->state) { + case CHEST_STATE_OPEN: + sprite_draw( + c->animation.sprite, + c->animation.sprite->nrows - 1, + c->animation.sprite->ncols - 1, + X(c), + Y(c) + ); + break; + case CHEST_STATE_ANIMATE: + animation_draw(&c->animation, X(c), Y(c)); + break; + default: + sprite_draw(c->animation.sprite, 0, 0, X(c), Y(c)); + break; + } +} + +void +chest_init(struct chest *c) +{ + assert(c); + + if (c->save && c->property) { + if (!save_get_property(c->save, c->property)) + panic(); + + /* TODO: add an utility. */ + if (strcmp(c->property->value, "true") == 0) + c->state = CHEST_STATE_OPEN; + } +} + +struct action * +chest_action(struct chest *c) +{ + assert(c); + + c->action.data = c; + c->action.handle = handle; + c->action.update = update; + c->action.draw = draw; + + return &c->action; +} diff -r eadd3dbfa0af -r a49ae1b6ea4f libmlk-adventure/adventure/action/chest.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libmlk-adventure/adventure/action/chest.h Sat Dec 12 12:16:47 2020 +0100 @@ -0,0 +1,59 @@ +/* + * chest.h -- chest object + * + * Copyright (c) 2020 David Demelier + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef MOLKO_ADVENTURE_ACTIONS_CHEST_H +#define MOLKO_ADVENTURE_ACTIONS_CHEST_H + +#include +#include + +struct map; +struct sound; + +enum chest_state { + CHEST_STATE_CLOSED, + CHEST_STATE_ANIMATE, + CHEST_STATE_OPEN +}; + +struct chest { + /* Mandatory. */ + int x; + int y; + struct map *map; + struct animation animation; + + /* Defaulted. */ + enum chest_state state; + struct action action; + + /* Optional. */ + struct save *save; + struct save_property *property; + struct sound *sound; + void *data; + void (*exec)(struct chest *); +}; + +void +chest_init(struct chest *c); + +struct action * +chest_action(struct chest *c); + +#endif /* !MOLKO_ADVENTURE_ACTIONS_CHEST_H */ diff -r eadd3dbfa0af -r a49ae1b6ea4f libmlk-adventure/adventure/action/spawner.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libmlk-adventure/adventure/action/spawner.c Sat Dec 12 12:16:47 2020 +0100 @@ -0,0 +1,95 @@ +/* + * spawner.c -- spawn battle while moving + * + * Copyright (c) 2020 David Demelier + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include + +#include +#include +#include + +#include + +#include "spawner.h" + +struct self { + struct action action; + struct map *map; + int last_x; + int last_y; + unsigned int steps; +}; + +static inline unsigned int +distance(const struct self *self) +{ + unsigned int gap_x = fmax(self->last_x, self->map->player_x) - + fmin(self->last_x, self->map->player_x); + unsigned int gap_y = fmax(self->last_y, self->map->player_y) - + fmin(self->last_y, self->map->player_y); + + return fmin(self->steps, gap_x + gap_y); +} + +static bool +update(struct action *act, unsigned int ticks) +{ + (void)ticks; + + struct self *self = act->data; + + if (self->map->player_movement) { + self->steps -= distance(self); + self->last_x = self->map->player_x; + self->last_y = self->map->player_y; + + if (self->steps == 0) { + /* TODO: start battle here. */ + return false; + } + } + + return false; +} + +static void +finish(struct action *act) +{ + free(act->data); +} + +struct action * +spawner_new(struct map *map, unsigned int low, unsigned int high) +{ + assert(map); + + struct self *self; + + self = alloc_new0(sizeof (*self)); + self->map = map; + self->last_x = map->player_x; + self->last_y = map->player_y; + self->steps = util_nrand(low, high); + + self->action.data = self; + self->action.update = update; + self->action.finish = finish; + + return &self->action; +} diff -r eadd3dbfa0af -r a49ae1b6ea4f libmlk-adventure/adventure/action/spawner.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libmlk-adventure/adventure/action/spawner.h Sat Dec 12 12:16:47 2020 +0100 @@ -0,0 +1,25 @@ +/* + * spawner.h -- spawn battle while moving + * + * Copyright (c) 2020 David Demelier + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef MOLKO_ADVENTURE_ACTIONS_SPAWNER_H +#define MOLKO_ADVENTURE_ACTIONS_SPAWNER_H + +struct action * +spawner_new(struct map *, unsigned int, unsigned int); + +#endif /* !MOLKO_ADVENTURE_ACTIONS_SPAWNER_H */ diff -r eadd3dbfa0af -r a49ae1b6ea4f libmlk-adventure/adventure/action/teleport.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libmlk-adventure/adventure/action/teleport.c Sat Dec 12 12:16:47 2020 +0100 @@ -0,0 +1,161 @@ +/* + * teleport.c -- teleport contact + * + * Copyright (c) 2020 David Demelier + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +#include "teleport.h" + +struct teleport { + struct action action; + struct texture overlay; + struct map *map; + char destination[FILENAME_MAX]; + unsigned int elapsed; + unsigned int alpha; + int origin_x; + int origin_y; + int x; + int y; + unsigned int w; + unsigned int h; +}; + +static void +draw(struct action *act) +{ + struct teleport *tp = act->data; + + texture_set_blend_mode(&tp->overlay, TEXTURE_BLEND_BLEND); + texture_set_alpha_mod(&tp->overlay, tp->alpha); + + PAINTER_BEGIN(&tp->overlay); + painter_set_color(0x000000ff); + painter_clear(); + PAINTER_END(); + + texture_draw(&tp->overlay, 0, 0); +} + +static bool +update_fadeout(struct action *act, unsigned int ticks) +{ + struct teleport *tp = act->data; + + tp->elapsed += ticks; + + if (tp->elapsed >= 10) { + if (tp->alpha >= 255) { + molko_teleport("assets/maps/map-world.map", tp->origin_x, tp->origin_y); + return true; + } + + tp->elapsed = 0; + tp->alpha += 5; + } + + return false; +} + +static bool +update_touch(struct action *act, unsigned int ticks) +{ + (void)ticks; + + struct teleport *tp = act->data; + const int x = tp->x - tp->map->player_sprite->cellw; + const int y = tp->y - tp->map->player_sprite->cellh; + const unsigned int w = tp->w + tp->map->player_sprite->cellw; + const unsigned int h = tp->h + tp->map->player_sprite->cellh; + + if (maths_is_boxed(x, y, w, h, tp->map->player_x, tp->map->player_y)) { + /* Stop movement and disable input. */ + tp->map->player_movement = 0; + game.inhibit = INHIBIT_STATE_INPUT; + + /* + * We change our update function and add a draw function that + * fade the screen out. + */ + if (!texture_new(&tp->overlay, window.w, window.h)) + panic(); + + act->update = update_fadeout; + act->draw = draw; + } + + return false; +} + +static void +finish(struct action *act) +{ + struct teleport *self = act->data; + + texture_finish(&self->overlay); + + free(act->data); +} + +struct action * +teleport_new(struct map *map, + const char *destination, + int x, + int y, + unsigned int w, + unsigned int h, + int origin_x, + int origin_y) +{ + assert(map); + assert(destination); + + struct teleport *tp; + + tp = alloc_new0(sizeof (*tp)); + tp->map = map; + tp->x = x; + tp->y = y; + tp->w = w; + tp->h = h; + tp->origin_x = origin_x; + tp->origin_y = origin_y; + strlcpy(tp->destination, destination, sizeof (tp->destination)); + + tp->action.data = tp; + tp->action.update = update_touch; + tp->action.finish = finish; + + return &tp->action; +} diff -r eadd3dbfa0af -r a49ae1b6ea4f libmlk-adventure/adventure/action/teleport.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libmlk-adventure/adventure/action/teleport.h Sat Dec 12 12:16:47 2020 +0100 @@ -0,0 +1,32 @@ +/* + * teleport.h -- teleport contact + * + * Copyright (c) 2020 David Demelier + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef MOLKO_ADVENTURE_ACTIONS_TELEPORT_H +#define MOLKO_ADVENTURE_ACTIONS_TELEPORT_H + +struct action * +teleport_new(struct map *map, + const char *destination, + int x, + int y, + unsigned int w, + unsigned int h, + int origin_x, + int origin_y); + +#endif /* !MOLKO_ADVENTURE_ACTIONS_TELEPORT_H */ diff -r eadd3dbfa0af -r a49ae1b6ea4f libmlk-adventure/adventure/actions/chest.c --- a/libmlk-adventure/adventure/actions/chest.c Sat Dec 12 12:14:44 2020 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,153 +0,0 @@ -/* - * chest.c -- chest object - * - * Copyright (c) 2020 David Demelier - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "chest.h" - -#define X(c) ((c)->x - ((c)->map)->view_x) -#define Y(c) ((c)->y - ((c)->map)->view_y) -#define W(c) ((c)->animation.sprite->cellw) -#define H(c) ((c)->animation.sprite->cellh) -#define TOLERANCE (10) - -static bool -is_near(const struct chest *c) -{ - const int x = c->x - c->map->player_sprite->cellw - TOLERANCE; - const int y = c->y - c->map->player_sprite->cellh - TOLERANCE; - const unsigned int w = W(c) + c->map->player_sprite->cellw + (TOLERANCE * 2); - const unsigned int h = H(c) + c->map->player_sprite->cellh + (TOLERANCE * 2); - - return maths_is_boxed(x, y, w, h, c->map->player_x, c->map->player_y); -} - -static void -invoke(struct chest *c) -{ - c->state = CHEST_STATE_ANIMATE; - - animation_start(&c->animation); - - if (c->sound) - sound_play(c->sound, -1, 0); -} - -static void -handle(struct action *act, const union event *ev) -{ - struct chest *c = act->data; - - if (!is_near(c) || c->state != CHEST_STATE_CLOSED) - return; - - switch (ev->type) { - case EVENT_KEYDOWN: - if (ev->key.key == KEY_ENTER) - invoke(c); - break; - case EVENT_CLICKDOWN: - if (maths_is_boxed(X(c), Y(c), W(c), H(c), ev->click.x, ev->click.y)) - invoke(c); - break; - default: - break; - } -} - -static bool -update(struct action *act, unsigned int ticks) -{ - struct chest *c = act->data; - - if (c->state != CHEST_STATE_ANIMATE) - return false; - - if (animation_update(&c->animation, ticks)) { - c->state = CHEST_STATE_OPEN; - - if (c->exec) - c->exec(c); - } - - return false; -} - -static void -draw(struct action *act) -{ - struct chest *c = act->data; - - switch (c->state) { - case CHEST_STATE_OPEN: - sprite_draw( - c->animation.sprite, - c->animation.sprite->nrows - 1, - c->animation.sprite->ncols - 1, - X(c), - Y(c) - ); - break; - case CHEST_STATE_ANIMATE: - animation_draw(&c->animation, X(c), Y(c)); - break; - default: - sprite_draw(c->animation.sprite, 0, 0, X(c), Y(c)); - break; - } -} - -void -chest_init(struct chest *c) -{ - assert(c); - - if (c->save && c->property) { - if (!save_get_property(c->save, c->property)) - panic(); - - /* TODO: add an utility. */ - if (strcmp(c->property->value, "true") == 0) - c->state = CHEST_STATE_OPEN; - } -} - -struct action * -chest_action(struct chest *c) -{ - assert(c); - - c->action.data = c; - c->action.handle = handle; - c->action.update = update; - c->action.draw = draw; - - return &c->action; -} diff -r eadd3dbfa0af -r a49ae1b6ea4f libmlk-adventure/adventure/actions/chest.h --- a/libmlk-adventure/adventure/actions/chest.h Sat Dec 12 12:14:44 2020 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,59 +0,0 @@ -/* - * chest.h -- chest object - * - * Copyright (c) 2020 David Demelier - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef MOLKO_ADVENTURE_ACTIONS_CHEST_H -#define MOLKO_ADVENTURE_ACTIONS_CHEST_H - -#include -#include - -struct map; -struct sound; - -enum chest_state { - CHEST_STATE_CLOSED, - CHEST_STATE_ANIMATE, - CHEST_STATE_OPEN -}; - -struct chest { - /* Mandatory. */ - int x; - int y; - struct map *map; - struct animation animation; - - /* Defaulted. */ - enum chest_state state; - struct action action; - - /* Optional. */ - struct save *save; - struct save_property *property; - struct sound *sound; - void *data; - void (*exec)(struct chest *); -}; - -void -chest_init(struct chest *c); - -struct action * -chest_action(struct chest *c); - -#endif /* !MOLKO_ADVENTURE_ACTIONS_CHEST_H */ diff -r eadd3dbfa0af -r a49ae1b6ea4f libmlk-adventure/adventure/actions/spawner.c --- a/libmlk-adventure/adventure/actions/spawner.c Sat Dec 12 12:14:44 2020 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,95 +0,0 @@ -/* - * spawner.c -- spawn battle while moving - * - * Copyright (c) 2020 David Demelier - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include -#include -#include - -#include -#include -#include - -#include - -#include "spawner.h" - -struct self { - struct action action; - struct map *map; - int last_x; - int last_y; - unsigned int steps; -}; - -static inline unsigned int -distance(const struct self *self) -{ - unsigned int gap_x = fmax(self->last_x, self->map->player_x) - - fmin(self->last_x, self->map->player_x); - unsigned int gap_y = fmax(self->last_y, self->map->player_y) - - fmin(self->last_y, self->map->player_y); - - return fmin(self->steps, gap_x + gap_y); -} - -static bool -update(struct action *act, unsigned int ticks) -{ - (void)ticks; - - struct self *self = act->data; - - if (self->map->player_movement) { - self->steps -= distance(self); - self->last_x = self->map->player_x; - self->last_y = self->map->player_y; - - if (self->steps == 0) { - /* TODO: start battle here. */ - return false; - } - } - - return false; -} - -static void -finish(struct action *act) -{ - free(act->data); -} - -struct action * -spawner_new(struct map *map, unsigned int low, unsigned int high) -{ - assert(map); - - struct self *self; - - self = alloc_new0(sizeof (*self)); - self->map = map; - self->last_x = map->player_x; - self->last_y = map->player_y; - self->steps = util_nrand(low, high); - - self->action.data = self; - self->action.update = update; - self->action.finish = finish; - - return &self->action; -} diff -r eadd3dbfa0af -r a49ae1b6ea4f libmlk-adventure/adventure/actions/spawner.h --- a/libmlk-adventure/adventure/actions/spawner.h Sat Dec 12 12:14:44 2020 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -/* - * spawner.h -- spawn battle while moving - * - * Copyright (c) 2020 David Demelier - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef MOLKO_ADVENTURE_ACTIONS_SPAWNER_H -#define MOLKO_ADVENTURE_ACTIONS_SPAWNER_H - -struct action * -spawner_new(struct map *, unsigned int, unsigned int); - -#endif /* !MOLKO_ADVENTURE_ACTIONS_SPAWNER_H */ diff -r eadd3dbfa0af -r a49ae1b6ea4f libmlk-adventure/adventure/actions/teleport.c --- a/libmlk-adventure/adventure/actions/teleport.c Sat Dec 12 12:14:44 2020 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,161 +0,0 @@ -/* - * teleport.c -- teleport contact - * - * Copyright (c) 2020 David Demelier - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include - -#include - -#include "teleport.h" - -struct teleport { - struct action action; - struct texture overlay; - struct map *map; - char destination[FILENAME_MAX]; - unsigned int elapsed; - unsigned int alpha; - int origin_x; - int origin_y; - int x; - int y; - unsigned int w; - unsigned int h; -}; - -static void -draw(struct action *act) -{ - struct teleport *tp = act->data; - - texture_set_blend_mode(&tp->overlay, TEXTURE_BLEND_BLEND); - texture_set_alpha_mod(&tp->overlay, tp->alpha); - - PAINTER_BEGIN(&tp->overlay); - painter_set_color(0x000000ff); - painter_clear(); - PAINTER_END(); - - texture_draw(&tp->overlay, 0, 0); -} - -static bool -update_fadeout(struct action *act, unsigned int ticks) -{ - struct teleport *tp = act->data; - - tp->elapsed += ticks; - - if (tp->elapsed >= 10) { - if (tp->alpha >= 255) { - molko_teleport("assets/maps/map-world.map", tp->origin_x, tp->origin_y); - return true; - } - - tp->elapsed = 0; - tp->alpha += 5; - } - - return false; -} - -static bool -update_touch(struct action *act, unsigned int ticks) -{ - (void)ticks; - - struct teleport *tp = act->data; - const int x = tp->x - tp->map->player_sprite->cellw; - const int y = tp->y - tp->map->player_sprite->cellh; - const unsigned int w = tp->w + tp->map->player_sprite->cellw; - const unsigned int h = tp->h + tp->map->player_sprite->cellh; - - if (maths_is_boxed(x, y, w, h, tp->map->player_x, tp->map->player_y)) { - /* Stop movement and disable input. */ - tp->map->player_movement = 0; - game.inhibit = INHIBIT_STATE_INPUT; - - /* - * We change our update function and add a draw function that - * fade the screen out. - */ - if (!texture_new(&tp->overlay, window.w, window.h)) - panic(); - - act->update = update_fadeout; - act->draw = draw; - } - - return false; -} - -static void -finish(struct action *act) -{ - struct teleport *self = act->data; - - texture_finish(&self->overlay); - - free(act->data); -} - -struct action * -teleport_new(struct map *map, - const char *destination, - int x, - int y, - unsigned int w, - unsigned int h, - int origin_x, - int origin_y) -{ - assert(map); - assert(destination); - - struct teleport *tp; - - tp = alloc_new0(sizeof (*tp)); - tp->map = map; - tp->x = x; - tp->y = y; - tp->w = w; - tp->h = h; - tp->origin_x = origin_x; - tp->origin_y = origin_y; - strlcpy(tp->destination, destination, sizeof (tp->destination)); - - tp->action.data = tp; - tp->action.update = update_touch; - tp->action.finish = finish; - - return &tp->action; -} diff -r eadd3dbfa0af -r a49ae1b6ea4f libmlk-adventure/adventure/actions/teleport.h --- a/libmlk-adventure/adventure/actions/teleport.h Sat Dec 12 12:14:44 2020 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,32 +0,0 @@ -/* - * teleport.h -- teleport contact - * - * Copyright (c) 2020 David Demelier - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef MOLKO_ADVENTURE_ACTIONS_TELEPORT_H -#define MOLKO_ADVENTURE_ACTIONS_TELEPORT_H - -struct action * -teleport_new(struct map *map, - const char *destination, - int x, - int y, - unsigned int w, - unsigned int h, - int origin_x, - int origin_y); - -#endif /* !MOLKO_ADVENTURE_ACTIONS_TELEPORT_H */ diff -r eadd3dbfa0af -r a49ae1b6ea4f libmlk-adventure/adventure/state/map.c --- a/libmlk-adventure/adventure/state/map.c Sat Dec 12 12:14:44 2020 +0100 +++ b/libmlk-adventure/adventure/state/map.c Sat Dec 12 12:16:47 2020 +0100 @@ -32,8 +32,8 @@ #include #include -#include -#include +#include +#include #include