view LightClone/Source/World.cpp @ 49:48705dc6e568

Fix window issues; Start on GuiRenderContext
author koryspansel
date Fri, 23 Sep 2011 16:54:51 -0700
parents 6790cf9e8bd8
children efd2b1ca5b77
line wrap: on
line source

/*
 * World
 */

#include "World.h"
#include "VertexTypes.h"
#include "GuiLabel.h"
#include "GuiImage.h"
#include "GuiButton.h"

/*
 * World
 */
World::World()
{
	nGameState			= GameState_Active;
	nGameStatePrevious	= GameState_Active;
	nSimulationState	= SimulationState_Idle;
	nCurrentLevel		= 0;
}

/*
 * Initialize
 */
ErrorCode World::Initialize(ResourceManager* pResourceManager, InputManager* pInput)
{
	ErrorCode eCode = Error_Fail;

	if(pResourceManager && pInput)
	{
		pInputManager = pInput;

		eCode = kEnvironment.Initialize(pResourceManager);
		if(eCode == Error_Success)
		{
			eCode = kBot.Initialize(pResourceManager);
			if(eCode == Error_Success)
			{
				eCode = kProgram.Initialize();
				if(eCode == Error_Success)
				{
					eCode = InitializeInterface(pResourceManager);				
					if(eCode == Error_Success)
					{
						nCurrentLevel	= 0;
						nGameState		= GameState_LoadMap;
					}
					else
					{
						TRACE("Error: Failed to initialize interface\n");
					}
				}
				else
				{
					TRACE("Error: Failed to initialize program\n");
				}
			}
			else
			{
				TRACE("Error: Failed to initialize bot\n");
			}
		}
		else
		{
			TRACE("Error: Failed to initialize environment\n");
		}
	}

	return eCode;
}

/*
 * Terminate
 */
void World::Terminate()
{
	kInterface.Terminate();
	kProgram.Terminate();
	kBot.Terminate();
	kEnvironment.Terminate();
}

/*
 * Activate
 */
void World::Activate()
{
	nGameState = GameState_LoadMap;
}

/*
 * Deactivate
 */
void World::Deactivate()
{
	nGameState = GameState_Exit;
}

/*
 * IsActive
 */
bool World::IsActive()
{
	return nGameState != GameState_Exit;
}

/*
 * Load
 */
ErrorCode World::Load(const char* pName)
{
	ErrorCode eCode = kLoader.Load(pName);
	if(eCode == Error_Success)
	{
		const Size& kSize = kLoader.GetSize();

		eCode = kEnvironment.Setup(kSize.X, kSize.Y);
		if(eCode == Error_Success)
		{
			for(uint32 nY = 0; nY < kSize.Y; ++nY)
			{
				for(uint32 nX = 0; nX < kSize.X; ++nX)
				{
					kEnvironment.SetType(nX, nY, kLoader.GetTowerType(nX, nY));
					kEnvironment.SetAltitude(nX, nY, kLoader.GetTowerHeight(nX, nY));
				}
			}

			kBot.Setup(&kEnvironment);
			kBot.SetPosition(kLoader.GetInitialPosition());
			kBot.SetDirection(kLoader.GetInitialDirection());
		}
	}

	return eCode;
}

/*
 * Update
 */
void World::Update(float fElapsed)
{
	ProcessInput(fElapsed);

	if(nGameState == GameState_LoadMap)
	{
		char kBuffer[256];
		sprintf(kBuffer, "Data\\Maps\\Map%02d.map", nCurrentLevel++);

		ErrorCode eCode = Load(kBuffer);
		if(eCode == Error_Success)
		{
			kProgram.Clear();

			pMain->Clear();
			pFunctionA->Clear();
			pFunctionB->Clear();

			nGameState = GameState_Active;
		}
		else
		{
			kMessageDialog.SetButton(0, "Ok", 0);
			kMessageDialog.SetMessage("Congratulations!\nYou have completed the game");
			kMessageDialog.Show();

			nGameState = GameState_Over;
		}

		nSimulationState = SimulationState_Idle;
	}
	else

	if(nGameState == GameState_Active)
	{
		if(nSimulationState == SimulationState_Active)
		{
			if(kBot.Update(fElapsed))
			{
				if(kEnvironment.RequirementsMet())
				{
					kMessageDialog.SetButton(0, "Ok", 0);
					kMessageDialog.SetMessage("Congratulations!\nYou have completed level %d", nCurrentLevel);
					kMessageDialog.Show();

					nGameState = GameState_Complete;
				}
			}
		}
	}

	kInterface.Update(fElapsed);
}

/*
 * Render
 */
