changeset 273:e28429dbdaaf

adventure: uniform actions
author David Demelier <markand@malikania.fr>
date Sat, 12 Dec 2020 12:31:23 +0100
parents a49ae1b6ea4f
children a9064c09dde6
files libmlk-adventure/adventure/action/chest.h libmlk-adventure/adventure/action/spawner.c libmlk-adventure/adventure/action/spawner.h libmlk-adventure/adventure/action/teleport.c libmlk-adventure/adventure/action/teleport.h libmlk-adventure/adventure/molko.h libmlk-adventure/adventure/state/map.c libmlk-adventure/adventure/trace_hud.h
diffstat 8 files changed, 90 insertions(+), 182 deletions(-) [+]
line wrap: on
line diff
--- a/libmlk-adventure/adventure/action/chest.h	Sat Dec 12 12:16:47 2020 +0100
+++ b/libmlk-adventure/adventure/action/chest.h	Sat Dec 12 12:31:23 2020 +0100
@@ -16,8 +16,8 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#ifndef MOLKO_ADVENTURE_ACTIONS_CHEST_H
-#define MOLKO_ADVENTURE_ACTIONS_CHEST_H
+#ifndef MOLKO_ADVENTURE_ACTION_CHEST_H
+#define MOLKO_ADVENTURE_ACTION_CHEST_H
 
 #include <core/action.h>
 #include <core/animation.h>
@@ -56,4 +56,4 @@
 struct action *
 chest_action(struct chest *c);
 
-#endif /* !MOLKO_ADVENTURE_ACTIONS_CHEST_H */
+#endif /* !MOLKO_ADVENTURE_ACTION_CHEST_H */
--- a/libmlk-adventure/adventure/action/spawner.c	Sat Dec 12 12:16:47 2020 +0100
+++ b/libmlk-adventure/adventure/action/spawner.c	Sat Dec 12 12:31:23 2020 +0100
@@ -28,23 +28,15 @@
 
 #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 spawner *s)
+{
+	unsigned int gap_x = fmax(s->last_x, s->map->player_x) -
+	                     fmin(s->last_x, s->map->player_x);
+	unsigned int gap_y = fmax(s->last_y, s->map->player_y) -
+			     fmin(s->last_y, s->map->player_y);
 
-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);
+	return fmin(s->steps, gap_x + gap_y);
 }
 
 static bool
