
#include "JancoProject.h"
#include <GL/freeglut.h>
#include <cstring>


int CrystalPoint::width = 0;
int CrystalPoint::height = 0;

bool state = false;

void CrystalPoint::init()
{
	player = Player::getInstance();
	world = World::getInstance();

	lastFrameTime = 0;
	state = true;

	glClearColor(0.7f, 0.7f, 1.0f, 1.0f);
}


void CrystalPoint::draw()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	//Draw world
	glEnable(GL_DEPTH_TEST);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(70, width / (float)height, 0.1f, 15000);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	
	world->draw();
	

	glutSwapBuffers();
}


void CrystalPoint::update()
{
	float frameTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
	float deltaTime = frameTime - lastFrameTime;
	lastFrameTime = frameTime;

	if (keyboardState.keys[27] && !prevKeyboardState.keys[27])
        state = !state;

		Player* player = Player::getInstance();

		if (keyboardState.keys[27])
			exit(0);

		player->rotation.y += mouseOffset.x / 10.0f;
		player->rotation.x += mouseOffset.y / 10.0f;


		float speed = 30;

		Vec3f oldPosition = player->position;
		if (keyboardState.keys['a']) player->setPosition(0, deltaTime*speed, false);
		if (keyboardState.keys['d']) player->setPosition(180, deltaTime*speed, false);
		if (keyboardState.keys['w']) player->setPosition(90, deltaTime*speed, false);
		if (keyboardState.keys['s']) player->setPosition(270, deltaTime*speed, false);
		if (keyboardState.keys['q']) player->setPosition(1, deltaTime*speed, true);
		if (keyboardState.keys['e']) player->setPosition(-1, deltaTime*speed, true);
		if (keyboardState.keys['z']) world->objects[1].scale+=1*deltaTime;
		if (keyboardState.keys['x']) world->objects[1].scale-=1*deltaTime;


		if (player->rotation.x > 90)
			player->rotation.x = 90;
		if (player->rotation.x < -90)
			player->rotation.x = -90;


		world->update(deltaTime);


	mouseOffset = Vec2f(0, 0);
	prevKeyboardState = keyboardState;
	glutPostRedisplay();

}

KeyboardState::KeyboardState()
{
	memset(keys, 0, sizeof(keys));
	memset(special, 0, sizeof(special));
}
