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
5#include <engine/graphics.h>
6
7#include <game/layers.h>
8#include <game/mapitems.h>
9
10CFlow::CFlow()
11{
12 m_pCells = nullptr;
13 m_Height = 0;
14 m_Width = 0;
15 m_Spacing = 16;
16}
17
18void CFlow::DbgRender()
19{
20 if(!m_pCells)
21 return;
22
23 Graphics()->TextureClear();
24 IGraphics::CLineItemBatch LineItemBatch;
25 Graphics()->LinesBatchBegin(pBatch: &LineItemBatch);
26 for(int y = 0; y < m_Height; y++)
27 {
28 for(int x = 0; x < m_Width; x++)
29 {
30 vec2 Pos(x * m_Spacing, y * m_Spacing);
31 vec2 Vel = m_pCells[y * m_Width + x].m_Vel * 0.01f;
32 const IGraphics::CLineItem Item = IGraphics::CLineItem(Pos.x, Pos.y, Pos.x + Vel.x, Pos.y + Vel.y);
33 Graphics()->LinesBatchDraw(pBatch: &LineItemBatch, pArray: &Item, Num: 1);
34 }
35 }
36 Graphics()->LinesBatchEnd(pBatch: &LineItemBatch);
37}
38
39void CFlow::Init()
40{
41 free(ptr: m_pCells);
42 m_pCells = nullptr;
43
44 CMapItemLayerTilemap *pTilemap = Layers()->GameLayer();
45 m_Width = pTilemap->m_Width * 32 / m_Spacing;
46 m_Height = pTilemap->m_Height * 32 / m_Spacing;
47
48 // allocate and clear
49 m_pCells = (CCell *)calloc(nmemb: (size_t)m_Width * m_Height, size: sizeof(CCell));
50 for(int y = 0; y < m_Height; y++)
51 for(int x = 0; x < m_Width; x++)
52 m_pCells[y * m_Width + x].m_Vel = vec2(0.0f, 0.0f);
53}
54
55void CFlow::Update()
56{
57 if(!m_pCells)
58 return;
59
60 for(int y = 0; y < m_Height; y++)
61 for(int x = 0; x < m_Width; x++)
62 m_pCells[y * m_Width + x].m_Vel *= 0.85f;
63}
64
65vec2 CFlow::Get(vec2 Pos)
66{
67 if(!m_pCells)
68 return vec2(0, 0);
69
70 int x = (int)(Pos.x / m_Spacing);
71 int y = (int)(Pos.y / m_Spacing);
72 if(x < 0 || y < 0 || x >= m_Width || y >= m_Height)
73 return vec2(0, 0);
74
75 return m_pCells[y * m_Width + x].m_Vel;
76}
77
78void CFlow::Add(vec2 Pos, vec2 Vel, float Size)
79{
80 if(!m_pCells)
81 return;
82
83 int x = (int)(Pos.x / m_Spacing);
84 int y = (int)(Pos.y / m_Spacing);
85 if(x < 0 || y < 0 || x >= m_Width || y >= m_Height)
86 return;
87
88 m_pCells[y * m_Width + x].m_Vel += Vel;
89}
90