From eb345cfc258a13017db5b5e4d7f31c73f40419f9 Mon Sep 17 00:00:00 2001 From: Linus_Probert Date: Thu, 15 Mar 2018 11:30:18 +0100 Subject: [PATCH] Adds the windy room effect --- src/map.c | 5 +++ src/map.h | 2 ++ src/map_room_modifiers.h | 23 ++++++++++++++ src/particle_engine.c | 69 +++++++++++++++++++++++++++++++++++++--- src/particle_engine.h | 4 +++ 5 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 src/map_room_modifiers.h diff --git a/src/map.c b/src/map.c index 91c16f3..06846cb 100644 --- a/src/map.c +++ b/src/map.c @@ -23,6 +23,7 @@ #include "item.h" #include "item_builder.h" #include "gui.h" +#include "particle_engine.h" static Room* create_room(void) @@ -31,6 +32,7 @@ Room* create_room(void) Room *room; room = ec_malloc(sizeof(Room)); + room->modifier.type = RMOD_TYPE_NONE; for (i=0; i < MAP_ROOM_WIDTH; ++i) { for (j=0; j < MAP_ROOM_HEIGHT; ++j) { room->tiles[i][j] = NULL; @@ -236,6 +238,9 @@ void map_render(Map *map, Camera *cam) cam); } } + if (room->modifier.type == RMOD_TYPE_WINDY) { + particle_engine_wind(room->modifier.data.wind.direction); + } monsterItem = map->monsters; while (monsterItem != NULL) { diff --git a/src/map.h b/src/map.h index 3511a7c..b0d2784 100644 --- a/src/map.h +++ b/src/map.h @@ -30,6 +30,7 @@ #include "defines.h" #include "monster.h" #include "player.h" +#include "map_room_modifiers.h" typedef struct MapTile_t { int textureIndex0; @@ -43,6 +44,7 @@ typedef struct MapTile_t { typedef struct Room_t { MapTile* tiles[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT]; MapTile* decorations[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT]; + RoomModifierData modifier; } Room; typedef struct Map_t { diff --git a/src/map_room_modifiers.h b/src/map_room_modifiers.h new file mode 100644 index 0000000..70c5752 --- /dev/null +++ b/src/map_room_modifiers.h @@ -0,0 +1,23 @@ +#ifndef MAP_ROOM_MODIFIERS_H_ +#define MAP_ROOM_MODIFIERS_H_ + + +typedef enum RoomModifierType_e { + RMOD_TYPE_NONE, + RMOD_TYPE_WINDY +} RoomModifierType; + +typedef struct WindData_t { + Vector2d direction; +} WindData; + +typedef union RoomModifierDataContainer_t { + WindData wind; +} RoomModifierDataContainer; + +typedef struct RoomModifierData_t { + RoomModifierType type; + RoomModifierDataContainer data; +} RoomModifierData; + +#endif // MAP_ROOM_MODIFIERS_H_ diff --git a/src/particle_engine.c b/src/particle_engine.c index 84cffef..ccf515e 100644 --- a/src/particle_engine.c +++ b/src/particle_engine.c @@ -31,6 +31,7 @@ typedef struct Particle_t { Dimension dim; unsigned int movetime; unsigned int lifetime; + bool fixed; SDL_Color color; } Particle; @@ -40,6 +41,20 @@ typedef struct Engine_t { static Engine *engine = NULL; +static Particle* +create_particle(void) +{ + Particle *p = ec_malloc(sizeof(Particle)); + p->pos = (Position) { 0, 0 }; + p->dim = (Dimension) { 1, 1 }; + p->velocity = VECTOR2D_NODIR; + p->movetime = 100; + p->lifetime = 100; + p->fixed = false; + p->color = (SDL_Color) { 255, 255, 255, 255 }; + return p; +} + static void check_engine(void) { @@ -82,7 +97,7 @@ particle_engine_bloodspray(Position pos, Dimension dim, unsigned int count) w = get_random(3) + 2; h = get_random(3) + 2; - p = ec_malloc(sizeof(Particle)); + p = create_particle(); p->pos = (Position) { x, y }; p->velocity = (Vector2d) { (float) xv, (float) yv }; p->movetime = mt; @@ -118,7 +133,7 @@ create_explosion(Position pos, Dimension dim, unsigned int c_count, ...) lt = get_random(10); - p = ec_malloc(sizeof(Particle)); + p = create_particle(); p->pos = (Position) { x, y }; p->velocity = (Vector2d) { (float) xv, (float) yv }; p->movetime = lt; @@ -127,6 +142,7 @@ create_explosion(Position pos, Dimension dim, unsigned int c_count, ...) p->color = colors[get_random((unsigned int) c_count-1)]; linkedlist_append(&engine->particles, p); } + free(colors); } void @@ -167,7 +183,7 @@ particle_engine_speed_lines(Position pos, Dimension dim, bool horizontal) lt = get_random(10); - p = ec_malloc(sizeof(Particle)); + p = create_particle(); p->pos = (Position) { x, y }; p->velocity = (Vector2d) { 0, 0 }; p->movetime = lt; @@ -181,6 +197,46 @@ particle_engine_speed_lines(Position pos, Dimension dim, bool horizontal) } } +void +particle_engine_wind(Vector2d direction) +{ + static SDL_Color color = { 255, 255, 255, 255 }; + unsigned int count = 5; + + Position pos = { 0, 0 }; + Dimension dim = { GAME_VIEW_WIDTH, GAME_VIEW_HEIGHT }; + + if (dim.width == 0 || dim.height == 0) + return; + + for (unsigned int i = 0; i < count; ++i) { + int x, y; + int w, h; + unsigned int lt; + Particle *p; + int velocity; + + x = get_random(dim.width) + pos.x; + y = get_random(dim.height) + pos.y; + w = get_random(2) + 1; + h = get_random(2) + 1; + + velocity = get_random(500) + 500; + + lt = get_random(500); + + p = create_particle(); + p->pos = (Position) { x, y }; + p->velocity = (Vector2d) { direction.x * (float) velocity, direction.y * (float) velocity }; + p->movetime = lt; + p->lifetime = lt; + p->dim = (Dimension) { w, h }; + p->color = color; + p->fixed = true; + linkedlist_append(&engine->particles, p); + } +} + static void move_particle(Particle *particle, float deltaTime) { @@ -230,7 +286,12 @@ particle_engine_update(float deltaTime) static void render_particle(Particle *p, Camera *cam) { - Position pos = camera_to_camera_position(cam, &p->pos); + Position pos; + if (p->fixed) + pos = p->pos; + else + pos = camera_to_camera_position(cam, &p->pos); + SDL_Rect box = { pos.x, pos.y, p->dim.width, p->dim.height }; SDL_SetRenderDrawColor(cam->renderer, p->color.r, diff --git a/src/particle_engine.h b/src/particle_engine.h index ac57f91..07facef 100644 --- a/src/particle_engine.h +++ b/src/particle_engine.h @@ -23,6 +23,7 @@ #include "position.h" #include "dimension.h" #include "camera.h" +#include "vector2d.h" void particle_engine_init(void); @@ -39,6 +40,9 @@ particle_engine_eldritch_explosion(Position, Dimension); void particle_engine_speed_lines(Position, Dimension, bool horizontal); +void +particle_engine_wind(Vector2d direction); + void particle_engine_update(float deltatime);