Add Weapon from Weapon Pickup to Current Inventory
This commit is contained in:
		
							parent
							
								
									0f0ab4c6e7
								
							
						
					
					
						commit
						b955732fb8
					
				| @ -315,3 +315,30 @@ void APlayerCharacter::SetCurrentWeapon(AWeapon* weapon) | ||||
| 		CurrentWeapon->SetActorHiddenInGame(false); | ||||
| 	}	 | ||||
| } | ||||
| 
 | ||||
| void APlayerCharacter::AddWeaponToInventory(TSubclassOf<class AWeapon> weapon) | ||||
| { | ||||
| 	if (weapon) | ||||
| 	{ | ||||
| 		AWeapon* newWeapon = InitializeWeapon(weapon); | ||||
| 		WeaponInventory.Add(newWeapon); | ||||
| 
 | ||||
| 		if (WeaponInventory.Num() == 1) | ||||
| 		{ | ||||
| 			SetCurrentWeapon(WeaponInventory[0]); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void APlayerCharacter::RemoveWeaponFromInventory(int i) | ||||
| { | ||||
| 	// TODO: Add more checking here
 | ||||
| 	WeaponInventory[i]->Destroy(); | ||||
| 	WeaponInventory.RemoveAt(i); | ||||
| } | ||||
| 
 | ||||
| void APlayerCharacter::RemoveCurrentWeaponFromInventory() | ||||
| { | ||||
| 	// TODO: Add more checking here
 | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -124,4 +124,10 @@ public: | ||||
| 	AWeapon* GetCurrentWeapon(); | ||||
| 
 | ||||
| 	void SetCurrentWeapon(AWeapon* weapon); | ||||
| 
 | ||||
| 	void AddWeaponToInventory(TSubclassOf<class AWeapon> weapon); | ||||
| 
 | ||||
| 	void RemoveWeaponFromInventory(int i); | ||||
| 
 | ||||
| 	void RemoveCurrentWeaponFromInventory(); | ||||
| }; | ||||
|  | ||||
| @ -23,6 +23,8 @@ void AWeaponPickup::BeginPlay() | ||||
| { | ||||
| 	Super::BeginPlay(); | ||||
| 
 | ||||
| 	if (Weapon) | ||||
| 	{ | ||||
| 		FActorSpawnParameters SpawnParameters; | ||||
| 		SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; | ||||
| 		WeaponComponent = GetWorld()->SpawnActor<AWeapon>(Weapon, SpawnParameters); | ||||
| @ -34,12 +36,15 @@ void AWeaponPickup::BeginPlay() | ||||
| 
 | ||||
| 		SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AWeaponPickup::OnOverlapBegin); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| // Called every frame
 | ||||
| void AWeaponPickup::Tick(float DeltaTime) | ||||
| { | ||||
| 	Super::Tick(DeltaTime); | ||||
| 
 | ||||
| 	if (WeaponComponent) | ||||
| 	{ | ||||
| 		// Rotate Weapon in desired direction
 | ||||
| 		WeaponComponent->AddActorLocalRotation((SpinRotation * RotationSpeed) * DeltaTime); | ||||
| 
 | ||||
| @ -48,12 +53,20 @@ void AWeaponPickup::Tick(float DeltaTime) | ||||
| 		float Sine = FMath::Sin(Time * MovementSpeed); | ||||
| 		WeaponComponent->SetActorLocation(WeaponStartingLocation + (MovementDirection * Sine)); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void AWeaponPickup::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, | ||||
| 	UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) | ||||
| { | ||||
| 	// TODO: Add weapon to player inventory
 | ||||
| 	// TODO: Add extra checking here
 | ||||
| 	auto player = Cast<APlayerCharacter>(OtherActor); | ||||
| 	 | ||||
| 	if (player && Weapon) | ||||
| 	{ | ||||
| 		player->AddWeaponToInventory(Weapon); | ||||
| 
 | ||||
| 		this->Destroy(); | ||||
| 		WeaponComponent->Destroy(); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -5,6 +5,7 @@ | ||||
| #include "Components/SphereComponent.h" | ||||
| #include "CoreMinimal.h" | ||||
| #include "GameFramework/Actor.h" | ||||
| #include "PlayerCharacter.h" | ||||
| #include "Weapon.h" | ||||
| #include "WeaponPickup.generated.h" | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user