void World::Render(RenderContext& kContext)
{
	const uint32 nColor = D3DCOLOR_XRGB(32, 32, 32);

	kContext.Begin(nColor);

	//TODO: Remove
	if(nGameState >= GameState_Active)
	{
		D3DVIEWPORT9 kOriginal;
		kContext.GetViewport(&kOriginal);

		D3DVIEWPORT9 kViewport;
		kViewport.X			= 0;
		kViewport.Y			= 0;
		kViewport.Width		= ScreenSizeX - 280;
		kViewport.Height	= ScreenSizeY;
		kViewport.MinZ		= kOriginal.MinZ;
		kViewport.MaxZ		= kOriginal.MaxZ;

		kContext.SetViewport(kViewport);

		kCameraController.SetMode(CameraMode_3D);
		Render3D(kContext);

		kContext.SetViewport(kOriginal);

		kCameraController.SetMode(CameraMode_2D);
		Render2D(kContext);
	}

	kContext.End();
}

/*
 * InitializeInterface
 */
ErrorCode World::InitializeInterface(ResourceManager* pResourceManager)
{
	ErrorCode eCode = kInterface.Initialize(pResourceManager, pInputManager);
	if(eCode == Error_Success)
	{
		pBackground = new GuiImage();
		pBackground->Initialize(pResourceManager);
		pBackground->SetTexture("Data\\Textures\\Background03.tga", true);
		pBackground->SetPosition(ScreenSizeX - pBackground->GetWidth(), 0.0f);

		pToolbar = new CodePanel(4, 2);
		pToolbar->Initialize(pResourceManager);
		pToolbar->SetTexture("Data\\Textures\\PanelA.png");
		pToolbar->SetPosition(16, 16.0f);
		pToolbar->SetAction(0, Action_Forward);
		pToolbar->SetAction(1, Action_RotateCW);
		pToolbar->SetAction(2, Action_RotateCCW);
		pToolbar->SetAction(3, Action_Jump);
		pToolbar->SetAction(4, Action_Light);
		pToolbar->SetAction(5, Action_FunctionA);
		pToolbar->SetAction(6, Action_FunctionB);
		pToolbar->SetPermanent(true);

		GuiLabel* pMainLabel = new GuiLabel();
		pMainLabel->Initialize(pResourceManager);
		pMainLabel->SetFont("Courier New", 16);
		pMainLabel->SetText("Main:");
		pMainLabel->SetColor(D3DCOLOR_XRGB(0, 0, 0));
		pMainLabel->SetPosition(26.0f, 149.0f);

		pMain = new CodePanel(4, 3);
		pMain->Initialize(pResourceManager);
		pMain->SetTexture("Data\\Textures\\PanelB.png");
		pMain->SetPosition(16.0f, 160.0f);
		pMain->Subscribe(CodePanel::EventAction, &World::OnAction, this);

		GuiLabel* pFunctionALabel = new GuiLabel();
		pFunctionALabel->Initialize(pResourceManager);
		pFunctionALabel->SetFont("Courier New", 16);
		pFunctionALabel->SetText("Function 1:");
		pFunctionALabel->SetColor(D3DCOLOR_XRGB(0, 0, 0));
		pFunctionALabel->SetPosition(26.0f, 349.0f);

		pFunctionA = new CodePanel(4, 2);
		pFunctionA->Initialize(pResourceManager);
		pFunctionA->SetTexture("Data\\Textures\\PanelA.png");
		pFunctionA->SetPosition(16.0f, 360.0f);
		pFunctionA->Subscribe(CodePanel::EventAction, &World::OnAction, this);

		GuiLabel* pFunctionBLabel = new GuiLabel();
		pFunctionBLabel->Initialize(pResourceManager);
		pFunctionBLabel->SetFont("Courier New", 16);
		pFunctionBLabel->SetText("Function 2:");
		pFunctionBLabel->SetColor(D3DCOLOR_XRGB(0, 0, 0));
		pFunctionBLabel->SetPosition(26.0f, 493.0f);

		pFunctionB = new CodePanel(4, 2);
		pFunctionB->Initialize(pResourceManager);
		pFunctionB->SetTexture("Data\\Textures\\PanelA.png");
		pFunctionB->SetPosition(16.0f, 504.0f);
		pFunctionB->Subscribe(CodePanel::EventAction, &World::OnAction, this);

		const float fButtonPadding	= 32.0f;
		const float fButtonSpacing	= 8.0f;
		const float fButtonSize		= 48.0f;

		pButtonPlay = new GuiButton();
		pButtonPlay->Initialize(pResourceManager);
		pButtonPlay->SetTexture(GuiButtonState_Normal, "Data\\Textures\\Button1N.png", true);
		pButtonPlay->SetTexture(GuiButtonState_Hover, "Data\\Textures\\Button1H.png", true);
		pButtonPlay->SetTexture(GuiButtonState_Down, "Data\\Textures\\Button1D.png", true);
		pButtonPlay->SetFont("Courier New", 16, FW_BOLD);
		pButtonPlay->SetText("Play");
		pButtonPlay->SetColor(D3DCOLOR_XRGB(0, 0, 0));
		pButtonPlay->SetPosition(fButtonPadding + 0.0f * (fButtonSize + fButtonSpacing), 652.0f);
		pButtonPlay->Subscribe(GuiButton::EventClick, &World::OnPlay, this);

		pButtonStop = new GuiButton();
		pButtonStop->Initialize(pResourceManager);
		pButtonStop->SetTexture(GuiButtonState_Normal, "Data\\Textures\\Button1N.png", true);
		pButtonStop->SetTexture(GuiButtonState_Hover, "Data\\Textures\\Button1H.png", true);
		pButtonStop->SetTexture(GuiButtonState_Down, "Data\\Textures\\Button1D.png", true);
		pButtonStop->SetFont("Courier New", 16, FW_BOLD);
		pButtonStop->SetText("Stop");
		pButtonStop->SetColor(D3DCOLOR_XRGB(0, 0, 0));
		pButtonStop->SetPosition(fButtonPadding + 1.0f * (fButtonSize + fButtonSpacing), 652.0f);
		pButtonStop->Subscribe(GuiButton::EventClick, &World::OnStop, this);

		pButtonReset = new GuiButton();
		pButtonReset->Initialize(pResourceManager);
		pButtonReset->SetTexture(GuiButtonState_Normal, "Data\\Textures\\Button1N.png", true);
		pButtonReset->SetTexture(GuiButtonState_Hover, "Data\\Textures\\Button1H.png", true);
		pButtonReset->SetTexture(GuiButtonState_Down, "Data\\Textures\\Button1D.png", true);
		pButtonReset->SetFont("Courier New", 16, FW_BOLD);
		pButtonReset->SetText("Reset");
		pButtonReset->SetColor(D3DCOLOR_XRGB(0, 0, 0));
		pButtonReset->SetPosition(fButtonPadding + 2.0f * (fButtonSize + fButtonSpacing), 652.0f);
		pButtonReset->Subscribe(GuiButton::EventClick, &World::OnReset, this);

		pButtonExit = new GuiButton();
		pButtonExit->Initialize(pResourceManager);
		pButtonExit->SetTexture(GuiButtonState_Normal, "Data\\Textures\\Button1N.png", true);
		pButtonExit->SetTexture(GuiButtonState_Hover, "Data\\Textures\\Button1H.png", true);
		pButtonExit->SetTexture(GuiButtonState_Down, "Data\\Textures\\Button1D.png", true);
		pButtonExit->SetFont("Courier New", 16, FW_BOLD);
		pButtonExit->SetText("Exit");
		pButtonExit->SetColor(D3DCOLOR_XRGB(0, 0, 0));
		pButtonExit->SetPosition(fButtonPadding + 3.0f * (fButtonSize + fButtonSpacing), 652.0f);
		pButtonExit->Subscribe(GuiButton::EventClick, &World::OnExit, this);

		pBackground->Add(pToolbar);
		pBackground->Add(pMainLabel);
		pBackground->Add(pMain);
		pBackground->Add(pFunctionALabel);
		pBackground->Add(pFunctionA);
		pBackground->Add(pFunctionBLabel);
		pBackground->Add(pFunctionB);
		pBackground->Add(pButtonPlay);
		pBackground->Add(pButtonStop);
		pBackground->Add(pButtonReset);
		pBackground->Add(pButtonExit);

		kMessageDialog.Initialize(pResourceManager);
		kMessageDialog.Subscribe(GuiDialog::EventResult, &World::OnResult, this);

		const D3DXVECTOR2& kMessageSize = kMessageDialog.GetDimensions();
		kMessageDialog.SetPosition(0.5f * ((ScreenSizeX - pBackground->GetWidth()) - kMessageSize.x), 0.5f * (ScreenSizeY - kMessageSize.y));

		kConfirmDialog.Initialize(pResourceManager);
		kConfirmDialog.Subscribe(GuiDialog::EventResult, &World::OnConfirm, this);

		const D3DXVECTOR2& kConfigSize = kMessageDialog.GetDimensions();
		kConfirmDialog.SetPosition(0.5f * ((ScreenSizeX - pBackground->GetWidth()) - kConfigSize.x), 0.5f * (ScreenSizeY - kConfigSize.y));

		kInterface.Add(pBackground);
		kInterface.Add(&kMessageDialog);
		kInterface.Add(&kConfirmDialog);
	}

	return eCode;
}

