Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/components/CCmpWaterManager.cpp
Show All 14 Lines | |||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
#include "precompiled.h" | #include "precompiled.h" | ||||
#include "simulation2/system/Component.h" | #include "simulation2/system/Component.h" | ||||
#include "ICmpWaterManager.h" | #include "ICmpWaterManager.h" | ||||
#include "graphics/GameView.h" | |||||
#include "graphics/RenderableObject.h" | #include "graphics/RenderableObject.h" | ||||
#include "graphics/Terrain.h" | #include "graphics/Terrain.h" | ||||
#include "graphics/WaterManager.h" | |||||
#include "ps/Game.h" | |||||
#include "renderer/Renderer.h" | #include "renderer/Renderer.h" | ||||
#include "renderer/WaterManager.h" | |||||
#include "simulation2/MessageTypes.h" | #include "simulation2/MessageTypes.h" | ||||
#include "simulation2/components/ICmpTerrain.h" | |||||
#include "tools/atlas/GameInterface/GameLoop.h" | #include "tools/atlas/GameInterface/GameLoop.h" | ||||
class CCmpWaterManager : public ICmpWaterManager | class CCmpWaterManager : public ICmpWaterManager | ||||
{ | { | ||||
public: | public: | ||||
static void ClassInit(CComponentManager& componentManager) | static void ClassInit(CComponentManager& componentManager) | ||||
{ | { | ||||
// No need to subscribe to WaterChanged since we're actually the one sending those. | // No need to subscribe to WaterChanged since we're actually the one sending those. | ||||
componentManager.SubscribeToMessageType(MT_Interpolate); | |||||
componentManager.SubscribeToMessageType(MT_TerrainChanged); | componentManager.SubscribeToMessageType(MT_TerrainChanged); | ||||
} | } | ||||
DEFAULT_COMPONENT_ALLOCATOR(WaterManager) | DEFAULT_COMPONENT_ALLOCATOR(WaterManager) | ||||
// Dynamic state: | // Dynamic state: | ||||
entity_pos_t m_WaterHeight; | entity_pos_t m_WaterHeight; | ||||
static std::string GetSchema() | static std::string GetSchema() | ||||
{ | { | ||||
return "<a:component type='system'/><empty/>"; | return "<a:component type='system'/><empty/>"; | ||||
} | } | ||||
virtual void Init(const CParamNode& UNUSED(paramNode)) | virtual void Init(const CParamNode& UNUSED(paramNode)) | ||||
{ | { | ||||
LOGWARNING("Init"); | |||||
if (CRenderer::IsInitialised()) | |||||
g_Game->GetView()->GetMutableWaterManager().SetRenderingEnabled(true); | |||||
} | } | ||||
virtual void Deinit() | virtual void Deinit() | ||||
{ | { | ||||
// Clear the map size & data. | |||||
if (CRenderer::IsInitialised()) | |||||
g_Renderer.GetWaterManager()->SetMapSize(0); | |||||
} | } | ||||
virtual void Serialize(ISerializer& serialize) | virtual void Serialize(ISerializer& serialize) | ||||
{ | { | ||||
serialize.NumberFixed_Unbounded("height", m_WaterHeight); | serialize.NumberFixed_Unbounded("height", m_WaterHeight); | ||||
} | } | ||||
virtual void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize) | virtual void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize) | ||||
{ | { | ||||
Init(paramNode); | Init(paramNode); | ||||
deserialize.NumberFixed_Unbounded("height", m_WaterHeight); | deserialize.NumberFixed_Unbounded("height", m_WaterHeight); | ||||
if (CRenderer::IsInitialised()) | if (CRenderer::IsInitialised()) | ||||
g_Renderer.GetWaterManager()->SetMapSize(GetSimContext().GetTerrain().GetVerticesPerSide()); | g_Game->GetView()->GetMutableWaterManager().SetMapSize(GetSimContext().GetTerrain().GetVerticesPerSide()); | ||||
RecomputeWaterData(); | |||||
} | } | ||||
virtual void HandleMessage(const CMessage& msg, bool UNUSED(global)) | virtual void HandleMessage(const CMessage& msg, bool UNUSED(global)) | ||||
{ | { | ||||
switch (msg.GetType()) | switch (msg.GetType()) | ||||
{ | { | ||||
case MT_Interpolate: | |||||
{ | |||||
const CMessageInterpolate& msgData = static_cast<const CMessageInterpolate&> (msg); | |||||
if (CRenderer::IsInitialised()) | |||||
g_Renderer.GetWaterManager()->m_WaterTexTimer += msgData.deltaSimTime; | |||||
break; | |||||
} | |||||
case MT_TerrainChanged: | case MT_TerrainChanged: | ||||
{ | { | ||||
LOGWARNING("TerrainChange"); | |||||
// Tell the renderer to redraw part of the map. | // Tell the renderer to redraw part of the map. | ||||
if (CRenderer::IsInitialised()) | if (CRenderer::IsInitialised()) | ||||
{ | { | ||||
const CMessageTerrainChanged& msgData = static_cast<const CMessageTerrainChanged&> (msg); | const CMessageTerrainChanged& msgData = static_cast<const CMessageTerrainChanged&> (msg); | ||||
GetSimContext().GetTerrain().MakeDirty(msgData.i0,msgData.j0,msgData.i1,msgData.j1,RENDERDATA_UPDATE_VERTICES); | g_Game->GetView()->GetMutableWaterManager().SetMapSize(GetSimContext().GetTerrain().GetVerticesPerSide()); | ||||
g_Game->GetView()->GetMutableWaterManager().MarkDirty(msgData.i0, msgData.i1, msgData.j0, msgData.j1); | |||||
} | } | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
virtual void RecomputeWaterData() | |||||
{ | |||||
if (CRenderer::IsInitialised()) | |||||
{ | |||||
g_Renderer.GetWaterManager()->RecomputeWaterData(); | |||||
g_Renderer.GetWaterManager()->m_WaterHeight = m_WaterHeight.ToFloat(); | |||||
} | |||||
// Tell the terrain it'll need to recompute its cached render data | |||||
GetSimContext().GetTerrain().MakeDirty(RENDERDATA_UPDATE_VERTICES); | |||||
} | |||||
virtual void SetWaterLevel(entity_pos_t h) | virtual void SetWaterLevel(entity_pos_t h) | ||||
wraitii: Feel like this should be moved to CWater, but that might be a separate diff | |||||
{ | { | ||||
if (m_WaterHeight == h) | if (m_WaterHeight == h) | ||||
return; | return; | ||||
m_WaterHeight = h; | m_WaterHeight = h; | ||||
RecomputeWaterData(); | LOGWARNING("SetWaterLevel"); | ||||
if (CRenderer::IsInitialised()) | |||||
{ | |||||
g_Game->GetView()->GetMutableWaterManager().SetWaterHeight(m_WaterHeight.ToFloat()); | |||||
CmpPtr<ICmpTerrain> cmpTerrain(GetSystemEntity()); | |||||
if (cmpTerrain && cmpTerrain->IsLoaded()) | |||||
{ | |||||
i32 size = cmpTerrain->GetVerticesPerSide(); | |||||
g_Game->GetView()->GetMutableWaterManager().MarkDirty(0, size, 0, size); | |||||
} | |||||
} | |||||
Done Inline ActionsCould early return earlier? Stan: Could early return earlier? | |||||
Done Inline Actionsmeh wraitii: meh | |||||
CMessageWaterChanged msg; | CMessageWaterChanged msg; | ||||
GetSimContext().GetComponentManager().BroadcastMessage(msg); | GetSimContext().GetComponentManager().BroadcastMessage(msg); | ||||
} | } | ||||
virtual entity_pos_t GetWaterLevel(entity_pos_t UNUSED(x), entity_pos_t UNUSED(z)) const | virtual entity_pos_t GetWaterLevel(entity_pos_t UNUSED(x), entity_pos_t UNUSED(z)) const | ||||
{ | { | ||||
return m_WaterHeight; | return m_WaterHeight; | ||||
} | } | ||||
virtual float GetExactWaterLevel(float UNUSED(x), float UNUSED(z)) const | virtual float GetExactWaterLevel(float UNUSED(x), float UNUSED(z)) const | ||||
{ | { | ||||
return m_WaterHeight.ToFloat(); | return m_WaterHeight.ToFloat(); | ||||
} | } | ||||
Done Inline ActionsDo we need x and z? If yes in the future then it should be in CWater as well. vladislavbelov: Do we need `x` and `z`? If yes in the future then it should be in `CWater` as well. | |||||
}; | }; | ||||
REGISTER_COMPONENT_TYPE(WaterManager) | REGISTER_COMPONENT_TYPE(WaterManager) |
Wildfire Games · Phabricator
Feel like this should be moved to CWater, but that might be a separate diff