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 "SpawnComponent.hpp" 00023 #include "SpawnManager.hpp" 00024 #include "../action/all.hpp" 00025 #include "../thing/Actor.hpp" 00026 #include "../area/Area.hpp" 00027 #include "util/type/String.hpp" 00028 #include "util/error/Log.hpp" 00029 00030 00031 namespace se_core { 00032 SpawnComponent 00033 ::SpawnComponent(Composite* owner, PosComponent* pos) 00034 : Component(sct_SPAWN, owner), pos_(pos), spawnCount_(0), spawnPointCount_(0), spawnPoints_(0) { 00035 } 00036 00037 SpawnComponent 00038 ::SpawnComponent(Composite* owner, const ComponentFactory* factory) 00039 : Component(sct_SCRIPT, owner, factory) 00040 , spawnCount_(0), spawnPointCount_(0), spawnPoints_(0) { 00041 pos_ = static_cast<PosComponent*>(owner_->component(sct_POS)); 00042 Assert(pos_); 00043 } 00044 00045 00046 SpawnComponent 00047 ::~SpawnComponent() { 00048 cleanup(); 00049 } 00050 00051 00052 void SpawnComponent 00053 ::cleanup() { 00054 resetSpawner(); 00055 } 00056 00057 00058 Composite* SpawnComponent 00059 ::spawn(const char* thingName, int spawnPointId, long deniedTsMask) { 00060 // Get spawn point displace and face direction 00061 const ViewPoint* sp = spawnPoint(spawnPointId); 00062 AssertFatal(sp, "Spawn point does not exist"); 00063 ViewPoint vp(*sp); 00064 00065 // Calculate area coor of spawn point 00066 const Pos& pos = pos_->pos(); 00067 vp.add(pos.world_); 00068 00069 // Spawn it in area (with area as parent) 00070 Area::Ptr area(pos.area()); 00071 area = area->neighbour(vp.coor_); 00072 if(!area) { 00073 LogWarning("Area not found"); 00074 return 0; 00075 } 00076 PosComponent::Ptr aPos(*area); 00077 vp.sub(aPos->pos().world_); 00078 Composite* t = area->spawn(thingName, vp, deniedTsMask); 00079 00080 // Remember spawner 00081 if(t) { 00082 SpawnComponent::Ptr tSpawn(*t); 00083 Assert(!tSpawn.isNull()); 00084 tSpawn->setSpawner(owner()); 00085 } 00086 00087 return t; 00088 } 00089 00090 void SpawnComponent 00091 ::setSpawnPoints(int count, const ViewPoint* const* const spawnPoints) { 00092 AssertFatal(spawnPoints_ == 0, "Spawn points set twice: " << owner()->name()); 00093 00094 spawnPointCount_ = count; 00095 spawnPoints_ = spawnPoints; 00096 } 00097 00098 00099 bool SpawnComponent 00100 ::hasSpawnPoint(short id) const { 00101 if(id < 0 || id >= spawnPointCount_) 00102 return false; 00103 return spawnPoints_[id] != 0; 00104 } 00105 00106 const ViewPoint* SpawnComponent 00107 ::spawnPoint(short id) const { 00108 AssertFatal(id >= 0 && id < spawnPointCount_, ": Id=" << id << " Count=" << spawnPointCount_); 00109 AssertFatal(spawnPoints_[id] != 0, ": Id=" << id); 00110 return spawnPoints_[id]; 00111 } 00112 00113 00114 Composite* SpawnComponent 00115 ::spawner() const { 00116 if(spawner_.isNull()) { 00117 return 0; 00118 } 00119 return static_cast<Composite*>(spawner_.object()); 00120 } 00121 00122 00123 void SpawnComponent 00124 ::setSpawner(Composite* spawner) { 00125 Assert(spawner != 0); 00126 spawner_.set(spawner->ref()); 00127 SpawnComponent::Ptr(*spawner)->incSpawnCount(); 00128 Assert(!spawner_.isNull()); 00129 } 00130 00131 00132 void SpawnComponent 00133 ::resetSpawner() { 00134 if(!spawner_.isNull()) { 00135 Composite* s = static_cast<Composite*>(spawner_.object()); 00136 SpawnComponent::Ptr(*s)->decSpawnCount(); 00137 spawner_.reset(); 00138 } 00139 } 00140 00141 00142 void SpawnComponent 00143 ::setDead() { 00144 SpawnManager::singleton().scheduleForDestruction(*this->owner()); 00145 } 00146 00147 }
Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:12 2007 by Doxygen version 1.3.9.1.