void UCollidingPawnMovementComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
	// some amazing coding here

	// Make sure that every thing is still valid and we are allowed to move
	if (!PawnOwner || !UpdatedComponent || ShouldSkipUpdate(DeltaTime))
	{
		return;
	}

	// Get (and then clear) the movement vector that we set in ACollidingPawn::Tick
	FVector DesiredMovementThisFrame = ConsumeInputVector().GetClampedToMaxSize(1.0f)*DeltaTime*150.0f;
	if (!DesiredMovementThisFrame.IsNearlyZero())
	{
		FHitResult Hit;
		SafeMoveUpdatedComponent(DesiredMovementThisFrame, UpdatedComponent->GetComponentRotation(), true, Hit);

		// If we bumped into something, try to slide along it // this check is important because otherwise the pawn will simply stop
	/*	if (Hit.IsValidBlockingHit())
		{
			SlideAlongSurface(DesiredMovementThisFrame, 1.f - Hit.Time, Hit.Normal, Hit);
		}*/
	}

}
Ejemplo n.º 2
0
void UFloatingPawnMovement::ApplyControlInputToVelocity(float DeltaTime)
{
	const FVector ControlAcceleration = GetPendingInputVector().ClampMaxSize(1.f);

	const float AnalogInputModifier = (ControlAcceleration.SizeSquared() > 0.f ? ControlAcceleration.Size() : 0.f);
	const float MaxPawnSpeed = GetMaxSpeed() * AnalogInputModifier;
	const bool bExceedingMaxSpeed = IsExceedingMaxSpeed(MaxPawnSpeed);

	if (AnalogInputModifier > 0.f && !bExceedingMaxSpeed)
	{
		// Apply change in velocity direction
		if (Velocity.SizeSquared() > 0.f)
		{
			Velocity -= (Velocity - ControlAcceleration * Velocity.Size()) * FMath::Min(DeltaTime * 8.f, 1.f);
		}
	}
	else
	{
		// Dampen velocity magnitude based on deceleration.
		if (Velocity.SizeSquared() > 0.f)
		{
			const FVector OldVelocity = Velocity;
			const float VelSize = FMath::Max(Velocity.Size() - FMath::Abs(Deceleration) * DeltaTime, 0.f);
			Velocity = Velocity.SafeNormal() * VelSize;

			// Don't allow braking to lower us below max speed if we started above it.
			if (bExceedingMaxSpeed && Velocity.SizeSquared() < FMath::Square(MaxPawnSpeed))
			{
				Velocity = OldVelocity.SafeNormal() * MaxPawnSpeed;
			}
		}
	}

	// Apply acceleration and clamp velocity magnitude.
	const float NewMaxSpeed = (IsExceedingMaxSpeed(MaxPawnSpeed)) ? Velocity.Size() : MaxPawnSpeed;
	Velocity += ControlAcceleration * FMath::Abs(Acceleration) * DeltaTime;
	Velocity = Velocity.ClampMaxSize(NewMaxSpeed);

	ConsumeInputVector();
}
void UMainCharacterMovementComponent::TickComponent(float deltaTime, enum ELevelTick tickType, FActorComponentTickFunction *thisTickFunction)
{
    UPawnMovementComponent::TickComponent(deltaTime, tickType, thisTickFunction);

    if (!PawnOwner || !UpdatedComponent || ShouldSkipUpdate(deltaTime))
    {
        return;
    }

    if (m_character)
    {
        const FVector posInRoom = m_character->GetRelativePositionInRoom(); // pos should be center of room

        if (auto* room = m_character->GetCurrentRoom())
        {
            int ladderPosInRoom = room->GetLadderPos();
            bool isInsideLadderRegion = abs(posInRoom.X - ladderPosInRoom) <= (ARoom::c_ladderWidth * 0.5);
            
            // handle climbing up
            if (isInsideLadderRegion)
            {
                if (m_wantsToClimbUp)
                {
                    if (room->HasLadderUp())
                    {
                        if (auto* neighbour = room->GetNeighbour(RoomNeighbour::Up))
                        {
                            m_isMoving = false;
                            m_character->TeleportToRoom(neighbour);
                        }
                    }
                }

                // handle climbing down
                if (m_wantsToClimbDown)
                {
                    if (room->HasLadderDown())
                    {
                        if (auto* neighbour = room->GetNeighbour(RoomNeighbour::Down))
                        {
                            m_isMoving = false;
                            m_character->TeleportToRoom(neighbour);
                        }
                    }
                }
            }

            // handle left and right
            FVector desiredMovementThisFrame = ConsumeInputVector().GetClampedToMaxSize(1.0f) * deltaTime * m_walkSpeed;
            if (!desiredMovementThisFrame.IsNearlyZero())
            {
                m_isMoving = true;
                if (desiredMovementThisFrame.X < 0.0f)
                {
                    m_direction = FacingDirection::Left;
                }
                else if (desiredMovementThisFrame.X > 0.0f)
                {
                    m_direction = FacingDirection::Right;
                }


                const float leftWallPos = room->GetLeftWallXPos();
                const float rightWallPos = room->GetRightWallXPos();

                FVector desiredPosInRoom = posInRoom - desiredMovementThisFrame;

                // no neighbour == wall
                // poor collision detection that won't work in low FPS (but hey, who gives a f***s?)
                // hint: not me
                if (!room->GetNeighbour(RoomNeighbour::Left) &&
                    desiredMovementThisFrame.X < 0 &&
                    desiredPosInRoom.X < leftWallPos)
                {
                    desiredMovementThisFrame.X = 0;
                    m_isMoving = false;

                }

                if (!room->GetNeighbour(RoomNeighbour::Right) &&
                    desiredMovementThisFrame.X > 0 &&
                    desiredPosInRoom.X > rightWallPos)
                {
                    desiredMovementThisFrame.X = 0;
                    m_isMoving = false;
                }

                FHitResult hit;
                MoveUpdatedComponent(desiredMovementThisFrame, UpdatedComponent->GetComponentRotation().Quaternion(), true, &hit);
            }
            else
            {
                m_isMoving = false;
            }
        }
    }

    m_wantsToClimbUp = false;
    m_wantsToClimbDown = false;
}