/*
 * Render3D
 */
void World::Render3D(RenderContext& kContext)
{
	kEnvironment.Render(kContext, kCameraController);
	kBot.Render(kContext, kCameraController);
}

/* 
 * Render2D
 */
void World::Render2D(RenderContext& kContext)
{
	kInterface.Render(kContext, kCameraController);
}

/*
 * ProcessInput
 */
void World::ProcessInput(float fElapsed)
{
	pInputManager->Update(fElapsed);

	#if defined(_DEBUG)
	if(pInputManager->IsKeyDown(DIK_LEFT))
	{
		kCameraController.Yaw(0.01f);
	}
	else

	if(pInputManager->IsKeyDown(DIK_RIGHT))
	{
		kCameraController.Yaw(-0.01f);
	}

	if(pInputManager->IsKeyDown(DIK_UP))
	{
		kCameraController.Pitch(0.01f);
	}
	else

	if(pInputManager->IsKeyDown(DIK_DOWN))
	{
		kCameraController.Pitch(-0.01f);
	}

	if(pInputManager->IsKeyDown(DIK_NEXT))
	{
		kCameraController.Move(0.1f);
	}
	else

	if(pInputManager->IsKeyDown(DIK_PRIOR))
	{
		kCameraController.Move(-0.1f);
	}
	#endif
}

