मेरा पसंदीदा खेल इंजन संरचना लगभग सभी भागों के बीच संचार के लिए संदेश का उपयोग करके इंटरफ़ेस और ऑब्जेक्ट <-> घटक मॉडल है।
आपके मुख्य प्रबंधक भागों जैसे आपके दृश्य प्रबंधक, संसाधन लोडर, ऑडियो, रेंडरर, भौतिकी, आदि के लिए कई इंटरफेस हैं।
मेरे पास 3D दृश्य / दुनिया में सभी वस्तुओं के प्रभारी प्रबंधक हैं।
ऑब्जेक्ट एक बहुत ही परमाणु वर्ग है, जिसमें केवल कुछ चीजें हैं जो आपके दृश्य में लगभग सब कुछ सामान्य हैं, मेरे इंजन में ऑब्जेक्ट क्लास केवल स्थिति, रोटेशन, घटकों की एक सूची और एक अद्वितीय आईडी रखती है। प्रत्येक ऑब्जेक्ट की आईडी एक स्थैतिक इंट द्वारा बनाई गई है, ताकि कोई भी दो ऑब्जेक्ट्स में एक ही आईडी न हो, इससे आप ऑब्जेक्ट के लिए पॉइंटर रखने के बजाय इसकी आईडी द्वारा किसी ऑब्जेक्ट को संदेश भेज सकते हैं।
ऑब्जेक्ट पर घटकों की सूची वह है जो बताती है कि ऑब्जेक्ट मुख्य गुण हैं। उदाहरण के लिए, किसी ऐसी चीज के लिए जिसे आप 3D दुनिया में देख सकते हैं, आप अपनी वस्तु को एक रेंडर घटक देंगे जिसमें रेंडर मेष के बारे में जानकारी होगी। यदि आप भौतिकी के लिए एक वस्तु चाहते हैं तो आप इसे एक भौतिक घटक देंगे। यदि आप एक कैमरा के रूप में कार्य करना चाहते हैं, तो इसे एक कैमरा घटक दें। घटकों की सूची पर और पर जा सकते हैं।
इंटरफेस, ऑब्जेक्ट्स और कंपोनेंट्स के बीच कम्युनिकेशन की कुंजी है। मेरे इंजन में मेरे पास एक सामान्य संदेश वर्ग है जिसमें केवल एक विशिष्ट आईडी और एक संदेश प्रकार आईडी है। यूनिक आईडी उस वस्तु की आईडी है जिसे आप संदेश में जाना चाहते हैं, और संदेश टाइप आईडी का उपयोग संदेश प्राप्त करने वाली वस्तु द्वारा किया जाता है ताकि यह पता चले कि यह किस प्रकार का संदेश है।
ऑब्जेक्ट्स संदेश को संभाल सकते हैं यदि उन्हें ज़रूरत है, और वे अपने प्रत्येक घटक पर संदेश पास कर सकते हैं, और घटक अक्सर संदेश के साथ महत्वपूर्ण काम करेंगे। उदाहरण के लिए, यदि आप ऑब्जेक्ट को बदलना चाहते हैं और ऑब्जेक्ट की स्थिति भेजते हैं, तो ऑब्जेक्ट सेटपेसन संदेश भेजता है, ऑब्जेक्ट मैसेज मिलने पर इसके पोजीशन चर को अपडेट कर सकता है, लेकिन रेंडर घटक की स्थिति को अपडेट करने के लिए रेंडर घटक को संदेश की आवश्यकता हो सकती है, और भौतिकी घटक को भौतिकी शरीर की स्थिति को अद्यतन करने के लिए संदेश की आवश्यकता हो सकती है।
यहाँ दृश्य प्रबंधक, ऑब्जेक्ट और घटक और संदेश प्रवाह का एक बहुत ही सरल लेआउट है, जिसे मैंने C ++ में लिखा है, लगभग एक घंटे में मार पड़ी है। जब इसे चलाया जाता है तो यह ऑब्जेक्ट पर स्थिति सेट करता है, और संदेश रेंडर घटक से गुजरता है, फिर ऑब्जेक्ट से स्थिति को पुनः प्राप्त करता है। का आनंद लें!
साथ ही, मैंने किसी भी व्यक्ति के लिए नीचे दिए गए कोड का C # संस्करण और स्काला संस्करण लिखा है जो C ++ के बजाय उन में धाराप्रवाह हो सकता है।
#include <iostream>
#include <stdio.h>
#include <list>
#include <map>
using namespace std;
struct Vector3
{
public:
Vector3() : x(0.0f), y(0.0f), z(0.0f)
{}
float x, y, z;
};
enum eMessageType
{
SetPosition,
GetPosition,
};
class BaseMessage
{
protected: // Abstract class, constructor is protected
BaseMessage(int destinationObjectID, eMessageType messageTypeID)
: m_destObjectID(destinationObjectID)
, m_messageTypeID(messageTypeID)
{}
public: // Normally this isn't public, just doing it to keep code small
int m_destObjectID;
eMessageType m_messageTypeID;
};
class PositionMessage : public BaseMessage
{
protected: // Abstract class, constructor is protected
PositionMessage(int destinationObjectID, eMessageType messageTypeID,
float X = 0.0f, float Y = 0.0f, float Z = 0.0f)
: BaseMessage(destinationObjectID, messageTypeID)
, x(X)
, y(Y)
, z(Z)
{
}
public:
float x, y, z;
};
class MsgSetPosition : public PositionMessage
{
public:
MsgSetPosition(int destinationObjectID, float X, float Y, float Z)
: PositionMessage(destinationObjectID, SetPosition, X, Y, Z)
{}
};
class MsgGetPosition : public PositionMessage
{
public:
MsgGetPosition(int destinationObjectID)
: PositionMessage(destinationObjectID, GetPosition)
{}
};
class BaseComponent
{
public:
virtual bool SendMessage(BaseMessage* msg) { return false; }
};
class RenderComponent : public BaseComponent
{
public:
/*override*/ bool SendMessage(BaseMessage* msg)
{
// Object has a switch for any messages it cares about
switch(msg->m_messageTypeID)
{
case SetPosition:
{
// Update render mesh position/translation
cout << "RenderComponent handling SetPosition\n";
}
break;
default:
return BaseComponent::SendMessage(msg);
}
return true;
}
};
class Object
{
public:
Object(int uniqueID)
: m_UniqueID(uniqueID)
{
}
int GetObjectID() const { return m_UniqueID; }
void AddComponent(BaseComponent* comp)
{
m_Components.push_back(comp);
}
bool SendMessage(BaseMessage* msg)
{
bool messageHandled = false;
// Object has a switch for any messages it cares about
switch(msg->m_messageTypeID)
{
case SetPosition:
{
MsgSetPosition* msgSetPos = static_cast<MsgSetPosition*>(msg);
m_Position.x = msgSetPos->x;
m_Position.y = msgSetPos->y;
m_Position.z = msgSetPos->z;
messageHandled = true;
cout << "Object handled SetPosition\n";
}
break;
case GetPosition:
{
MsgGetPosition* msgSetPos = static_cast<MsgGetPosition*>(msg);
msgSetPos->x = m_Position.x;
msgSetPos->y = m_Position.y;
msgSetPos->z = m_Position.z;
messageHandled = true;
cout << "Object handling GetPosition\n";
}
break;
default:
return PassMessageToComponents(msg);
}
// If the object didn't handle the message but the component
// did, we return true to signify it was handled by something.
messageHandled |= PassMessageToComponents(msg);
return messageHandled;
}
private: // Methods
bool PassMessageToComponents(BaseMessage* msg)
{
bool messageHandled = false;
auto compIt = m_Components.begin();
for ( compIt; compIt != m_Components.end(); ++compIt )
{
messageHandled |= (*compIt)->SendMessage(msg);
}
return messageHandled;
}
private: // Members
int m_UniqueID;
std::list<BaseComponent*> m_Components;
Vector3 m_Position;
};
class SceneManager
{
public:
// Returns true if the object or any components handled the message
bool SendMessage(BaseMessage* msg)
{
// We look for the object in the scene by its ID
std::map<int, Object*>::iterator objIt = m_Objects.find(msg->m_destObjectID);
if ( objIt != m_Objects.end() )
{
// Object was found, so send it the message
return objIt->second->SendMessage(msg);
}
// Object with the specified ID wasn't found
return false;
}
Object* CreateObject()
{
Object* newObj = new Object(nextObjectID++);
m_Objects[newObj->GetObjectID()] = newObj;
return newObj;
}
private:
std::map<int, Object*> m_Objects;
static int nextObjectID;
};
// Initialize our static unique objectID generator
int SceneManager::nextObjectID = 0;
int main()
{
// Create a scene manager
SceneManager sceneMgr;
// Have scene manager create an object for us, which
// automatically puts the object into the scene as well
Object* myObj = sceneMgr.CreateObject();
// Create a render component
RenderComponent* renderComp = new RenderComponent();
// Attach render component to the object we made
myObj->AddComponent(renderComp);
// Set 'myObj' position to (1, 2, 3)
MsgSetPosition msgSetPos(myObj->GetObjectID(), 1.0f, 2.0f, 3.0f);
sceneMgr.SendMessage(&msgSetPos);
cout << "Position set to (1, 2, 3) on object with ID: " << myObj->GetObjectID() << '\n';
cout << "Retreiving position from object with ID: " << myObj->GetObjectID() << '\n';
// Get 'myObj' position to verify it was set properly
MsgGetPosition msgGetPos(myObj->GetObjectID());
sceneMgr.SendMessage(&msgGetPos);
cout << "X: " << msgGetPos.x << '\n';
cout << "Y: " << msgGetPos.y << '\n';
cout << "Z: " << msgGetPos.z << '\n';
}