diff --git a/Config/DefaultInput.ini b/Config/DefaultInput.ini index 7e7f73c..7821d03 100644 --- a/Config/DefaultInput.ini +++ b/Config/DefaultInput.ini @@ -75,6 +75,7 @@ DefaultViewportMouseLockMode=LockOnCapture FOVScale=0.011110 DoubleClickTime=0.200000 +ActionMappings=(ActionName="Jump",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=SpaceBar) ++ActionMappings=(ActionName="Equip",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=E) +AxisMappings=(AxisName="MoveForward",Scale=1.000000,Key=W) +AxisMappings=(AxisName="MoveForward",Scale=-1.000000,Key=S) +AxisMappings=(AxisName="MoveRight",Scale=1.000000,Key=D) diff --git a/Content/Assets/LearningKit_Games/Assets/Characters/Character/Mesh/SK_EpicCharacter_Skeleton.uasset b/Content/Assets/LearningKit_Games/Assets/Characters/Character/Mesh/SK_EpicCharacter_Skeleton.uasset index 2c476a7..5e81ab3 100644 Binary files a/Content/Assets/LearningKit_Games/Assets/Characters/Character/Mesh/SK_EpicCharacter_Skeleton.uasset and b/Content/Assets/LearningKit_Games/Assets/Characters/Character/Mesh/SK_EpicCharacter_Skeleton.uasset differ diff --git a/Source/Blaster/Character/BlasterCharacter.cpp b/Source/Blaster/Character/BlasterCharacter.cpp index 2171f54..980f8c4 100644 --- a/Source/Blaster/Character/BlasterCharacter.cpp +++ b/Source/Blaster/Character/BlasterCharacter.cpp @@ -3,6 +3,7 @@ #include "BlasterCharacter.h" +#include "Blaster/Components/CombatComponent.h" #include "Blaster/Weapon/Weapon.h" #include "Camera/CameraComponent.h" #include "Components/WidgetComponent.h" @@ -29,6 +30,9 @@ ABlasterCharacter::ABlasterCharacter() OverheadWidget = CreateDefaultSubobject(TEXT("OverheadWidget")); OverheadWidget->SetupAttachment(RootComponent); + + Combat = CreateDefaultSubobject(TEXT("CombatComponent")); + Combat->SetIsReplicated(true); } void ABlasterCharacter::GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const @@ -38,6 +42,16 @@ void ABlasterCharacter::GetLifetimeReplicatedProps(TArray& Ou DOREPLIFETIME_CONDITION(ABlasterCharacter, OverlappingWeapon, COND_OwnerOnly); } +void ABlasterCharacter::PostInitializeComponents() +{ + Super::PostInitializeComponents(); + + if (Combat) + { + Combat->Character = this; + } +} + void ABlasterCharacter::BeginPlay() { Super::BeginPlay(); @@ -53,6 +67,8 @@ void ABlasterCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputCo Super::SetupPlayerInputComponent(PlayerInputComponent); PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump); + PlayerInputComponent->BindAction("Equip", IE_Pressed, this, &ABlasterCharacter::EquipButtonPressed); + PlayerInputComponent->BindAxis("MoveForward", this, &ABlasterCharacter::MoveForward); PlayerInputComponent->BindAxis("MoveRight", this, &ABlasterCharacter::MoveRight); PlayerInputComponent->BindAxis("Turn", this, &ABlasterCharacter::Turn); @@ -91,13 +107,21 @@ void ABlasterCharacter::LookUp(float Value) AddControllerPitchInput(Value); } +void ABlasterCharacter::EquipButtonPressed() +{ + if (Combat && HasAuthority()) + { + Combat->EquipWeapon(OverlappingWeapon); + } +} + void ABlasterCharacter::SetOverlappingWeapon(AWeapon* Weapon) { if (OverlappingWeapon) { OverlappingWeapon->ShowPickupWidget(false); } - + OverlappingWeapon = Weapon; if (IsLocallyControlled()) { diff --git a/Source/Blaster/Character/BlasterCharacter.h b/Source/Blaster/Character/BlasterCharacter.h index 56dd6ca..b20faa1 100644 --- a/Source/Blaster/Character/BlasterCharacter.h +++ b/Source/Blaster/Character/BlasterCharacter.h @@ -16,6 +16,8 @@ public: virtual void Tick(float DeltaTime) override; virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; virtual void GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const override; + virtual void PostInitializeComponents() override; + protected: virtual void BeginPlay() override; @@ -23,7 +25,8 @@ protected: void MoveRight(float Value); void Turn(float Value); void LookUp(float Value); - + void EquipButtonPressed(); + private: UPROPERTY(VisibleAnywhere, Category="Camera") class USpringArmComponent* CameraBoom; @@ -39,7 +42,10 @@ private: UFUNCTION() void OnRep_OverlappingWeapon(AWeapon* LastWeapon); - + + UPROPERTY(VisibleAnywhere) + class UCombatComponent* Combat; + public: void SetOverlappingWeapon(AWeapon* Weapon); }; diff --git a/Source/Blaster/Components/CombatComponent.cpp b/Source/Blaster/Components/CombatComponent.cpp new file mode 100644 index 0000000..1d37681 --- /dev/null +++ b/Source/Blaster/Components/CombatComponent.cpp @@ -0,0 +1,39 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "CombatComponent.h" + +#include "Blaster/Character/BlasterCharacter.h" +#include "Blaster/Weapon/Weapon.h" +#include "Engine/SkeletalMeshSocket.h" + +UCombatComponent::UCombatComponent() +{ + PrimaryComponentTick.bCanEverTick = false; +} + +void UCombatComponent::BeginPlay() +{ + Super::BeginPlay(); +} + +void UCombatComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) +{ + Super::TickComponent(DeltaTime, TickType, ThisTickFunction); + +} + +void UCombatComponent::EquipWeapon(AWeapon* WeaponToEquip) +{ + if (Character == nullptr || WeaponToEquip == nullptr) return; + + EquippedWeapon = WeaponToEquip; + EquippedWeapon->SetWeaponState(EWeaponState::EWS_Equipped); + const USkeletalMeshSocket* HandSocket = Character->GetMesh()->GetSocketByName(FName("RightHandSocket")); + if (HandSocket) + { + HandSocket->AttachActor(EquippedWeapon, Character->GetMesh()); + } + EquippedWeapon->SetOwner(Character); + EquippedWeapon->ShowPickupWidget(false); +} diff --git a/Source/Blaster/Components/CombatComponent.h b/Source/Blaster/Components/CombatComponent.h new file mode 100644 index 0000000..6b3f9cd --- /dev/null +++ b/Source/Blaster/Components/CombatComponent.h @@ -0,0 +1,28 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "Components/ActorComponent.h" +#include "CombatComponent.generated.h" + + +UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) +class BLASTER_API UCombatComponent : public UActorComponent +{ + GENERATED_BODY() + +public: + UCombatComponent(); + virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; + friend class ABlasterCharacter; + + void EquipWeapon(class AWeapon* WeaponToEquip); + +protected: + virtual void BeginPlay() override; + +private: + class ABlasterCharacter* Character; + class AWeapon* EquippedWeapon; +}; diff --git a/Source/Blaster/Weapon/Weapon.h b/Source/Blaster/Weapon/Weapon.h index de54ee0..3ddb7d6 100644 --- a/Source/Blaster/Weapon/Weapon.h +++ b/Source/Blaster/Weapon/Weapon.h @@ -12,6 +12,7 @@ enum class EWeaponState : uint8 EWS_Initial UMETA(DisplayName = "Initial State"), EWS_Equipped UMETA(DisplayName = "Equipped"), EWS_Dropped UMETA(DisplayName = "Dropped"), + EWS_MAX UMETA(DisplayName = "DefaultMAX") }; @@ -59,4 +60,5 @@ private: class UWidgetComponent* PickupWidget; public: + FORCEINLINE void SetWeaponState(EWeaponState State) { WeaponState = State; }; };