Untitled

 avatar
unknown
plain_text
a year ago
4.0 kB
6
Indexable
#include "Game.h"
#include "Pellet.h"
#include "Mesh.h"
#include "MeshComponent.h"
#include "CollisionComponent.h"
#include "Renderer.h"
#include "Player.h"
#include "Portal.h"

Pellet::Pellet(Game* game)
: Actor(game)
{
	mPelletMesh = new MeshComponent(this);
	mPelletMesh->SetMesh(game->GetRenderer()->GetMesh("Assets/Meshes/Sphere.gpmesh"));
	mPelletMesh->SetTextureIndex(1);
	mPelletColl = new CollisionComponent(this);
	mPelletColl->SetSize(PELLET_SIZE, PELLET_SIZE, PELLET_SIZE);
}

void Pellet::OnUpdate(float deltaTime)
{
	mInvunlerableTime -= deltaTime;
	mTeleportCooldown -= deltaTime;

	mVelocity = SPEED * mForward;
	Vector3 newPos = GetPosition() + (mVelocity * deltaTime);
	SetPosition(newPos);
	std::vector<Actor*>& tempColliders = GetGame()->GetColliders();
	//if the pellet collides with the player
	if (mPelletColl->Intersect(GetGame()->GetPlayer()->GetComponent<CollisionComponent>()))
	{
		SetState(ActorState::Destroy);
	}

	//check for pellet teleport
	if (mTeleportCooldown <= 0.0f)
	{
		Game* game = GetGame();
		Portal* blue = game->GetBluePortal();
		Portal* orange = game->GetOrangePortal();
		//check if both portal exist
		if (blue != nullptr && orange != nullptr)
		{
			// Vector3 newPos;
			//enters the blue portal
			if (mPelletColl->Intersect(blue->GetComponent<CollisionComponent>()))
			{
				// mVelocity = blue->GetPortalOutVector(mVelocity, orange, 0.0f) *
				// 			orange->GetQuatForward();
				// newPos = blue->GetPortalOutVector(GetPosition(), orange, 1.0f);
				// SetPosition(newPos);
				Teleport(blue, orange);
				mTeleportCooldown = TELEPORT_COOLDOWN;
			}
			//enters the orange portal
			else if (mPelletColl->Intersect(orange->GetComponent<CollisionComponent>()))
			{
				// mVelocity = orange->GetPortalOutVector(mVelocity, blue, 0.0f) *
				// 			blue->GetQuatForward();
				// newPos = orange->GetPortalOutVector(GetPosition(), blue, 1.0f);
				// SetPosition(newPos);
				Teleport(orange, blue);
				mTeleportCooldown = TELEPORT_COOLDOWN;
			}
		}
	}

	//0.25 second have passed since first spawning pellet
	if (mInvunlerableTime < 0.0f && mTeleportCooldown < 0.0f)
	{
		//check for collision with other colliders
		for (auto collider : tempColliders)
		{
			if (mPelletColl->Intersect(collider->GetComponent<CollisionComponent>()))
			{
				SetState(ActorState::Destroy);
			}
		}
	}
}

void Pellet::Teleport(class Portal* entry, class Portal* exit)
{
	//calculate the rotation for the player
	if (Vector3::Cross(exit->GetQuatForward(), Vector3::UnitZ).Length() != 0.0f)
	{
		Vector3 desireFace;
		if (Vector3::Cross(entry->GetQuatForward(), Vector3::UnitZ).Length() == 0.0f)
		{
			desireFace = exit->GetQuatForward();
		}
		else
		{
			desireFace = entry->GetPortalOutVector(GetForward(), exit, 0.0f);
		}

		//update player rotation
		Vector3 crossOfXDesire = Vector3::Cross(Vector3::Normalize(Vector3::UnitX),
												Vector3::Normalize(desireFace));
		float angle = Math::Acos(
			Vector3::Dot(Vector3::Normalize(desireFace), Vector3::Normalize(Vector3::UnitX)));
		if (crossOfXDesire.z > 0.0f)
		{
			SetRotation(angle);
		}
		else
		{
			SetRotation(-angle);
		}
	}

	float maxVelLength = Math::Max((1.5f * mVelocity.Length()), 350.0f);
	//find the direction of the Player
	Vector3 direction;
	if (Vector3::Cross(exit->GetQuatForward(), Vector3::UnitZ).Length() == 0.0f ||
		Vector3::Cross(entry->GetQuatForward(), Vector3::UnitZ).Length() == 0.0f)
	{
		direction = exit->GetQuatForward();
	}
	else
	{
		direction = entry->GetPortalOutVector(Vector3::Normalize(mVelocity), exit, 0.0f);
	}
	mVelocity = direction * maxVelLength;

	//find new position of the player
	Vector3 basePoint;
	if (Vector3::Cross(exit->GetQuatForward(), Vector3::UnitZ).Length() == 0.0f ||
		Vector3::Cross(entry->GetQuatForward(), Vector3::UnitZ).Length() == 0.0f)
	{
		basePoint = exit->GetPosition();
	}
	else
	{
		basePoint = entry->GetPortalOutVector(GetPosition(), exit, 1.0f);
	}
	SetPosition(basePoint + exit->GetQuatForward() * 50.0f);
}
Editor is loading...
Leave a Comment