1
- #ifndef _RULES_H
2
- #define _RULES_H
1
+ #ifndef _DYNAMICTYPING_H
2
+ #define _DYNAMICTYPING_H
3
+
4
+ // Provides dynamic classification (attributes, flags, inheritence) of objects for
5
+ // scripting. Users can define types and flags and tag them to existing scriptable
6
+ // objects for better user game logic.
3
7
4
8
#include < vector>
9
+ #include < map>
10
+ #include < algorithm>
5
11
#include < boost/dynamic_bitset.hpp>
6
- // Rules
7
- // Lays out the game mechanics, items, weapons, and provides scriptable interface to them
12
+ #include " IScriptable.h"
8
13
9
- // eventually stuff like this (less stringly tho, using ID system preferably):
10
14
// rules->addType("rocket")
11
15
// rules->classSpec("rocket")->inherit("ammo")
12
16
// inventory->addItem("rocket", 5);
13
17
// if (inventory->anyOfType("ammo")) {
14
18
// // etc...
15
19
// }
16
20
17
- class ClassSpec
21
+ class DynamicType
18
22
{
19
23
public:
20
24
21
- ClassSpec (const std::string& name):
25
+ DynamicType (const std::string& name):
22
26
m_sName (name)
23
27
{}
24
- virtual ~ClassSpec () {}
28
+ virtual ~DynamicType () {}
25
29
26
30
bool inherit (unsigned int id) {
27
- if (m_Types.find ( id) != m_Types.end ())
31
+ if (std::find ( m_Types.begin (), m_Types. end (), id) != m_Types.end ())
28
32
return false ;
29
33
m_Types.push_back (id);
30
- return true ;
34
+ return true ;
31
35
}
32
36
unsigned int id () const { return m_ID; }
33
37
const std::string& name () const { return m_sName; }
34
38
35
39
private:
36
40
37
- unsigned int m_ID; // ID of type, also index in Rules ' class member m_Types
41
+ unsigned int m_ID; // ID of type, also index in DynamicContext ' class member m_Types
38
42
39
43
std::string m_sName; // name for scripting and lookup
40
44
std::vector<unsigned int > m_Types; // inherited type info
@@ -45,24 +49,24 @@ class ClassSpec
45
49
46
50
boost::dynamic_bitset<> m_Flags;
47
51
std::map<unsigned int , std::string> m_FlagNames;
48
- }
52
+ };
49
53
50
- class Rules : public IScriptable
54
+ class DynamicContext : public IScriptable
51
55
{
52
56
private:
53
57
54
58
public:
55
59
56
- std::vector<ClassSpec > m_Types; // indexed by type id (array position)
60
+ std::vector<DynamicType > m_Types; // indexed by type id (array position)
57
61
58
- Rules (std::string fn):
62
+ DynamicContext (std::string fn):
59
63
IScriptable (fn)
60
64
{
61
- m_Types.push_back (ClassSpec (" object" ));
65
+ m_Types.push_back (DynamicType (" object" ));
62
66
}
63
- virtual ~Rules () {}
67
+ virtual ~DynamicContext () {}
64
68
65
- unsigned int addType (ClassSpec spec) {
69
+ unsigned int addType (DynamicType spec) {
66
70
m_Types.push_back (spec);
67
71
return m_Types.size ()-1 ;
68
72
}
@@ -71,24 +75,19 @@ class Rules : public IScriptable
71
75
for (auto itr = m_Types.begin ();
72
76
itr != m_Types.end ();
73
77
++itr)
74
- if (spec. name () == name)
78
+ if (itr-> name () == name)
75
79
return 0 ;
76
80
else
77
81
return itr->id ();
78
82
}
79
83
80
84
// makes a new type that inherits from other types
81
- unsigned int inherit (std::string name, std::vector<unsigned int > types) {
82
- // TODO:
83
- return 0 ;
84
- }
85
+ unsigned int inherit (std::string name, std::vector<unsigned int > types);
85
86
86
87
// deep lookup if a type matches another type
87
88
// warning: one-way relationship possible here
88
89
// square is rectangle, rectangle is not always square, etc.
89
- bool isType (unsigned int type) const {
90
-
91
- }
90
+ bool isType (unsigned int type, unsigned int type_cmp);
92
91
};
93
92
94
93
#endif
0 commit comments