//------------------------------------------------------------------------------
// 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 "Core/Utility/stdafx.h"
#include "Player/System/PlayerSystem.h"
#include "Player/Anzu/Anzu.h"

// インスタンス
PlayerSystem* PlayerSystem::instance_ = NULL;

//------------------------------------------------------------------------------
// 生成、破棄
//------------------------------------------------------------------------------
// コンストラクタ
PlayerSystem::PlayerSystem() : playerNode_(NULL){
	// インスタンスポインタ設定
	Assert(instance_ == NULL);
	instance_ = this;
}
//------------------------------------------------------------------------------
// デストラクタ
PlayerSystem::~PlayerSystem(){
	Assert(getCount() == 0);

	// インスタンスポインタ後始末
	Assert(instance_ == this);
	instance_ = NULL;
}
//------------------------------------------------------------------------------
// ロード
//------------------------------------------------------------------------------
// ロード
void PlayerSystem::load(Player::Type type){
	// プレイヤノードが無ければ作成
	if(playerNode_ == NULL){
		// プレイヤノードはルートノードに連結されていない
		playerNode_ =
			getScene()->getSceneNodeManager()->createSceneNode("PlayerNode");
	}
	// プレイヤのロード
	if(type == Player::typeAnzu){
		Anzu::load(playerNode_);
	}else{
		ErrorOut("PlayerSystem::load() 対応していないタイプです %d", type);
	}
}
//------------------------------------------------------------------------------
// 実行
void PlayerSystem::run(){
	int count = getCount();
	for(int i = 0; i < count; i++){ get(i)->run(); }
}
//------------------------------------------------------------------------------
// プレイヤ
//------------------------------------------------------------------------------
// プレイヤの作成
Player* PlayerSystem::create(Player::Type type){
	Player* player = NULL;
	if(type == Player::typeAnzu){
		player = new Anzu();
		players_.add(player);
	}else{
		ErrorOut("PlayerSystem::load() 対応していないタイプです %d", type);
	}
	return player;
}
//------------------------------------------------------------------------------
// プレイヤの破棄
void PlayerSystem::destroy(Player* player){
	int result = players_.removeByValue(player);
	Assert(result != -1);
	delete player;
}
//------------------------------------------------------------------------------
// プレイヤのクリア
void PlayerSystem::clear(){
	for(int i = getCount() - 1; i >= 0; i--){ delete get(i); }
	players_.clear();
	// プレイヤノードの破棄
	if(playerNode_ != NULL){
		SceneNode::recursiveDestroy(playerNode_);
		playerNode_ = NULL;
	}
}
//------------------------------------------------------------------------------
