1 /* Copyright (C) 2017 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #include "precompiled.h" 19 20 #include "simulation2/system/Component.h" 21 #include "ICmpTest.h" 22 23 #include "simulation2/scripting/ScriptComponent.h" 24 #include "simulation2/MessageTypes.h" 25 26 #include "ps/Profile.h" 27 28 class CCmpTest1A : public ICmpTest1 29 { 30 public: ClassInit(CComponentManager & componentManager)31 static void ClassInit(CComponentManager& componentManager) 32 { 33 componentManager.SubscribeToMessageType(MT_TurnStart); 34 componentManager.SubscribeToMessageType(MT_Interpolate); 35 componentManager.SubscribeToMessageType(MT_Destroy); 36 } 37 38 DEFAULT_COMPONENT_ALLOCATOR(Test1A) 39 40 int32_t m_x; 41 GetSchema()42 static std::string GetSchema() 43 { 44 return "<a:component type='test'/><ref name='anything'/>"; 45 } 46 Init(const CParamNode & paramNode)47 virtual void Init(const CParamNode& paramNode) 48 { 49 if (paramNode.GetChild("x").IsOk()) 50 m_x = paramNode.GetChild("x").ToInt(); 51 else 52 m_x = 11000; 53 } 54 Deinit()55 virtual void Deinit() 56 { 57 } 58 Serialize(ISerializer & serialize)59 virtual void Serialize(ISerializer& serialize) 60 { 61 serialize.NumberI32_Unbounded("x", m_x); 62 } 63 Deserialize(const CParamNode & UNUSED (paramNode),IDeserializer & deserialize)64 virtual void Deserialize(const CParamNode& UNUSED(paramNode), IDeserializer& deserialize) 65 { 66 deserialize.NumberI32_Unbounded("x", m_x); 67 } 68 GetX()69 virtual int GetX() 70 { 71 return m_x; 72 } 73 HandleMessage(const CMessage & msg,bool UNUSED (global))74 virtual void HandleMessage(const CMessage& msg, bool UNUSED(global)) 75 { 76 switch (msg.GetType()) 77 { 78 case MT_Destroy: 79 GetSimContext().GetComponentManager().DynamicSubscriptionNonsync(MT_RenderSubmit, this, false); 80 break; 81 case MT_TurnStart: 82 m_x += 1; 83 break; 84 case MT_Interpolate: 85 m_x += 2; 86 break; 87 default: 88 m_x = 0; 89 break; 90 } 91 } 92 }; 93 94 REGISTER_COMPONENT_TYPE(Test1A) 95 96 class CCmpTest1B : public ICmpTest1 97 { 98 public: ClassInit(CComponentManager & componentManager)99 static void ClassInit(CComponentManager& componentManager) 100 { 101 componentManager.SubscribeToMessageType(MT_Update); 102 componentManager.SubscribeGloballyToMessageType(MT_Interpolate); 103 } 104 105 DEFAULT_COMPONENT_ALLOCATOR(Test1B) 106 107 int32_t m_x; 108 GetSchema()109 static std::string GetSchema() 110 { 111 return "<a:component type='test'/><empty/>"; 112 } 113 Init(const CParamNode &)114 virtual void Init(const CParamNode&) 115 { 116 m_x = 12000; 117 } 118 Deinit()119 virtual void Deinit() 120 { 121 } 122 Serialize(ISerializer & serialize)123 virtual void Serialize(ISerializer& serialize) 124 { 125 serialize.NumberI32_Unbounded("x", m_x); 126 } 127 Deserialize(const CParamNode & UNUSED (paramNode),IDeserializer & deserialize)128 virtual void Deserialize(const CParamNode& UNUSED(paramNode), IDeserializer& deserialize) 129 { 130 deserialize.NumberI32_Unbounded("x", m_x); 131 } 132 GetX()133 virtual int GetX() 134 { 135 return m_x; 136 } 137 HandleMessage(const CMessage & msg,bool UNUSED (global))138 virtual void HandleMessage(const CMessage& msg, bool UNUSED(global)) 139 { 140 switch (msg.GetType()) 141 { 142 case MT_Update: 143 m_x += 10; 144 break; 145 case MT_Interpolate: 146 m_x += 20; 147 break; 148 default: 149 m_x = 0; 150 break; 151 } 152 } 153 }; 154 155 REGISTER_COMPONENT_TYPE(Test1B) 156 157 class CCmpTest2A : public ICmpTest2 158 { 159 public: ClassInit(CComponentManager & componentManager)160 static void ClassInit(CComponentManager& componentManager) 161 { 162 componentManager.SubscribeToMessageType(MT_TurnStart); 163 componentManager.SubscribeToMessageType(MT_Update); 164 } 165 166 DEFAULT_COMPONENT_ALLOCATOR(Test2A) 167 168 int32_t m_x; 169 GetSchema()170 static std::string GetSchema() 171 { 172 return "<a:component type='test'/><empty/>"; 173 } 174 Init(const CParamNode &)175 virtual void Init(const CParamNode&) 176 { 177 m_x = 21000; 178 } 179 Deinit()180 virtual void Deinit() 181 { 182 } 183 Serialize(ISerializer & serialize)184 virtual void Serialize(ISerializer& serialize) 185 { 186 serialize.NumberI32_Unbounded("x", m_x); 187 } 188 Deserialize(const CParamNode & UNUSED (paramNode),IDeserializer & deserialize)189 virtual void Deserialize(const CParamNode& UNUSED(paramNode), IDeserializer& deserialize) 190 { 191 deserialize.NumberI32_Unbounded("x", m_x); 192 } 193 GetX()194 virtual int GetX() 195 { 196 return m_x; 197 } 198 HandleMessage(const CMessage & msg,bool UNUSED (global))199 virtual void HandleMessage(const CMessage& msg, bool UNUSED(global)) 200 { 201 switch (msg.GetType()) 202 { 203 case MT_TurnStart: 204 m_x += 50; 205 break; 206 case MT_Update: 207 m_x += static_cast<const CMessageUpdate&> (msg).turnLength.ToInt_RoundToZero(); 208 break; 209 default: 210 m_x = 0; 211 break; 212 } 213 } 214 }; 215 216 REGISTER_COMPONENT_TYPE(Test2A) 217 218 //////////////////////////////////////////////////////////////// 219 220 class CCmpTest1Scripted : public ICmpTest1 221 { 222 public: DEFAULT_SCRIPT_WRAPPER(Test1Scripted)223 DEFAULT_SCRIPT_WRAPPER(Test1Scripted) 224 225 virtual int GetX() 226 { 227 return m_Script.Call<int> ("GetX"); 228 } 229 }; 230 231 REGISTER_COMPONENT_SCRIPT_WRAPPER(Test1Scripted) 232 233 //////////////////////////////////////////////////////////////// 234 235 class CCmpTest2Scripted : public ICmpTest2 236 { 237 public: DEFAULT_SCRIPT_WRAPPER(Test2Scripted)238 DEFAULT_SCRIPT_WRAPPER(Test2Scripted) 239 240 virtual int GetX() 241 { 242 return m_Script.Call<int> ("GetX"); 243 } 244 }; 245 246 REGISTER_COMPONENT_SCRIPT_WRAPPER(Test2Scripted) 247