1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#include "flow.h"
4#include <engine/graphics.h>
5#include <game/layers.h>
6#include <game/mapitems.h>
7
8CFlow::CFlow()
9{
10 m_pCells = 0;
11 m_Height = 0;
12 m_Width = 0;
13 m_Spacing = 16;
14}
15
16void CFlow::DbgRender()
17{
18 if(!m_pCells)
19 return;
20
21 IGraphics::CLineItem Array[1024];
22 int NumItems = 0;
23 Graphics()->TextureClear();
24 Graphics()->LinesBegin();
25 for(int y = 0; y < m_Height; y++)
26 for(int x = 0; x < m_Width; x++)
27 {
28 vec2 Pos(x * m_Spacing, y * m_Spacing);
29 vec2 Vel = m_pCells[y * m_Width + x].m_Vel * 0.01f;
30 Array[NumItems++] = IGraphics::CLineItem(Pos.x, Pos.y, Pos.x + Vel.x, Pos.y + Vel.y);
31 if(NumItems == 1024)
32 {
33 Graphics()->LinesDraw(pArray: Array, Num: 1024);
34 NumItems = 0;
35 }
36 }
37
38 if(NumItems)
39 Graphics()->LinesDraw(pArray: Array, Num: NumItems);
40 Graphics()->LinesEnd();
41}
42
43void CFlow::Init()
44{
45 free(ptr: m_pCells);
46 m_pCells = 0;
47
48 CMapItemLayerTilemap *pTilemap = Layers()->GameLayer();
49 m_Width = pTilemap->m_Width * 32 / m_Spacing;
50 m_Height = pTilemap->m_Height * 32 / m_Spacing;
51
52 // allocate and clear
53 m_pCells = (CCell *)calloc(nmemb: (size_t)m_Width * m_Height, size: sizeof(CCell));
54 for(int y = 0; y < m_Height; y++)
55 for(int x = 0; x < m_Width; x++)
56 m_pCells[y * m_Width + x].m_Vel = vec2(0.0f, 0.0f);
57}
58
59void CFlow::Update()
60{
61 if(!m_pCells)
62 return;
63
64 for(int y = 0; y < m_Height; y++)
65 for(int x = 0; x < m_Width; x++)
66 m_pCells[y * m_Width + x].m_Vel *= 0.85f;
67}
68
69vec2 CFlow::Get(vec2 Pos)
70{
71 if(!m_pCells)
72 return vec2(0, 0);
73
74 int x = (int)(Pos.x / m_Spacing);
75 int y = (int)(Pos.y / m_Spacing);
76 if(x < 0 || y < 0 || x >= m_Width || y >= m_Height)
77 return vec2(0, 0);
78
79 return m_pCells[y * m_Width + x].m_Vel;
80}
81
82void CFlow::Add(vec2 Pos, vec2 Vel, float Size)
83{
84 if(!m_pCells)
85 return;
86
87 int x = (int)(Pos.x / m_Spacing);
88 int y = (int)(Pos.y / m_Spacing);
89 if(x < 0 || y < 0 || x >= m_Width || y >= m_Height)
90 return;
91
92 m_pCells[y * m_Width + x].m_Vel += Vel;
93}
94