Adds the windy room effect

This commit is contained in:
Linus_Probert 2018-03-15 11:30:18 +01:00
parent 1bea221369
commit eb345cfc25
5 changed files with 99 additions and 4 deletions

View File

@ -23,6 +23,7 @@
#include "item.h" #include "item.h"
#include "item_builder.h" #include "item_builder.h"
#include "gui.h" #include "gui.h"
#include "particle_engine.h"
static static
Room* create_room(void) Room* create_room(void)
@ -31,6 +32,7 @@ Room* create_room(void)
Room *room; Room *room;
room = ec_malloc(sizeof(Room)); room = ec_malloc(sizeof(Room));
room->modifier.type = RMOD_TYPE_NONE;
for (i=0; i < MAP_ROOM_WIDTH; ++i) { for (i=0; i < MAP_ROOM_WIDTH; ++i) {
for (j=0; j < MAP_ROOM_HEIGHT; ++j) { for (j=0; j < MAP_ROOM_HEIGHT; ++j) {
room->tiles[i][j] = NULL; room->tiles[i][j] = NULL;
@ -236,6 +238,9 @@ void map_render(Map *map, Camera *cam)
cam); cam);
} }
} }
if (room->modifier.type == RMOD_TYPE_WINDY) {
particle_engine_wind(room->modifier.data.wind.direction);
}
monsterItem = map->monsters; monsterItem = map->monsters;
while (monsterItem != NULL) { while (monsterItem != NULL) {

View File

@ -30,6 +30,7 @@
#include "defines.h" #include "defines.h"
#include "monster.h" #include "monster.h"
#include "player.h" #include "player.h"
#include "map_room_modifiers.h"
typedef struct MapTile_t { typedef struct MapTile_t {
int textureIndex0; int textureIndex0;
@ -43,6 +44,7 @@ typedef struct MapTile_t {
typedef struct Room_t { typedef struct Room_t {
MapTile* tiles[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT]; MapTile* tiles[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT];
MapTile* decorations[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT]; MapTile* decorations[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT];
RoomModifierData modifier;
} Room; } Room;
typedef struct Map_t { typedef struct Map_t {

23
src/map_room_modifiers.h Normal file
View File

@ -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_

View File

@ -31,6 +31,7 @@ typedef struct Particle_t {
Dimension dim; Dimension dim;
unsigned int movetime; unsigned int movetime;
unsigned int lifetime; unsigned int lifetime;
bool fixed;
SDL_Color color; SDL_Color color;
} Particle; } Particle;
@ -40,6 +41,20 @@ typedef struct Engine_t {
static Engine *engine = NULL; 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 static void
check_engine(void) check_engine(void)
{ {
@ -82,7 +97,7 @@ particle_engine_bloodspray(Position pos, Dimension dim, unsigned int count)
w = get_random(3) + 2; w = get_random(3) + 2;
h = get_random(3) + 2; h = get_random(3) + 2;
p = ec_malloc(sizeof(Particle)); p = create_particle();
p->pos = (Position) { x, y }; p->pos = (Position) { x, y };
p->velocity = (Vector2d) { (float) xv, (float) yv }; p->velocity = (Vector2d) { (float) xv, (float) yv };
p->movetime = mt; p->movetime = mt;
@ -118,7 +133,7 @@ create_explosion(Position pos, Dimension dim, unsigned int c_count, ...)
lt = get_random(10); lt = get_random(10);
p = ec_malloc(sizeof(Particle)); p = create_particle();
p->pos = (Position) { x, y }; p->pos = (Position) { x, y };
p->velocity = (Vector2d) { (float) xv, (float) yv }; p->velocity = (Vector2d) { (float) xv, (float) yv };
p->movetime = lt; 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)]; p->color = colors[get_random((unsigned int) c_count-1)];
linkedlist_append(&engine->particles, p); linkedlist_append(&engine->particles, p);
} }
free(colors);
} }
void void
@ -167,7 +183,7 @@ particle_engine_speed_lines(Position pos, Dimension dim, bool horizontal)
lt = get_random(10); lt = get_random(10);
p = ec_malloc(sizeof(Particle)); p = create_particle();
p->pos = (Position) { x, y }; p->pos = (Position) { x, y };
p->velocity = (Vector2d) { 0, 0 }; p->velocity = (Vector2d) { 0, 0 };
p->movetime = lt; 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 static void
move_particle(Particle *particle, float deltaTime) move_particle(Particle *particle, float deltaTime)
{ {
@ -230,7 +286,12 @@ particle_engine_update(float deltaTime)
static void static void
render_particle(Particle *p, Camera *cam) 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_Rect box = { pos.x, pos.y, p->dim.width, p->dim.height };
SDL_SetRenderDrawColor(cam->renderer, SDL_SetRenderDrawColor(cam->renderer,
p->color.r, p->color.r,

View File

@ -23,6 +23,7 @@
#include "position.h" #include "position.h"
#include "dimension.h" #include "dimension.h"
#include "camera.h" #include "camera.h"
#include "vector2d.h"
void void
particle_engine_init(void); particle_engine_init(void);
@ -39,6 +40,9 @@ particle_engine_eldritch_explosion(Position, Dimension);
void void
particle_engine_speed_lines(Position, Dimension, bool horizontal); particle_engine_speed_lines(Position, Dimension, bool horizontal);
void
particle_engine_wind(Vector2d direction);
void void
particle_engine_update(float deltatime); particle_engine_update(float deltatime);