/[enigma]/enigma/src/stones_simple.cc
ViewVC logotype

Diff of /enigma/src/stones_simple.cc

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.32 by reallysoft, Wed Jul 9 07:01:05 2003 UTC revision 1.33 by reallysoft, Fri Jul 11 11:04:51 2003 UTC
# Line 27  Line 27 
27  #include "stones_internal.hh"  #include "stones_internal.hh"
28    
29  #include <cassert>  #include <cassert>
30    #include <vector>
31    
32  using namespace std;  using namespace std;
33  using namespace world;  using namespace world;
# Line 34  using namespace enigma; Line 35  using namespace enigma;
35  using namespace stones;  using namespace stones;
36    
37  //----------------------------------------  //----------------------------------------
38  // SimpleStone  // SimpleStoneTraits
39  //  //
40  // This kind of stone can be defined from Lua programs with  // This class stores some atrributes for SimpleStones.
41  // `DefineSimpleStone'.  // Only one instance is created for each type of SimpleStone.
42  //----------------------------------------  //----------------------------------------
43  namespace  namespace
44  {  {
45      class SimpleStone : public Stone {      class SimpleStoneTraits {
46      public:          string sound;           // collision sound
47          SimpleStone(const string &knd, const string & snd)          bool   hollow;          // whether stone is hollow
48              : Stone(knd.c_str()), sound(snd)          bool   glass;        // whether its a glass stone
49    
50            // static list of all allocated SimpleStoneTraits (never freed yet)
51            static vector<SimpleStoneTraits*> simple_stone_traits;
52    
53            SimpleStoneTraits(const string& sound_, bool hollow_, bool glass_)
54                : sound(sound_) , hollow(hollow_) , glass(glass_)
55          {}          {}
     private:  
         const char *collision_sound() {  
             return sound.c_str();  
         }  
56    
57          Object *clone() {      public:
58              return new SimpleStone(get_kind(), sound);          SimpleStoneTraits() {}
59    
60    //         static void clear() {
61    //             vector<SimpleStoneTraits*>::iterator i = simple_stone_traits.begin();
62    //             vector<SimpleStoneTraits*>::iterator e = simple_stone_traits.end();
63    //             for (; i != e; ++e)
64    //                 delete simple_stone_traits[i];
65    //             simple_stone_traits.clear();
66    //         }
67    
68            static const SimpleStoneTraits* Register(const string& sound_, bool hollow_, bool glass_) {
69                simple_stone_traits.push_back(new SimpleStoneTraits(sound_, hollow_, glass_));
70                return simple_stone_traits.back();
71          }          }
         void dispose() { delete this; }  
72    
73          string sound;          const string& get_sound() const { return sound; }
74            bool is_hollow() const { return hollow; }
75            bool is_glass() const { return glass; }
76      };      };
77    
78        vector<SimpleStoneTraits*> SimpleStoneTraits::simple_stone_traits;
79  }  }
80    
81  //----------------------------------------  //----------------------------------------
82  // SimpleStoneHollow  // SimpleStone
83  //  //
84  // This kind of stone can be defined from Lua programs with  // This kind of stone can be defined from Lua programs with
85  // `DefineSimpleStoneHollow'.  // `DefineSimpleStone'.
86  //----------------------------------------  //----------------------------------------
87  namespace  namespace
88  {  {
89      class SimpleStoneHollow : public Stone {      class SimpleStone : public Stone {
90      public:      public:
91          SimpleStoneHollow(const string &knd)          SimpleStone(const string &knd, const string & snd, bool hollow, bool is_glass)
92              : Stone(knd.c_str())              : Stone(knd.c_str())
93                , traits(SimpleStoneTraits::Register(snd, hollow, is_glass))
94          {}          {}
95      private:      private:
96          bool is_floating() const { return true; }          SimpleStone(const SimpleStone& other)
97                : Stone(other.get_kind())
98                , traits(other.traits)
99            {}
100    
101          Object *clone() {          Object *clone() { return new SimpleStone(*this); }
             return new SimpleStoneHollow(get_kind());  
         }  
         StoneResponse collision_response(const StoneContact &/*sc*/) {  
             return STONE_PASS;  
         }  
         bool on_laserhit(Direction) { return true; }  
102          void dispose() { delete this; }          void dispose() { delete this; }
     };  
 }  
103    
 //----------------------------------------  
 // SimpleStoneGlass  
 //  
 // This kind of stone can be defined from Lua programs with  
 // `DefineSimpleStoneGlass'.  
 //----------------------------------------  
 namespace  
 {  
     class SimpleStoneGlass : public Stone {  
     public:  
         SimpleStoneGlass(const string &knd, const string & snd)  
             : Stone(knd.c_str()), sound(snd)  
         {}  
     private:  
104          const char *collision_sound() {          const char *collision_sound() {
105              return sound.c_str();              return traits->get_sound().c_str();
106            }
107            StoneResponse collision_response(const StoneContact &/*sc*/) {
108                return traits->is_hollow() ? STONE_PASS : STONE_REBOUND;
109          }          }
110    
111          bool on_laserhit(Direction) {return true;};          bool is_floating() const {
112                return traits->is_hollow();
113          Object *clone() {          }
114              return new SimpleStoneGlass(get_kind(), sound);          bool on_laserhit(Direction) {
115                return traits->is_hollow() || traits->is_glass();
116          }          }
         void dispose() { delete this; }  
117    
118          string sound;          const SimpleStoneTraits *traits; // owned by simple_stone_traits
119      };      };
120  }  }
121    
# Line 120  namespace Line 123  namespace
123  // SimpleStoneMovable  // SimpleStoneMovable
124  //  //
125  // This kind of stone can be defined from Lua programs with  // This kind of stone can be defined from Lua programs with
126  // `DefineSimpleStoneMovable'.  // `DefineSimpleStone'.
127  //----------------------------------------  //----------------------------------------
128  namespace  namespace
129  {  {
130      class SimpleStoneMovable : public MovableStone {      class SimpleStoneMovable : public MovableStone {
131      public:      public:
132          SimpleStoneMovable(const string &knd, const string & snd)          SimpleStoneMovable(const string &knd, const string & snd, bool is_glass)
133              : MovableStone(knd.c_str()), sound(snd)              : MovableStone(knd.c_str())
134                , traits(SimpleStoneTraits::Register(snd, false, is_glass))
135          {}          {}
136    
137      private:      private:
138          const char *collision_sound() {          SimpleStoneMovable(const SimpleStoneMovable& other)
139              return sound.c_str();              : MovableStone(other.get_kind())
140          }              , traits(other.traits)
141            {}
142    
143          bool on_laserhit(Direction) { return false; }          Object *clone() { return new SimpleStoneMovable(*this); }
144            void dispose() { delete this; }
145    
146          Object *clone() {          const char *collision_sound() {
147              return new SimpleStoneMovable(get_kind(), sound);              return traits->get_sound().c_str();
148            }
149            bool on_laserhit(Direction) {
150                return traits->is_glass();
151          }          }
         void dispose() { delete this; }  
152    
153          string sound;          const SimpleStoneTraits *traits; // owned by simple_stone_traits
154      };      };
155  }  }
156    
# Line 2163  namespace Line 2172  namespace
2172    
2173    
2174  void  void
2175  world::DefineSimpleStone(const std::string &kind,  world::DefineSimpleStone(const std::string &kind, const std::string &sound,
2176                           const std::string &sound)                           int hollow, int glass)
 {  
     Register(new SimpleStone(kind,sound));  
 }  
   
 void  
 world::DefineSimpleStoneHollow(const std::string &kind)  
 {  
     Register(new SimpleStoneHollow(kind));  
 }  
   
 void  
 world::DefineSimpleStoneGlass(const std::string &kind,  
                          const std::string &sound)  
2177  {  {
2178      Register(new SimpleStoneGlass(kind,sound));      Register(new SimpleStone(kind, sound, hollow, glass));
2179  }  }
2180    
2181  void  void
2182  world::DefineSimpleStoneMovable(const std::string &kind,  world::DefineSimpleStoneMovable(const std::string &kind, const std::string &sound, int glass)
                          const std::string &sound)  
2183  {  {
2184      Register(new SimpleStoneMovable(kind,sound));      Register(new SimpleStoneMovable(kind, sound, glass));
2185  }  }
2186    
2187  // --------------------------------------------------------------------------------  // --------------------------------------------------------------------------------

Legend:
Removed from v.1.32  
changed lines
  Added in v.1.33

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26