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); | 		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(); | 	AWeapon* GetCurrentWeapon(); | ||||||
| 
 | 
 | ||||||
| 	void SetCurrentWeapon(AWeapon* weapon); | 	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(); | 	Super::BeginPlay(); | ||||||
| 
 | 
 | ||||||
|  | 	if (Weapon) | ||||||
|  | 	{ | ||||||
| 		FActorSpawnParameters SpawnParameters; | 		FActorSpawnParameters SpawnParameters; | ||||||
| 		SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; | 		SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; | ||||||
| 		WeaponComponent = GetWorld()->SpawnActor<AWeapon>(Weapon, SpawnParameters); | 		WeaponComponent = GetWorld()->SpawnActor<AWeapon>(Weapon, SpawnParameters); | ||||||
| @ -33,6 +35,7 @@ void AWeaponPickup::BeginPlay() | |||||||
| 		WeaponStartingLocation = WeaponComponent->GetActorLocation(); | 		WeaponStartingLocation = WeaponComponent->GetActorLocation(); | ||||||
| 
 | 
 | ||||||
| 		SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AWeaponPickup::OnOverlapBegin); | 		SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AWeaponPickup::OnOverlapBegin); | ||||||
|  | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Called every frame
 | // Called every frame
 | ||||||
| @ -40,6 +43,8 @@ void AWeaponPickup::Tick(float DeltaTime) | |||||||
| { | { | ||||||
| 	Super::Tick(DeltaTime); | 	Super::Tick(DeltaTime); | ||||||
| 
 | 
 | ||||||
|  | 	if (WeaponComponent) | ||||||
|  | 	{ | ||||||
| 		// Rotate Weapon in desired direction
 | 		// Rotate Weapon in desired direction
 | ||||||
| 		WeaponComponent->AddActorLocalRotation((SpinRotation * RotationSpeed) * DeltaTime); | 		WeaponComponent->AddActorLocalRotation((SpinRotation * RotationSpeed) * DeltaTime); | ||||||
| 
 | 
 | ||||||
| @ -47,13 +52,21 @@ void AWeaponPickup::Tick(float DeltaTime) | |||||||
| 		float Time = GetWorld()->GetRealTimeSeconds(); | 		float Time = GetWorld()->GetRealTimeSeconds(); | ||||||
| 		float Sine = FMath::Sin(Time * MovementSpeed); | 		float Sine = FMath::Sin(Time * MovementSpeed); | ||||||
| 		WeaponComponent->SetActorLocation(WeaponStartingLocation + (MovementDirection * Sine)); | 		WeaponComponent->SetActorLocation(WeaponStartingLocation + (MovementDirection * Sine)); | ||||||
|  | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void AWeaponPickup::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, | void AWeaponPickup::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, | ||||||
| 	UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) | 	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(); | 		this->Destroy(); | ||||||
| 		WeaponComponent->Destroy(); | 		WeaponComponent->Destroy(); | ||||||
|  | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -5,6 +5,7 @@ | |||||||
| #include "Components/SphereComponent.h" | #include "Components/SphereComponent.h" | ||||||
| #include "CoreMinimal.h" | #include "CoreMinimal.h" | ||||||
| #include "GameFramework/Actor.h" | #include "GameFramework/Actor.h" | ||||||
|  | #include "PlayerCharacter.h" | ||||||
| #include "Weapon.h" | #include "Weapon.h" | ||||||
| #include "WeaponPickup.generated.h" | #include "WeaponPickup.generated.h" | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user