Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "TunicsCharacter.h"
- #include "TunicsProjectile.h"
- #include "Animation/AnimInstance.h"
- #include "Camera/CameraComponent.h"
- #include "Components/CapsuleComponent.h"
- #include "Components/InputComponent.h"
- #include "GameFramework/InputSettings.h"
- #include "HeadMountedDisplayFunctionLibrary.h"
- #include "Kismet/GameplayStatics.h"
- #include <DrawDebugHelpers.h>
- #include <GameFramework/Character.h>
- #include "Components/InputComponent.h"
- #include "GameFramework/Character.h"
- #include "GameFramework/CharacterMovementComponent.h"
- #include "GameFramework/PawnMovementComponent.h"
- #include <ConstructorHelpers.h>
- #include <LogMacros.h>
- #include <Engine/EngineTypes.h>
- #include <UserWidget.h>
- #include <CanvasPanel.h>
- #include <CanvasPanelSlot.h>
- #include "MainHud.h"
- #include "PlayerInvetory.h"
- #include <WidgetBlueprintLibrary.h>
- #include "Net/UnrealNetwork.h"
- #include <UObjectGlobals.h>
- #include <Animation/AnimBlueprintGeneratedClass.h>
- #include "Attachment.h"
- #include "Door.h"
- #include <Engine/Engine.h>
- #include "ComplexInventory.h"
- DEFINE_LOG_CATEGORY_STATIC(LogFPChar, Warning, All);
- //////////////////////////////////////////////////////////////////////////
- // ATunicsCharacter
- ATunicsCharacter::ATunicsCharacter()
- {
- PrimaryActorTick.bCanEverTick = true;
- GetCapsuleComponent()->InitCapsuleSize(55.f, 96.0f);
- BaseTurnRate = 45.f;
- BaseLookUpRate = 45.f;
- Mesh1P = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("CharacterMesh1P"));
- Mesh1P->SetupAttachment(GetCapsuleComponent());
- Mesh1P->RelativeRotation = FRotator(1.9f, -19.19f, 5.2f);
- Mesh1P->RelativeLocation = FVector(-0.5f, -4.4f, -155.7f);
- FirstPersonCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
- FirstPersonCameraComponent->SetupAttachment(Mesh1P);
- FirstPersonCameraComponent->RelativeLocation = FVector(-39.56f, 1.75f, 64.f);
- FirstPersonCameraComponent->bUsePawnControlRotation = true;
- TriggerCapsule = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Trigger Capsule"));
- TriggerCapsule->InitCapsuleSize(55.f, 96.0f);
- TriggerCapsule->SetCollisionProfileName(TEXT("Trigger"));
- TriggerCapsule->SetupAttachment(RootComponent);
- TriggerCapsule->OnComponentBeginOverlap.AddDynamic(this, &ATunicsCharacter::OnOverlapBegin);
- TriggerCapsule->OnComponentEndOverlap.AddDynamic(this, &ATunicsCharacter::OnOverlapEnd);
- GunOffset = FVector(100.0f, 0.0f, 10.0f);
- PrimarySelected = true;
- CurrentItem = EmptyHands;
- Inventory = CreateDefaultSubobject<UComplexInventory>(TEXT("Complex Inventory"));
- Inventory->Mesh1p = Mesh1P;
- SceneCapture = CreateDefaultSubobject<USceneCaptureComponent2D>(TEXT("SceneCapture"));
- SceneCapture->SetupAttachment(FirstPersonCameraComponent);
- SceneCapture->bCaptureEveryFrame = true;
- SceneCapture->bHiddenInGame = true;
- SceneCapture->FOVAngle = 3.0f;
- }
- void ATunicsCharacter::BeginPlay()
- {
- // Call the base class
- Super::BeginPlay();
- //Attach gun mesh component to Skeleton, doing it here because the skeleton is not yet created in the constructor
- FirstPersonCameraComponent->AttachToComponent(Mesh1P, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT("camera_socket"));
- FActorSpawnParameters SpawnInfo;
- SpawnInfo.Owner = this;
- EmptyHands = GetWorld()->SpawnActor<AItem>(EmptyHandsInstance, FVector(0, 0, 0), FRotator(0, 0, 0), SpawnInfo);
- EmptyHands->SetOwner(this);
- CurrentItem = EmptyHands;
- ChangeAnimClass(CurrentItem->ABP);
- SceneCapture->TextureTarget = ScopeRender;
- }
- void ATunicsCharacter::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
- GroundItems.Empty();
- TArray<AActor*> OverlappedItems;
- TriggerCapsule->GetOverlappingActors(OverlappedItems, AItem::StaticClass());
- for (int i = 0; i < OverlappedItems.Num(); i++) {
- AItem* groundItem = Cast<AItem>(OverlappedItems[i]);
- if (groundItem) { //ignore this item because its in mine or another players hands
- if (groundItem->CanPickup) {
- GroundItems.Add(groundItem);
- }
- }
- }
- if (GetNetMode() == NM_Client)
- {
- // Send input to server
- ServerUpdateInput(FPlayerInputStruct(isFiring, isAiming, isSprinting, isEquipping,IsCrouching, GetControlRotation()));
- }
- if (Role == ROLE_Authority) {
- ClientLookUp(GetControlRotation());
- }
- }
- //////////////////////////////////////////////////////////////////////////
- // Input
- void ATunicsCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
- {
- check(PlayerInputComponent);
- PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
- PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
- PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &ATunicsCharacter::BeginFire);
- PlayerInputComponent->BindAction("Fire", IE_Released, this, &ATunicsCharacter::EndFire);
- PlayerInputComponent->BindAction("Aim", IE_Pressed, this, &ATunicsCharacter::Aim);
- PlayerInputComponent->BindAction("Aim", IE_Released, this, &ATunicsCharacter::UnAim);
- PlayerInputComponent->BindAction("Reload", IE_Pressed, this, &ATunicsCharacter::Reload);
- PlayerInputComponent->BindAction("Chamber", IE_Pressed, this, &ATunicsCharacter::Chamber);
- PlayerInputComponent->BindAction("SwapWeapon1", IE_Pressed, this, &ATunicsCharacter::SelectWeapon1);
- PlayerInputComponent->BindAction("SwapWeapon2", IE_Pressed, this, &ATunicsCharacter::SelectWeapon2);
- PlayerInputComponent->BindAction("Interact", IE_Pressed, this, &ATunicsCharacter::Interact);
- PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &ATunicsCharacter::BeginCrouch);
- PlayerInputComponent->BindAction("Crouch", IE_Released, this, &ATunicsCharacter::EndCrouch);
- PlayerInputComponent->BindAction("DropWeapon", IE_Released, this, &ATunicsCharacter::DropWeapon);
- PlayerInputComponent->BindAction("Sprint", IE_Pressed, this, &ATunicsCharacter::BeingSprint);
- PlayerInputComponent->BindAction("Sprint", IE_Released, this, &ATunicsCharacter::EndSprint);
- PlayerInputComponent->BindAxis("MoveForward", this, &ATunicsCharacter::MoveForward);
- PlayerInputComponent->BindAxis("MoveRight", this, &ATunicsCharacter::MoveRight);
- PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
- PlayerInputComponent->BindAxis("TurnRate", this, &ATunicsCharacter::TurnAtRate);
- PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
- PlayerInputComponent->BindAxis("LookUpRate", this, &ATunicsCharacter::LookUpAtRate);
- }
- void ATunicsCharacter::OnOverlapBegin(UPrimitiveComponent* OVerllapedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
- {
- AItem* grounditem = Cast<AItem>(OtherActor);
- if (grounditem) {
- if (grounditem->CanPickup) {
- OverlappedWeapon = grounditem;
- FInventorySlot newslot;
- newslot.item = grounditem;
- }
- }
- }
- void ATunicsCharacter::OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
- {
- AItem* grounditem = Cast<AItem>(OtherActor);
- if (grounditem) {
- if (grounditem->CanPickup) {
- OverlappedWeapon = NULL;
- }
- }
- }
- void ATunicsCharacter::Aim() {
- GetCharacterMovement()->MaxWalkSpeed = 600;
- isAiming = true;
- cMoveSpeed = 0.5;
- if (SceneCapture->FOVAngle > 0.f && Controller && Controller->IsLocalController())
- {
- SceneCapture->bHiddenInGame = false;
- }
- }
- void ATunicsCharacter::UnAim() {
- GetCharacterMovement()->MaxWalkSpeed = 600;
- isAiming = false;
- cMoveSpeed = 1;
- SceneCapture->bHiddenInGame = true;
- }
- void ATunicsCharacter::BeginCrouch()
- {
- IsCrouching = true;
- isSprinting = false;
- cMoveSpeed = 0.5;
- }
- void ATunicsCharacter::EndCrouch()
- {
- IsCrouching = false;
- cMoveSpeed = 1;
- }
- void ATunicsCharacter::ToggleInventory()
- {
- APlayerController* playercontroller = Cast<APlayerController>(GetController());
- if (isInventoryOpen && SpawnedInventory) {
- isInventoryOpen = false;
- GroundSpawnedInventory->RemoveFromParent();
- GroundSpawnedInventory = NULL;
- SpawnedInventory->RemoveFromParent();
- SpawnedInventory = NULL;
- playercontroller->bShowMouseCursor = false;
- playercontroller->SetIgnoreLookInput(false);
- FInputModeGameOnly InputMode;
- playercontroller->SetInputMode(InputMode);
- }
- else {
- isInventoryOpen = true;
- GroundSpawnedInventory = CreateWidget<UPlayerInvetory>(GetWorld(), GroundInventoryInstance);
- GroundSpawnedInventory->Player = this;
- UCanvasPanelSlot* newslot1 = HudWidget->MainHudPanel->AddChildToCanvas(GroundSpawnedInventory);
- newslot1->SetAutoSize(true);
- newslot1->SetAlignment(FVector2D(0.5f, 0.5f));
- FAnchors anchor1;
- anchor1.Minimum = FVector2D(0.5f, 0.5f);
- anchor1.Maximum = FVector2D(0.5f, 0.5f);
- newslot1->SetAnchors(anchor1);
- newslot1->SetPosition(FVector2D(-500, 0));
- SpawnedInventory = CreateWidget<UPlayerInvetory>(GetWorld(), InventoryInstance);
- SpawnedInventory->Player = this;
- UCanvasPanelSlot* newslot = HudWidget->MainHudPanel->AddChildToCanvas(SpawnedInventory);
- newslot->SetAutoSize(true);
- newslot->SetAlignment(FVector2D(0.5f, 0.5f));
- FAnchors anchor;
- anchor.Minimum = FVector2D(0.5f, 0.5f);
- anchor.Maximum = FVector2D(0.5f, 0.5f);
- newslot->SetAnchors(anchor);
- newslot->SetPosition(FVector2D(500, 0));
- playercontroller->bShowMouseCursor = true;
- playercontroller->SetIgnoreLookInput(true);
- FInputModeGameAndUI InputMode;
- playercontroller->SetInputMode(InputMode);
- }
- }
- void ATunicsCharacter::Interact()
- {
- //operate door
- FHitResult Hit;
- FVector Start = FirstPersonCameraComponent->GetComponentLocation();
- FVector ForwardVector = FirstPersonCameraComponent->GetForwardVector();
- FVector End = ((ForwardVector * 200.f) + Start);
- FCollisionQueryParams CollisionParams;
- CollisionParams.AddIgnoredActor(this);
- if (GetWorld()->LineTraceSingleByChannel(Hit, Start, End, ECC_WorldDynamic, CollisionParams))
- {
- //GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, Hit.Actor->GetFName().ToString());
- if (Hit.bBlockingHit)
- {
- ADoor* currentDoor = Cast<ADoor>(Hit.GetActor());
- if (currentDoor) {
- currentDoor->SetOwner(this);
- currentDoor->ServerToggleDoor(this);
- }
- }
- }
- //pickup ground item
- if (GroundItems.Num() > 0) {
- if (isUnEquipping == false && isEquipping == false) {
- PickUpWeapon(GroundItems[0]);
- }
- }
- }
- void ATunicsCharacter::DropItemFromInventory(int InventoryIndex)
- {
- if (Inventory->HeldItems[InventoryIndex].item) {
- if (Inventory->HeldItems[InventoryIndex].item->CanBeDropped) {
- if (Role < ROLE_Authority) { // this is a client
- ServerDropItemFromInventory(InventoryIndex);
- }
- Inventory->HeldItems[InventoryIndex].item->DetachRootComponentFromParent();
- Inventory->HeldItems[InventoryIndex].item->CanPickup = true;
- Inventory->HeldItems[InventoryIndex].item->SetActorHiddenInGame(false);
- Inventory->RemoveItemFromSlot(Inventory->HeldItems[InventoryIndex].item, InventoryIndex);
- if (InventoryIndex == PrimaryIndex && PrimarySelected) {
- CurrentItem = EmptyHands;
- SwitchWeapons(true);
- }
- else if(InventoryIndex == SecondaryIndex &&PrimarySelected ==false){
- Inventory->RemoveItemFromSlot(CurrentItem, SecondaryIndex);
- CurrentItem = EmptyHands;
- SwitchWeapons(false);
- }
- }
- }
- }
- bool ATunicsCharacter::ServerDropItemFromInventory_Validate(int InventoryIndex)
- {
- return true;
- }
- void ATunicsCharacter::ServerDropItemFromInventory_Implementation(int InventoryIndex)
- {
- DropItemFromInventory(InventoryIndex);
- }
- void ATunicsCharacter::AssignPrimary(int InventoryIndex)
- {
- PrimaryIndex = InventoryIndex;
- if (PrimarySelected) {
- SwitchWeapons(true);
- }
- }
- void ATunicsCharacter::AssignSecondary(int InventoryIndex)
- {
- SecondaryIndex = InventoryIndex;
- if (!PrimarySelected) {
- SwitchWeapons(false);
- }
- }
- void ATunicsCharacter::PickUpWeapon(AItem* Weapon)
- {
- if (Role < ROLE_Authority) {
- ServerPickUpWeapon(Weapon);
- }
- if (Inventory->FindSuitableInventorySlot(Weapon)!=-1) {
- Weapon->CanPickup = false;
- Weapon->AttachToComponent(Mesh1P, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT("GripPoint"));
- Weapon->SetActorHiddenInGame(true);
- Weapon->SetOwner(this);
- Inventory->AddItemToInventory(Weapon);
- if (Role == ROLE_Authority) {
- ClientPickUpWeapon(Weapon);
- }
- }
- }
- void ATunicsCharacter::ServerPickUpWeapon_Implementation(AItem* Weapon)
- {
- PickUpWeapon(Weapon);
- }
- bool ATunicsCharacter::ServerPickUpWeapon_Validate(AItem* Weapon)
- {
- return true;
- }
- void ATunicsCharacter::ClientPickUpWeapon_Implementation(AItem* Weapon)
- {
- if (Weapon) {
- Weapon->CanPickup = false;
- Weapon->AttachToComponent(Mesh1P, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT("GripPoint"));
- Weapon->SetActorHiddenInGame(true);
- Weapon->SetOwner(this);
- }
- }
- void ATunicsCharacter::DropWeapon()
- {
- if (PrimarySelected) {
- DropItemFromInventory(PrimaryIndex);
- }
- else {
- DropItemFromInventory(SecondaryIndex);
- }
- }
- void ATunicsCharacter::BeginFire()
- {
- isFiring = true;
- CurrentItem->IsFiring = true;
- }
- void ATunicsCharacter::EndFire()
- {
- isFiring = false;
- CurrentItem->IsFiring = false;
- }
- void ATunicsCharacter::MoveForward(float Value)
- {
- if (Value != 0.0f)
- {
- AddMovementInput(GetActorForwardVector(), Value*cMoveSpeed);
- }
- }
- void ATunicsCharacter::MoveRight(float Value)
- {
- if (Value != 0.0f)
- {
- // add movement in that direction
- AddMovementInput(GetActorRightVector(), Value*cMoveSpeed);
- }
- }
- void ATunicsCharacter::BeingSprint()
- {
- IsCrouching = false;
- isAiming = false;
- isSprinting = true;
- GetCharacterMovement()->MaxWalkSpeed = 1200;
- }
- void ATunicsCharacter::EndSprint()
- {
- isSprinting = false;
- cMoveSpeed = 1;
- GetCharacterMovement()->MaxWalkSpeed = 600;
- }
- void ATunicsCharacter::TurnAtRate(float Rate)
- {
- AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
- }
- void ATunicsCharacter::LookUpAtRate(float Rate)
- {
- AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());
- LookRot = GetControlRotation();
- }
- void ATunicsCharacter::ClientLookUp_Implementation(FRotator rotation)
- {
- if (IsLocallyControlled()) {
- LookRot = rotation;
- }
- }
- void ATunicsCharacter::OnRep_LookRot() {
- }
- void ATunicsCharacter::Reload()
- {
- if (isReloading == false&& isAiming ==false) {
- AFireArm* CurrentWeapon = Cast<AFireArm>(CurrentItem);
- if (CurrentWeapon) { //this is a firearm
- if (Inventory->HasClipFor(CurrentWeapon)) {
- isReloading = true;
- CurrentWeapon->OnReload();
- Inventory->RemoveClipFor(CurrentWeapon);
- }
- }
- }
- }
- void ATunicsCharacter::Chamber()
- {
- if (isChambering == false && isAiming == false && isFiring == false) {
- isChambering = true;
- CurrentItem->OnChambered();
- }
- }
- void ATunicsCharacter::OnReloadFinished()
- {
- isReloading = false;
- CurrentItem->OnReloadFinished();
- }
- void ATunicsCharacter::OnEquipFinsied()
- {
- isEquipping = false;
- }
- void ATunicsCharacter::OnChamberFinished()
- {
- isChambering = false;
- CurrentItem->OnChamberedFinished();
- }
- void ATunicsCharacter::ServerUpdateInput_Implementation(const FPlayerInputStruct & PlayerInput)
- {
- isAiming = PlayerInput.bPressedAim;
- LookRot = PlayerInput.fRotation;
- // Fire
- if (PlayerInput.bPressedFire)
- {
- if (!isFiring)
- {
- BeginFire();
- }
- }
- else
- {
- if (isFiring)
- {
- EndFire();
- }
- }
- // Sprint
- if (PlayerInput.bPressedSprint)
- {
- if (!isSprinting)
- {
- BeingSprint();
- }
- }
- else
- {
- if (isSprinting)
- {
- EndSprint();
- }
- }
- if (PlayerInput.bCrouching) {
- if (!IsCrouching) {
- BeginCrouch();
- }
- }
- else {
- if (IsCrouching) {
- EndCrouch();
- }
- }
- }
- bool ATunicsCharacter::ServerUpdateInput_Validate(const FPlayerInputStruct & PlayerInput)
- {
- return true;
- }
- void ATunicsCharacter::SelectWeapon1()
- {
- SwitchWeapons(true);
- }
- void ATunicsCharacter::SelectWeapon2()
- {
- SwitchWeapons(false);
- }
- void ATunicsCharacter::SwitchWeapons(bool gotoPrimary)
- {
- PrimarySelected = gotoPrimary;
- if (CurrentItem != NULL) {
- CurrentItem->SetActorHiddenInGame(true);
- }
- if (PrimarySelected) {
- if (Inventory->HeldItems[PrimaryIndex].item != NULL) {
- CurrentItem = Cast<AItem>(Inventory->HeldItems[PrimaryIndex].item);
- ChangeAnimClass(CurrentItem->ABP);
- // Mesh1P->SetAnimInstanceClass(CurrentItem->ABP->GeneratedClass);
- CurrentItem->SetActorHiddenInGame(false);
- }
- else {
- CurrentItem = EmptyHands;
- ChangeAnimClass(CurrentItem->ABP);
- // Mesh1P->SetAnimInstanceClass(CurrentItem->ABP->GeneratedClass);
- }
- }
- else {
- if (Inventory->HeldItems[SecondaryIndex].item != NULL) {
- CurrentItem = Cast<AItem>(Inventory->HeldItems[SecondaryIndex].item);
- ChangeAnimClass(CurrentItem->ABP);
- // Mesh1P->SetAnimInstanceClass(CurrentItem->ABP->GeneratedClass);
- CurrentItem->SetActorHiddenInGame(false);
- }
- else {
- CurrentItem = EmptyHands;
- ChangeAnimClass(CurrentItem->ABP);
- // Mesh1P->SetAnimInstanceClass(CurrentItem->ABP->GeneratedClass);
- }
- }
- if (Role < ROLE_Authority) {
- ServerSwitchWeapon(CurrentItem);
- }
- if (Role == ROLE_Authority) {
- ClientSwitchWeapon(CurrentItem);
- }
- isEquipping = true;
- }
- void ATunicsCharacter::ServerSwitchWeapon_Implementation(AItem* item)
- {
- if (item) {
- if(item->CanPickup==false)CurrentItem->SetActorHiddenInGame(true);
- CurrentItem = item;
- ChangeAnimClass(CurrentItem->ABP);
- CurrentItem->SetActorHiddenInGame(false);
- }
- //SwitchWeapons(PrimarySelected);
- ClientSwitchWeapon(item);
- }
- bool ATunicsCharacter::ServerSwitchWeapon_Validate(AItem* item)
- {
- return true;
- }
- void ATunicsCharacter::ClientSwitchWeapon_Implementation(AItem* item)
- {
- if (item) {
- //CurrentItem->SetActorHiddenInGame(true);
- CurrentItem = item;
- ChangeAnimClass(CurrentItem->ABP);
- CurrentItem->SetActorHiddenInGame(false);
- }
- }
- void ATunicsCharacter::ChangeAnimClass(UAnimBlueprintGeneratedClass* Class)
- {
- Mesh1P->SetAnimInstanceClass(Class);
- }
- void ATunicsCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const {
- Super::GetLifetimeReplicatedProps(OutLifetimeProps);
- DOREPLIFETIME(ATunicsCharacter, CurrentItem);
- DOREPLIFETIME(ATunicsCharacter, isFiring);
- DOREPLIFETIME(ATunicsCharacter, PrimarySelected);
- DOREPLIFETIME(ATunicsCharacter, IsCrouching);
- DOREPLIFETIME(ATunicsCharacter, PrimaryIndex);
- DOREPLIFETIME(ATunicsCharacter, SecondaryIndex);
- DOREPLIFETIME(ATunicsCharacter, LookRot);
- }
Advertisement
Add Comment
Please, Sign In to add comment