So, with this new conceptual programming I am playing with things are setup like this: All objects derive from cEntity. The cEntity object has tow maps of values, strings and doubles. The cEntity class has API functions to Get, Set, Modfiy, or Remove attributes. The cEntity class also has private API functions that are invoked when those public API functions are used. Here is that class:
class cEntity{private: CRITICAL_SECTION cs; std::map classStrings; std::mapdouble
protected:
bool SetNumber(const std::string & name, double value);
bool RemoveNumber(const std::string & name);
bool ModifyNumber(const std::string & name, double value, double * outValue);
bool GetNumber(const std::string & name, double & outValue);
bool SetString(const std::string & name, std::string value);
bool RemoveString(const std::string & name);
bool GetString(const std::string & name, std::string & outValue);
virtual bool OnSetNumber(cEntity * entity, const std::string & name, double & value);
virtual bool OnRemoveNumber(cEntity * entity, const std::string & name);
virtual bool OnModifyNumber(cEntity * entity, const std::string & name, double value, double * outValue);
virtual bool OnGetNumber(cEntity * entity, const std::string & name, double & outValue);
virtual bool OnSetString(cEntity * entity, const std::string & name, std::string value);
virtual bool OnRemoveString(cEntity * entity, const std::string & name);
virtual bool OnGetString(cEntity * entity, const std::string & name, std::string & outValue);
public:
cEntity();
virtual ~cEntity();
cEntity(const cEntity & rhs);
cEntity & operator =(const cEntity & rhs);
bool SetNumber(cEntity * entity, const std::string & name, double value);
bool RemoveNumber(cEntity * entity, const std::string & name);
bool ModifyNumber(cEntity * entity, const std::string & name, double value, double * outValue);
bool GetNumber(cEntity * entity, const std::string & name, double & outValue);
bool SetString(cEntity * entity, const std::string & name, std::string value);
bool RemoveString(cEntity * entity, const std::string & name);
bool GetString(cEntity * entity, const std::string & name, std::string & outValue);
virtual void Interact(cEntity * entity);
virtual void Update(double delta);
};
Now this class is still being worked on but that is the basics of it all. The Interact function is an interface function that represents one entity interacting with another. The update would represent real time "processing" of all entities, but is not used yet.
Here is an example use of this setup.
#ifndef ENTITY_H_ #include "Entity.h"#endif//-----------------------------------------------------------------------------class cBox : public cEntity{private:public: cBox() { SetString("name", "Box"); SetNumber("x", 0); SetNumber("y", 0); SetNumber("z", 0); SetNumber("gravity", 1); }};class cSpaceBox : public cEntity{private:public: cSpaceBox() { SetString("name", "SpaceBox"); SetNumber("x", 0); SetNumber("y", 0); SetNumber("z", 0); }};class cImpossibleBox : public cEntity{private:public: cImpossibleBox() { SetString("name", "ImpossibleBox"); SetNumber("x", 0); SetNumber("y", 0); SetNumber("z", 0); SetNumber("gravity", 1); } bool OnModifyNumber(cEntity * entity, const std::string & name, double value, double * outValue) { if(!entity || entity == this) return false; if(name == "y") { std::string ename; if(entity->GetString(this, "name", ename)) { if(ename == "Gravity") return false; } } return true; }};class cGravity : public cEntity{private:public: cGravity() { SetString("name", "Gravity"); SetNumber("g", -9.17); } void Interact(cEntity * entity) { double g = 0; double y = 0; double gravity = 0; if(entity->GetNumber(this, "gravity", gravity)) { if(entity->GetNumber(this, "y", y)) { GetNumber("g", g); printf("Position started at: %f\n", y); if(entity->ModifyNumber(this, "y", g * gravity, &y)) { entity->GetNumber(this, "y", y); printf("Position is now at: %f\n", y); } else { printf("Entity denied the affects of gravity.\n"); } } else { printf("Entity has not properties that can be affected by gravity.\n"); } } else { printf("Entity cannot be affected by gravity.\n"); } }};//-----------------------------------------------------------------------------int main(int argc, char * argv[]){ cGravity gravity; cBox box1; cSpaceBox box2; cImpossibleBox box3; gravity.Interact(&box1); printf("\n"); gravity.Interact(&box2); printf("\n"); gravity.Interact(&box3); printf("\n"); return 0;}//-----------------------------------------------------------------------------
After running the program, the output is:
Position started at: 0.000000Position is now at: -9.170000Entity cannot be affected by gravity.Position started at: 0.000000Entity denied the affects of gravity.Press any key to continue . . .
Whoa. Just imagine being able to have such control over any interactions in real life haha! Good bye gravity! Anyways, the design is meant to allow freedom and flexibility in terms of what an object represents as well as how it is modified. From a technological standpoint, anything would be possible as seen with preventing gravity. You could make gravity inversed so you actually float up or reduce/strengthen the effects of it. Of course, I am just using gravity as the prime example, any "force" would work the same, including things that we do not have, such as "side ways" gravity, which more or less would be a constant wind.
Moving on, I don't have any practical things to do with this concept, just experimenting. R&D so to speak. There are some problems that I've yet to really address. For example, let's assume I have my small universe of objects. Naturally due to how computers work, I will be handling Interactions and Updates in a sequential manner. Because of this, I do not have a way of implementing "order" or "priority" in the world. This would mean the implementation of the world is up to the actual program and not the class itself. I.e. one program might implement a number property that is a 1,2,3 and establishes a priority like that and another program might use a different system.
My idea was, if even possible, somehow have a game where players create their interactions with world objects. This is a bit vague so let me make up an example of what could be possible.
Start out with a cEntity that is a virtual tree. Let's say it has a number property of "wood" as 5000. Now, the player has their own cEntity that represents them. By intuition, the player Interacts with the tree and learns it has wood that is harvestable. For the sake of simplicity, let's say the player calls the ModifyNumber function of the tree with the "wood" property and a value of 1. The tree object verifies the operation in its OnModifyNumber and either allows the operation or denies it. Let's say it was successful and it then sets the wood property for the player to 1.
Not the best example and a lot of questions result from that, but that's the gist of it all. I think it'd be interesting if properly implemented, so I am going to keep playing with the idea until I get distracted by something else or make a breakthrough.