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 "PhysicsComponent.hpp" 00023 #include "../react/CollisionComponent.hpp" 00024 #include "../schema/SimSchema.hpp" 00025 #include "util/error/Log.hpp" 00026 #include "../thing/Actor.hpp" 00027 00028 00029 00030 namespace se_core { 00031 PhysicsComponent 00032 ::PhysicsComponent(Composite* owner, PosComponent* posComponent) 00033 : NodeComponent(sct_PHYSICS, owner), currentPhysics_(0), affect_(0), posComponent_(posComponent) { 00034 physics_[currentPhysics_] = 0; 00035 } 00036 00037 PhysicsComponent 00038 ::PhysicsComponent(Composite* owner, const ComponentFactory* factory) 00039 : NodeComponent(sct_PHYSICS, owner, factory), currentPhysics_(0), affect_(0) { 00040 posComponent_ = static_cast<PosComponent*>(owner_->component(sct_POS)); 00041 Assert(posComponent_); 00042 physics_[currentPhysics_] = 0; 00043 } 00044 00045 00046 00047 PhysicsComponent 00048 ::~PhysicsComponent() { 00049 } 00050 00051 00052 void PhysicsComponent 00053 ::popPhysics() { 00054 AssertFatal(currentPhysics_ > 0 || physics_[0] != 0, owner()->name()); 00055 //LogWarning(owner()->name() << " had physics " << physics_[currentPhysics_]->name() << " popped"); 00056 if(currentPhysics_ == 0) 00057 clearPhysics(); 00058 else 00059 --currentPhysics_; 00060 } 00061 00062 00063 void PhysicsComponent 00064 ::clearPhysics() { 00065 //LogWarning(owner()->name() << " had physics cleared"); 00066 currentPhysics_ = 0; 00067 physics_[0] = 0; 00068 } 00069 00070 00071 void PhysicsComponent 00072 ::pushPhysics(const Physics* ph) { 00073 //LogWarning(owner()->name() << " had physics " << ph->name() << " pushed"); 00074 AssertFatal(ph != physics_[ currentPhysics_ ], owner()->name()); 00075 if(hasDefaultPhysics() && physics().isStacker()) { 00076 // Increase script counter 00077 ++currentPhysics_; 00078 } 00079 Assert(currentPhysics_ < MAX_PHYSICS_STACK_SIZE - 1); 00080 physics_[ currentPhysics_ ] = ph; 00081 } 00082 00083 00084 void PhysicsComponent 00085 ::pushPhysics(const char* name) { 00086 const Physics* p = Physics::lookup(name); 00087 pushPhysics(p); 00088 } 00089 00090 00091 bool PhysicsComponent 00092 ::calcNextCoor() { 00093 if(!hasDefaultPhysics()) { 00094 didMove_ = false; 00095 affect_ = 0; 00096 return false; 00097 } 00098 00099 move_ = nextMove_; 00100 physics().calcNext(*this, posComponent_->pos(), posComponent_->nextPos(), move(), nextMove()); 00101 AssertFatal(!posComponent_->nextPos().worldCoor().isNan(), "Nan: " << owner()->name() << " - " << physics().name()); 00102 AssertFatal(!posComponent_->nextPos().localCoor().isNan(), "Nan: " << owner()->name() << " - " << physics().name()); 00103 move_ = nextMove_; 00104 nextMove_.resetForces(); 00105 00106 // 00107 didMove_ = posComponent_->nextPos().didParentMove() || !posComponent_->nextPos().localEquals(posComponent_->pos()); 00108 00109 // Move to new position in collision grid 00110 CollisionComponent::Ptr cc(*this); 00111 if(cc && cc->isCollideable() && posComponent_->pos().area() == posComponent_->nextPos().area()) { 00112 cc->move(); 00113 } 00114 affect_ = &physics(); 00115 00116 return didMove_; 00117 } 00118 00119 00120 bool PhysicsComponent 00121 ::isMover() const { 00122 // Actors that has physics can move 00123 return hasDefaultPhysics(); 00124 } 00125 00126 00127 void PhysicsComponent 00128 ::flip() { 00129 posComponent_->flip(); 00130 } 00131 00132 } 00133
Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:11 2007 by Doxygen version 1.3.9.1.