comparison libmlk-rpg/rpg/battle-entity-state-blinking.c @ 243:71b3b7036de7

misc: lot of cleanups, - prefix libraries with libmlk, - move assets from source directories closes #2520, - prefix header guards closes #2519
author David Demelier <markand@malikania.fr>
date Sat, 28 Nov 2020 22:37:30 +0100
parents librpg/rpg/battle-entity-state-blinking.c@fb304a94a05c
children 6367b976112d
comparison
equal deleted inserted replaced
242:4c24604efcab 243:71b3b7036de7
1 /*
2 * battle-entity-state-blinking.c -- the entity is blinking
3 *
4 * Copyright (c) 2020 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-blinking.h"
29 #include "battle-entity-state.h"
30 #include "character.h"
31
32 #define TRANSPARENT (150)
33 #define OPAQUE (255)
34
35 struct blink {
36 struct battle_entity_state state;
37 struct texture *tex;
38 unsigned int elapsed;
39 unsigned int count;
40 };
41
42 static bool
43 update(struct battle_entity_state *st, struct battle_entity *et, unsigned int ticks)
44 {
45 (void)et;
46
47 struct blink *blk = st->data;
48
49 blk->elapsed += ticks;
50
51 if (blk->elapsed >= 80) {
52 blk->count += 1;
53 blk->elapsed = 0;
54 }
55
56 texture_set_alpha_mod(blk->tex, blk->count % 2 == 0 ? TRANSPARENT : OPAQUE);
57
58 return blk->count >= 3;
59 }
60
61 static void
62 finish(struct battle_entity_state *st, struct battle_entity *et)
63 {
64 (void)et;
65
66 free(st->data);
67 }
68
69 void
70 battle_entity_state_blinking(struct battle_entity *et)
71 {
72 assert(et);
73
74 struct blink *blk;
75
76 if (!(blk = alloc_new0(sizeof (*blk))))
77 panic();
78
79 blk->tex = et->ch->sprites[CHARACTER_SPRITE_NORMAL]->texture;
80 texture_set_alpha_mod(blk->tex, TRANSPARENT);
81
82 blk->state.data = blk;
83 blk->state.update = update;
84 blk->state.finish = finish;
85
86 battle_entity_switch(et, &blk->state);
87 }