-
Notifications
You must be signed in to change notification settings - Fork 0
/
Weapon.cpp
147 lines (118 loc) · 4.33 KB
/
Weapon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Fill out your copyright notice in the Description page of Project Settings.
#include "Weapon.h"
#include "Enemy.h"
#include "Components/BoxComponent.h"
#include "Components/SkeletalMeshComponent.h"
#include "Engine/SkeletalMeshSocket.h"
#include "Kismet/GameplayStatics.h"
#include "Particles/ParticleSystemComponent.h"
AWeapon::AWeapon()
{
SkeletalMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Skeletal Mesh"));
SkeletalMesh->SetupAttachment(GetRootComponent());
CombatCollision = CreateDefaultSubobject<UBoxComponent>(TEXT("Box Component"));
CombatCollision->SetupAttachment(GetRootComponent());
bWeaponParticle = false;
Damage = 25.f;
WeaponState = EWeaponState::EWS_PickUp;
}
void AWeapon::BeginPlay()
{
Super::BeginPlay();
CombatCollision->OnComponentBeginOverlap.AddDynamic(this,&AWeapon::CombatCollisionOnOverlapBegin);
CombatCollision->OnComponentEndOverlap.AddDynamic(this,&AWeapon::CombatCollisionOnEndOverlap);
//使用 Collision 來 控制 出血效果
CombatCollision->SetCollisionEnabled(ECollisionEnabled::NoCollision);
CombatCollision->SetCollisionObjectType(ECC_WorldDynamic);
CombatCollision->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
CombatCollision->SetCollisionResponseToChannel(ECC_Pawn,ECR_Overlap);
}
void AWeapon::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
{
Super::OnOverlapBegin(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);
if((WeaponState == EWeaponState::EWS_PickUp) && OtherActor)
{
AMyMainCharacter* MainCharacter =Cast<AMyMainCharacter>(OtherActor);
if(MainCharacter)
{
MainCharacter->SetActiveOverlappingItem(this);
}
}
}
void AWeapon::OnEndOverlap (UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
Super::OnEndOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex);
AMyMainCharacter* MainCharacter =Cast<AMyMainCharacter>(OtherActor);
if(MainCharacter)
{
MainCharacter->SetActiveOverlappingItem(nullptr);
}
}
void AWeapon::CombatCollisionOnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if(OtherActor)
{
AEnemy* Enemy = Cast<AEnemy>(OtherActor);
if(Enemy)
{
if(Enemy->HitParticle)
{
const USkeletalMeshSocket* WeaponSocket = SkeletalMesh->GetSocketByName("WeaponSocket");
if(WeaponSocket)
{
FVector SocketLocation = WeaponSocket->GetSocketLocation(SkeletalMesh);
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(),Enemy->HitParticle,SocketLocation,FRotator(0.f),false);
}
}
if(Enemy->HitSound)
{
UGameplayStatics::PlaySound2D(this,Enemy->HitSound);
UGameplayStatics::PlaySound2D(this,SwingSound);
}
if(DamageTypeClass)
{
UGameplayStatics::ApplyDamage(Enemy,Damage,WeaponInstigator,this,DamageTypeClass);
}
}
}
}
void AWeapon::CombatCollisionOnEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
}
void AWeapon::Equip(AMyMainCharacter* MainCharacter)
{
if(IsValid(MainCharacter))
{
SetWeaponInstigator(MainCharacter->GetController()); //控制權限 由 MainCharacter 給予
SkeletalMesh->SetCollisionResponseToChannel(ECollisionChannel::ECC_Camera,ECR_Ignore);
SkeletalMesh->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn,ECR_Ignore);
SkeletalMesh->SetSimulatePhysics(false);
const USkeletalMeshSocket* RightHandSocket = MainCharacter->GetMesh()->GetSocketByName("R_WeaponSocket");
if(RightHandSocket)
{
RightHandSocket->AttachActor(this,MainCharacter->GetMesh());
bRotate = false;
MainCharacter->SetEquippedWeapon(this);
MainCharacter->SetActiveOverlappingItem(nullptr);
WeaponState = EWeaponState::EWS_Equipped;
}
if(OnEquipSound)
{
UGameplayStatics::PlaySound2D(this,OnEquipSound);
}
if(!bWeaponParticle)
{
IdleParticleComponent->Deactivate();
}
}
}
void AWeapon::ActiveCollision()
{
CombatCollision->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
}
void AWeapon::DeActiveCollision()
{
CombatCollision->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}