//------------------------------------------------------------------------------
// 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 "Load/System/LoadTask.h"
#include "Game/System/GameSystem.h"
#include "Game/Rendering/RenderingSystem.h"
#include "Load/Request/SceneLoadRequest.h"
#include "Load/Request/AnimationLoadRequest.h"
#include "Load/Request/CollisionLoadRequest.h"
#include "Load/Request/SoundListLoadRequest.h"

//------------------------------------------------------------------------------
// 生成、破棄
//------------------------------------------------------------------------------
// コンストラクタ
LoadTask::LoadTask() : currentRequest_(NULL), totalRequestCount_(0),
	nextTask_(NULL){
}
//------------------------------------------------------------------------------
// デストラクタ
LoadTask::~LoadTask(){
}
//------------------------------------------------------------------------------
// 初期化、後始末
//------------------------------------------------------------------------------
// 初期化
void LoadTask::initialize(){
	Assert(currentRequest_ == NULL);
	Assert(totalRequestCount_ == 0);
	Assert(nextTask_ != NULL);
	// リクエストが無い場合は、次のタスクへ移行
	totalRequestCount_ = requests_.getCount();
	if(totalRequestCount_ == 0){
		getGameSystem()->setNextTask(nextTask_);
		return;
	}
	// カレントのリクエストを設定
	currentRequest_ = requests_.popFront();

	drawCounter_ = 0;
}
//------------------------------------------------------------------------------
// 後始末
void LoadTask::finalize(){
	// 次のタスクをクリア
	nextTask_ = NULL;
	// リクエストをクリア
	SafeDelete(currentRequest_);
	int requestCount = requests_.getCount();
	for(int i = 0; i < requestCount; i++){ SafeDelete(requests_.get(i)); }
	requests_.clear();
	totalRequestCount_ = 0;
}
//------------------------------------------------------------------------------
// 実行
//------------------------------------------------------------------------------
// 実行
void LoadTask::run(){
	// 一度描画されるまでは処理しない
	if(drawCounter_ < 2){ return; }

	if(currentRequest_->load()){
		// ロードが終了したのでカレントリクエストを削除
		SafeDelete(currentRequest_);
		// リクエストリストからリクエストを取り出してカレントに設定
		if(requests_.getCount() != 0){ currentRequest_ = requests_.popFront(); }
		drawCounter_ = 0;
	}

	// 全リクエストのロードが終わったら次のタスクへ移行する
	if((requests_.getCount() == 0) && (currentRequest_ == NULL)){
		Assert(nextTask_ != NULL);
		getGameSystem()->setNextTask(nextTask_);
	}
}
//------------------------------------------------------------------------------
// 描画準備
//------------------------------------------------------------------------------
// 描画準備
void LoadTask::renderSetup(){
}
//------------------------------------------------------------------------------
// 描画
//------------------------------------------------------------------------------
// 描画
void LoadTask::render(){
	// 背景クリア
	RenderingSystem::getInstance()->clearBackground();
	// ローディングメッセージ
	String loadingMessage;
	loadingMessage.format("Now Loading...  %2d / %2d\n",
		totalRequestCount_ - requests_.getCount(), totalRequestCount_);
	DebugHUD::setBottomRight(loadingMessage);
	drawCounter_++;
}
//------------------------------------------------------------------------------
// ロードリクエスト
//------------------------------------------------------------------------------
// シーンのリクエスト
void LoadTask::requestScene(const String& fileName, Scene* scene,
	SceneNode* sceneNode){
	SceneLoadRequest* request =
		new SceneLoadRequest(fileName, scene, sceneNode);
	requests_.pushBack(request);
}
//------------------------------------------------------------------------------
// アニメーションのリクエスト
void LoadTask::requestAnimation(const String& fileName,
	AnimationManager* animationManager, Scene* bindScene){
	AnimationLoadRequest* request =
		new AnimationLoadRequest(fileName, animationManager, bindScene);
	requests_.pushBack(request);
}
//------------------------------------------------------------------------------
// コリジョンのリクエスト
void LoadTask::requestCollision(const String& fileName,
	CollisionScene* scene, CollisionNode* collisionNode){
	CollisionLoadRequest* request =
		new CollisionLoadRequest(fileName, scene, collisionNode);
	requests_.pushBack(request);
}
//------------------------------------------------------------------------------
// サウンドリストのリクエスト
void LoadTask::requestSoundList(const String& fileName, SoundCache* soundCache){
	SoundListLoadRequest* request =
		new SoundListLoadRequest(fileName, soundCache);
	requests_.pushBack(request);
}
//------------------------------------------------------------------------------
