diff src/libmlk-rpg/rpg/battle-state-victory.c @ 382:43d155668a55

rpg: expose battle state functions
author David Demelier <markand@malikania.fr>
date Sun, 13 Feb 2022 10:35:26 +0100
parents 460c78706989
children c458441ff472
line wrap: on
line diff
--- a/src/libmlk-rpg/rpg/battle-state-victory.c	Fri Jan 07 21:50:37 2022 +0100
+++ b/src/libmlk-rpg/rpg/battle-state-victory.c	Sun Feb 13 10:35:26 2022 +0100
@@ -23,15 +23,15 @@
 #include <core/panic.h>
 #include <core/window.h>
 
-#include <rpg/message.h>
-
 #include "battle.h"
 #include "battle-state.h"
+#include "battle-state-closing.h"
+#include "battle-state-victory.h"
 #include "rpg_p.h"
 
-struct victory {
-	struct battle_state self;
-	struct message msg;
+struct self {
+	struct battle_state_victory data;
+	struct battle_state state;
 };
 
 static void
@@ -39,9 +39,7 @@
 {
 	(void)bt;
 
-	struct victory *vic = st->data;
-
-	message_handle(&vic->msg, ev);
+	battle_state_victory_handle(st->data, ev);
 }
 
 static int
@@ -49,12 +47,7 @@
 {
 	(void)bt;
 
-	struct victory *vic = st->data;
-
-	if (message_update(&vic->msg, ticks))
-		battle_state_closing(bt);
-
-	return 0;
+	return battle_state_victory_update(st->data, bt, ticks);
 }
 
 static void
@@ -62,27 +55,14 @@
 {
 	(void)bt;
 
-	const struct victory *vic = st->data;
-
-	message_draw(&vic->msg);
+	battle_state_victory_draw(st->data);
 }
 
 void
-battle_state_victory(struct battle *bt)
+battle_state_victory_init(struct battle_state_victory *vic, struct battle *bt)
 {
 	assert(bt);
 
-	struct victory *vic;
-
-	if (!(vic = alloc_new0(sizeof (*vic))))
-		panic();
-
-	/* TODO: compute money, xp and drop. */
-	vic->self.data = vic;
-	vic->self.handle = handle;
-	vic->self.update = update;
-	vic->self.draw = draw;
-
 	vic->msg.text[0] = _("Victory!");
 	vic->msg.theme = bt->theme;
 	vic->msg.flags = MESSAGE_FLAGS_AUTOMATIC |
@@ -99,8 +79,53 @@
 	vic->msg.x = (window.w - vic->msg.w) / 2;
 
 	bt->status = BATTLE_STATUS_WON;
-	battle_switch(bt, &vic->self);
 
 	if (bt->music[1])
-		music_play(bt->music[1], MUSIC_NONE, 0);
+		music_play(bt->music[1], MUSIC_NONE);
+}
+
+void
+battle_state_victory_handle(struct battle_state_victory *vic, const union event *ev)
+{
+	assert(vic);
+
+	message_handle(&vic->msg, ev);
+}
+
+int
+battle_state_victory_update(struct battle_state_victory *vic, struct battle *bt, unsigned int ticks)
+{
+	assert(vic);
+	assert(bt);
+
+	if (message_update(&vic->msg, ticks))
+		battle_state_closing(bt);
+
+	return 0;
 }
+
+void
+battle_state_victory_draw(struct battle_state_victory *vic)
+{
+	assert(vic);
+
+	message_draw(&vic->msg);
+}
+
+void
+battle_state_victory(struct battle *bt)
+{
+	assert(bt);
+
+	struct self *self;
+
+	/* TODO: compute money, xp and drop. */
+	self = alloc_new0(sizeof (*self));
+	self->state.data = self;
+	self->state.handle = handle;
+	self->state.update = update;
+	self->state.draw = draw;
+
+	battle_state_victory_init(&self->data, bt);
+	battle_switch(bt, &self->state);
+}