CollisionManager.cpp

Go to the documentation of this file.
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 "CollisionManager.hpp"
00023 #include "CollisionAreaComponent.hpp"
00024 #include "sim_react.hpp"
00025 #include "../schema/SimSchema.hpp"
00026 #include "../area/AreaManager.hpp"
00027 #include "../sim.hpp"
00028 #include "../config/sim_config.hpp"
00029 
00030 
00031 namespace se_core {
00032     CollisionManager
00033     ::CollisionManager()
00034             : RootComponent(sct_POS) 
00035             , gridCount_(0), gridPoolCount_(0) 
00036             , isGodModeOn_(false), contactCount_(0) {
00037         collisionGrids_ = new CollisionGrid*[ MAX_ACTIVE ];
00038         gridPool_ = new CollisionGrid*[ MAX_ACTIVE ];
00039         contactList_ = new Contact[ MAX_CONTACTS ];
00040     }
00041 
00042 
00043     CollisionManager
00044     ::~CollisionManager() {
00045         for(int i = 0; i < gridCount_; ++i) {
00046             delete collisionGrids_[i];
00047         }
00048         gridCount_ = 0;
00049         delete[] collisionGrids_;
00050         delete[] gridPool_;
00051         delete[] contactList_;
00052         LogDetail("Destroyed area grids");
00053     }
00054 
00055 
00056     CollisionManager&  CollisionManager
00057     ::singleton() {
00058         static CollisionManager instance;
00059         return instance;
00060     }
00061 
00062 
00063     void CollisionManager
00064     ::step(long when) {
00065         if(isGodModeOn_)
00066             return;
00067         getContactList();
00068         resolveContacts();
00069     }
00070 
00071 
00072     CollisionGrid* CollisionManager
00073     ::grabCollisionGrid() {
00074         // Create grid object if necessary
00075         if(!gridPoolCount_) {
00076             Assert(gridCount_ < MAX_ACTIVE);
00077 
00078             coor_tile_t maxWidth = SimSchema::areaManager.maxWidth();
00079             coor_tile_t maxHeight = SimSchema::areaManager.maxHeight();
00080             short d = 2;
00081             while((1 << (d + 1)) < maxWidth / 4 && (1 << (d + 1)) < maxHeight / 4)
00082                 ++d;
00083 
00084             // TODO: Fix this desperate solution for border creatures (* 2)
00085             // See also CollisionAreaManager
00086             CollisionGrid* g = new CollisionGrid(maxWidth * 2, maxHeight * 2, d);
00087             gridPool_[ gridPoolCount_++ ] = g;
00088             collisionGrids_[ gridCount_++ ] = g;
00089         }
00090 
00091         return gridPool_[ --gridPoolCount_ ];
00092     }
00093 
00094 
00095     void CollisionManager
00096     ::releaseCollisionGrid(CollisionGrid* g) {
00097         g->clear();
00098         gridPool_[ gridPoolCount_++ ] = g;
00099     }
00100 
00101 
00102     void CollisionManager
00103     ::getContactList() {
00104         contactCount_ = 0;
00105 
00106         NodeComponentList::Iterator it(children());
00107         while(it.hasNext()) {
00108             CollisionAreaComponent& cac = static_cast<CollisionAreaComponent&>(it.next());
00109             cac.getContactList(contactList_, contactCount_, MAX_CONTACTS);
00110         }
00111     }
00112 
00113     void CollisionManager
00114     ::resolveContacts() {
00115         /*
00116         static int last = 0;
00117         if(contactCount_ != last) {
00118             last = contactCount_;
00119             LogWarning(contactCount_ << " collisions");
00120             for(int i = 0; i < contactCount_; ++i) {
00121                 Contact& c = contactList_[i];
00122                 LogWarning(c.ci1_.cc_->owner()->name() << " vs " << c.ci2_.cc_->owner()->name());
00123             }
00124         }
00125         */
00126         for(int i = 0; i < contactCount_; ++i) {
00127             Contact& c = contactList_[i];
00128             c.ci1_.cc_->pushThing(c.ci1_, c.ci2_);
00129             c.ci2_.cc_->pushThing(c.ci2_, c.ci1_);
00130         }
00131     }
00132 
00133 
00134 
00135 }

Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:11 2007 by Doxygen version 1.3.9.1.

SourceForge.net Logo