//------------------------------------------------------------------------------
// 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
 * チュートリアル2 キャラクタ移動実装
 * @author Junpee
 */

#include <LampBasic.h>
#include <Framework/System/SceneFramework.h>
#include <Graphics/Scene/Scene.h>
#include <Graphics/SceneNode/SceneNodeManager.h>
#include <Animation/System/AnimationManager.h>
#include <Animation/System/Animation.h>
#include <Input/Pad/PS2Pad.h>

// Lampのネームスペースを有効にする
using namespace Lamp;

//------------------------------------------------------------------------------
// クラス定義、通常はヘッダファイルに格納する
//------------------------------------------------------------------------------
/**
 * チュートリアル2 キャラクタ移動
 */
class Tutorial2_CharacterMove : public SceneFramework{
public:
	/**
	 * コンストラクタ
	 */
	Tutorial2_CharacterMove();

	/**
	 * 初期化
	 * @return 初期化に成功すればtrueを返す
	 */
	virtual bool initialize();

	/**
	 * 実行
	 */
	virtual void run();

protected:
	// キャラクタ移動ノード
	SceneNode* characterMoveNode_;
	// キャラクタ回転ノード
	SceneNode* characterRotationNode_;
	// アニメーション
	Animation* animation_;

};
//------------------------------------------------------------------------------
// プログラムエントリポイント
//------------------------------------------------------------------------------
// メイン関数
int WINAPI WinMain(
	HINSTANCE instance, HINSTANCE previousInstance, LPSTR command, int show){
	Tutorial2_CharacterMove tutorial;
	return tutorial.execute(instance);
}
//------------------------------------------------------------------------------
// クラス実装
//------------------------------------------------------------------------------
// コンストラクタ
Tutorial2_CharacterMove::Tutorial2_CharacterMove() :
	SceneFramework("Tutorial2_CharacterMove"),
	characterMoveNode_(NULL), characterRotationNode_(NULL){
}
//------------------------------------------------------------------------------
// 初期化
bool Tutorial2_CharacterMove::initialize(){
	// シーンのロード
	if(!loadScene("../Media/Tutorial.bsn")){ return false; }
	// 移動ノードの取得
	characterMoveNode_ =
		scene_->getSceneNodeManager()->search("MakotoMove");
	// 回転ノードの取得
	characterRotationNode_ =
		scene_->getSceneNodeManager()->search("MakotoRotation");
	// アニメーションの取得
	animation_ = animationManager_->search("Tutorial");
	return true;
}
//------------------------------------------------------------------------------
// 実行
void Tutorial2_CharacterMove::run(){
	Vector3 addPosition(Vector3::zero);
	float moveSpeed = 0.4f;
	// パッド入力処理
	if(pad_ != NULL){
		Point2f leftAxis = pad_->getLeftAxis();
		leftAxis *= moveSpeed;
		addPosition += Vector3(leftAxis.x, 0.f, leftAxis.y);
	}

	// キーボード入力処理
	if(keyboard_->pressed(Keyboard::keyUp)){ addPosition.z -= moveSpeed; }
	if(keyboard_->pressed(Keyboard::keyDown)){ addPosition.z += moveSpeed; }
	if(keyboard_->pressed(Keyboard::keyRight)){ addPosition.x += moveSpeed; }
	if(keyboard_->pressed(Keyboard::keyLeft)){ addPosition.x -= moveSpeed; }

	// 位置の設定
	Vector3 position = characterMoveNode_->getTranslation();
	position += addPosition;
	characterMoveNode_->setTranslation(position);

	// 回転とアニメーションの設定
	if(!addPosition.isZero()){
		characterRotationNode_->setRotationXYZ(Vector3(0.f,
			Math::atan2(addPosition.x, addPosition.z), 0.f));
		if(animation_->getSequence() != 0){ animation_->setSequence(0); }
	}else{
		if(animation_->getSequence() != 3){ animation_->setSequence(3); }
	}
}
//------------------------------------------------------------------------------
