nbarber20

TunicsCharacter.cpp

Dec 21st, 2018
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.46 KB | None | 0 0
  1. #include "TunicsCharacter.h"
  2. #include "TunicsProjectile.h"
  3. #include "Animation/AnimInstance.h"
  4. #include "Camera/CameraComponent.h"
  5. #include "Components/CapsuleComponent.h"
  6. #include "Components/InputComponent.h"
  7. #include "GameFramework/InputSettings.h"
  8. #include "HeadMountedDisplayFunctionLibrary.h"
  9. #include "Kismet/GameplayStatics.h"
  10. #include <DrawDebugHelpers.h>
  11. #include <GameFramework/Character.h>
  12. #include "Components/InputComponent.h"
  13. #include "GameFramework/Character.h"
  14. #include "GameFramework/CharacterMovementComponent.h"
  15. #include "GameFramework/PawnMovementComponent.h"
  16. #include <ConstructorHelpers.h>
  17. #include <LogMacros.h>
  18. #include <Engine/EngineTypes.h>
  19. #include <UserWidget.h>
  20. #include <CanvasPanel.h>
  21. #include <CanvasPanelSlot.h>
  22. #include "MainHud.h"
  23. #include "PlayerInvetory.h"
  24. #include <WidgetBlueprintLibrary.h>
  25. #include "Net/UnrealNetwork.h"
  26. #include <UObjectGlobals.h>
  27. #include <Animation/AnimBlueprintGeneratedClass.h>
  28. #include "Attachment.h"
  29. #include "Door.h"
  30. #include <Engine/Engine.h>
  31. #include "ComplexInventory.h"
  32. DEFINE_LOG_CATEGORY_STATIC(LogFPChar, Warning, All);
  33.  
  34. //////////////////////////////////////////////////////////////////////////
  35. // ATunicsCharacter
  36.  
  37. ATunicsCharacter::ATunicsCharacter()
  38. {
  39.     PrimaryActorTick.bCanEverTick = true;
  40.  
  41.     GetCapsuleComponent()->InitCapsuleSize(55.f, 96.0f);
  42.  
  43.     BaseTurnRate = 45.f;
  44.     BaseLookUpRate = 45.f;
  45.  
  46.     Mesh1P = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("CharacterMesh1P"));
  47.     Mesh1P->SetupAttachment(GetCapsuleComponent());
  48.  
  49.     Mesh1P->RelativeRotation = FRotator(1.9f, -19.19f, 5.2f);
  50.     Mesh1P->RelativeLocation = FVector(-0.5f, -4.4f, -155.7f);
  51.  
  52.     FirstPersonCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
  53.     FirstPersonCameraComponent->SetupAttachment(Mesh1P);
  54.     FirstPersonCameraComponent->RelativeLocation = FVector(-39.56f, 1.75f, 64.f);
  55.     FirstPersonCameraComponent->bUsePawnControlRotation = true;
  56.  
  57.     TriggerCapsule = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Trigger Capsule"));
  58.     TriggerCapsule->InitCapsuleSize(55.f, 96.0f);
  59.     TriggerCapsule->SetCollisionProfileName(TEXT("Trigger"));
  60.     TriggerCapsule->SetupAttachment(RootComponent);
  61.     TriggerCapsule->OnComponentBeginOverlap.AddDynamic(this, &ATunicsCharacter::OnOverlapBegin);
  62.     TriggerCapsule->OnComponentEndOverlap.AddDynamic(this, &ATunicsCharacter::OnOverlapEnd);
  63.  
  64.     GunOffset = FVector(100.0f, 0.0f, 10.0f);
  65.     PrimarySelected = true;
  66.     CurrentItem = EmptyHands;  
  67.  
  68.  
  69.     Inventory = CreateDefaultSubobject<UComplexInventory>(TEXT("Complex Inventory"));
  70.     Inventory->Mesh1p = Mesh1P;
  71.  
  72.     SceneCapture = CreateDefaultSubobject<USceneCaptureComponent2D>(TEXT("SceneCapture"));
  73.     SceneCapture->SetupAttachment(FirstPersonCameraComponent);
  74.     SceneCapture->bCaptureEveryFrame = true;
  75.     SceneCapture->bHiddenInGame = true;
  76.     SceneCapture->FOVAngle = 3.0f;
  77. }
  78.  
  79.  
  80.  
  81. void ATunicsCharacter::BeginPlay()
  82. {
  83.     // Call the base class  
  84.     Super::BeginPlay();
  85.     //Attach gun mesh component to Skeleton, doing it here because the skeleton is not yet created in the constructor
  86.     FirstPersonCameraComponent->AttachToComponent(Mesh1P, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT("camera_socket"));
  87.  
  88.     FActorSpawnParameters SpawnInfo;
  89.     SpawnInfo.Owner = this;
  90.     EmptyHands = GetWorld()->SpawnActor<AItem>(EmptyHandsInstance, FVector(0, 0, 0), FRotator(0, 0, 0), SpawnInfo);
  91.     EmptyHands->SetOwner(this);
  92.     CurrentItem = EmptyHands;
  93.     ChangeAnimClass(CurrentItem->ABP);
  94.    
  95.     SceneCapture->TextureTarget = ScopeRender;
  96.  
  97. }
  98.  
  99. void ATunicsCharacter::Tick(float DeltaTime)
  100. {
  101.     Super::Tick(DeltaTime);
  102.     GroundItems.Empty();
  103.     TArray<AActor*> OverlappedItems;
  104.     TriggerCapsule->GetOverlappingActors(OverlappedItems, AItem::StaticClass());
  105.     for (int i = 0; i < OverlappedItems.Num(); i++) {
  106.         AItem* groundItem = Cast<AItem>(OverlappedItems[i]);
  107.         if (groundItem) { //ignore this item because its in mine or another players hands
  108.             if (groundItem->CanPickup) {
  109.                 GroundItems.Add(groundItem);
  110.             }
  111.         }
  112.     }
  113.  
  114.     if (GetNetMode() == NM_Client)
  115.     {
  116.         // Send input to server
  117.         ServerUpdateInput(FPlayerInputStruct(isFiring, isAiming, isSprinting, isEquipping,IsCrouching, GetControlRotation()));
  118.     }
  119.     if (Role == ROLE_Authority) {
  120.         ClientLookUp(GetControlRotation());
  121.     }
  122.  
  123. }
  124. //////////////////////////////////////////////////////////////////////////
  125. // Input
  126.  
  127. void ATunicsCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
  128. {
  129.     check(PlayerInputComponent);
  130.     PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
  131.     PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
  132.     PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &ATunicsCharacter::BeginFire);
  133.     PlayerInputComponent->BindAction("Fire", IE_Released, this, &ATunicsCharacter::EndFire);
  134.     PlayerInputComponent->BindAction("Aim", IE_Pressed, this, &ATunicsCharacter::Aim);
  135.     PlayerInputComponent->BindAction("Aim", IE_Released, this, &ATunicsCharacter::UnAim);
  136.     PlayerInputComponent->BindAction("Reload", IE_Pressed, this, &ATunicsCharacter::Reload);
  137.     PlayerInputComponent->BindAction("Chamber", IE_Pressed, this, &ATunicsCharacter::Chamber);
  138.     PlayerInputComponent->BindAction("SwapWeapon1", IE_Pressed, this, &ATunicsCharacter::SelectWeapon1);
  139.     PlayerInputComponent->BindAction("SwapWeapon2", IE_Pressed, this, &ATunicsCharacter::SelectWeapon2);
  140.     PlayerInputComponent->BindAction("Interact", IE_Pressed, this, &ATunicsCharacter::Interact);
  141.     PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &ATunicsCharacter::BeginCrouch);
  142.     PlayerInputComponent->BindAction("Crouch", IE_Released, this, &ATunicsCharacter::EndCrouch);
  143.     PlayerInputComponent->BindAction("DropWeapon", IE_Released, this, &ATunicsCharacter::DropWeapon);
  144.     PlayerInputComponent->BindAction("Sprint", IE_Pressed, this, &ATunicsCharacter::BeingSprint);
  145.     PlayerInputComponent->BindAction("Sprint", IE_Released, this, &ATunicsCharacter::EndSprint);
  146.  
  147.     PlayerInputComponent->BindAxis("MoveForward", this, &ATunicsCharacter::MoveForward);
  148.     PlayerInputComponent->BindAxis("MoveRight", this, &ATunicsCharacter::MoveRight);
  149.     PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
  150.     PlayerInputComponent->BindAxis("TurnRate", this, &ATunicsCharacter::TurnAtRate);
  151.     PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
  152.     PlayerInputComponent->BindAxis("LookUpRate", this, &ATunicsCharacter::LookUpAtRate);
  153. }
  154.  
  155. void ATunicsCharacter::OnOverlapBegin(UPrimitiveComponent* OVerllapedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
  156. {
  157.     AItem* grounditem = Cast<AItem>(OtherActor);
  158.     if (grounditem) {
  159.         if (grounditem->CanPickup) {
  160.             OverlappedWeapon = grounditem;
  161.             FInventorySlot newslot;
  162.             newslot.item = grounditem;
  163.         }
  164.     }
  165. }
  166.  
  167.  
  168. void ATunicsCharacter::OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
  169. {
  170.     AItem* grounditem = Cast<AItem>(OtherActor);
  171.     if (grounditem) {
  172.         if (grounditem->CanPickup) {
  173.             OverlappedWeapon = NULL;
  174.         }
  175.     }
  176. }
  177.  
  178.  
  179.  
  180. void ATunicsCharacter::Aim() {
  181.  
  182.     GetCharacterMovement()->MaxWalkSpeed = 600;
  183.     isAiming = true;
  184.     cMoveSpeed = 0.5;
  185.     if (SceneCapture->FOVAngle > 0.f && Controller && Controller->IsLocalController())
  186.     {
  187.         SceneCapture->bHiddenInGame = false;
  188.     }
  189. }
  190.  
  191. void ATunicsCharacter::UnAim() {
  192.  
  193.     GetCharacterMovement()->MaxWalkSpeed = 600;
  194.     isAiming = false;
  195.     cMoveSpeed = 1;
  196.     SceneCapture->bHiddenInGame = true;
  197. }
  198.  
  199. void ATunicsCharacter::BeginCrouch()
  200. {
  201.     IsCrouching = true;
  202.     isSprinting = false;
  203.     cMoveSpeed = 0.5;
  204. }
  205.  
  206.  
  207. void ATunicsCharacter::EndCrouch()
  208. {
  209.     IsCrouching = false;
  210.     cMoveSpeed = 1;
  211. }
  212.  
  213. void ATunicsCharacter::ToggleInventory()
  214. {
  215.     APlayerController* playercontroller = Cast<APlayerController>(GetController());
  216.     if (isInventoryOpen && SpawnedInventory) {
  217.         isInventoryOpen = false;
  218.         GroundSpawnedInventory->RemoveFromParent();
  219.         GroundSpawnedInventory = NULL;
  220.         SpawnedInventory->RemoveFromParent();
  221.         SpawnedInventory = NULL;
  222.         playercontroller->bShowMouseCursor = false;
  223.         playercontroller->SetIgnoreLookInput(false);
  224.         FInputModeGameOnly InputMode;
  225.         playercontroller->SetInputMode(InputMode);
  226.  
  227.     }
  228.     else {
  229.         isInventoryOpen = true;
  230.  
  231.         GroundSpawnedInventory = CreateWidget<UPlayerInvetory>(GetWorld(), GroundInventoryInstance);
  232.         GroundSpawnedInventory->Player = this;
  233.         UCanvasPanelSlot* newslot1 = HudWidget->MainHudPanel->AddChildToCanvas(GroundSpawnedInventory);
  234.         newslot1->SetAutoSize(true);
  235.         newslot1->SetAlignment(FVector2D(0.5f, 0.5f));
  236.         FAnchors anchor1;
  237.         anchor1.Minimum = FVector2D(0.5f, 0.5f);
  238.         anchor1.Maximum = FVector2D(0.5f, 0.5f);
  239.         newslot1->SetAnchors(anchor1);
  240.         newslot1->SetPosition(FVector2D(-500, 0));
  241.  
  242.         SpawnedInventory = CreateWidget<UPlayerInvetory>(GetWorld(), InventoryInstance);
  243.         SpawnedInventory->Player = this;
  244.         UCanvasPanelSlot* newslot = HudWidget->MainHudPanel->AddChildToCanvas(SpawnedInventory);
  245.         newslot->SetAutoSize(true);
  246.         newslot->SetAlignment(FVector2D(0.5f, 0.5f));
  247.         FAnchors anchor;
  248.         anchor.Minimum = FVector2D(0.5f, 0.5f);
  249.         anchor.Maximum = FVector2D(0.5f, 0.5f);
  250.         newslot->SetAnchors(anchor);
  251.         newslot->SetPosition(FVector2D(500, 0));
  252.  
  253.         playercontroller->bShowMouseCursor = true;
  254.         playercontroller->SetIgnoreLookInput(true);
  255.         FInputModeGameAndUI InputMode;
  256.         playercontroller->SetInputMode(InputMode);
  257.     }
  258. }
  259.  
  260. void ATunicsCharacter::Interact()
  261. {
  262.     //operate door
  263.     FHitResult Hit;
  264.     FVector Start = FirstPersonCameraComponent->GetComponentLocation();
  265.     FVector ForwardVector = FirstPersonCameraComponent->GetForwardVector();
  266.     FVector End = ((ForwardVector * 200.f) + Start);
  267.     FCollisionQueryParams CollisionParams;
  268.     CollisionParams.AddIgnoredActor(this);
  269.  
  270.     if (GetWorld()->LineTraceSingleByChannel(Hit, Start, End, ECC_WorldDynamic, CollisionParams))
  271.     {
  272.         //GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, Hit.Actor->GetFName().ToString());
  273.  
  274.         if (Hit.bBlockingHit)
  275.         {
  276.             ADoor* currentDoor = Cast<ADoor>(Hit.GetActor());
  277.  
  278.             if (currentDoor) {
  279.                 currentDoor->SetOwner(this);
  280.                 currentDoor->ServerToggleDoor(this);
  281.                
  282.             }
  283.         }
  284.     }
  285.  
  286.     //pickup ground item
  287.     if (GroundItems.Num() > 0) {
  288.         if (isUnEquipping == false && isEquipping == false) {
  289.                 PickUpWeapon(GroundItems[0]);
  290.         }
  291.     }
  292. }
  293.  
  294.  
  295. void ATunicsCharacter::DropItemFromInventory(int InventoryIndex)
  296. {
  297.     if (Inventory->HeldItems[InventoryIndex].item) {
  298.         if (Inventory->HeldItems[InventoryIndex].item->CanBeDropped) {
  299.             if (Role < ROLE_Authority) { // this is a client
  300.                 ServerDropItemFromInventory(InventoryIndex);
  301.             }
  302.  
  303.  
  304.             Inventory->HeldItems[InventoryIndex].item->DetachRootComponentFromParent();
  305.             Inventory->HeldItems[InventoryIndex].item->CanPickup = true;
  306.             Inventory->HeldItems[InventoryIndex].item->SetActorHiddenInGame(false);
  307.            
  308.            
  309.             Inventory->RemoveItemFromSlot(Inventory->HeldItems[InventoryIndex].item, InventoryIndex);
  310.             if (InventoryIndex == PrimaryIndex && PrimarySelected) {
  311.                 CurrentItem = EmptyHands;
  312.                 SwitchWeapons(true);
  313.             }
  314.             else if(InventoryIndex == SecondaryIndex &&PrimarySelected ==false){
  315.                 Inventory->RemoveItemFromSlot(CurrentItem, SecondaryIndex);
  316.                 CurrentItem = EmptyHands;
  317.                 SwitchWeapons(false);
  318.             }
  319.         }
  320.     }
  321.    
  322. }
  323.  
  324. bool ATunicsCharacter::ServerDropItemFromInventory_Validate(int InventoryIndex)
  325. {
  326.  
  327.     return true;
  328. }
  329.  
  330. void ATunicsCharacter::ServerDropItemFromInventory_Implementation(int InventoryIndex)
  331. {
  332.     DropItemFromInventory(InventoryIndex);
  333. }
  334.  
  335. void ATunicsCharacter::AssignPrimary(int InventoryIndex)
  336. {
  337.     PrimaryIndex = InventoryIndex;
  338.     if (PrimarySelected) {
  339.         SwitchWeapons(true);
  340.     }
  341. }
  342.  
  343. void ATunicsCharacter::AssignSecondary(int InventoryIndex)
  344. {
  345.     SecondaryIndex = InventoryIndex;
  346.     if (!PrimarySelected) {
  347.         SwitchWeapons(false);
  348.     }
  349. }
  350.  
  351. void ATunicsCharacter::PickUpWeapon(AItem* Weapon)
  352. {
  353.     if (Role < ROLE_Authority) {
  354.         ServerPickUpWeapon(Weapon);
  355.     }
  356.    
  357.     if (Inventory->FindSuitableInventorySlot(Weapon)!=-1) {
  358.         Weapon->CanPickup = false;
  359.         Weapon->AttachToComponent(Mesh1P, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT("GripPoint"));
  360.         Weapon->SetActorHiddenInGame(true);
  361.         Weapon->SetOwner(this);
  362.         Inventory->AddItemToInventory(Weapon);
  363.         if (Role == ROLE_Authority) {
  364.             ClientPickUpWeapon(Weapon);
  365.         }
  366.     }
  367. }
  368.  
  369. void ATunicsCharacter::ServerPickUpWeapon_Implementation(AItem* Weapon)
  370. {
  371.     PickUpWeapon(Weapon);
  372. }
  373.  
  374. bool ATunicsCharacter::ServerPickUpWeapon_Validate(AItem* Weapon)
  375. {
  376.     return true;
  377. }
  378.  
  379. void ATunicsCharacter::ClientPickUpWeapon_Implementation(AItem* Weapon)
  380. {
  381.     if (Weapon) {
  382.         Weapon->CanPickup = false;
  383.         Weapon->AttachToComponent(Mesh1P, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT("GripPoint"));
  384.         Weapon->SetActorHiddenInGame(true);
  385.         Weapon->SetOwner(this);
  386.     }
  387. }
  388.  
  389. void ATunicsCharacter::DropWeapon()
  390. {
  391.     if (PrimarySelected) {
  392.         DropItemFromInventory(PrimaryIndex);
  393.     }
  394.     else {
  395.  
  396.         DropItemFromInventory(SecondaryIndex);
  397.     }
  398. }
  399.  
  400. void ATunicsCharacter::BeginFire()
  401. {
  402.     isFiring = true;   
  403.     CurrentItem->IsFiring = true;
  404. }
  405.  
  406. void ATunicsCharacter::EndFire()
  407. {
  408.     isFiring = false;
  409.     CurrentItem->IsFiring = false;
  410. }
  411.  
  412. void ATunicsCharacter::MoveForward(float Value)
  413. {
  414.     if (Value != 0.0f)
  415.     {
  416.         AddMovementInput(GetActorForwardVector(), Value*cMoveSpeed);
  417.     }
  418. }
  419.  
  420. void ATunicsCharacter::MoveRight(float Value)
  421. {
  422.     if (Value != 0.0f)
  423.     {
  424.         // add movement in that direction
  425.         AddMovementInput(GetActorRightVector(), Value*cMoveSpeed);
  426.     }
  427. }
  428.  
  429. void ATunicsCharacter::BeingSprint()
  430. {
  431.     IsCrouching = false;
  432.     isAiming = false;
  433.     isSprinting = true;
  434.     GetCharacterMovement()->MaxWalkSpeed = 1200;
  435. }
  436.  
  437. void ATunicsCharacter::EndSprint()
  438. {
  439.     isSprinting = false;
  440.     cMoveSpeed = 1;
  441.     GetCharacterMovement()->MaxWalkSpeed = 600;
  442.  
  443. }
  444.  
  445. void ATunicsCharacter::TurnAtRate(float Rate)
  446. {
  447.     AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
  448. }
  449.  
  450. void ATunicsCharacter::LookUpAtRate(float Rate)
  451. {
  452.    
  453.     AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());
  454.     LookRot = GetControlRotation();
  455. }
  456.  
  457. void ATunicsCharacter::ClientLookUp_Implementation(FRotator rotation)
  458. {
  459.     if (IsLocallyControlled()) {
  460.         LookRot = rotation;
  461.     }
  462. }
  463.  
  464. void ATunicsCharacter::OnRep_LookRot() {
  465. }
  466.  
  467. void ATunicsCharacter::Reload()
  468. {      
  469.     if (isReloading == false&& isAiming ==false) {
  470.  
  471.         AFireArm* CurrentWeapon = Cast<AFireArm>(CurrentItem);
  472.         if (CurrentWeapon) { //this is a firearm
  473.             if (Inventory->HasClipFor(CurrentWeapon)) {
  474.                 isReloading = true;
  475.                 CurrentWeapon->OnReload();
  476.                 Inventory->RemoveClipFor(CurrentWeapon);
  477.             }
  478.         }
  479.     }  
  480. }
  481.  
  482. void ATunicsCharacter::Chamber()
  483. {
  484.     if (isChambering == false && isAiming == false && isFiring == false) {
  485.         isChambering = true;
  486.         CurrentItem->OnChambered();
  487.     }
  488. }
  489.  
  490. void ATunicsCharacter::OnReloadFinished()
  491. {
  492.     isReloading = false;
  493.     CurrentItem->OnReloadFinished();
  494. }
  495.  
  496. void ATunicsCharacter::OnEquipFinsied()
  497. {
  498.     isEquipping = false;
  499. }
  500.  
  501. void ATunicsCharacter::OnChamberFinished()
  502. {
  503.     isChambering = false;
  504.     CurrentItem->OnChamberedFinished();
  505. }
  506. void ATunicsCharacter::ServerUpdateInput_Implementation(const FPlayerInputStruct & PlayerInput)
  507. {
  508.     isAiming = PlayerInput.bPressedAim;
  509.     LookRot = PlayerInput.fRotation;
  510.    
  511.     // Fire
  512.     if (PlayerInput.bPressedFire)
  513.     {
  514.         if (!isFiring)
  515.         {
  516.             BeginFire();
  517.         }
  518.     }
  519.     else
  520.     {
  521.         if (isFiring)
  522.         {
  523.             EndFire();
  524.         }
  525.     }
  526.  
  527.     // Sprint
  528.     if (PlayerInput.bPressedSprint)
  529.     {
  530.         if (!isSprinting)
  531.         {
  532.             BeingSprint();
  533.         }
  534.     }
  535.     else
  536.     {
  537.         if (isSprinting)
  538.         {
  539.             EndSprint();
  540.         }
  541.     }
  542.  
  543.     if (PlayerInput.bCrouching) {
  544.         if (!IsCrouching) {
  545.             BeginCrouch();
  546.         }
  547.     }
  548.     else {
  549.         if (IsCrouching) {
  550.             EndCrouch();
  551.         }
  552.     }
  553.  
  554.  
  555. }
  556.  
  557. bool ATunicsCharacter::ServerUpdateInput_Validate(const FPlayerInputStruct & PlayerInput)
  558. {
  559.     return true;
  560. }
  561.  
  562. void ATunicsCharacter::SelectWeapon1()
  563. {
  564.     SwitchWeapons(true);
  565. }
  566.  
  567. void ATunicsCharacter::SelectWeapon2()
  568. {
  569.     SwitchWeapons(false);
  570. }
  571.  
  572. void ATunicsCharacter::SwitchWeapons(bool gotoPrimary)
  573. {
  574.     PrimarySelected = gotoPrimary;
  575.     if (CurrentItem != NULL) {
  576.         CurrentItem->SetActorHiddenInGame(true);
  577.     }
  578.     if (PrimarySelected) {
  579.        
  580.         if (Inventory->HeldItems[PrimaryIndex].item != NULL) {
  581.             CurrentItem = Cast<AItem>(Inventory->HeldItems[PrimaryIndex].item);
  582.             ChangeAnimClass(CurrentItem->ABP);
  583.         //  Mesh1P->SetAnimInstanceClass(CurrentItem->ABP->GeneratedClass);
  584.             CurrentItem->SetActorHiddenInGame(false);
  585.         }
  586.         else {
  587.             CurrentItem = EmptyHands;
  588.             ChangeAnimClass(CurrentItem->ABP);
  589.         //  Mesh1P->SetAnimInstanceClass(CurrentItem->ABP->GeneratedClass);
  590.         }
  591.     }
  592.     else {
  593.         if (Inventory->HeldItems[SecondaryIndex].item != NULL) {
  594.             CurrentItem = Cast<AItem>(Inventory->HeldItems[SecondaryIndex].item);
  595.             ChangeAnimClass(CurrentItem->ABP);
  596.         //  Mesh1P->SetAnimInstanceClass(CurrentItem->ABP->GeneratedClass);
  597.             CurrentItem->SetActorHiddenInGame(false);
  598.         }
  599.         else {
  600.             CurrentItem = EmptyHands;
  601.             ChangeAnimClass(CurrentItem->ABP);
  602.         //  Mesh1P->SetAnimInstanceClass(CurrentItem->ABP->GeneratedClass);
  603.         }
  604.     }
  605.     if (Role < ROLE_Authority) {
  606.         ServerSwitchWeapon(CurrentItem);
  607.     }
  608.     if (Role == ROLE_Authority) {
  609.         ClientSwitchWeapon(CurrentItem);
  610.     }
  611.     isEquipping = true;
  612. }
  613.  
  614.  
  615. void ATunicsCharacter::ServerSwitchWeapon_Implementation(AItem* item)
  616. {
  617.     if (item) {
  618.         if(item->CanPickup==false)CurrentItem->SetActorHiddenInGame(true);
  619.         CurrentItem = item;
  620.         ChangeAnimClass(CurrentItem->ABP);
  621.         CurrentItem->SetActorHiddenInGame(false);
  622.     }
  623.     //SwitchWeapons(PrimarySelected);
  624.     ClientSwitchWeapon(item);
  625. }
  626.  
  627. bool ATunicsCharacter::ServerSwitchWeapon_Validate(AItem* item)
  628. {
  629.     return true;
  630. }
  631.  
  632. void ATunicsCharacter::ClientSwitchWeapon_Implementation(AItem* item)
  633. {
  634.     if (item) {
  635.         //CurrentItem->SetActorHiddenInGame(true);
  636.         CurrentItem = item;
  637.         ChangeAnimClass(CurrentItem->ABP);
  638.         CurrentItem->SetActorHiddenInGame(false);
  639.     }
  640. }
  641.  
  642. void ATunicsCharacter::ChangeAnimClass(UAnimBlueprintGeneratedClass* Class)
  643. {
  644.     Mesh1P->SetAnimInstanceClass(Class);
  645. }
  646.  
  647. void ATunicsCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const {
  648.     Super::GetLifetimeReplicatedProps(OutLifetimeProps);
  649.     DOREPLIFETIME(ATunicsCharacter, CurrentItem);
  650.     DOREPLIFETIME(ATunicsCharacter, isFiring);
  651.     DOREPLIFETIME(ATunicsCharacter, PrimarySelected);  
  652.     DOREPLIFETIME(ATunicsCharacter, IsCrouching);
  653.     DOREPLIFETIME(ATunicsCharacter, PrimaryIndex);
  654.     DOREPLIFETIME(ATunicsCharacter, SecondaryIndex);
  655.     DOREPLIFETIME(ATunicsCharacter, LookRot);
  656. }
Advertisement
Add Comment
Please, Sign In to add comment