/*
 * OnAction
 */
void World::OnAction(GuiEventArguments& kArguments)
{
	ActionArguments& kActionArguments = (ActionArguments&)kArguments;

	if(kArguments.pSource == pMain)
	{
		kProgram.SetAction(0, kActionArguments.nSlot, pMain->GetAction(kActionArguments.nSlot));
	}
	else

	if(kArguments.pSource == pFunctionA)
	{
		kProgram.SetAction(1, kActionArguments.nSlot, pFunctionA->GetAction(kActionArguments.nSlot));
	}
	else

	if(kArguments.pSource == pFunctionB)
	{
		kProgram.SetAction(2, kActionArguments.nSlot, pFunctionB->GetAction(kActionArguments.nSlot));
	}
}

/*
 * OnPlay
 */
void World::OnPlay(GuiEventArguments& kArguments)
{
	if(nSimulationState == SimulationState_Idle)
	{
		kEnvironment.Reset();

		kBot.Reset();
		kBot.SetPosition(kLoader.GetInitialPosition());
		kBot.SetDirection(kLoader.GetInitialDirection());
		kBot.Upload(kProgram);

		nSimulationState = SimulationState_Active;
	}
}

/*
 * OnStop
 */
void World::OnStop(GuiEventArguments& kArguments)
{
	if(nSimulationState == SimulationState_Active)
	{
		kEnvironment.Reset();

		kBot.Reset();
		kBot.SetPosition(kLoader.GetInitialPosition());
		kBot.SetDirection(kLoader.GetInitialDirection());

		nSimulationState = SimulationState_Idle;
	}
}

/*
 * OnReset
 */
void World::OnReset(GuiEventArguments& kArguments)
{
	kProgram.Clear();

	pMain->Clear();
	pFunctionA->Clear();
	pFunctionB->Clear();
}

/*
 * OnExit
 */
void World::OnExit(GuiEventArguments& kArguments)
{
	kConfirmDialog.SetMessage("Are you sure you want to exit?");
	kConfirmDialog.SetButton(0, "Yes", DialogResult_Yes);
	kConfirmDialog.SetButton(1, "No", DialogResult_No);
	kConfirmDialog.Show();

	nGameStatePrevious	= nGameState;
	nGameState			= GameState_Confirm;
}

/*
 * OnResult
 */
void World::OnResult(GuiEventArguments& kArguments)
{
	if(nGameState == GameState_Complete)
	{
		nGameState = GameState_LoadMap;
	}
	else

	if(nGameState == GameState_Over)
	{
		nCurrentLevel	= 0;
		nGameState		= GameState_LoadMap;
	}
}

/*
 * OnConfirm
 */
void World::OnConfirm(GuiEventArguments& kArguments)
{
	GuiResultArguments& kResultArguments = (GuiResultArguments&)kArguments;

	if(kResultArguments.nResult == DialogResult_Yes)
	{
		nGameState = GameState_Exit;
	}
	else

	if(kResultArguments.nResult == DialogResult_No)
	{
		nGameState = nGameStatePrevious;
	}
}