diff --git a/UnrealHyperTwist/Source/UnrealHyperTwist/Private/HyperTwistSimulation/HyperTwistClassicCubeActor.cpp b/UnrealHyperTwist/Source/UnrealHyperTwist/Private/HyperTwistSimulation/HyperTwistClassicCubeActor.cpp index a5444c3..90a6bdc 100644 --- a/UnrealHyperTwist/Source/UnrealHyperTwist/Private/HyperTwistSimulation/HyperTwistClassicCubeActor.cpp +++ b/UnrealHyperTwist/Source/UnrealHyperTwist/Private/HyperTwistSimulation/HyperTwistClassicCubeActor.cpp @@ -58,6 +58,45 @@ void AHyperTwistClassicCubeActor::ResetCube() GenerateCube(); } +bool AHyperTwistClassicCubeActor::ProcessClick(const FVector& RayOrigin, const FVector& RayDirection, bool bCounterClockwise) +{ + if (!GetWorld()) + { + return false; + } + + FHitResult Hit; + FCollisionQueryParams Params(SCENE_QUERY_STAT(CubeClick), false, this); + const FVector End = RayOrigin + RayDirection * 10000.0f; + bool bHit = GetWorld()->LineTraceSingleByChannel(Hit, RayOrigin, End, ECC_Visibility, Params); + + if (!bHit || !Hit.GetComponent()) + { + return false; + } + + // Map hit normal to cube face + const FVector& Normal = Hit.ImpactNormal; + EHyperTwistClassicCubeFace Face = EHyperTwistClassicCubeFace::Up; + bool bValidFace = true; + + if (FMath::IsNearlyEqual(Normal.Z, 1.0f)) Face = EHyperTwistClassicCubeFace::Up; + else if (FMath::IsNearlyEqual(Normal.Z, -1.0f)) Face = EHyperTwistClassicCubeFace::Down; + else if (FMath::IsNearlyEqual(Normal.Y, 1.0f)) Face = EHyperTwistClassicCubeFace::Front; + else if (FMath::IsNearlyEqual(Normal.Y, -1.0f)) Face = EHyperTwistClassicCubeFace::Back; + else if (FMath::IsNearlyEqual(Normal.X, 1.0f)) Face = EHyperTwistClassicCubeFace::Right; + else if (FMath::IsNearlyEqual(Normal.X, -1.0f)) Face = EHyperTwistClassicCubeFace::Left; + else bValidFace = false; + + if (!bValidFace) + { + return false; + } + + RotateFace(Face, bCounterClockwise ? EHyperTwistRotationDirection::CounterClockwise : EHyperTwistRotationDirection::Clockwise); + return true; +} + void AHyperTwistClassicCubeActor::ClearPieces() { for (const FTrackedPiece& Piece : TrackedPieces) diff --git a/UnrealHyperTwist/Source/UnrealHyperTwist/Public/HyperTwistSimulation/HyperTwistClassicCubeActor.h b/UnrealHyperTwist/Source/UnrealHyperTwist/Public/HyperTwistSimulation/HyperTwistClassicCubeActor.h index 12c8832..e29b09e 100644 --- a/UnrealHyperTwist/Source/UnrealHyperTwist/Public/HyperTwistSimulation/HyperTwistClassicCubeActor.h +++ b/UnrealHyperTwist/Source/UnrealHyperTwist/Public/HyperTwistSimulation/HyperTwistClassicCubeActor.h @@ -54,6 +54,16 @@ public: UFUNCTION(BlueprintCallable, Category = "HyperTwist|Cube") void ResetCube(); + /** + * Process a click ray. If it hits a visible cube face, rotates that face. + * @param RayOrigin World-space ray origin (e.g. camera location) + * @param RayDirection World-space ray direction (normalized) + * @param bCounterClockwise If true, rotate counter-clockwise; else clockwise + * @return True if a face was hit and rotation was queued + */ + UFUNCTION(BlueprintCallable, Category = "HyperTwist|Cube") + bool ProcessClick(const FVector& RayOrigin, const FVector& RayDirection, bool bCounterClockwise = false); + virtual void OnConstruction(const FTransform& Transform) override; protected: