|
2 | 2 |
|
3 | 3 | #include "BatteryCollector.h" |
4 | 4 | #include "BatteryCollectorCharacter.h" |
| 5 | +#include "Pickup.h" |
5 | 6 |
|
6 | 7 | ////////////////////////////////////////////////////////////////////////// |
7 | 8 | // ABatteryCollectorCharacter |
@@ -56,6 +57,8 @@ void ABatteryCollectorCharacter::SetupPlayerInputComponent(class UInputComponent |
56 | 57 | InputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump); |
57 | 58 | InputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping); |
58 | 59 |
|
| 60 | + InputComponent->BindAction("Collect", IE_Pressed, this, &ABatteryCollectorCharacter::CollectPickups); |
| 61 | + |
59 | 62 | InputComponent->BindAxis("MoveForward", this, &ABatteryCollectorCharacter::MoveForward); |
60 | 63 | InputComponent->BindAxis("MoveRight", this, &ABatteryCollectorCharacter::MoveRight); |
61 | 64 |
|
@@ -130,3 +133,25 @@ void ABatteryCollectorCharacter::MoveRight(float Value) |
130 | 133 | AddMovementInput(Direction, Value); |
131 | 134 | } |
132 | 135 | } |
| 136 | + |
| 137 | +void ABatteryCollectorCharacter::CollectPickups() |
| 138 | +{ |
| 139 | + // Get all overlapping Actors and store them in an array |
| 140 | + TArray<AActor*> CollectedActors; |
| 141 | + CollectionSphere->GetOverlappingActors(CollectedActors); |
| 142 | + |
| 143 | + // For each Actor we collected |
| 144 | + for (int32 iCollected = 0; iCollected < CollectedActors.Num(); ++iCollected) |
| 145 | + { |
| 146 | + // Cast the actor to APickup |
| 147 | + APickup* const TestPickup = Cast<APickup>(CollectedActors[iCollected]); |
| 148 | + // If the cast is successful and the pickup is valid and active |
| 149 | + if (TestPickup && !TestPickup->IsPendingKill() && TestPickup->IsActive()) |
| 150 | + { |
| 151 | + // Call the pickup's WasCollected function |
| 152 | + TestPickup->WasCollected(); |
| 153 | + // Deactivate the pickup |
| 154 | + TestPickup->SetActive(false); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments