changeset 383:b944cd41e8f9

rpg: do the same for battle entities
author David Demelier <markand@malikania.fr>
date Sun, 13 Feb 2022 11:13:17 +0100
parents 43d155668a55
children c458441ff472
files src/libmlk-rpg/CMakeLists.txt src/libmlk-rpg/rpg/battle-entity-state-attacking.c src/libmlk-rpg/rpg/battle-entity-state-attacking.h src/libmlk-rpg/rpg/battle-entity-state-blinking.c src/libmlk-rpg/rpg/battle-entity-state-blinking.h src/libmlk-rpg/rpg/battle-entity-state-moving.c src/libmlk-rpg/rpg/battle-entity-state-moving.h src/libmlk-rpg/rpg/battle-entity-state-normal.c src/libmlk-rpg/rpg/battle-entity-state-normal.h src/libmlk-rpg/rpg/battle-entity-state.h src/libmlk-rpg/rpg/battle-entity.c src/libmlk-rpg/rpg/battle-state-attacking.c src/libmlk-rpg/rpg/battle-state-item.c
diffstat 13 files changed, 304 insertions(+), 101 deletions(-) [+]
line wrap: on
line diff
--- a/src/libmlk-rpg/CMakeLists.txt	Sun Feb 13 10:35:26 2022 +0100
+++ b/src/libmlk-rpg/CMakeLists.txt	Sun Feb 13 11:13:17 2022 +0100
@@ -23,9 +23,13 @@
 	${libmlk-rpg_SOURCE_DIR}/rpg/battle-bar.c
 	${libmlk-rpg_SOURCE_DIR}/rpg/battle-bar.h
 	${libmlk-rpg_SOURCE_DIR}/rpg/battle-entity-state-attacking.c
+	${libmlk-rpg_SOURCE_DIR}/rpg/battle-entity-state-attacking.h
 	${libmlk-rpg_SOURCE_DIR}/rpg/battle-entity-state-blinking.c
+	${libmlk-rpg_SOURCE_DIR}/rpg/battle-entity-state-blinking.h
 	${libmlk-rpg_SOURCE_DIR}/rpg/battle-entity-state-moving.c
+	${libmlk-rpg_SOURCE_DIR}/rpg/battle-entity-state-moving.h
 	${libmlk-rpg_SOURCE_DIR}/rpg/battle-entity-state-normal.c
+	${libmlk-rpg_SOURCE_DIR}/rpg/battle-entity-state-normal.h
 	${libmlk-rpg_SOURCE_DIR}/rpg/battle-entity-state.c
 	${libmlk-rpg_SOURCE_DIR}/rpg/battle-entity-state.h
 	${libmlk-rpg_SOURCE_DIR}/rpg/battle-entity.c
--- a/src/libmlk-rpg/rpg/battle-entity-state-attacking.c	Sun Feb 13 10:35:26 2022 +0100
+++ b/src/libmlk-rpg/rpg/battle-entity-state-attacking.c	Sun Feb 13 11:13:17 2022 +0100
@@ -26,10 +26,11 @@
 
 #include "battle-entity.h"
 #include "battle-entity-state.h"
+#include "battle-entity-state-attacking.h"
 
-struct data {
+struct self {
+	struct battle_entity_state_attacking data;
 	struct battle_entity_state state;
-	struct animation anim;
 };
 
 static int
@@ -37,17 +38,13 @@
 {
 	(void)et;
 
-	struct data *data = st->data;
-
-	return animation_update(&data->anim, ticks);
+	return battle_entity_state_attacking_update(st->data, ticks);
 }
 
 static void
 draw(const struct battle_entity_state *st, const struct battle_entity *et)
 {
-	const struct data *data = st->data;
-
-	animation_draw(&data->anim, et->x, et->y);
+	battle_entity_state_attacking_draw(st->data, et);
 }
 
 static void
@@ -59,23 +56,46 @@
 }
 
 void
