Mercurial > molko
changeset 260:60a214ec1ab4
adventure: change the way states are stored
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 06 Dec 2020 22:50:48 +0100 |
parents | 16be1ad3ddba |
children | bfde372bf152 |
files | libmlk-adventure/CMakeLists.txt libmlk-adventure/adventure/actions/spawner.c libmlk-adventure/adventure/actions/spawner.h libmlk-adventure/adventure/actions/teleport.c libmlk-adventure/adventure/actions/teleport.h libmlk-adventure/adventure/state/map.c libmlk-adventure/assets/maps/map-world.json libmlk-core/core/game.c libmlk-core/core/game.h |
diffstat | 9 files changed, 189 insertions(+), 22 deletions(-) [+] |
line wrap: on
line diff
--- a/libmlk-adventure/CMakeLists.txt Sun Dec 06 11:22:03 2020 +0100 +++ b/libmlk-adventure/CMakeLists.txt Sun Dec 06 22:50:48 2020 +0100 @@ -20,6 +20,8 @@ set( SOURCES + ${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/adventure_p.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libmlk-adventure/adventure/actions/spawner.c Sun Dec 06 22:50:48 2020 +0100 @@ -0,0 +1,95 @@ +/* + * spawner.c -- spawn battle while moving + * + * Copyright (c) 2020 David Demelier <markand@malikania.fr> + * + * 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 <assert.h> +#include <math.h> +#include <stdlib.h> + +#include <core/alloc.h> +#include <core/game.h> +#include <core/util.h> + +#include <rpg/map.h> + +#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 = nrand(low, high); + + self->action.data = self; + self->action.update = update; + self->action.finish = finish; + + return &self->action; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libmlk-adventure/adventure/actions/spawner.h Sun Dec 06 22:50:48 2020 +0100 @@ -0,0 +1,25 @@ +/* + * spawner.h -- spawn battle while moving + * + * Copyright (c) 2020 David Demelier <markand@malikania.fr> + * + * 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 */
--- a/libmlk-adventure/adventure/actions/teleport.c Sun Dec 06 11:22:03 2020 +0100 +++ b/libmlk-adventure/adventure/actions/teleport.c Sun Dec 06 22:50:48 2020 +0100 @@ -19,9 +19,11 @@ #include <compat.h> #include <assert.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> +#include <core/action.h> #include <core/alloc.h> #include <core/maths.h> #include <core/painter.h> @@ -34,6 +36,21 @@ #include "molko.h" #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) {
--- a/libmlk-adventure/adventure/actions/teleport.h Sun Dec 06 11:22:03 2020 +0100 +++ b/libmlk-adventure/adventure/actions/teleport.h Sun Dec 06 22:50:48 2020 +0100 @@ -19,26 +19,6 @@ #ifndef MOLKO_ADVENTURE_ACTIONS_TELEPORT_H #define MOLKO_ADVENTURE_ACTIONS_TELEPORT_H -#include <stdio.h> - -#include <core/action.h> -#include <core/texture.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; -}; - struct action * teleport_new(struct map *map, const char *destination,
--- a/libmlk-adventure/adventure/state/map.c Sun Dec 06 11:22:03 2020 +0100 +++ b/libmlk-adventure/adventure/state/map.c Sun Dec 06 22:50:48 2020 +0100 @@ -31,6 +31,7 @@ #include <rpg/map.h> #include <rpg/map-file.h> +#include <adventure/actions/spawner.h> #include <adventure/actions/teleport.h> #include "molko.h" @@ -46,6 +47,18 @@ }; static void +load_spawner(struct map *map, int x, int y, int w, int h, const char *value) +{ + (void)x; + (void)y; + (void)w; + (void)h; + (void)value; + + action_stack_add(&map->astack_par, spawner_new(map, 100, 300)); +} + +static void load_teleport(struct map *map, int x, int y, int w, int h, const char *value) { char name[128] = {0}; @@ -62,7 +75,8 @@ const char *name; void (*load)(struct map *, int, int, int, int, const char *); } table[] = { - { "teleport|", load_teleport } + { "teleport|", load_teleport }, + { "spawner|", load_spawner } }; for (size_t i = 0; i < NELEM(table); ++i) {
--- a/libmlk-adventure/assets/maps/map-world.json Sun Dec 06 11:22:03 2020 +0100 +++ b/libmlk-adventure/assets/maps/map-world.json Sun Dec 06 22:50:48 2020 +0100 @@ -54,6 +54,23 @@ "width":48, "x":4272, "y":864 + }, + { + "height":108.722510501606, + "id":3, + "name":"", + "properties":[ + { + "name":"exec", + "type":"string", + "value":"spawner|" + }], + "rotation":0, + "type":"", + "visible":true, + "width":233.506300963677, + "x":3973.31356560415, + "y":447.244872745243 }], "opacity":1, "type":"objectgroup", @@ -62,7 +79,7 @@ "y":0 }], "nextlayerid":9, - "nextobjectid":3, + "nextobjectid":4, "orientation":"orthogonal", "properties":[ {
--- a/libmlk-core/core/game.c Sun Dec 06 11:22:03 2020 +0100 +++ b/libmlk-core/core/game.c Sun Dec 06 22:50:48 2020 +0100 @@ -51,6 +51,20 @@ game.state_next = state; } +struct state * +game_replace(struct state *state) +{ + assert(state); + + struct state *save = game.state; + + game.state = state; + + state_start(state); + + return save; +} + void game_handle(const union event *ev) {