2008-08-27 15:48:50 +00:00
|
|
|
#include <game/mapitems.hpp>
|
|
|
|
#include <game/layers.hpp>
|
|
|
|
#include "flow.hpp"
|
2008-01-29 21:39:41 +00:00
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
FLOW::FLOW()
|
2008-01-29 21:39:41 +00:00
|
|
|
{
|
2008-08-27 15:48:50 +00:00
|
|
|
cells = 0;
|
|
|
|
height = 0;
|
|
|
|
width = 0;
|
|
|
|
spacing = 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FLOW::dbg_render()
|
|
|
|
{
|
|
|
|
if(!cells)
|
|
|
|
return;
|
2008-01-29 21:39:41 +00:00
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
gfx_texture_set(-1);
|
|
|
|
gfx_lines_begin();
|
|
|
|
for(int y = 0; y < height; y++)
|
|
|
|
for(int x = 0; x < width; x++)
|
|
|
|
{
|
|
|
|
vec2 pos(x*spacing, y*spacing);
|
|
|
|
vec2 vel = cells[y*width+x].vel * 0.01f;
|
|
|
|
gfx_lines_draw(pos.x, pos.y, pos.x+vel.x, pos.y+vel.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx_lines_end();
|
|
|
|
}
|
2008-01-29 21:39:41 +00:00
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
void FLOW::init()
|
2008-01-29 21:39:41 +00:00
|
|
|
{
|
|
|
|
if(cells)
|
|
|
|
{
|
|
|
|
mem_free(cells);
|
|
|
|
cells = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MAPITEM_LAYER_TILEMAP *tilemap = layers_game_layer();
|
|
|
|
width = tilemap->width*32/spacing;
|
|
|
|
height = tilemap->height*32/spacing;
|
|
|
|
|
|
|
|
// allocate and clear
|
2008-08-27 15:48:50 +00:00
|
|
|
cells = (CELL *)mem_alloc(sizeof(CELL)*width*height, 1);
|
2008-01-29 21:39:41 +00:00
|
|
|
for(int y = 0; y < height; y++)
|
|
|
|
for(int x = 0; x < width; x++)
|
|
|
|
cells[y*width+x].vel = vec2(0.0f, 0.0f);
|
|
|
|
}
|
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
void FLOW::update()
|
2008-01-29 21:39:41 +00:00
|
|
|
{
|
2008-08-27 15:48:50 +00:00
|
|
|
if(!cells)
|
2008-01-29 21:39:41 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
for(int y = 0; y < height; y++)
|
|
|
|
for(int x = 0; x < width; x++)
|
|
|
|
cells[y*width+x].vel *= 0.85f;
|
|
|
|
}
|
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
vec2 FLOW::get(vec2 pos)
|
2008-01-29 21:39:41 +00:00
|
|
|
{
|
2008-08-27 15:48:50 +00:00
|
|
|
if(!cells)
|
|
|
|
return vec2(0,0);
|
|
|
|
|
2008-01-29 21:39:41 +00:00
|
|
|
int x = (int)(pos.x / spacing);
|
|
|
|
int y = (int)(pos.y / spacing);
|
|
|
|
if(x < 0 || y < 0 || x >= width || y >= height)
|
2008-08-27 15:48:50 +00:00
|
|
|
return vec2(0,0);
|
2008-01-29 21:39:41 +00:00
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
return cells[y*width+x].vel;
|
2008-01-29 21:39:41 +00:00
|
|
|
}
|
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
void FLOW::add(vec2 pos, vec2 vel, float size)
|
2008-01-29 21:39:41 +00:00
|
|
|
{
|
2008-08-27 15:48:50 +00:00
|
|
|
if(!cells)
|
|
|
|
return;
|
|
|
|
|
2008-01-29 21:39:41 +00:00
|
|
|
int x = (int)(pos.x / spacing);
|
|
|
|
int y = (int)(pos.y / spacing);
|
|
|
|
if(x < 0 || y < 0 || x >= width || y >= height)
|
2008-08-27 15:48:50 +00:00
|
|
|
return;
|
2008-01-29 21:39:41 +00:00
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
cells[y*width+x].vel += vel;
|
2008-01-29 21:39:41 +00:00
|
|
|
}
|