Exemplo n.º 1
0
// Bullets are updated until they're a certain distance away, at which point they are destroyed
void Player::HandleBullets(const jutil::GameTime& timespan)
{
	for (int i = 0; i < m_Bullets.size(); ++i)
	{
		m_Bullets[i].Update(timespan);

		if (std::abs(jmath::vec2::Distance(GetPositionXY(), m_Bullets[i].GetPositionXY()) > 150))
		{
			m_Bullets.erase(m_Bullets.begin() + i);
			i--;
		}
	}
}
bool PierrobotGear::CheckToSwitchDirection(LevelLoader& level)
{
	jmath::vec2 tileCheck;
	tileCheck.x = (int)GetRigidBody()->GetPosition().x - ((int)GetRigidBody()->GetPosition().x % 16);
	tileCheck.y = (int)GetRigidBody()->GetPosition().y - ((int)GetRigidBody()->GetPosition().y % 16);

	tileCheck.x += _direction ? 16 : -32;


	if (level.TileExists(tileCheck))
	{
		tileCheck.x += _direction ? -0 : 16;

		if (jphys::BoundingBoxCentred(tileCheck, jmath::vec4(GetPositionXY(), GetRigidBody()->GetHitboxXY())))
			return true;
	}

	return false;
}
Exemplo n.º 3
0
Vector2 ETHEntity::GetCurrentBucket(const ETHBucketManager& buckets) const
{
	return ETHBucketManager::GetBucket(GetPositionXY(), buckets.GetBucketSize());
}
Exemplo n.º 4
0
void ETHEntity::AddToPositionY(const float v, ETHBucketManager& buckets)
{
	const Vector2& oldPos(GetPositionXY());
	m_controller->AddToPosY(v);
	buckets.RequestBucketMove(this, oldPos, GetPositionXY());
}
Exemplo n.º 5
0
void ETHEntity::SetPositionXY(const Vector2& pos, ETHBucketManager& buckets)
{
	const Vector2& oldPos(GetPositionXY());
	m_controller->SetPosXY(pos);
	buckets.RequestBucketMove(this, oldPos, GetPositionXY());
}
Exemplo n.º 6
0
bool ETHRenderEntity::DrawProjShadow(const float maxHeight, const float minHeight, const ETHSceneProperties& sceneProps, const ETHLight& light, ETHSpriteEntity *pParent,
									 const bool maxOpacity, const bool drawToTarget, const float targetAngle, const Vector3& v3TargetPos)
{
	if (!m_pSprite || IsHidden())
		return false;

	VideoPtr video = m_provider->GetVideo();
	ETHShaderManagerPtr shaderManager = m_provider->GetShaderManager();
	SpritePtr pShadow = shaderManager->GetProjShadow();

	Vector3 v3LightPos;
	Vector3 v3ParentPos(0,0,0);

	const Vector3 v3EntityPos = GetPosition();

	if (pParent)
	{
		v3ParentPos = pParent->GetPosition();
		v3LightPos = Vector3(v3ParentPos.x, v3ParentPos.y, 0) + light.pos;
	}
	else
	{
		v3LightPos = light.pos;
	}

	// if the object is higher than the light, then the shadow shouldn't be cast on the floor
	if (v3LightPos.z < v3EntityPos.z)
	{
		return true;
	}

	const float scale = (m_properties.shadowScale <= 0.0f) ? 1.0f : m_properties.shadowScale;
	const float opacity = (m_properties.shadowOpacity <= 0.0f) ? 1.0f : m_properties.shadowOpacity;
	const Vector2 v2Size = GetCurrentSize();
	Vector2 v2ShadowSize(v2Size.x, v2Size.y);
	Vector2 v2ShadowPos(v3EntityPos.x, v3EntityPos.y);

	// if we are drawing to a target of a rotated entity
	if (drawToTarget && targetAngle != 0)
	{
		// rotate the shadow position according to entity angle
		Matrix4x4 matRot = RotateZ(-DegreeToRadian(targetAngle));
		Vector3 newShadowPos(v2ShadowPos, 0);
		newShadowPos = newShadowPos - v3TargetPos;
		newShadowPos = Multiply(newShadowPos, matRot);
		newShadowPos = newShadowPos + v3TargetPos;
		v2ShadowPos.x = newShadowPos.x;
		v2ShadowPos.y = newShadowPos.y;

		// rotate the light source to cast it correctly
		Vector3 newPos = v3LightPos - v3TargetPos;
		newPos = Multiply(newPos, matRot);
		v3LightPos = newPos + v3TargetPos;
	}

	Vector3 diff = v3EntityPos - v3LightPos;
	const float squaredDist = DP3(diff, diff);
	float squaredRange = light.range * light.range;

	if (squaredDist > squaredRange)
	{
		return true;
	}

	v2ShadowSize.x *= _ETH_SHADOW_SCALEX * scale;

	// calculate the correct shadow length according to the light height
	if ((GetPosition().z+v2Size.y) < light.pos.z) // if the light is over the entity
	{
		const float planarDist = Distance(GetPositionXY(), ETHGlobal::ToVector2(v3LightPos));
		const float verticalDist = Abs((v3EntityPos.z+v2Size.y)-v3LightPos.z);
		const float totalDist = (planarDist/verticalDist)*Abs(v3LightPos.z);
		v2ShadowSize.y = totalDist-planarDist;

		// clamp shadow length to the object's height. This is not realistic
		// but it looks better for the real-time shadows.
		v2ShadowSize.y = Min(v2Size.y*_ETH_SHADOW_FAKE_STRETCH, v2ShadowSize.y);
	}
	else
	{
		v2ShadowSize.y *= ((drawToTarget) ? _ETH_SHADOW_SCALEY : _ETH_SHADOW_SCALEY/4);
	}

	// specify a minimum length for the shadow
	v2ShadowSize.y = Max(v2ShadowSize.y, v2Size.y);

	ShaderPtr pVS = video->GetVertexShader();
	pVS->SetConstant(GS_L("shadowLength"), v2ShadowSize.y * m_properties.shadowLengthScale);
	pVS->SetConstant(GS_L("entityZ"), Max(m_shadowZ, v3EntityPos.z));
	pVS->SetConstant(GS_L("shadowZ"), m_shadowZ);
	pVS->SetConstant(GS_L("lightPos"), v3LightPos);
	video->SetSpriteDepth(
		(GetType() == ETHEntityProperties::ET_VERTICAL) ?
		ETHEntity::ComputeDepth(m_shadowZ, maxHeight, minHeight)
		: Max(0.0f, ComputeDepth(maxHeight, minHeight) - m_layrableMinimumDepth));

	v2ShadowSize.y = 1.0f;

	Vector2 lightPos2(v3LightPos.x, v3LightPos.y);
	const float shadowAngle = ::GetAngle((lightPos2 - Vector2(v3EntityPos.x, v3EntityPos.y))) + DegreeToRadian(targetAngle);

	squaredRange = Max(squaredDist, squaredRange);
	float attenBias = 1;

	// adjust brightness according to ambient light
	if (!maxOpacity)
	{
		attenBias = (1-(squaredDist/squaredRange));
		//fade the color according to the light brightness
		const float colorLen = Max(Max(light.color.x, light.color.y), light.color.z);
		attenBias *= Min(colorLen, 1.0f);

		//fade the color according to the ambient light brightness
		const Vector3 &ambientColor = sceneProps.ambient;
		const float ambientColorLen = 1.0f-((ambientColor.x + ambientColor.y + ambientColor.z)/3.0f);
		attenBias = Min(attenBias*ambientColorLen, 1.0f);
		attenBias *= Max(Min((1-(GetPosition().z/Max(GetCurrentSize().y, 1.0f))), 1.0f), 0.0f);
	}

	GS_BYTE alpha = static_cast<GS_BYTE>(attenBias*255.0f*opacity);

	if (alpha < 8)
		return true;

	Color dwShadowColor(alpha,255,255,255);

	pShadow->SetOrigin(Vector2(0.5f, 0.79f));
	pShadow->SetRectMode(Sprite::RM_THREE_TRIANGLES);
	pShadow->DrawShaped(v2ShadowPos, v2ShadowSize,
		dwShadowColor, dwShadowColor, dwShadowColor, dwShadowColor,
		RadianToDegree(shadowAngle));

	return true;
}
Exemplo n.º 7
0
void Player::HandleControls(const jutil::GameTime& timespan)
{

	// Directional movement
	if (p_Window->GetKey(Mega::Keys::MOVE_LEFT) == JKEY_PRESS)
		MoveX(-0.05f);
	if (p_Window->GetKey(Mega::Keys::MOVE_LEFT) == JKEY_HOLD)
	{
		GetRigidBody()->ApplyForce(jmath::vec3(-0.5f, 0.f, 0.f));
		m_Direction = Mega::Direction::LEFT;
	}
	else if (p_Window->GetKey(Mega::Keys::MOVE_LEFT) == JKEY_RELEASE)
		GetRigidBody()->ClearForce();
	if (p_Window->GetKey(Mega::Keys::MOVE_RIGHT) == JKEY_HOLD)
	{
		GetRigidBody()->ApplyForce(jmath::vec3(0.5f, 0.f, 0.f));
		m_Direction = Mega::Direction::RIGHT;
	}
	
	/// Jumping

	// If space bar is just released, prevent jumping (to stop double jumping in the air)
	if (p_Window->GetKey(Mega::Keys::JUMP) == JKEY_RELEASE && m_TimerJump.ElapsedMilliseconds() > 0)
		m_TimerJump.SetTime(JTime::Milliseconds(JUMP_LIMIT));

	// If the player is standing on a block, allow jumping
	if (m_HasCollidedBelow && m_TimerJump.ElapsedMilliseconds() > 0 && m_TimerJump.ElapsedMilliseconds() >= JUMP_LIMIT && !(p_Window->GetKey(Mega::Keys::JUMP) == JKEY_PRESS || p_Window->GetKey(Mega::Keys::JUMP) == JKEY_HOLD))
		m_TimerJump.SetTime(JTime::Milliseconds(0));

	// If the player is mid-jump and can still hold the button for a longer jump, apply forces
	if (m_TimerJump.ElapsedMilliseconds() >= 0 && m_TimerJump.ElapsedMilliseconds() < JUMP_LIMIT && p_Window->GetKey(Mega::Keys::JUMP) == JKEY_HOLD)
	{
		m_TimerJump += timespan;
		if (m_TimerJump.ElapsedMilliseconds() < JUMP_LIMIT)
		{
			m_PlayerState = PlayerState::JUMPING;

			float t = std::max(m_TimerJump.ElapsedMilliseconds(), 250);
			GetRigidBody()->ApplyForce(jmath::vec3(0, -0.0095f*((t) / 12.f), 0.f));

			if (m_TimerJump.ElapsedMilliseconds() == timespan.ElapsedMilliseconds())
				GetRigidBody()->ApplyForce(jmath::vec3(0.f, -3.f, 0.f));

			if (m_TimerJump.ElapsedMilliseconds() != 0 && m_TimerJump.ElapsedMilliseconds() < 100)
			{
				jmath::vec3 v = GetRigidBody()->GetVelocity();
				v.x = 0.f;
				GetRigidBody()->SetVelocity(v);

				v = m_CollisionY.GetRigidBody()->GetVelocity();
				v.x = 0.f;
				m_CollisionY.GetRigidBody()->SetVelocity(v);

			}
		}
	}
	// Otherwise, we've ran out of jumping time 
	else if (m_TimerJump.ElapsedMilliseconds() < 0) 
	{
		m_TimerJump += timespan;
		if (m_TimerJump.ElapsedMilliseconds() > 0)
			m_TimerJump.SetTime(0);
	}

	// If the jump button has been released since jumping and we're on a tile, change state
	if (p_Window->GetKey(Mega::Keys::JUMP) != JKEY_HOLD && m_HasCollidedBelow)
		m_PlayerState = PlayerState::GROUNDED;


	m_TimerBulletAnimationDelay += timespan;

	//// Firing
	if (p_Window->GetKey(Mega::Keys::FIRE) == JKEY_PRESS)// && m_TimerBulletAnimationDelay.ElapsedMilliseconds() > FIRING_DELAY)
	{
		m_TimerBulletAnimationDelay.SetTime(0);
		jmath::vec2 pos = GetPositionXY();

		pos.x += static_cast<bool>(m_Direction) ? 12.f : -12.f;
		pos.y--;
		m_Bullets.push_back(Bullet(pos, m_Direction));
		m_Bullets.back().SetTexture(GetTexture());
		m_Bullets.back().SetRectangle(jmath::vec4(104, 72, 8, 8));
	}

	if (p_Window->GetKey(JKEY_KEY_C) == JKEY_PRESS)
		m_Health = 0;
}