//------------------------------------------------------------------------------
// 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/Leaf/StaticSphereCollision.h"
#include "Collision/System/CollisionScene.h"
#include "Geometry/System/IntersectionResult.h"

namespace Lamp{

//------------------------------------------------------------------------------
// 生成、破棄
//------------------------------------------------------------------------------
// コンストラクタ
StaticSphereCollision::StaticSphereCollision(
	const String& name, CollisionScene* scene) :
	StaticCollisionLeaf(name, scene),
	sphere_(Sphere::zero), worldSphere_(Sphere::zero){
}
//------------------------------------------------------------------------------
// デストラクタ
StaticSphereCollision::~StaticSphereCollision(){
}
//------------------------------------------------------------------------------
// コピー
//------------------------------------------------------------------------------
// 静的球コリジョンのコピー
StaticSphereCollision*
	StaticSphereCollision::copyStaticSphereCollision() const{
	StaticSphereCollision* destination =
		getScene()->createStaticSphereCollision(
		getScene()->renameLeaf(getName()));
	// 静的コリジョンリーフの値をコピー
	copyStaticCollisionLeafValue(destination);
	// メンバのコピー
	destination->sphere_ = sphere_;
	destination->worldSphere_ = worldSphere_;
	return destination;
}
//------------------------------------------------------------------------------
// 交差
//------------------------------------------------------------------------------
// 球交差
void StaticSphereCollision::intersection(
	IntersectionResult* result, const Sphere& sphere, u_int collisionMask){
	// グローバルでの有効、無効判定
	if(!isGlobalEnabled()){ return; }
	// コリジョンマスク判定
	if((getCollisionMask() & collisionMask) == 0){ return; }
	// 球判定
	Intersection intersection;
	if(worldSphere_.intersect(&intersection, sphere)){
		result->add(intersection);
	}
}
//------------------------------------------------------------------------------
// 球コリジョン交差
void StaticSphereCollision::intersection(IntersectionResult* result,
	StaticSphereCollision* sphere, u_int collisionMask){
	// 自分自身なら交差しない
	if(sphere == this){ return; }
	// グローバルでの有効、無効判定
	if(!isGlobalEnabled()){ return; }
	// コリジョンマスク判定
	if((getCollisionMask() & collisionMask) == 0){ return; }
	// 球判定
	Intersection intersection;
	if(worldSphere_.intersect(&intersection, sphere->getWorldSphere())){
		result->add(intersection);
	}
}
//------------------------------------------------------------------------------
// 走査
//------------------------------------------------------------------------------
// 走査
void StaticSphereCollision::traverseImplement(
	const Matrix34& parentMatrix, bool parentEnabled, bool parentScaled,
	bool parentChanged){
	// 走査のセットアップ
	if(!traverseSetup(parentEnabled, parentChanged)){ return; }

	// 球の変換
	if(parentScaled){
		// スケール入り
		worldSphere_ = sphere_.scaledTransform(parentMatrix);
	}else{
		// スケール無し
		worldSphere_ = sphere_.transform(parentMatrix);
	}

	// ツリーへの挿入、またはツリー内の移動
}
//------------------------------------------------------------------------------
} // End of namespace Lamp
//------------------------------------------------------------------------------
