Move weapon and inventory logic to base Character class
This commit is contained in:
		
							parent
							
								
									84807d6416
								
							
						
					
					
						commit
						d56e25ad62
					
				| @ -41,3 +41,150 @@ void ANakatomiCharacter::SetHealthComponent(UHealthComponent* component) | ||||
| { | ||||
| 	HealthComponent = component; | ||||
| } | ||||
| 
 | ||||
| void ANakatomiCharacter::SetInventoryToDefault() | ||||
| { | ||||
| 	if (WeaponInventory.Num() > 0) | ||||
| 	{ | ||||
| 		for (size_t i = 0; i < WeaponInventory.Num(); i++) | ||||
| 		{ | ||||
| 			WeaponInventory[i]->Destroy(); | ||||
| 		} | ||||
| 
 | ||||
| 		WeaponInventory.Empty(); | ||||
| 	} | ||||
| 
 | ||||
| 	for (size_t i = 0; i < DefaultWeaponInventory.Num(); i++) | ||||
| 	{ | ||||
| 		if (DefaultWeaponInventory[i]) | ||||
| 		{ | ||||
| 			AWeapon* weapon = InitializeWeapon(DefaultWeaponInventory[i]); | ||||
| 			WeaponInventory.AddUnique(weapon); | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	if (WeaponInventory.Num() > 0) | ||||
| 	{ | ||||
| 		CurrentInventorySlot = 0; | ||||
| 		SetCurrentWeapon(WeaponInventory[CurrentInventorySlot]); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void ANakatomiCharacter::SelectInventorySlot(int slot) | ||||
| { | ||||
| 	if (slot < WeaponInventory.Num()) | ||||
| 	{ | ||||
| 		CurrentInventorySlot = slot; | ||||
| 		SetCurrentWeapon(WeaponInventory[CurrentInventorySlot]); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void ANakatomiCharacter::InventoryIncrement() | ||||
| { | ||||
| 	if (WeaponInventory.Num() > 0) | ||||
| 	{ | ||||
| 		SelectInventorySlot((CurrentInventorySlot + 1) % WeaponInventory.Num()); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void ANakatomiCharacter::InventoryDecrement() | ||||
| { | ||||
| 	if (WeaponInventory.Num() > 0) | ||||
| 	{ | ||||
| 		if (CurrentInventorySlot - 1 < 0) | ||||
| 		{ | ||||
| 			SelectInventorySlot(WeaponInventory.Num() - 1); | ||||
| 		} | ||||
| 		else | ||||
| 		{ | ||||
| 			SelectInventorySlot((CurrentInventorySlot - 1) % WeaponInventory.Num()); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void ANakatomiCharacter::RemoveWeaponFromInventory(int i) | ||||
| { | ||||
| 	if (WeaponInventory[i] == CurrentWeapon) | ||||
| 	{ | ||||
| 		CurrentWeapon = nullptr; | ||||
| 	} | ||||
| 
 | ||||
| 	WeaponInventory[i]->Destroy(); | ||||
| 	WeaponInventory.RemoveAt(i); | ||||
| 
 | ||||
| 	if (WeaponInventory.Num() == 0) | ||||
| 	{ | ||||
| 		CurrentInventorySlot = -1; | ||||
| 	} | ||||
| 	else if (int index = WeaponInventory.Find(CurrentWeapon) == INDEX_NONE) | ||||
| 	{ | ||||
| 		SetCurrentWeapon(WeaponInventory[CurrentInventorySlot % WeaponInventory.Num()]); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		CurrentInventorySlot = index; | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void ANakatomiCharacter::RemoveWeaponFromInventory(AWeapon* weapon) | ||||
| { | ||||
| 	if (int index = WeaponInventory.Find(weapon) != INDEX_NONE) | ||||
| 	{ | ||||
| 		RemoveWeaponFromInventory(index - 1); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void ANakatomiCharacter::RemoveCurrentWeaponFromInventory() | ||||
| { | ||||
| 	RemoveWeaponFromInventory(CurrentWeapon); | ||||
| } | ||||
| 
 | ||||
| void ANakatomiCharacter::AddWeaponToInventory(TSubclassOf<class AWeapon> weapon) | ||||
| { | ||||
| 	if (weapon) | ||||
| 	{ | ||||
| 		AWeapon* newWeapon = InitializeWeapon(weapon); | ||||
| 		WeaponInventory.Add(newWeapon); | ||||
| 
 | ||||
| 		if (WeaponInventory.Num() == 1) | ||||
| 		{ | ||||
| 			SetCurrentWeapon(WeaponInventory[0]); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| AWeapon* ANakatomiCharacter::InitializeWeapon(TSubclassOf<class AWeapon> weapon) | ||||
| { | ||||
| 	FActorSpawnParameters SpawnParameters; | ||||
| 	SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; | ||||
| 	AWeapon* Weapon = GetWorld()->SpawnActor<AWeapon>(weapon, SpawnParameters); | ||||
| 	Weapon->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetNotIncludingScale, "WeaponHand"); | ||||
| 	Weapon->SetActorEnableCollision(false); | ||||
| 	Weapon->SetActorHiddenInGame(true); | ||||
| 
 | ||||
| 	return Weapon; | ||||
| } | ||||
| 
 | ||||
| AWeapon* ANakatomiCharacter::GetCurrentWeapon() | ||||
| { | ||||
| 	return CurrentWeapon; | ||||
| } | ||||
| 
 | ||||
| void ANakatomiCharacter::SetCurrentWeapon(AWeapon* weapon) | ||||
| { | ||||
| 	if (CurrentWeapon == weapon) | ||||
| 	{ | ||||
| 		return; | ||||
| 	} | ||||
| 
 | ||||
| 	if (CurrentWeapon) | ||||
| 	{ | ||||
| 		CurrentWeapon->SetActorHiddenInGame(true); | ||||
| 	} | ||||
| 
 | ||||
| 	if (weapon) | ||||
| 	{ | ||||
| 		CurrentWeapon = weapon; | ||||
| 		CurrentWeapon->SetActorHiddenInGame(false); | ||||
| 	} | ||||
| } | ||||
| @ -5,6 +5,7 @@ | ||||
| #include "CoreMinimal.h" | ||||
| #include "GameFramework/Character.h" | ||||
| #include "HealthComponent.h" | ||||
| #include "Weapon.h" | ||||
| #include "NakatomiCharacter.generated.h" | ||||
| 
 | ||||
| /**
 | ||||
| @ -15,11 +16,24 @@ class NAKATOMI_API ANakatomiCharacter : public ACharacter | ||||
| { | ||||
| 	GENERATED_BODY() | ||||
| 
 | ||||
| public: | ||||
| 
 | ||||
| 	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) | ||||
| 	TArray<TSubclassOf<class AWeapon>> DefaultWeaponInventory; | ||||
| 
 | ||||
| 	UPROPERTY() | ||||
| 	TArray<AWeapon*> WeaponInventory; | ||||
| 
 | ||||
| 	UPROPERTY() | ||||
| 	AWeapon* CurrentWeapon = nullptr; | ||||
| 
 | ||||
| private: | ||||
| 
 | ||||
| 	UPROPERTY(VisibleDefaultsOnly) | ||||
| 	UHealthComponent* HealthComponent = nullptr; | ||||
| 
 | ||||
| 	int CurrentInventorySlot = 0; | ||||
| 
 | ||||
| public: | ||||
| 	// Sets default values for this character's properties
 | ||||
| 	ANakatomiCharacter(); | ||||
| @ -38,4 +52,26 @@ public: | ||||
| 	UHealthComponent* GetHealthComponent(); | ||||
| 
 | ||||
| 	void SetHealthComponent(UHealthComponent* healthComponent); | ||||
| 
 | ||||
| 	void SetInventoryToDefault(); | ||||
| 
 | ||||
| 	void SelectInventorySlot(int slot); | ||||
| 
 | ||||
| 	void InventoryIncrement(); | ||||
| 
 | ||||
| 	void InventoryDecrement(); | ||||
| 
 | ||||
| 	void AddWeaponToInventory(TSubclassOf<class AWeapon> weapon); | ||||
| 
 | ||||
| 	void RemoveWeaponFromInventory(int i); | ||||
| 
 | ||||
| 	void RemoveWeaponFromInventory(AWeapon* weapon); | ||||
| 
 | ||||
| 	void RemoveCurrentWeaponFromInventory(); | ||||
| 
 | ||||
| 	AWeapon* InitializeWeapon(TSubclassOf<class AWeapon> weapon); | ||||
| 
 | ||||
| 	AWeapon* GetCurrentWeapon(); | ||||
| 
 | ||||
| 	void SetCurrentWeapon(AWeapon* weapon); | ||||
| }; | ||||
|  | ||||
| @ -11,7 +11,6 @@ | ||||
| #include "Destructable.h" | ||||
| #include "EnemyCharacter.h" | ||||
| 
 | ||||
| 
 | ||||
| #define COLLISION_WEAPON	ECC_GameTraceChannel1 | ||||
| 
 | ||||
| // Sets default values
 | ||||
| @ -273,43 +272,8 @@ void APlayerCharacter::CalculateHits(TArray<FHitResult>* hits) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void APlayerCharacter::SetInventoryToDefault() | ||||
| { | ||||
| 	if (WeaponInventory.Num() > 0) | ||||
| 	{ | ||||
| 		for (size_t i = 0; i < WeaponInventory.Num(); i++) | ||||
| 		{ | ||||
| 			WeaponInventory[i]->Destroy(); | ||||
| 		} | ||||
| 
 | ||||
| 		WeaponInventory.Empty(); | ||||
| 	} | ||||
| 
 | ||||
| 	for (size_t i = 0; i < DefaultWeaponInventory.Num(); i++) | ||||
| 	{ | ||||
| 		if (DefaultWeaponInventory[i]) | ||||
| 		{ | ||||
| 			AWeapon* weapon = InitializeWeapon(DefaultWeaponInventory[i]); | ||||
| 			WeaponInventory.AddUnique(weapon); | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	if (WeaponInventory.Num() > 0) | ||||
| 	{ | ||||
| 		CurrentInventorySlot = 0; | ||||
| 		SetCurrentWeapon(WeaponInventory[CurrentInventorySlot]); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void APlayerCharacter::SelectInventorySlot(int slot) | ||||
| {	 | ||||
| 	if (slot < WeaponInventory.Num()) | ||||
| 	{ | ||||
| 		CurrentInventorySlot = slot; | ||||
| 		SetCurrentWeapon(WeaponInventory[CurrentInventorySlot]); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void APlayerCharacter::WeaponSwitchingCallback(const FInputActionInstance& Instance) | ||||
| @ -326,116 +290,6 @@ void APlayerCharacter::WeaponSwitchingCallback(const FInputActionInstance& Insta | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void APlayerCharacter::InventoryIncrement() | ||||
| { | ||||
| 	if (WeaponInventory.Num() > 0) | ||||
| 	{ | ||||
| 		SelectInventorySlot((CurrentInventorySlot + 1) % WeaponInventory.Num()); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void APlayerCharacter::InventoryDecrement() | ||||
| { | ||||
| 	if (WeaponInventory.Num() > 0) | ||||
| 	{ | ||||
| 		if (CurrentInventorySlot - 1 < 0) | ||||
| 		{ | ||||
| 			SelectInventorySlot(WeaponInventory.Num() - 1); | ||||
| 		} | ||||
| 		else | ||||
| 		{ | ||||
| 			SelectInventorySlot((CurrentInventorySlot - 1) % WeaponInventory.Num()); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| AWeapon* APlayerCharacter::InitializeWeapon(TSubclassOf<class AWeapon> weapon) | ||||
| { | ||||
| 	FActorSpawnParameters SpawnParameters; | ||||
| 	SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; | ||||
| 	AWeapon* Weapon = GetWorld()->SpawnActor<AWeapon>(weapon, SpawnParameters); | ||||
| 	Weapon->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetNotIncludingScale, "WeaponHand"); | ||||
| 	Weapon->SetActorEnableCollision(false); | ||||
| 	Weapon->SetActorHiddenInGame(true); | ||||
| 	 | ||||
| 	return Weapon; | ||||
| } | ||||
| 
 | ||||
| AWeapon* APlayerCharacter::GetCurrentWeapon() | ||||
| { | ||||
| 	return CurrentWeapon; | ||||
| } | ||||
| 
 | ||||
| void APlayerCharacter::SetCurrentWeapon(AWeapon* weapon) | ||||
| { | ||||
| 	if (CurrentWeapon == weapon) | ||||
| 	{ | ||||
| 		return; | ||||
| 	} | ||||
| 
 | ||||
| 	if (CurrentWeapon) | ||||
| 	{ | ||||
| 		CurrentWeapon->SetActorHiddenInGame(true); | ||||
| 	} | ||||
| 
 | ||||
| 	if (weapon) | ||||
| 	{ | ||||
| 		CurrentWeapon = 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) | ||||
| {	 | ||||
| 	if (WeaponInventory[i] == CurrentWeapon) | ||||
| 	{ | ||||
| 		CurrentWeapon = nullptr; | ||||
| 	} | ||||
| 
 | ||||
| 	WeaponInventory[i]->Destroy(); | ||||
| 	WeaponInventory.RemoveAt(i); | ||||
| 
 | ||||
| 	if (WeaponInventory.Num() == 0) | ||||
| 	{ | ||||
| 		CurrentInventorySlot = -1; | ||||
| 	} | ||||
| 	else if (int index = WeaponInventory.Find(CurrentWeapon) == INDEX_NONE) | ||||
| 	{		 | ||||
| 		SetCurrentWeapon(WeaponInventory[CurrentInventorySlot % WeaponInventory.Num()]); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		CurrentInventorySlot = index; | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void APlayerCharacter::RemoveWeaponFromInventory(AWeapon* weapon) | ||||
| { | ||||
| 	if (int index = WeaponInventory.Find(weapon) != INDEX_NONE) | ||||
| 	{ | ||||
| 		RemoveWeaponFromInventory(index - 1); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void APlayerCharacter::RemoveCurrentWeaponFromInventory() | ||||
| { | ||||
| 	RemoveWeaponFromInventory(CurrentWeapon); | ||||
| } | ||||
| 
 | ||||
| void APlayerCharacter::OnFire() | ||||
| { | ||||
| 	if (!IsFiring) | ||||
|  | ||||
| @ -51,12 +51,6 @@ public: | ||||
| 	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) | ||||
| 	int MappingPriority = 0; | ||||
| 
 | ||||
| 	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) | ||||
| 	TArray<TSubclassOf<class AWeapon>> DefaultWeaponInventory; | ||||
| 
 | ||||
| 	UPROPERTY() | ||||
| 	TArray<AWeapon*> WeaponInventory; | ||||
| 
 | ||||
| 	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) | ||||
| 	UInputAction* WeaponSwitchingAction; | ||||
| 
 | ||||
| @ -78,11 +72,6 @@ private: | ||||
| 
 | ||||
| 	float DefaultMovementSpeed; | ||||
| 
 | ||||
| 	int CurrentInventorySlot = 0; | ||||
| 
 | ||||
| 	UPROPERTY() | ||||
| 	AWeapon* CurrentWeapon = nullptr; | ||||
| 
 | ||||
| 	FTimerHandle FireTimerHandle; | ||||
| 
 | ||||
| 	FTimerHandle CooldownTimerHandle; | ||||
| @ -124,29 +113,7 @@ public: | ||||
| 
 | ||||
| 	void CalculateHits(TArray<FHitResult>* hits); | ||||
| 
 | ||||
| 	void SetInventoryToDefault(); | ||||
| 
 | ||||
| 	void SelectInventorySlot(int slot); | ||||
| 
 | ||||
| 	void WeaponSwitchingCallback(const FInputActionInstance& Instance); | ||||
| 
 | ||||
| 	void InventoryIncrement(); | ||||
| 
 | ||||
| 	void InventoryDecrement(); | ||||
| 
 | ||||
| 	AWeapon* InitializeWeapon(TSubclassOf<class AWeapon> weapon); | ||||
| 
 | ||||
| 	AWeapon* GetCurrentWeapon(); | ||||
| 
 | ||||
| 	void SetCurrentWeapon(AWeapon* weapon); | ||||
| 
 | ||||
| 	void AddWeaponToInventory(TSubclassOf<class AWeapon> weapon); | ||||
| 
 | ||||
| 	void RemoveWeaponFromInventory(int i); | ||||
| 
 | ||||
| 	void RemoveWeaponFromInventory(AWeapon* weapon); | ||||
| 
 | ||||
| 	void RemoveCurrentWeaponFromInventory(); | ||||
| 	 | ||||
| 	void OnFire(); | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user