@@ -52,14 +44,16 @@
 {
 	(void)ticks;
 
-	struct self *self = act->data;
+	struct spawner *s = 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 (s->map->player_movement) {
+		s->steps -= distance(s);
+		s->last_x = s->map->player_x;
+		s->last_y = s->map->player_y;
 
-		if (self->steps == 0) {
+		if (s->steps == 0) {
+			s->steps = util_nrand(s->low, s->high);
+
 			/* TODO: start battle here. */
 			return false;
 		}
@@ -68,28 +62,23 @@
 	return false;
 }
 
-static void
-finish(struct action *act)
+void
+spawner_init(struct spawner *s)
 {
-	free(act->data);
+	assert(s);
+
+	s->last_x = s->map->player_x;
+	s->last_y = s->map->player_y;
+	s->steps = util_nrand(s->low, s->high);
 }
 
 struct action *
-spawner_new(struct map *map, unsigned int low, unsigned int high)
+spawner_new(struct spawner *s)
 {
-	assert(map);
-
-	struct self *self;
+	assert(s);
 
-	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);
+	s->action.data = s;
+	s->action.update = update;
 
-	self->action.data = self;
-	self->action.update = update;
-	self->action.finish = finish;
-
-	return &self->action;
+	return &s->action;
 }
--- a/libmlk-adventure/adventure/action/spawner.h	Sat Dec 12 12:16:47 2020 +0100
+++ b/libmlk-adventure/adventure/action/spawner.h	Sat Dec 12 12:31:23 2020 +0100
@@ -16,10 +16,27 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#ifndef MOLKO_ADVENTURE_ACTIONS_SPAWNER_H
-#define MOLKO_ADVENTURE_ACTIONS_SPAWNER_H
+#ifndef MOLKO_ADVENTURE_ACTION_SPAWNER_H
+#define MOLKO_ADVENTURE_ACTION_SPAWNER_H
+
+#include <core/action.h>
+
+struct map;
+
+struct spawner {
+	struct map *map;
+	unsigned int low;
+	unsigned int high;
+	unsigned int steps;
+	struct action action;
+	int last_x;
+	int last_y;
+};
+
+void
+spawner_init(struct spawner *);
 
 struct action *
-spawner_new(struct map *, unsigned int, unsigned int);
+spawner_action(struct spawner *);
 
-#endif /* !MOLKO_ADVENTURE_ACTIONS_SPAWNER_H */
+#endif /* !MOLKO_ADVENTURE_ACTION_SPAWNER_H */
--- a/libmlk-adventure/adventure/action/teleport.c	Sat Dec 12 12:16:47 2020 +0100
+++ b/libmlk-adventure/adventure/action/teleport.c	Sat Dec 12 12:31:23 2020 +0100
@@ -37,21 +37,6 @@
 
 #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)
 {
@@ -77,7 +62,7 @@
 
 	if (tp->elapsed >= 10) {
 		if (tp->alpha >= 255) {
-			molko_teleport("assets/maps/map-world.map", tp->origin_x, tp->origin_y);
+			molko_teleport(tp->destination, tp->origin_x, tp->origin_y);
 			return true;
 		}
 
@@ -93,7 +78,7 @@
 {
 	(void)ticks;
 
-	struct teleport *tp = act->data;
+	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;
@@ -121,37 +106,17 @@
 static void
 finish(struct action *act)
 {
-	struct teleport *self = act->data;
+	struct teleport *tp = act->data;
 
-	texture_finish(&self->overlay);
+	texture_finish(&tp->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)
+teleport_new(struct teleport *tp)
 {
-	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));
+	assert(tp);
 
 	tp->action.data = tp;
 	tp->action.update = update_touch;
--- a/libmlk-adventure/adventure/action/teleport.h	Sat Dec 12 12:16:47 2020 +0100
+++ b/libmlk-adventure/adventure/action/teleport.h	Sat Dec 12 12:31:23 2020 +0100
@@ -19,14 +19,27 @@
 #ifndef MOLKO_ADVENTURE_ACTIONS_TELEPORT_H
 #define MOLKO_ADVENTURE_ACTIONS_TELEPORT_H
 
+#include <core/action.h>
+#include <core/texture.h>
+
+struct map;
+
+struct teleport {
+	int x;
+	int y;
+	unsigned int w;
+	unsigned int h;
+	struct map *map;
+	const char *destination;
+	int origin_x;
+	int origin_y;
+	struct action action;
+	struct texture overlay;
+	unsigned int elapsed;
+	unsigned int alpha;
+};
+
 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);
+teleport_action(struct teleport *);
 
 #endif /* !MOLKO_ADVENTURE_ACTIONS_TELEPORT_H */
--- a/libmlk-adventure/adventure/molko.h	Sat Dec 12 12:16:47 2020 +0100
+++ b/libmlk-adventure/adventure/molko.h	Sat Dec 12 12:31:23 2020 +0100
@@ -16,8 +16,8 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#ifndef MOLKO_MOLKO_H
-#define MOLKO_MOLKO_H
+#ifndef MOLKO_ADVENTURE_MOLKO_H
+#define MOLKO_ADVENTURE_MOLKO_H
 
 #include <core/game.h>
 #include <core/texture.h>
@@ -48,4 +48,4 @@
 void
 molko_finish(void);
 
-#endif /* !MOLKO_MOLKO_H */
+#endif /* !MOLKO_ADVENTURE_MOLKO_H */
--- a/libmlk-adventure/adventure/state/map.c	Sat Dec 12 12:16:47 2020 +0100
+++ b/libmlk-adventure/adventure/state/map.c	Sat Dec 12 12:31:23 2020 +0100
@@ -49,55 +49,10 @@
 };
 
 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};
-	int origin_x = -1, origin_y = -1;
-
-	sscanf(value, "%127[^|]|%d|%d", name, &origin_x, &origin_y);
-	action_stack_add(&map->astack_par, teleport_new(map, name, x, y, w, h, origin_x, origin_y));
-}
-
-static void
-load_action(struct map *map, int x, int y, int w, int h, const char *value)
-{
-	static const struct {
-		const char *name;
-		void (*load)(struct map *, int, int, int, int, const char *);
-	} table[] = {
-		{ "teleport|",  load_teleport },
-		{ "spawner|",   load_spawner }
-	};
-
-	for (size_t i = 0; i < UTIL_SIZE(table); ++i) {
-		size_t len = strlen(table[i].name);
-
-		if (strncmp(table[i].name, value, len) == 0) {
-			table[i].load(map, x, y, w, h, value + len);
-			break;
-		}
-	}
-}
-
-static void
 start(struct state *state)
 {
 	struct self *self = state->data;
 
-	self->map_file.load_action = load_action;
-
 	if (!map_file_open(&self->map_file, &self->map, molko_path(self->name)))
 		panic();
 
--- a/libmlk-adventure/adventure/trace_hud.h	Sat Dec 12 12:16:47 2020 +0100
+++ b/libmlk-adventure/adventure/trace_hud.h	Sat Dec 12 12:31:23 2020 +0100
@@ -16,61 +16,30 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#ifndef MOLKO_MOLKO_TRACE_HUD_H
-#define MOLKO_MOLKO_TRACE_HUD_H
+#ifndef MOLKO_ADVENTURE_TRACE_HUD_H
+#define MOLKO_ADVENTURE_TRACE_HUD_H
 
-/**
- * \file trace_hud.h
- * \brief On screen hud
- */
+#define TRACE_HUD_TIMEOUT_DEFAULT (3000)
 
 struct theme;
 
-/**
- * \brief Default time to remove messages.
- */
-#define TRACE_HUD_TIMEOUT_DEFAULT (3000)
-
-/**
- * \brief Trace HUD options.
- */
 struct trace_hud {
-	struct theme *theme;	/*!< (+&?) Theme to use. */
-	unsigned int timeout;   /*!< (+) Timeout to remove messages. */
+	struct theme *theme;
+	unsigned int timeout;
 };
 
-/**
- * Global trace HUD options.
- */
 extern struct trace_hud trace_hud;
 
-/**
- * Handler to use as \ref trace_handler.
- *
- * \pre str != NULL
- * \param str the line to store
- */
 void
-trace_hud_handler(const char *str);
+trace_hud_handler(const char *);
 
-/**
- * Update the HUD.
- *
- * \param ticks elapsed milliseconds since last frame.
- */
 void
-trace_hud_update(unsigned int ticks);
+trace_hud_update(unsigned int);
 
-/**
- * Draw the HUD.
- */
 void
 trace_hud_draw(void);
 
-/**
- * Clear the hud.
- */
 void
 trace_hud_clear(void);
 
-#endif /* !MOLKO_MOLKO_TRACE_HUD_H */
+#endif /* !MOLKO_ADVENTURE_TRACE_HUD_H */