-battle_entity_state_attacking(struct battle_entity *et, struct sprite *which)
+battle_entity_state_attacking_init(struct battle_entity_state_attacking *atk, const struct sprite *which)
+{
+	assert(atk);
+	assert(sprite_ok(which));
+
+	animation_init(&atk->anim, which, 100);
+	animation_start(&atk->anim);
+}
+
+int
+battle_entity_state_attacking_update(struct battle_entity_state_attacking *atk, unsigned int ticks)
+{
+	assert(atk);
+
+	return animation_update(&atk->anim, ticks);
+}
+
+void
+battle_entity_state_attacking_draw(const struct battle_entity_state_attacking *atk, const struct battle_entity *et)
+{
+	assert(atk);
+	assert(battle_entity_ok(et));
+
+	animation_draw(&atk->anim, et->x, et->y);
+}
+
+void
+battle_entity_state_attacking(struct battle_entity *et, const struct sprite *which)
 {
 	assert(battle_entity_ok(et));
 	assert(sprite_ok(which));
 
-	struct data *data;
-
-	if (!(data = alloc_new0(sizeof (*data))))
-		panic();
-
-	animation_init(&data->anim, which, 100);
-	animation_start(&data->anim);
+	struct self *self;
 
-	data->state.data = data;
-	data->state.update = update;
-	data->state.draw = draw;
-	data->state.finish = finish;
+	self = alloc_new0(sizeof (*self));
+	self->state.data = self;
+	self->state.update = update;
+	self->state.draw = draw;
+	self->state.finish = finish;
 
-	battle_entity_switch(et, &data->state);
+	battle_entity_state_attacking_init(&self->data, which);
+	battle_entity_switch(et, &self->state);
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/libmlk-rpg/rpg/battle-entity-state-attacking.h	Sun Feb 13 11:13:17 2022 +0100
@@ -0,0 +1,43 @@
+/*
+ * battle-entity-state-attacking.c -- the entity is attacking
+ *
+ * Copyright (c) 2020-2022 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 MLK_RPG_BATTLE_ENTITY_STATE_ATTACKING_H
+#define MLK_RPG_BATTLE_ENTITY_STATE_ATTACKING_H
+
+#include <core/animation.h>
+
+struct battle_entity;
+struct sprite;
+
+struct battle_entity_state_attacking {
+	struct animation anim;
+};
+
+void
+battle_entity_state_attacking_init(struct battle_entity_state_attacking *, const struct sprite *);
+
+int
+battle_entity_state_attacking_update(struct battle_entity_state_attacking *, unsigned int);
+
+void
+battle_entity_state_attacking_draw(const struct battle_entity_state_attacking *, const struct battle_entity *);
+
+void
+battle_entity_state_attacking(struct battle_entity *, const struct sprite *);
+
+#endif /* !MLK_RPG_BATTLE_ENTITY_STATE_ATTACKING_H */
--- a/src/libmlk-rpg/rpg/battle-entity-state-blinking.c	Sun Feb 13 10:35:26 2022 +0100
+++ b/src/libmlk-rpg/rpg/battle-entity-state-blinking.c	Sun Feb 13 11:13:17 2022 +0100
@@ -26,16 +26,15 @@
 
 #include "battle-entity.h"
 #include "battle-entity-state.h"
+#include "battle-entity-state-blinking.h"
 #include "character.h"
 
 #define TRANSPARENT     (150)
 #define OPAQUE          (255)
 
-struct blink {
+struct self {
+	struct battle_entity_state_blinking data;
 	struct battle_entity_state state;
-	struct texture *tex;
-	unsigned int elapsed;
-	unsigned int count;
 };
 
 static int
@@ -43,7 +42,31 @@
 {
 	(void)et;
 
-	struct blink *blk = st->data;
+	return battle_entity_state_blinking_update(st->data, ticks);
+}
+
+static void
+finish(struct battle_entity_state *st, struct battle_entity *et)
+{
+	(void)et;
+
+	free(st->data);
+}
+
+void
+battle_entity_state_blinking_init(struct battle_entity_state_blinking *blk, struct battle_entity *et)
+{
+	assert(blk);
+	assert(battle_entity_ok(et));
+
+	blk->tex = et->ch->sprites[CHARACTER_SPRITE_NORMAL]->texture;
+	texture_set_alpha_mod(blk->tex, TRANSPARENT);
+}
+
+int
+battle_entity_state_blinking_update(struct battle_entity_state_blinking *blk, unsigned int ticks)
+{
+	assert(blk);
 
 	blk->elapsed += ticks;
 
@@ -57,30 +80,18 @@
 	return blk->count >= 3;
 }
 
-static void
-finish(struct battle_entity_state *st, struct battle_entity *et)
-{
-	(void)et;
-
-	free(st->data);
-}
-
 void
 battle_entity_state_blinking(struct battle_entity *et)
 {
 	assert(et);
 
-	struct blink *blk;
-
-	if (!(blk = alloc_new0(sizeof (*blk))))
-		panic();
+	struct self *self;
 
-	blk->tex = et->ch->sprites[CHARACTER_SPRITE_NORMAL]->texture;
-	texture_set_alpha_mod(blk->tex, TRANSPARENT);
+	self = alloc_new0(sizeof (*self));
+	self->state.data = self;
+	self->state.update = update;
+	self->state.finish = finish;
 
-	blk->state.data = blk;
-	blk->state.update = update;
-	blk->state.finish = finish;
-
-	battle_entity_switch(et, &blk->state);
+	battle_entity_state_blinking_init(&self->data, et);
+	battle_entity_switch(et, &self->state);
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/libmlk-rpg/rpg/battle-entity-state-blinking.h	Sun Feb 13 11:13:17 2022 +0100
@@ -0,0 +1,40 @@
+/*
+ * battle-entity-state-blinking.h -- the entity is blinking
+ *
+ * Copyright (c) 2020-2022 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 MLK_RPG_BATTLE_ENTITY_STATE_BLINKING_H
+#define MLK_RPG_BATTLE_ENTITY_STATE_BLINKING_H
+
+struct battle_entity;
+struct texture;
+
+struct battle_entity_state_blinking {
+	struct texture *tex;
+	unsigned int elapsed;
+	unsigned int count;
+};
+
+void
+battle_entity_state_blinking_init(struct battle_entity_state_blinking *, struct battle_entity *et);
+
+int
+battle_entity_state_blinking_update(struct battle_entity_state_blinking *, unsigned int);
+
+void
+battle_entity_state_blinking(struct battle_entity *);
+
+#endif /* !MLK_RPG_BATTLE_ENTITY_STATE_BLINKING_H */
--- a/src/libmlk-rpg/rpg/battle-entity-state-moving.c	Sun Feb 13 10:35:26 2022 +0100
+++ b/src/libmlk-rpg/rpg/battle-entity-state-moving.c	Sun Feb 13 11:13:17 2022 +0100
@@ -25,6 +25,7 @@
 
 #include "battle-entity.h"
 #include "battle-entity-state.h"
+#include "battle-entity-state-moving.h"
 #include "character.h"
 #include "walksprite.h"
 
@@ -32,52 +33,29 @@
 #define SEC   1000
 #define WALK  40
 
-struct position {
+struct self {
+	struct battle_entity_state_moving data;
 	struct battle_entity_state state;
-	struct walksprite ws;
-
-	/* Destination. */
-	int x;
-	int y;
 };
 
 static inline unsigned int
-orientation(const struct position *pos, const struct battle_entity *et)
+orientation(const struct battle_entity_state_moving *mv, const struct battle_entity *et)
 {
 	/* TODO: support diagonal. */
 	/* See: walksprite definitions. */
-	return pos->x < et->x ? 6 : 2;
+	return mv->x < et->x ? 6 : 2;
 }
 
 static int
 update(struct battle_entity_state *st, struct battle_entity *et, unsigned int ticks)
 {
-	struct position *pos = st->data;
-	int step_x, step_y, delta_x, delta_y;
-
-	delta_x = pos->x < et->x ? -1 : +1;
-	delta_y = pos->y < et->y ? -1 : +1;
-	step_x = fmin(SPEED * ticks / SEC, abs(et->x - pos->x));
-	step_y = fmin(SPEED * ticks / SEC, abs(et->y - pos->y));
-
-	et->x += delta_x * step_x;
-	et->y += delta_y * step_y;
-
-	if (et->x != pos->x || et->y != pos->y)
-		walksprite_update(&pos->ws, ticks);
-	else
-		walksprite_reset(&pos->ws);
-
-	return et->x == pos->x && et->y == pos->y;
+	return battle_entity_state_moving_update(st->data, et, ticks);
 }
 
 static void
 draw(const struct battle_entity_state *st, const struct battle_entity *et)
 {
-	/* TODO: compute orientation. */
-	struct position *pos = st->data;
-
-	walksprite_draw(&pos->ws, orientation(pos, et), et->x, et->y);
+	battle_entity_state_moving_draw(st->data, et);
 }
 
 static void
@@ -89,23 +67,63 @@
 }
 
 void
-battle_entity_state_moving(struct battle_entity *et, int destx, int desty)
+battle_entity_state_moving_init(struct battle_entity_state_moving *mv, struct battle_entity *et, int dstx, int dsty)
+{
+	assert(mv);
+	assert(et);
+
+	walksprite_init(&mv->ws, et->ch->sprites[CHARACTER_SPRITE_NORMAL], 40);
+	mv->x = dstx;
+	mv->y = dsty;
+}
+
+int
+battle_entity_state_moving_update(struct battle_entity_state_moving *mv, struct battle_entity *et, unsigned int ticks)
+{
+	assert(mv);
+	assert(et);
+
+	int step_x, step_y, delta_x, delta_y;
+
+	delta_x = mv->x < et->x ? -1 : +1;
+	delta_y = mv->y < et->y ? -1 : +1;
+	step_x = fmin(SPEED * ticks / SEC, abs(et->x - mv->x));
+	step_y = fmin(SPEED * ticks / SEC, abs(et->y - mv->y));
+
+	et->x += delta_x * step_x;
+	et->y += delta_y * step_y;
+
+	if (et->x != mv->x || et->y != mv->y)
+		walksprite_update(&mv->ws, ticks);
+	else
+		walksprite_reset(&mv->ws);
+
+	return et->x == mv->x && et->y == mv->y;
+}
+
+void
+battle_entity_state_moving_draw(const struct battle_entity_state_moving *mv, const struct battle_entity *et)
+{
+	assert(mv);
+	assert(battle_entity_ok(et));
+
+	/* TODO: compute orientation. */
+	walksprite_draw(&mv->ws, orientation(mv, et), et->x, et->y);
+}
+
+void
+battle_entity_state_moving(struct battle_entity *et, int dstx, int dsty)
 {
 	assert(et);
 
-	struct position *pos;
-
-	if (!(pos = alloc_new0(sizeof (*pos))))
-		panic();
+	struct self *self;
 
-	walksprite_init(&pos->ws, et->ch->sprites[CHARACTER_SPRITE_NORMAL], 40);
-	pos->x = destx;
-	pos->y = desty;
+	self = alloc_new0(sizeof (*self));
+	self->state.data = self;
+	self->state.update = update;
+	self->state.draw = draw;
+	self->state.finish = finish;
 
-	pos->state.data = pos;
-	pos->state.update = update;
-	pos->state.draw = draw;
-	pos->state.finish = finish;
-
-	battle_entity_switch(et, &pos->state);
+	battle_entity_state_moving_init(&self->data, et, dstx, dsty);
+	battle_entity_switch(et, &self->state);
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/libmlk-rpg/rpg/battle-entity-state-moving.h	Sun Feb 13 11:13:17 2022 +0100
@@ -0,0 +1,42 @@
+/*
+ * battle-entity-state-moving.h -- the entity is moving
+ *
+ * Copyright (c) 2020-2022 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 MLK_RPG_BATTLE_ENTITY_STATE_MOVING_H
+#define MLK_RPG_BATTLE_ENTITY_STATE_MOVING_H
+
+#include <rpg/walksprite.h>
+
+struct battle_entity_state_moving {
+	struct walksprite ws;
+	int x;
+	int y;
+};
+
+void
+battle_entity_state_moving_init(struct battle_entity_state_moving *, struct battle_entity *, int, int);
+
+int
+battle_entity_state_moving_update(struct battle_entity_state_moving *, struct battle_entity *, unsigned int);
+
+void
+battle_entity_state_moving_draw(const struct battle_entity_state_moving *, const struct battle_entity *);
+
+void
+battle_entity_state_moving(struct battle_entity *, int, int);
+
+#endif /* !MLK_RPG_BATTLE_ENTITY_STATE_MOVING_H */
--- a/src/libmlk-rpg/rpg/battle-entity-state-normal.c	Sun Feb 13 10:35:26 2022 +0100
+++ b/src/libmlk-rpg/rpg/battle-entity-state-normal.c	Sun Feb 13 11:13:17 2022 +0100
@@ -20,6 +20,9 @@
 
 #include "battle-entity.h"
 #include "battle-entity-state.h"
+#include "battle-entity-state-normal.h"
+
+/* TODO: animate characters when they are inactive. */
 
 void
 battle_entity_state_normal(struct battle_entity *et)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/libmlk-rpg/rpg/battle-entity-state-normal.h	Sun Feb 13 11:13:17 2022 +0100
@@ -0,0 +1,28 @@
+/*
+ * battle-entity-state-normal.h -- the entity is normal
+ *
+ * Copyright (c) 2020-2022 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 MLK_RPG_BATTLE_ENTITY_STATE_NORMAL_H
+#define MLK_RPG_BATTLE_ENTITY_STATE_NORMAL_H
+
+struct battle_entity;
+
+void
+battle_entity_state_normal(struct battle_entity *);
+
+#endif /* !MLK_RPG_BATTLE_ENTITY_STATE_NORMAL_H */
+
--- a/src/libmlk-rpg/rpg/battle-entity-state.h	Sun Feb 13 10:35:26 2022 +0100
+++ b/src/libmlk-rpg/rpg/battle-entity-state.h	Sun Feb 13 11:13:17 2022 +0100
@@ -42,19 +42,6 @@
 void
 battle_entity_state_finish(struct battle_entity_state *, struct battle_entity *);
 
-/* Defined in their own files. */
-void
-battle_entity_state_normal(struct battle_entity *);
-
-void
-battle_entity_state_moving(struct battle_entity *, int, int);
-
-void
-battle_entity_state_blinking(struct battle_entity *);
-
-void
-battle_entity_state_attacking(struct battle_entity *, struct sprite *);
-
 CORE_END_DECLS
 
 #endif /* !MLK_RPG_BATTLE_ENTITY_STATE_H */
--- a/src/libmlk-rpg/rpg/battle-entity.c	Sun Feb 13 10:35:26 2022 +0100
+++ b/src/libmlk-rpg/rpg/battle-entity.c	Sun Feb 13 11:13:17 2022 +0100
@@ -26,6 +26,7 @@
 #include "battle.h"
 #include "battle-entity.h"
 #include "battle-entity-state.h"
+#include "battle-entity-state-normal.h"
 #include "character.h"
 
 static void
--- a/src/libmlk-rpg/rpg/battle-state-attacking.c	Sun Feb 13 10:35:26 2022 +0100
+++ b/src/libmlk-rpg/rpg/battle-state-attacking.c	Sun Feb 13 11:13:17 2022 +0100
@@ -28,6 +28,10 @@
 #include "battle-state-attacking.h"
 #include "battle-state-check.h"
 #include "battle-entity-state.h"
+#include "battle-entity-state-attacking.h"
+#include "battle-entity-state-blinking.h"
+#include "battle-entity-state-moving.h"
+#include "battle-entity-state-normal.h"
 #include "character.h"
 
 struct self {
--- a/src/libmlk-rpg/rpg/battle-state-item.c	Sun Feb 13 10:35:26 2022 +0100
+++ b/src/libmlk-rpg/rpg/battle-state-item.c	Sun Feb 13 11:13:17 2022 +0100
@@ -26,6 +26,8 @@
 #include <rpg/item.h>
 
 #include "battle-entity-state.h"
+#include "battle-entity-state-moving.h"
+#include "battle-entity-state-normal.h"
 #include "battle-message.h"
 #include "battle-state.h"
 #include "battle-state-item.h"