// Dark GDK - The Game Creators - www.thegamecreators.com

/*	===========================================================================
	Header File: Functions.cpp
	Created: 12/13/2014
	===========================================================================	*/

// include game header file
#include "Alliance.h"

// Declare Global Variables
float g_CamSpeed;
float g_Gravity;

void Initialize()
{
	// initialize global variables
	g_CamSpeed = 3.0f;

	// disable escape key and hide the mouse pointer
	dbDisableEscapeKey();
	dbHideMouse();

	// set camera range and FOV to match FPS Creator
	dbSetCameraRange(2.0f, 9000.0f);
	dbSetCameraFOV(75.0f);

	// initialize the terrain system
	dbSetupTerrain();

	// turn sync on
	dbSyncOn();
}
void KeyControl()
{
	// Keyboard input
	int keystate17, keystate30, keystate31, keystate32;

	// Mouse input
	float cammovex, cammovey;

	// Old camera position
	float cox, coy, coz;

	// New camera position
	float cnx, cny, cnz;

	// Camera movement
	float sx, sy, sz, y;

	// Camera angle
	float camangx, camangy;

	// Get keyboard input
	keystate17 = dbKeyState(17);
	keystate30 = dbKeyState(30);
	keystate31 = dbKeyState(31);
	keystate32 = dbKeyState(32);

	// Get mouse input
	cammovex = dbMouseMoveX();
	cammovey = dbMouseMoveY();

	// Store camera position
	cox = dbCameraPositionX();
	coy = dbCameraPositionY();
	coz = dbCameraPositionZ();

	// Control camera movement
	if ((keystate17 - keystate31) || (keystate32 - keystate30)) {
		sx = dbCameraAngleX();
		sy = dbCameraAngleY();
		sz = dbCameraAngleZ();
		y = dbAtanFull(keystate32 - keystate30, keystate17 - keystate31);
		dbRotateCamera(0.0f, sy + y, sz);
		dbMoveCamera(g_CamSpeed);
		dbRotateCamera(sx, sy, sz);
	}

	// Set new camera position
	cnx = dbCameraPositionX();
	cny = dbCameraPositionY() - g_Gravity;
	cnz = dbCameraPositionZ();

	// get terrain height
	cny = dbGetTerrainGroundHeight(TERID, cnx, cnz) + 52.0f;

	// Update camera and skybox positions
	dbPositionCamera(cnx, cny, cnz);
	dbPositionObject(SKYID, cnx, cny, cnz);

	// Control camera view
	camangx = dbCameraAngleX() + cammovey / 1.5f;
	camangy = dbWrapValue(dbCameraAngleY() + cammovex / 1.5f);
	if ((dbWrapValue(camangx) > 85) && (dbWrapValue(camangx) < 180))
		camangx = 85.0;
	if ((dbWrapValue(camangx) > 180) && (dbWrapValue(camangx) < 275))
		camangx = 275.0;
	dbRotateCamera(camangx, camangy, dbCameraAngleZ());

	// Update guns position and orientation
	dbPositionObject(GUNID, cnx, cny, cnz);
	dbSetObjectToCameraOrientation(GUNID);
	dbMoveObject(GUNID, -4.0f);
}
void LoadLevel(char* Level)
{
	// change directory
	dbSetDir(Level);

	// open imageblock
	dbOpenImageBlock("graphics.bin", 1);

	// load terrain
	dbLoadTerrain("terrain.dbo", TERID);
	dbPositionObject(TERID, 0.0f, 0.0f, -32256.0f);

	// load skybox
	dbLoadObject("sky.dbo", SKYID);
	dbSetObjectCull(SKYID, 1);
	dbDisableObjectZWrite(SKYID);
	dbSetObjectCollisionOff(SKYID);
	dbSetObjectTexture(SKYID, 2, 0);
	dbSetObjectAmbient(SKYID, 0);
	dbSetObjectLight(SKYID, 0);
	dbSetObjectFog(SKYID, 0);

	// load gun
	dbLoadObject("colt45.dbo", GUNID);
	dbSetObjectFOV(GUNID, 45.0f);
	dbYRotateObject(GUNID, 180.0f);
	dbFixObjectPivot(GUNID);
	dbDisableObjectZDepth(GUNID);
	dbSetObjectFrame(GUNID, 10);

	// close the imageblock
	dbCloseImageBlock();

	// restore the directory
	dbSetDir("..");
}