Skip to content

Commit 67d8f3d

Browse files
authored
Adds a proper explosion animation (#139)
* Adds a proper explosion animation
1 parent d2e9df8 commit 67d8f3d

7 files changed

Lines changed: 140 additions & 2 deletions

File tree

src/animation.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ animation_update(Animation *animation)
5454
}
5555

5656
if (timer_get_ticks(animation->clipTimer)
57-
> animation->clips[animation->currentClip].renderTime)
57+
>= animation->clips[animation->currentClip].renderTime)
5858
{
5959
animation->currentClip++;
6060
if (animation->currentClip >= animation->clipCount) {

src/animation_controller.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include "animation_controller.h"
2+
#include "animation.h"
3+
#include "dimension.h"
4+
#include "linkedlist.h"
5+
#include "sprite.h"
6+
#include "texturecache.h"
7+
#include "util.h"
8+
#include <stdlib.h>
9+
10+
struct {
11+
LinkedList *animations;
12+
} s_controller;
13+
14+
static void
15+
create_explosion(Position pos)
16+
{
17+
Animation *a = animation_create(8);
18+
linkedlist_append(&s_controller.animations, a);
19+
20+
Texture *t = texturecache_add("Extras/Explosion.png");
21+
sprite_set_texture(a->sprite, t, 0);
22+
a->sprite->pos.x = pos.x - 16;
23+
a->sprite->pos.y = pos.y - 16;
24+
a->sprite->dim = (Dimension) { 64, 64 };
25+
a->sprite->clip = (SDL_Rect) { 0, 0, 32, 32 };
26+
a->sprite->rotationPoint = (SDL_Point) { 32, 32 };
27+
28+
a->loop = false;
29+
a->running = true;
30+
animation_set_frames(a, (AnimationClip[]) {
31+
{ 0, 0, 32, 32, 100 },
32+
{ 32, 0, 32, 32, 100 },
33+
{ 64, 0, 32, 32, 100 },
34+
{ 96, 0, 32, 32, 100 },
35+
{ 128, 0, 32, 32, 100 },
36+
{ 160, 0, 32, 32, 100 },
37+
{ 192, 0, 32, 32, 300 },
38+
{ 224, 0, 32, 32, 300 }
39+
});
40+
}
41+
42+
void
43+
animation_controller_create(enum AnimationType type, Position pos)
44+
{
45+
switch (type) {
46+
case EXPLOSION_ANIMATION:
47+
create_explosion(pos);
48+
break;
49+
default:
50+
fatal("Invalid animation type: %d", type);
51+
break;
52+
}
53+
}
54+
55+
void
56+
animation_controller_init(void)
57+
{
58+
s_controller.animations = NULL;
59+
}
60+
61+
void
62+
animation_controller_update(void)
63+
{
64+
LinkedList *node = s_controller.animations;
65+
LinkedList *prev = NULL;
66+
while (node != NULL) {
67+
Animation *a = node->data;
68+
if (!a->running) {
69+
if (prev) {
70+
prev->next = node->next;
71+
} else {
72+
s_controller.animations = node->next;
73+
}
74+
LinkedList *tmp = node;
75+
node = node->next;
76+
animation_destroy(tmp->data);
77+
free(tmp);
78+
continue;
79+
}
80+
animation_update(a);
81+
prev = node;
82+
node = node->next;
83+
}
84+
}
85+
86+
void
87+
animation_controller_render(Camera *cam)
88+
{
89+
LinkedList *node = s_controller.animations;
90+
while (node) {
91+
animation_render(node->data, cam);
92+
node = node->next;
93+
}
94+
}
95+
96+
void
97+
animation_controller_close(void)
98+
{
99+
while (s_controller.animations) {
100+
animation_destroy(linkedlist_pop(&s_controller.animations));
101+
}
102+
}

src/animation_controller.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef ANIMATION_CONTROLLER_H_
2+
#define ANIMATION_CONTROLLER_H_
3+
4+
#include "camera.h"
5+
6+
enum AnimationType {
7+
EXPLOSION_ANIMATION,
8+
};
9+
10+
void
11+
animation_controller_create(enum AnimationType type, Position pos);
12+
13+
void
14+
animation_controller_init(void);
15+
16+
void
17+
animation_controller_update(void);
18+
19+
void
20+
animation_controller_render(Camera *cam);
21+
22+
void
23+
animation_controller_close(void);
24+
25+
#endif // ANIMATION_CONTROLLER_H_
26+

src/main.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <SDL3/SDL_main.h>
2525
#include <physfs.h>
2626
#include <string.h>
27+
#include "animation_controller.h"
2728
#include "linkedlist.h"
2829
#include "player.h"
2930
#include "screenresolution.h"
@@ -259,6 +260,7 @@ initGame(void)
259260
#ifdef DEBUG
260261
gPointer = pointer_create(gRenderer);
261262
#endif // DEBUG
263+
animation_controller_init();
262264
menuTimer = _timer_create();
263265
actiontextbuilder_init(gRenderer);
264266

@@ -976,6 +978,7 @@ run_game_update(void)
976978

977979
gui_update_player_stats(gGui, gPlayer, gMap, gRenderer);
978980
camera_update(gCamera, updateData.deltatime);
981+
animation_controller_update();
979982
particle_engine_update(deltaTime);
980983
roommatrix_update(&updateData);
981984
actiontextbuilder_update(&updateData);
@@ -1046,14 +1049,15 @@ render_game(void)
10461049
{
10471050
SDL_SetRenderViewport(gRenderer, &gameViewport);
10481051
map_render(gMap, gCamera);
1049-
particle_engine_render_game(gCamera);
10501052

1053+
particle_engine_render_game(gCamera);
10511054
map_render_mid_layer(gMap, gCamera);
10521055

10531056
if (!is_player_dead()) {
10541057
player_render(gPlayer, gCamera);
10551058
player_render_toplayer(gPlayer, gCamera);
10561059
}
1060+
animation_controller_render(gCamera);
10571061

10581062
map_render_top_layer(gMap, gRoomMatrix, gCamera);
10591063

@@ -1384,6 +1388,7 @@ void close(void)
13841388
actiontextbuilder_close();
13851389
item_builder_close();
13861390
particle_engine_close();
1391+
animation_controller_close();
13871392
timer_destroy(menuTimer);
13881393
mixer_close();
13891394
texturecache_close();

src/monster.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <string.h>
2222

2323
#include "monster.h"
24+
#include "animation_controller.h"
2425
#include "util.h"
2526
#include "player.h"
2627
#include "monster.h"

src/player.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <string.h>
2222

2323
#include "player.h"
24+
#include "animation_controller.h"
2425
#include "monster.h"
2526
#include "util.h"
2627
#include "gui.h"
@@ -166,6 +167,7 @@ on_monster_collision(Player *player,
166167
if (monster->stats.hp <= 0 && (player_has_artifact(player, EXPLOSIVE_KILLS))) {
167168
mixer_play_effect(EXPLOSION_EFFECT);
168169
particle_engine_fire_explosion(monster->sprite->pos, DIM(32, 32));
170+
animation_controller_create(EXPLOSION_ANIMATION, monster->sprite->pos);
169171
effect_damage_surroundings(&monster->sprite->pos,
170172
matrix,
171173
player,

src/projectile.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <stdlib.h>
2020
#include <string.h>
2121
#include "projectile.h"
22+
#include "animation_controller.h"
2223
#include "util.h"
2324
#include "texturecache.h"
2425
#include "player.h"
@@ -103,6 +104,7 @@ perform_dagger_explosion(Player *player, RoomMatrix *rm, Position *collisionPos)
103104
if (player_has_artifact(player, VOLATILE_DAGGERS)) {
104105
mixer_play_effect(EXPLOSION_EFFECT);
105106
particle_engine_fire_explosion(*collisionPos, DIM(32, 32));
107+
animation_controller_create(EXPLOSION_ANIMATION, *collisionPos);
106108
effect_damage_surroundings(collisionPos,
107109
rm,
108110
player,

0 commit comments

Comments
 (0)