Mercurial > molko
view examples/example-battle/spell-fire.c @ 314:d01e83210ca2
misc: update years
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 22 Sep 2021 07:22:20 +0200 |
parents | 196264679079 |
children |
line wrap: on
line source
/* * spell-fire.c -- example of spell: fire * * Copyright (c) 2020-2021 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. */ #include <stdlib.h> #include <core/action.h> #include <core/animation.h> #include <core/alloc.h> #include <ui/align.h> #include <rpg/battle.h> #include <rpg/character.h> #include <rpg/spell.h> #include "registry.h" #include "spell-fire.h" struct data { struct battle *battle; struct character *target; struct animation animation; struct action action; unsigned int selection; }; static int update(struct action *act, unsigned int ticks) { struct data *data = act->data; return animation_update(&data->animation, ticks); } static void draw(struct action *act) { const struct data *data = act->data; const struct battle_entity *et = &data->battle->enemies[data->selection]; const struct sprite *sprite = et->ch->sprites[CHARACTER_SPRITE_NORMAL]; int x, y; align(ALIGN_CENTER, &x, &y, data->animation.sprite->cellw, data->animation.sprite->cellh, et->x, et->y, sprite->cellw, sprite->cellh); animation_draw(&data->animation, x, y); } static void end(struct action *act) { struct data *data = act->data; struct character *ch = data->battle->enemies[data->selection].ch; /* TODO: compute damage. */ const unsigned int damage = 100; if ((unsigned int)ch->hp < damage) ch->hp = 0; else ch->hp -= damage; battle_indicator_hp(data->battle, data->battle->enemies[data->selection].ch, 100); } static void finish(struct action *act) { free(act->data); } static void fire_select(const struct battle *bt, struct selection *slt) { slt->index_side = 0; selection_first(slt, bt); } static void fire_action(struct battle *bt, struct character *owner, const struct selection *slt) { struct data *data; (void)owner; data = alloc_new0(sizeof (*data)); data->selection = slt->index_character; data->battle = bt; data->action.data = data; data->action.update = update; data->action.draw = draw; data->action.finish = finish; data->action.end = end; animation_init(&data->animation, ®istry_sprites[REGISTRY_TEXTURE_EXPLOSION], 12); animation_start(&data->animation); sound_play(®istry_sounds[REGISTRY_SOUND_FIRE], -1, 0); action_stack_add(&bt->actions[0], &data->action); } const struct spell spell_fire = { .name = "Fire", .description = "A delicate fire.", .mp = 5, .type = SPELL_TYPE_FIRE, .select = fire_select, .select_kind = SELECTION_KIND_ONE, .select_side = SELECTION_SIDE_ENEMY, .action = fire_action };