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,16 +23,19 @@ void AWeaponPickup::BeginPlay() | |||||||
| { | { | ||||||
| 	Super::BeginPlay(); | 	Super::BeginPlay(); | ||||||
| 
 | 
 | ||||||
| 	FActorSpawnParameters SpawnParameters; | 	if (Weapon) | ||||||
| 	SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; | 	{ | ||||||
| 	WeaponComponent = GetWorld()->SpawnActor<AWeapon>(Weapon, SpawnParameters); | 		FActorSpawnParameters SpawnParameters; | ||||||
| 	FAttachmentTransformRules TransformRules = FAttachmentTransformRules(EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, true); | 		SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; | ||||||
| 	WeaponComponent->AttachToComponent(RootComponent, TransformRules); | 		WeaponComponent = GetWorld()->SpawnActor<AWeapon>(Weapon, SpawnParameters); | ||||||
| 	WeaponComponent->SetActorRelativeLocation(FVector(0.0f, 0.0f, 5.0f)); | 		FAttachmentTransformRules TransformRules = FAttachmentTransformRules(EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, true); | ||||||
| 	WeaponComponent->SetActorEnableCollision(false); | 		WeaponComponent->AttachToComponent(RootComponent, TransformRules); | ||||||
| 	WeaponStartingLocation = WeaponComponent->GetActorLocation(); | 		WeaponComponent->SetActorRelativeLocation(FVector(0.0f, 0.0f, 5.0f)); | ||||||
|  | 		WeaponComponent->SetActorEnableCollision(false); | ||||||
|  | 		WeaponStartingLocation = WeaponComponent->GetActorLocation(); | ||||||
| 
 | 
 | ||||||
| 	SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AWeaponPickup::OnOverlapBegin); | 		SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AWeaponPickup::OnOverlapBegin); | ||||||
|  | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Called every frame
 | // Called every frame
 | ||||||
| @ -40,20 +43,30 @@ void AWeaponPickup::Tick(float DeltaTime) | |||||||
| { | { | ||||||
| 	Super::Tick(DeltaTime); | 	Super::Tick(DeltaTime); | ||||||
| 
 | 
 | ||||||
| 	// Rotate Weapon in desired direction
 | 	if (WeaponComponent) | ||||||
| 	WeaponComponent->AddActorLocalRotation((SpinRotation * RotationSpeed) * DeltaTime); | 	{ | ||||||
|  | 		// Rotate Weapon in desired direction
 | ||||||
|  | 		WeaponComponent->AddActorLocalRotation((SpinRotation * RotationSpeed) * DeltaTime); | ||||||
| 
 | 
 | ||||||
| 	// Bob weapon up and down
 | 		// Bob weapon up and down
 | ||||||
| 	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
 | ||||||
| 	this->Destroy(); | 	auto player = Cast<APlayerCharacter>(OtherActor); | ||||||
| 	WeaponComponent->Destroy(); | 	 | ||||||
|  | 	if (player && Weapon) | ||||||
|  | 	{ | ||||||
|  | 		player->AddWeaponToInventory(Weapon); | ||||||
|  | 
 | ||||||
|  | 		this->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