summaryrefslogtreecommitdiff
path: root/saedit/action.c
blob: a9b31e22e209bb18ceae701aec232b182cb5f194 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "action.h"
#include "animation.h"
#include "imageset.h"
#include "context.h"

Action *
action_new (
	const SpriteContext *context,
	const XMLNode *node, 
	gint included_from
) {
	Action *action;
	GList  *list;
	gchar  *imgset_name;
	gboolean fail = FALSE;
	Imageset *imageset;

	g_return_val_if_fail (g_strcmp0 (node->name, "action") == 0, NULL);
	
	action = (Action *) g_new0 (Action, 1);

	action->name = xml_node_get_attr_value (node, "name");
	if (action->name == NULL) {
		/* TODO: report error */
		fail = TRUE;
	}

	imgset_name = xml_node_get_attr_value (node, "imageset");
	if (imgset_name == NULL) {
		/* TODO: report error */
		fail = TRUE;
	} else {
		imageset = sprite_context_get_imageset (context, imgset_name);
		if (imageset == NULL) {
			/* TODO: report error */
			fail = TRUE;
		}
	}

	if (fail) {
		g_free (action);
		return NULL;
	}

	action->hp = xml_node_get_int_attr_value (node, "hp", 100);

	list = node->sub_nodes;
	while (list != NULL) {
		XMLNode *current = (XMLNode *) list->data;
		
		if (g_strcmp0 (current->name, "animation") == 0) {
			action_add_animation (
				action,
				imageset,
				current,
				included_from
			);
		} else {
			/* TODO: action contains something unknown */
		}

		list = g_list_next (list);
	}

	return action;
}

void
action_add_animation (
	Action *action,
	const Imageset *imageset,
	const XMLNode *node,
	gint included_from
) {
	Animation *animation;

	g_return_if_fail (g_strcmp0 (node->name, "animation") == 0);

	animation = animation_new (imageset, node, included_from);
	if (animation == NULL)
		return;

	if (g_list_find_custom (
		action->animations, animation,
		(GCompareFunc) animation_compare_by_direction) != NULL
	) {
		animation_free (animation);
		return;
	}

	action->animations = g_list_append (action->animations, animation);
}

void
action_free (Action *action) {
	g_list_free_full (
		action->animations,
		(GDestroyNotify) animation_free
	);

	g_free (action->name);
	g_free (action);
}

gint
action_compare_by_hp_and_name (
	const Action *first,
	const Action *second
) {
	if (first->hp != second->hp) {
		return first->hp - second->hp;
	}
	return g_strcmp0 (first->name, second->name);
}

gboolean
action_hp_and_name_equals (
	const Action *action,
	gint hp,
	const gchar *name
) {
	return
		action->hp == hp &&
		g_strcmp0 (action->name, name) == 0;
}

const Animation *
action_get_animation (
	const Action *action,
	const gchar *direction
) {
	GList *animation = action->animations;
	g_return_val_if_fail (animation != NULL, NULL);
	
	while (direction != NULL && animation != NULL) {
		Animation *current = (Animation *) animation->data;
		g_assert (current != NULL);
		if (animation_direction_equals (current, direction))
			return current;
		
		animation = g_list_next (animation);
	}

	return (Animation *) action->animations->data;
}

void
action_get_hp_and_name (
	const Action *action,
	gint *hp,
	gchar **name
) {
	if (hp != NULL)
		*hp = action->hp;
	if (name != NULL)
		*name = action->name;
}

GList *
action_get_directions (const Action *action) {
	GList *result = NULL;
	GList *animations = action->animations;

	while (animations != NULL) {
		Animation *animation = (Animation *) animations->data;
		result = g_list_append (result, animation->direction);
		animations = g_list_next (animations);
	}

	return result;
}

gchar *
get_action_id (gint hp, const gchar *name) {
	return g_strdup_printf ("%d:%s", hp, name);
}