Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "FireArm.h"
- #include <Kismet/GameplayStatics.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 "TunicsCharacter.h"
- #include "Net/UnrealNetwork.h"
- #include <Particles/ParticleSystemComponent.h>
- #include <Engine/Engine.h>
- #include <Components/StaticMeshComponent.h>
- // Sets default values
- AFireArm::AFireArm()
- {
- // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
- PrimaryActorTick.bCanEverTick = true;
- RootLocation = CreateDefaultSubobject<USceneComponent>(TEXT("RootLocation"));
- RootLocation->SetupAttachment(RootComponent);
- GunOffset = CreateDefaultSubobject<USceneComponent>(TEXT("GunOffset"));
- GunOffset->SetupAttachment(RootLocation);
- FP_Gun = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("FP_Gun"));
- FP_Gun->SetupAttachment(GunOffset);
- FP_IronSight= CreateDefaultSubobject<UStaticMeshComponent>(TEXT("FP_IronSight"));
- FP_IronSight->SetupAttachment(FP_Gun);
- FP_IronSight->SetCollisionEnabled(ECollisionEnabled::NoCollision);
- FP_RedDot = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("FP_RedDot"));
- FP_RedDot->SetupAttachment(FP_Gun);
- FP_RedDot->SetCollisionEnabled(ECollisionEnabled::NoCollision);
- FP_Scope = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("FP_Scope"));
- FP_Scope->SetupAttachment(FP_Gun);
- FP_Scope->SetCollisionEnabled(ECollisionEnabled::NoCollision);
- FP_MuzzleLocation = CreateDefaultSubobject<USceneComponent>(TEXT("FP_MuzzleLocation"));
- FP_MuzzleLocation->SetupAttachment(FP_Gun);
- //FP_MuzzleLocation->SetRelativeLocation(FVector(0.2f, 48.4f, -10.6f));
- BulletChambered = true;
- CAmmoInClip = AmmoInClip;
- MuzzleLocation = FP_MuzzleLocation;
- SetReplicates(true);
- }
- // Called when the game starts or when spawned
- void AFireArm::BeginPlay()
- {
- Super::BeginPlay();
- UpdateAttachment();
- }
- // Called every frame
- void AFireArm::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
- if (CanPickup) {
- FP_Gun->SetSimulatePhysics(true);
- }
- else {
- FP_Gun->SetSimulatePhysics(false);
- FP_Gun->AttachToComponent(GunOffset, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true));
- }
- }
- //called from an anim notify in holding characters abp
- void AFireArm::OnFire() {
- if (IsFiring == true) {
- if (BulletChambered) {
- if (CAmmoInClip > 0) {
- CAmmoInClip--;
- FP_Gun->PlayAnimation(FireAnimation, false);
- for (int i = 0; i < numberOfBullets; i++) {
- SpawnBullet();
- }
- if (CAmmoInClip == 0) {
- BulletChambered = false;
- }
- }
- else {
- UGameplayStatics::PlaySoundAtLocation(this, NoAmmoSound, GetActorLocation());
- if (EmptyIdleAnimation) {
- FP_Gun->PlayAnimation(EmptyIdleAnimation, false);
- }
- }
- }
- else {
- UGameplayStatics::PlaySoundAtLocation(this, NoAmmoSound, GetActorLocation());
- if (EmptyIdleAnimation) {
- FP_Gun->PlayAnimation(EmptyIdleAnimation, false);
- }
- }
- }
- }
- void AFireArm::OnChambered() {
- if (CanOnlyChamberEmptyChamber) {
- if (!BulletChambered) {
- FP_Gun->PlayAnimation(ChamberAnimation, false);
- }
- }
- else {
- FP_Gun->PlayAnimation(ChamberAnimation, false);
- }
- }
- void AFireArm::OnChamberedFinished() {
- //DoubleChambered
- if (BulletChambered) {
- CAmmoInClip--;
- }
- BulletChambered = true;
- }
- void AFireArm::OnReload()
- {
- FP_Gun->PlayAnimation(ReloadAnimation, false);
- UGameplayStatics::PlaySoundAtLocation(this, ReloadSound, GetActorLocation());
- }
- void AFireArm::OnReloadFinished()
- {
- CAmmoInClip = AmmoInClip;
- BulletChambered = false;
- }
- void AFireArm::SpawnBullet()
- {
- if (Role < ROLE_Authority)
- {
- ServerSpawnBullet();
- }
- FHitResult hit;
- FVector Offest = FVector(FMath::RandRange(-bulletSpread, bulletSpread), FMath::RandRange(-bulletSpread, bulletSpread), FMath::RandRange(-bulletSpread, bulletSpread));
- FVector ShotDir = MuzzleLocation->GetComponentRotation().Vector();
- FVector ShotEnd;
- FVector TraceEnd = MuzzleLocation->GetComponentLocation() + ((ShotDir+ Offest) * 10000);
- FCollisionQueryParams queryparams;
- queryparams.AddIgnoredActor(this);
- queryparams.bTraceComplex = true;
- ShotEnd = TraceEnd;
- if (GetWorld()->LineTraceSingleByChannel(hit, MuzzleLocation->GetComponentLocation(), TraceEnd, ECC_Visibility, queryparams)) {
- AActor* HitActor = hit.GetActor();
- UGameplayStatics::ApplyPointDamage(HitActor, 20, ShotDir, hit, GetInstigatorController(), this, DamageType);
- ShotEnd = hit.Location;
- }
- //DrawDebugLine(GetWorld(), MuzzleLocation->GetComponentLocation(), TraceEnd, FColor::White, false, 1.0f, 0, 1.0f);
- if (FireSound != NULL)
- {
- UGameplayStatics::PlaySoundAtLocation(this, FireSound, GetActorLocation());
- }
- UGameplayStatics::SpawnEmitterAttached(MuzzleEffect, FP_Gun, "Muzzle");
- UParticleSystemComponent* tracercomp = UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), TracerEffect, FP_Gun->GetSocketLocation("Muzzle"));
- tracercomp->SetVectorParameter("BeamEnd", ShotEnd);
- if (Role == ROLE_Authority) {
- HitScanTrace.TraceEnd = ShotEnd;
- }
- }
- void AFireArm::OnRep_HitScanTrace()
- {
- UGameplayStatics::SpawnEmitterAttached(MuzzleEffect, FP_Gun, "Muzzle");
- UParticleSystemComponent* tracercomp = UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), TracerEffect, FP_Gun->GetSocketLocation("Muzzle"));
- tracercomp->SetVectorParameter("BeamEnd", HitScanTrace.TraceEnd);
- }
- void AFireArm::UpdateAttachment()
- {
- FP_IronSight->SetHiddenInGame(true);
- FP_RedDot->SetHiddenInGame(true);
- FP_Scope->SetHiddenInGame(true);
- switch (Attachment)
- {
- case EScopeEnum::SE_IronSight:
- FP_IronSight->SetHiddenInGame(false);
- break;
- case EScopeEnum::SE_RedDot:
- FP_RedDot->SetHiddenInGame(false);
- break;
- case EScopeEnum::SE_Scope:
- FP_Scope->SetHiddenInGame(false);
- break;
- default:
- break;
- }
- }
- void AFireArm::ServerSpawnBullet_Implementation()
- {
- //GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Some debug message!"));
- SpawnBullet();
- }
- bool AFireArm::ServerSpawnBullet_Validate()
- {
- return true;
- }
- void AFireArm::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const {
- Super::GetLifetimeReplicatedProps(OutLifetimeProps);
- DOREPLIFETIME_CONDITION(AFireArm, HitScanTrace,COND_SkipOwner);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement