//------------------------------------------------------------------------------
// Lamp : Open source game middleware
// Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//------------------------------------------------------------------------------

/** @file
 * コリジョンリーフ実装
 * @author Junpee
 */

#include "LampBasic.h"
#include "Collision/System/CollisionLeaf.h"
#include "Collision/System/CollisionScene.h"
#include "Collision/System/CollisionNode.h"

namespace Lamp{

//------------------------------------------------------------------------------
// コンストラクタ
CollisionLeaf::CollisionLeaf(const String& name, CollisionScene* scene) :
	CollisionObject(name, scene), collisionMask_(1){
	setGlobalEnabled(false);
}
//------------------------------------------------------------------------------
// デストラクタ
CollisionLeaf::~CollisionLeaf(){
}
//------------------------------------------------------------------------------
// 破棄
int CollisionLeaf::destroy(CollisionLeaf* collisionLeaf){
	Assert(collisionLeaf != NULL);
	// 親から削除
	CollisionNode* parent = collisionLeaf->getParent();
	if(parent != NULL){ parent->removeChild(collisionLeaf); }
	// 引数の破棄
	collisionLeaf->getScene()->destroyLeaf(collisionLeaf);
	return 1;
}
//------------------------------------------------------------------------------
// 走査
//------------------------------------------------------------------------------
// 走査のセットアップ
bool CollisionLeaf::traverseSetup(bool parentEnabled, bool parentChanged){
	// グローバル有効フラグ設定
	bool globalEnabled = (parentEnabled && isEnabled());
	setGlobalEnabled(globalEnabled);
	// グローバルで有効で無いなら処理しない
	if(!globalEnabled){ return false; }

	// グローバル変更フラグ設定
	bool globalChanged = (parentChanged || isChanged());
	setGlobalChanged(globalChanged);
	// 変更フラグのリセット
	setChanged(false);
	// グローバルで変更が無いなら処理しない
	if(!globalChanged){ return false; }

	return true;
}
//------------------------------------------------------------------------------
// スケール
//------------------------------------------------------------------------------
// スケールの設定
void CollisionLeaf::setScale(const Vector3& scale){
	Assert(getParent() != NULL);
	getParent()->setScale(scale);
}
//------------------------------------------------------------------------------
// スケールの取得
const Vector3& CollisionLeaf::getScale() const{
	Assert(getParent() != NULL);
	return getParent()->getScale();
}
//------------------------------------------------------------------------------
// スケールを使用しているか
bool CollisionLeaf::isScaled() const{
	Assert(getParent() != NULL);
	return getParent()->isScaled();
}
//------------------------------------------------------------------------------
// グローバルでスケールを使用しているか
bool CollisionLeaf::isGlobalScaled() const{
	Assert(getParent() != NULL);
	return getParent()->isGlobalScaled();
}
//------------------------------------------------------------------------------
// 回転
//------------------------------------------------------------------------------
// XYZ回転の設定
void CollisionLeaf::setRotationXYZ(const Vector3& rotation){
	Assert(getParent() != NULL);
	getParent()->setRotationXYZ(rotation);
}
//------------------------------------------------------------------------------
// XYZ回転の取得
const Vector3& CollisionLeaf::getRotationXYZ(){
	Assert(getParent() != NULL);
	return getParent()->getRotationXYZ();
}
//------------------------------------------------------------------------------
// 四元数回転の設定
void CollisionLeaf::setRotationQuaternion(const Quaternion& rotation){
	Assert(getParent() != NULL);
	getParent()->setRotationQuaternion(rotation);
}
//------------------------------------------------------------------------------
// 四元数回転の取得
const Quaternion& CollisionLeaf::getRotationQuaternion(){
	Assert(getParent() != NULL);
	return getParent()->getRotationQuaternion();
}
//------------------------------------------------------------------------------
// 移動
//------------------------------------------------------------------------------
// 移動の設定
void CollisionLeaf::setTranslation(const Vector3& translation){
	Assert(getParent() != NULL);
	getParent()->setTranslation(translation);
}
//------------------------------------------------------------------------------
// 移動の取得
const Vector3& CollisionLeaf::getTranslation() const{
	Assert(getParent() != NULL);
	return getParent()->getTranslation();
}
//------------------------------------------------------------------------------
// 行列
//------------------------------------------------------------------------------
// ワールド行列の取得
const Matrix34& CollisionLeaf::getWorldMatrix() const{
	Assert(getParent() != NULL);
	return getParent()->getWorldMatrix();
}
//------------------------------------------------------------------------------
// ローカル行列の取得
const Matrix34& CollisionLeaf::getLocalMatrix() const{
	Assert(getParent() != NULL);
	return getParent()->getLocalMatrix();
}
//------------------------------------------------------------------------------
} // End of namespace Lamp
//------------------------------------------------------------------------------
