00001 /* 00002 SagaEngine library 00003 Copyright (c) 2002-2006 Skalden Studio AS 00004 00005 This software is provided 'as-is', without any express or implied 00006 warranty. In no event will the authors be held liable for any 00007 damages arising from the use of this software. 00008 00009 Permission is granted to distribute the library under the terms of the 00010 Q Public License version 1.0. Be sure to read and understand the license 00011 before using the library. It should be included here, or you may read it 00012 at http://www.trolltech.com/products/qt/licenses/licensing/qpl 00013 00014 The original version of this library can be located at: 00015 http://www.sagaengine.com/ 00016 00017 Rune Myrland 00018 rune@skalden.com 00019 */ 00020 00021 00022 #include "SpawnManager.hpp" 00023 #include "SpawnComponent.hpp" 00024 #include "sim/schema/SimSchema.hpp" 00025 #include "comp/list/NodeComponentList.hpp" 00026 #include "util/error/Log.hpp" 00027 #include "comp/CompositeFactory.hpp" 00028 #include "comp/Composite.hpp" 00029 #include <cstring> 00030 00031 using namespace se_core; 00032 00033 namespace se_core { 00034 SpawnManager 00035 ::SpawnManager() 00036 : RootComponent(sct_SPAWN) 00037 , factoryCount_(0), destructionCount_(0) 00038 , nextDestructionCount_(0) { 00039 factories_ = new CompositeFactory*[ MAX_FACTORIES ]; 00040 thingsScheduledForDestruction_ = new Composite*[ MAX_THINGS_FOR_DESTRUCTION ]; 00041 } 00042 00043 SpawnManager 00044 ::~SpawnManager() { 00045 delete [] factories_; 00046 delete [] thingsScheduledForDestruction_; 00047 resetAll(); 00048 } 00049 00050 00051 SpawnManager& SpawnManager 00052 ::singleton() { 00053 static SpawnManager instance; 00054 return instance; 00055 } 00056 00057 00058 void SpawnManager 00059 ::step(long when) { 00060 /* 00061 NodeComponentList::Iterator it(children_); 00062 while(it.hasNext()) { 00063 SpawnAreaComponent& c = static_cast<SpawnAreaComponent&>(it.next()); 00064 //c.propagate(); 00065 } 00066 */ 00067 } 00068 00069 00070 void SpawnManager 00071 ::initGame() { 00072 } 00073 00074 00075 void SpawnManager 00076 ::cleanupGame() { 00077 } 00078 00079 00080 00081 void SpawnManager 00082 ::addFactory(CompositeFactory* factory) { 00083 Assert(factoryCount_ < MAX_FACTORIES - 1); 00084 LogDetail(factoryCount_ << ": " << factory->name()); 00085 factories_[ factoryCount_++ ] = factory; 00086 } 00087 00088 00089 CompositeFactory* SpawnManager 00090 ::factory(const char* name) { 00091 for(int i = 0; i < factoryCount_; ++i) { 00092 if(strcmp(factories_[i]->name(), name) == 0) { 00093 return factories_[i]; 00094 } 00095 } 00096 LogFatal("SpawnManager.cpp: Tried to get thing with unkown name: " << name << " - " << factoryCount_); 00097 return 0; 00098 } 00099 00100 00101 00102 Composite* SpawnManager 00103 ::create(const char* name) { 00104 for(int i = 0; i < factoryCount_; ++i) { 00105 if(strcmp(factories_[i]->name(), name) == 0) { 00106 const CompositeFactory* f = factories_[i]; 00107 Composite* t = static_cast<Composite*>(f->create()); 00108 //t->setFactory(f); 00109 return t; 00110 } 00111 } 00112 LogFatal("Tried to create thing with unkown name: " << name); 00113 return 0; 00114 } 00115 00116 00117 void SpawnManager 00118 ::scheduleForDestruction(Composite& thing) { 00119 AssertFatal(nextDestructionCount_ < MAX_THINGS_FOR_DESTRUCTION, "Too many things scheduled for destruction already."); 00120 00121 // TODO: The below is debug code, remove for performance increase when stable 00122 DebugExec(for(int i = 0; i < nextDestructionCount_; ++i)) { 00123 DebugExec(if(&thing == thingsScheduledForDestruction_[ i ])) { 00124 DbgLogFatal("Trying to destroy thing twice."); 00125 } 00126 } 00127 // End of debug code 00128 thingsScheduledForDestruction_[ nextDestructionCount_++ ] = &thing; 00129 } 00130 00131 00132 void SpawnManager 00133 ::neutraliseDestructions() { 00134 for(int i = 0; i < nextDestructionCount_; ++i) { 00135 thingsScheduledForDestruction_[ i ]->cleanup(); 00136 } 00137 } 00138 00139 00140 void SpawnManager 00141 ::performDestructions() { 00142 while(destructionCount_ > 0) { 00143 //LogDetail("Dest count: " << destructionCount_); 00144 Composite* t = thingsScheduledForDestruction_[ --destructionCount_ ]; 00145 Assert(t->factory()); 00146 // Let the facory that created the object do the deletion. 00147 t->factory()->release(t); 00148 thingsScheduledForDestruction_[ destructionCount_ ] = 00149 thingsScheduledForDestruction_[ --nextDestructionCount_ ]; 00150 } 00151 neutraliseDestructions(); 00152 destructionCount_ = nextDestructionCount_; 00153 } 00154 00155 00156 void SpawnManager 00157 ::reset() { 00158 neutraliseDestructions(); 00159 performDestructions(); 00160 performDestructions(); 00161 } 00162 00163 void SpawnManager 00164 ::resetAll() { 00165 reset(); 00166 while(factoryCount_ > 0) { 00167 delete factories_[ --factoryCount_ ]; 00168 } 00169 destructionCount_ = 0; 00170 nextDestructionCount_ = 0; 00171 factoryCount_ = 0; 00172 } 00173 00174 00175 }
Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:12 2007 by Doxygen version 1.3.9.1.