1 /* Copyright (C) 2010 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 #ifndef INCLUDED_ICOMPONENT
19 #define INCLUDED_ICOMPONENT
20 
21 #include "Components.h"
22 #include "Message.h"
23 #include "Entity.h"
24 #include "SimContext.h"
25 
26 #include "scriptinterface/ScriptTypes.h"
27 
28 class CParamNode;
29 class CMessage;
30 class ISerializer;
31 class IDeserializer;
32 
33 class IComponent
34 {
35 public:
36 	virtual ~IComponent();
37 
38 	static std::string GetSchema();
39 
40 	virtual void Init(const CParamNode& paramNode) = 0;
41 	virtual void Deinit() = 0;
42 
43 	virtual void HandleMessage(const CMessage& msg, bool global);
44 
GetEntityHandle()45 	CEntityHandle GetEntityHandle() const { return m_EntityHandle; }
SetEntityHandle(CEntityHandle ent)46 	void SetEntityHandle(CEntityHandle ent) { m_EntityHandle = ent; }
47 
GetEntityId()48 	entity_id_t GetEntityId() const { return m_EntityHandle.GetId(); }
49 
GetSystemEntity()50 	CEntityHandle GetSystemEntity() const { return m_SimContext->GetSystemEntity(); }
51 
GetSimContext()52 	const CSimContext& GetSimContext() const { return *m_SimContext; }
SetSimContext(const CSimContext & context)53 	void SetSimContext(const CSimContext& context) { m_SimContext = &context; }
54 
GetSerializationVersion()55 	static u8 GetSerializationVersion() { return 0; }
56 	virtual void Serialize(ISerializer& serialize) = 0;
57 	virtual void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize) = 0;
58 
59 	/**
60 	 * Returns false by default, indicating that a scripted wrapper of this IComponent is not supported.
61 	 * Derrived classes should return true if they implement such a wrapper.
62 	 */
63 	virtual bool NewJSObject(const ScriptInterface& scriptInterface, JS::MutableHandleObject out) const;
64 	virtual JS::Value GetJSInstance() const;
65 	virtual int GetComponentTypeId() const = 0;
66 
67 private:
68 	CEntityHandle m_EntityHandle;
69 	const CSimContext* m_SimContext;
70 };
71 
72 #endif // INCLUDED_ICOMPONENT
73