void AiController::OnTick(const natU64 _dt)
{
	if(!m_lifeController->IsAlive())
	{
		// called spawner first
		//m_spawner->OnKilled(GetEntity());

		Spawned* spawned = GetEntity()->GetComponent<Spawned>();
		assert(spawned);
		spawned->Kill();
	}
	else
	{
		// move toward player
		Entity* player = m_playersManager->GetLocalPlayer();
		Transform* transform_player = player->GetComponent<Transform>();
		Transform* transform = GetEntity()->GetComponent<Transform>();
		RigidBody* rigidbody = GetEntity()->GetComponent<RigidBody>();

		glm::vec3 direction = transform_player->GetPos() - transform->GetPos();

		if(direction != glm::vec3(0.f))
		{
			direction = glm::normalize(direction);
			//transform->m_pos += static_cast<natF32>(_dt) * direction * m_speed;
			direction = static_cast<natF32>(_dt) * direction;
			rigidbody->ApplyLinearImpulse(direction);

		}
	}
}
glm::vec3 Transform::GetPos()
{
	Transform* parent = GetEntity()->GetParent()->GetComponent<Transform>();

	if(parent)
	{
		return m_pos + parent->GetPos();
	}
	else
	{
		return m_pos;
	}
}
void AiController::OnInit()
{
	Transform* transform = GetEntity()->GetComponent<Transform>();
	m_center = transform->GetPos();
	m_radius = 200.0f;
	m_t = 0.0f;

	m_playersManager = GetEntity()->GetKernel()->GetLayer(Layer::s_LayerManager)->GetRootEntity()->GetComponent<PlayersManager>();
	m_lifeController = GetEntity()->GetComponent<LifeController>();

	assert(m_playersManager);
	assert(m_lifeController);
}
Esempio n. 4
0
int main( int argc, char* args[] )
{

	
	srand(time(NULL));

	Display display(800, 600, "Hello World!");

	GameObject paddle2Obj(vertexPositions1, sizeof(vertexPositions1), indicesPositions, sizeof(indicesPositions));
	Mesh testMesh(vertexPositions1, sizeof(vertexPositions1), indicesPositions, sizeof(indicesPositions));
	Mesh paddle2(vertexPositions1, sizeof(vertexPositions1), indicesPositions, sizeof(indicesPositions));
	Mesh paddle1(vertexPositions1, sizeof(vertexPositions1), indicesPositions, sizeof(indicesPositions));
	Mesh ball(vertexPositions1, sizeof(vertexPositions1), indicesPositions, sizeof(indicesPositions));
	Mesh background(vertexPositions1, sizeof(vertexPositions1), indicesPositions, sizeof(indicesPositions));
	Mesh pointBlock(vertexPositions1, sizeof(vertexPositions1), indicesPositions, sizeof(indicesPositions));
	Mesh skyBox(vertexPositions1, sizeof(vertexPositions1), indicesPositions, sizeof(indicesPositions));

	//Shader shader(path); //  Shader
	Texture texture(texturePath); // Create a texture
	Texture texture2(texturePath2); // Create a texture
	Texture texture3(texturePath3); // Create a texture
	Texture texture4(texturePath4); // Create a texture
	Texture texture5(texturePath5); // Create a texture
	Texture texture6(texturePath6); // Create a texture
	Transform paddle1Transform; // , Transformations
	Transform paddle2Transform; // , Transformations
	Transform ballTransform;
	Transform backgroundTransform;
	Transform pointBlockTransform;
	Transform skyBoxTransform;
	Camera perspectiveCamera(glm::vec3(0.f, 0.f, 2.f), 70.f, (float)800.f / (float)600.f, 0.1f, 10000.0f);
	Camera orthographicCamera(glm::vec3(0.f, 0.f, 3.f), -1.0f, 1.0f, -1.0f, 1.0f, 0.1f, 100.0f);

	Camera renderCamera = orthographicCamera; // Camera used in the scene
	
	GLint player1Points = 0;
	GLint player2Points = 0;
	float value = 0.0f;

	pointBlockTransform.SetPos(glm::vec3(-0.96,0.95f, 0.f));
	pointBlockTransform.SetScale(glm::vec3(0.04f, 0.04f, 0.04f));

	backgroundTransform.GetPos().z = -3.0f;
	backgroundTransform.SetScale(glm::vec3(5.0f, 4.8f, 1.0f));

	paddle2Transform.GetPos().x = 0.9f;
	paddle1Transform.GetPos().x = -0.9f;
	ballTransform.GetPos().x = -0.8f;

	skyBoxTransform.GetScale() *= 10000;

	float ballSpeed = 15.f;
	glm::vec3 ballVelocity(ballSpeed, -ballSpeed, 0.0f); // Set the initial velocity

	paddle1Transform.SetScale(glm::vec3(.05f, .3f, .1f));
	paddle2Transform.SetScale(glm::vec3(.05f, .3f, .1f));
	ballTransform.SetScale(glm::vec3(.05f, .075f, .075f));
	//paddle2Obj.transform = paddle1Transform;

	double lastElapsedTime = SDL_GetTicks() - 1, deltaTime = 0.0f;
	while (!display.IsWindowClosed()) /// While the window is open
	{
		GLint randomNo = rand() % 2 + 1;
		display.Clear(0.0f, 0.0f, 0.0f, 0.0f);
		// Calculate deltaTime from how long it took between frames
		double elapsedTime = SDL_GetTicks();
		deltaTime = (elapsedTime / lastElapsedTime) / 1000.0f;
		lastElapsedTime = elapsedTime;
		//cout << deltaTime << endl;
		if ((ballTransform.GetPos().x) > 1) // Point for player1
		{
			ballTransform.GetPos().x = 0.8f;
			ballTransform.GetPos().y = paddle2Transform.GetPos().y;
			ballVelocity.x = -ballVelocity.x;
			player1Points++; // Increase points
			cout << "Player 1 Point" << endl;

			if (player1Points == 20)
			{
				player1Points = 0;
				player2Points = 0;
				cout << "Player 2 wins" << endl;
			}
		}
		else if (ballTransform.GetPos().x < -1) // Point for player2
		{
			ballTransform.GetPos().x = -0.8f;
			ballTransform.GetPos().y = paddle1Transform.GetPos().y;
			ballVelocity.x = -ballVelocity.x;
			player2Points++; // Increase points
			cout << "Player 2 Point" << endl;

			if (player2Points == 20)
			{
				player2Points = 0;
				player1Points = 0;
				cout << "Player 2 wins" << endl;
			}
				
		}


		background.Draw(paddle1Transform, renderCamera);
		
		ballTransform.GetPos() += (ballVelocity * (float)deltaTime);

		if (ballTransform.GetPos().y > 0.9 || ballTransform.GetPos().y < -0.9)
				ballVelocity.y = -ballVelocity.y;
			

		if (display.m_perspective) // perspective projection
		{
			display.Clear(0.0f, 0.0f, 0.0f, 0.0f);
			ballTransform.GetRot().z += 200 * deltaTime;
			texture5.Bind();
			skyBox.Draw(skyBoxTransform, renderCamera, false);
			glBindTexture(GL_TEXTURE_2D, 0); // No texture
			if (!display.m_cameraMode)
			renderCamera = perspectiveCamera;

			if (display.m_viewPort == FOLLOWBALL)
				renderCamera.m_view = glm::lookAt(ballTransform.GetPos() - glm::vec3(0.f, 0.f, -1.f), glm::vec3(0.f, 0.f, -10.f) + renderCamera.m_forward, renderCamera.m_up);
				
			else if (display.m_viewPort == PADDLE1)
				renderCamera.m_view = glm::lookAt(paddle1Transform.GetPos() - glm::vec3(-0.001f, 0.f, 0.f), paddle1Transform.GetPos() - glm::vec3(-0.002f, 0.f, 0.f), renderCamera.m_up);
			else if (display.m_viewPort == PADDLE2)
				renderCamera.m_view = glm::lookAt(paddle2Transform.GetPos() - glm::vec3(0.001f, 0.f, 0.f), paddle2Transform.GetPos() - glm::vec3(0.002f, 0.f, 0.f), renderCamera.m_up);
			texture3.Bind();
			background.Draw(backgroundTransform, renderCamera);
			glBindTexture(GL_TEXTURE_2D, 0); // No texture
			texture.Bind();
			paddle1.Draw(paddle1Transform, renderCamera); // Draw the mesh
			glBindTexture(GL_TEXTURE_2D, 0); // No texture
			texture6.Bind();
			paddle2.Draw(paddle2Transform, renderCamera); // Draw second mesh
			glBindTexture(GL_TEXTURE_2D, 0); // No texture
			texture4.Bind();
			ball.Draw(ballTransform, renderCamera);
			glBindTexture(GL_TEXTURE_2D, 0); // No texture
			texture2.Bind();
			pointBlockTransform.SetRot(glm::vec3(sinf(value), sinf(value), sinf(value)));
		}
		else // orthographic projection
		{
			display.Clear(1.0f, 1.0f, 1.0f, 1.0f);
			ballTransform.GetRot().z = 0.f;
			if (!display.m_cameraMode)
			renderCamera = orthographicCamera;
			glBindTexture(GL_TEXTURE_2D, 0); // No texture
			paddle1.Draw(paddle1Transform, renderCamera); // Draw the mesh
			paddle2.Draw(paddle2Transform, renderCamera); // Draw second mesh
			
			ball.Draw(ballTransform, renderCamera);
			pointBlockTransform.SetRot(glm::vec3(0.f, 0.f, pointBlockTransform.GetRot().z + 30.f * deltaTime));
			//texture2.Bind();
		}

		
		

		for (int i = 0; i < player1Points; i++)
		{
			Transform temp(pointBlockTransform);
			temp.GetPos().y = pointBlockTransform.GetPos().y - (i * 0.1f);
			pointBlock.Draw(temp, orthographicCamera);
		}

		for (int i = 0; i < player2Points; i++)
		{
			Transform temp(pointBlockTransform);
			temp.GetPos().x = -temp.GetPos().x;
			temp.GetPos().y = pointBlockTransform.GetPos().y - (i * 0.1f);
			pointBlock.Draw(temp, orthographicCamera);
		}
		
		glBindTexture(GL_TEXTURE_2D, 0); // No texture
		
		// Ball collision with bats
		if (ballTransform.GetPos().y <= paddle2Transform.GetPos().y + 0.1 && ballTransform.GetPos().y > paddle2Transform.GetPos().y - 0.1 && ballTransform.GetPos().x >= paddle2Transform.GetPos().x - 0.05)
		{
			
			ballVelocity = -ballVelocity;

			if (randomNo == 1)
				ballVelocity.y = ballSpeed;
			else
				ballVelocity.y = -ballSpeed;
			
			
		}
		
		else if (ballTransform.GetPos().y <= paddle1Transform.GetPos().y + 0.1 && ballTransform.GetPos().y > paddle1Transform.GetPos().y - 0.1 && ballTransform.GetPos().x <= paddle1Transform.GetPos().x + 0.05 && ballTransform.GetPos().x > paddle1Transform.GetPos().x - 0.05)
		{
			ballVelocity = -ballVelocity;
			if (randomNo == 1)
				ballVelocity.y = ballSpeed;
			else
				ballVelocity.y = -ballSpeed;
		}
			
			//paddle1Transform.GetRot().z = value;
		//perspectiveCamera.m_pos = ballTransform.GetPos();
		//perspectiveCamera.m_pos.z += 1.f;

		/*perspectiveCamera.m_pos = paddle1Transform.GetPos();
		perspectiveCamera.m_pos.x += 2.f;
		perspectiveCamera.m_pos.z += 2.f;
		perspectiveCamera.m_forward = glm::vec3(1.0f, 0.f, 1.f);*/

		
		//perspectiveCamera.m_pos.y += 1;
		value += 0.01f;
		paddle2Obj.m_mesh.Draw(paddle2Obj.transform, renderCamera);
		//testMesh.Draw(paddle2Obj.transform, renderCamera);
		
		renderCamera.Update();
		display.Update(deltaTime, renderCamera, paddle1Transform, paddle2Transform, paddle1Transform); // Update the display
		
		
	}


	//while (!done && (SDL_GetTicks() < 5000000)) //LOOP FROM HERE, for 2000ms (or if done flag is set)
	//	//WARNING: SDL_GetTicks is only accurate to milliseconds, use SDL_GetPerformanceCounter and SDL_GetPerformanceFrequency for higher accuracy
	//{
	//	//GET INPUT HERE - PLACEHOLDER

	//	mesh.UpdateSimulation(0.02); //call update simulation with an amount of time to simulate for (in seconds)
	//	  //WARNING - we are always updating by a constant amount of time. This should be tied to how long has elapsed
	//	    // see, for example, http://headerphile.blogspot.co.uk/2014/07/part-9-no-more-delays.html
 //       glViewport(0,0,800,600);
	//	display.Clear(0.0f, 0.0f, 1.0f, 1.0f);

	//	mesh.Render(*shader); //RENDER HERE - PLACEHOLDER

	//	//SDL_GL_SwapWindow(display->m_pWin);; //present the frame buffer to the display (swapBuffers)
	//	display.Update();

	//} //LOOP TO HERE

	//cleanup and exit
	//display->CleanUp();
	SDL_Quit();

	return 0;
}