2010-11-20 10:37:14 +00:00
|
|
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
|
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
2010-05-29 07:25:38 +00:00
|
|
|
#include <engine/graphics.h>
|
|
|
|
#include <game/mapitems.h>
|
|
|
|
#include <game/layers.h>
|
|
|
|
#include "flow.h"
|
2008-01-29 21:39:41 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
CFlow::CFlow()
|
2008-01-29 21:39:41 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
m_pCells = 0;
|
|
|
|
m_Height = 0;
|
|
|
|
m_Width = 0;
|
|
|
|
m_Spacing = 16;
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CFlow::DbgRender()
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
if(!m_pCells)
|
2008-08-27 15:48:50 +00:00
|
|
|
return;
|
2008-01-29 21:39:41 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
IGraphics::CLineItem Array[1024];
|
|
|
|
int NumItems = 0;
|
2009-10-27 14:38:53 +00:00
|
|
|
Graphics()->TextureSet(-1);
|
|
|
|
Graphics()->LinesBegin();
|
2010-05-29 07:25:38 +00:00
|
|
|
for(int y = 0; y < m_Height; y++)
|
|
|
|
for(int x = 0; x < m_Width; x++)
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
vec2 Pos(x*m_Spacing, y*m_Spacing);
|
|
|
|
vec2 Vel = m_pCells[y*m_Width+x].m_Vel * 0.01f;
|
|
|
|
Array[NumItems++] = IGraphics::CLineItem(Pos.x, Pos.y, Pos.x+Vel.x, Pos.y+Vel.y);
|
|
|
|
if(NumItems == 1024)
|
|
|
|
{
|
|
|
|
Graphics()->LinesDraw(Array, 1024);
|
|
|
|
NumItems = 0;
|
|
|
|
}
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(NumItems)
|
|
|
|
Graphics()->LinesDraw(Array, NumItems);
|
2009-10-27 14:38:53 +00:00
|
|
|
Graphics()->LinesEnd();
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
2008-01-29 21:39:41 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CFlow::Init()
|
2008-01-29 21:39:41 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
if(m_pCells)
|
2008-01-29 21:39:41 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
mem_free(m_pCells);
|
|
|
|
m_pCells = 0;
|
2008-01-29 21:39:41 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
CMapItemLayerTilemap *pTilemap = Layers()->GameLayer();
|
|
|
|
m_Width = pTilemap->m_Width*32/m_Spacing;
|
|
|
|
m_Height = pTilemap->m_Height*32/m_Spacing;
|
2008-01-29 21:39:41 +00:00
|
|
|
|
2011-04-13 18:37:12 +00:00
|
|
|
// allocate and clear
|
2010-05-29 07:25:38 +00:00
|
|
|
m_pCells = (CCell *)mem_alloc(sizeof(CCell)*m_Width*m_Height, 1);
|
|
|
|
for(int y = 0; y < m_Height; y++)
|
|
|
|
for(int x = 0; x < m_Width; x++)
|
|
|
|
m_pCells[y*m_Width+x].m_Vel = vec2(0.0f, 0.0f);
|
2008-01-29 21:39:41 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CFlow::Update()
|
2008-01-29 21:39:41 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
if(!m_pCells)
|
2008-01-29 21:39:41 +00:00
|
|
|
return;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
for(int y = 0; y < m_Height; y++)
|
|
|
|
for(int x = 0; x < m_Width; x++)
|
|
|
|
m_pCells[y*m_Width+x].m_Vel *= 0.85f;
|
2008-01-29 21:39:41 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
vec2 CFlow::Get(vec2 Pos)
|
2008-01-29 21:39:41 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
if(!m_pCells)
|
2008-08-27 15:48:50 +00:00
|
|
|
return vec2(0,0);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int x = (int)(Pos.x / m_Spacing);
|
|
|
|
int y = (int)(Pos.y / m_Spacing);
|
|
|
|
if(x < 0 || y < 0 || x >= m_Width || y >= m_Height)
|
2008-08-27 15:48:50 +00:00
|
|
|
return vec2(0,0);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
|
|
|
return m_pCells[y*m_Width+x].m_Vel;
|
2008-01-29 21:39:41 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CFlow::Add(vec2 Pos, vec2 Vel, float Size)
|
2008-01-29 21:39:41 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
if(!m_pCells)
|
2008-08-27 15:48:50 +00:00
|
|
|
return;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int x = (int)(Pos.x / m_Spacing);
|
|
|
|
int y = (int)(Pos.y / m_Spacing);
|
|
|
|
if(x < 0 || y < 0 || x >= m_Width || y >= m_Height)
|
2008-08-27 15:48:50 +00:00
|
|
|
return;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
m_pCells[y*m_Width+x].m_Vel += Vel;
|
2008-01-29 21:39:41 +00:00
|
|
|
}
|