Mercurial > molko
comparison src/libmlk-rpg/rpg/battle-entity-state-blinking.c @ 320:8f9937403749
misc: improve loading of data
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 01 Oct 2021 20:30:00 +0200 |
parents | libmlk-rpg/rpg/battle-entity-state-blinking.c@d01e83210ca2 |
children | 460c78706989 |
comparison
equal
deleted
inserted
replaced
319:b843eef4cc35 | 320:8f9937403749 |
---|---|
1 /* | |
2 * battle-entity-state-blinking.c -- the entity is blinking | |
3 * | |
4 * Copyright (c) 2020-2021 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #include <assert.h> | |
20 #include <stdlib.h> | |
21 | |
22 #include <core/alloc.h> | |
23 #include <core/panic.h> | |
24 #include <core/sprite.h> | |
25 #include <core/texture.h> | |
26 | |
27 #include "battle-entity.h" | |
28 #include "battle-entity-state.h" | |
29 #include "character.h" | |
30 | |
31 #define TRANSPARENT (150) | |
32 #define OPAQUE (255) | |
33 | |
34 struct blink { | |
35 struct battle_entity_state state; | |
36 struct texture *tex; | |
37 unsigned int elapsed; | |
38 unsigned int count; | |
39 }; | |
40 | |
41 static int | |
42 update(struct battle_entity_state *st, struct battle_entity *et, unsigned int ticks) | |
43 { | |
44 (void)et; | |
45 | |
46 struct blink *blk = st->data; | |
47 | |
48 blk->elapsed += ticks; | |
49 | |
50 if (blk->elapsed >= 80) { | |
51 blk->count += 1; | |
52 blk->elapsed = 0; | |
53 } | |
54 | |
55 texture_set_alpha_mod(blk->tex, blk->count % 2 == 0 ? TRANSPARENT : OPAQUE); | |
56 | |
57 return blk->count >= 3; | |
58 } | |
59 | |
60 static void | |
61 finish(struct battle_entity_state *st, struct battle_entity *et) | |
62 { | |
63 (void)et; | |
64 | |
65 free(st->data); | |
66 } | |
67 | |
68 void | |
69 battle_entity_state_blinking(struct battle_entity *et) | |
70 { | |
71 assert(et); | |
72 | |
73 struct blink *blk; | |
74 | |
75 if (!(blk = alloc_new0(sizeof (*blk)))) | |
76 panic(); | |
77 | |
78 blk->tex = et->ch->sprites[CHARACTER_SPRITE_NORMAL]->texture; | |
79 texture_set_alpha_mod(blk->tex, TRANSPARENT); | |
80 | |
81 blk->state.data = blk; | |
82 blk->state.update = update; | |
83 blk->state.finish = finish; | |
84 | |
85 battle_entity_switch(et, &blk->state); | |
86 } |