基于C++代码的UE4学习(五十)——坦克类的完整代码

 

头文件

 

1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 #pragma once
 4 
 5 #include "CoreMinimal.h"
 6 #include "GameFramework/Pawn.h"
 7 #include "UObject/ConstructorHelpers.h"
 8 #include "Particles/ParticleSystemComponent.h"
 9 #include "Kismet/GameplayStatics.h"
10 #include "Tank.generated.h"
11 
12 UCLASS()
13 class BATTLETANK_API ATank : public APawn
14 {
15     GENERATED_BODY()
16 
17 public:
18     // Sets default values for this pawn's properties
19     ATank();
20 
21 protected:
22     // Called when the game starts or when spawned
23     virtual void BeginPlay() override;
24 
25 public:    
26     // Called every frame
27     virtual void Tick(float DeltaTime) override;
28 
29     // Called to bind functionality to input
30     virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
31 
32 
33 public:
34     FVector KeyInput;
35     FRotator MouseInput;
36     FRotator turnning;
37     FVector CameraForward;
38     FRotator CameraRotator;
39     FRotator PaoRotator;
40     FRotator rorationTemp;
41     float Mass;
42     float rotationFriction;
43     float moveSpeed;
44     float RotationSpeed;
45 
46     UPROPERTY(EditAnywhere, BlueprintReadWrite)
47     class UStaticMeshComponent* head;
48     UPROPERTY(EditAnywhere, BlueprintReadWrite)
49     class UStaticMeshComponent* Pao;
50     UPROPERTY(EditAnywhere, BlueprintReadWrite)
51     class UStaticMeshComponent* Body;
52     //class USceneComponent* Scenes;
53     UPROPERTY(EditAnywhere,BlueprintReadWrite)
54     class USpringArmComponent* springArm;
55     UPROPERTY(EditAnywhere, BlueprintReadWrite)
56     class UCameraComponent* myCamera;
57     UPROPERTY(EditAnywhere, BlueprintReadWrite)
58     class USceneComponent* spot;
59 
60     UPROPERTY(EditAnywhere, BlueprintReadWrite)
61     class USceneComponent* Paospot;
62 
63     UPROPERTY(EditAnywhere, BlueprintReadWrite)
64     class USceneComponent* shootSpot;
65 
66 
67     UPROPERTY(EditAnywhere, BlueprintReadWrite)
68     class UParticleSystemComponent* fire;
69     
70 
71 
72 
73     FVector SpringArmBaseLocation;
74     FVector CameraBaseLocation;
75 
76     FRotator SpringArmBaseRotation;
77     FRotator CameraBaseRotation;
78 
79     FRotator HeadYawing;
80 
81     void forward(float value);
82     void turnHead(float value);
83     void turnPao(float value);
84     void Turning(float value);
85     void attack();
86 
87 private:
88     USoundWave* fireSound;
89     USoundCue* soundcue;
90 
91 };

 

 

源文件:

 

  1 // Fill out your copyright notice in the Description page of Project Settings.
  2 
  3 
  4 #include "Tank.h"
  5 #include "Components/InputComponent.h"
  6 #include "Components/StaticMeshComponent.h"
  7 #include "Camera/CameraComponent.h"
  8 #include "Engine.h"
  9 #include "Missle.h"
 10 
 11 // Sets default values
 12 ATank::ATank()
 13 {
 14      // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
 15     PrimaryActorTick.bCanEverTick = true;
 16     Mass = 100;
 17     rotationFriction = 0.01;
 18     moveSpeed = (1 / Mass) * 100000 * rotationFriction;
 19     KeyInput = FVector::ZeroVector;
 20     MouseInput = FRotator::ZeroRotator;
 21     turnning = FRotator::ZeroRotator;
 22     HeadYawing = FRotator::ZeroRotator;
 23     PaoRotator = FRotator::ZeroRotator;
 24     RotationSpeed = 0.75;
 25 
 26     head = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Head"));
 27     Pao = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Pao"));
 28     Body = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Body"));
 29     spot = CreateDefaultSubobject<USceneComponent>(TEXT("spot"));
 30     Paospot = CreateDefaultSubobject<USceneComponent>(TEXT("Paospot"));
 31     shootSpot = CreateDefaultSubobject<USceneComponent>(TEXT("shootSpot"));
 32     springArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("springArm"));
 33     myCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("myCamera"));
 34     fire = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("fire"));
 35 
 36     auto fireAsset = ConstructorHelpers::FObjectFinder<UParticleSystem>(TEXT("ParticleSystem'/Game/StarterContent/Particles/P_Explosion.P_Explosion'"));
 37 
 38     if (fireAsset.Succeeded())
 39     {
 40         fire->SetTemplate(fireAsset.Object);
 41         fire->bAutoActivate = false;
 42         fire->SetWorldScale3D(FVector(15.0f));
 43     }
 44 
 45 
 46     auto matAsset = ConstructorHelpers::FObjectFinder<UMaterialInterface>(TEXT("Material'/Engine/EditorMaterials/Cloth/CameraLitDoubleSided.CameraLitDoubleSided'"));
 47 
 48     if (matAsset.Succeeded()) {
 49             Body->SetMaterial(0, matAsset.Object);
 50             head->SetMaterial(0, matAsset.Object);
 51             Pao->SetMaterial(0, matAsset.Object);
 52     }
 53 
 54     auto headAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Game/Meshes/Head.Head'"));
 55     auto PaoAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Game/Meshes/Pao.Pao'"));
 56     auto BodyAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Game/Meshes/Body.Body'"));
 57 
 58     if (headAsset.Succeeded() && PaoAsset.Succeeded() && BodyAsset.Succeeded()) {
 59         Body->SetStaticMesh(BodyAsset.Object);
 60         head->SetStaticMesh(headAsset.Object);
 61         Pao->SetStaticMesh(PaoAsset.Object);
 62     }
 63 
 64     springArm->TargetArmLength = 200.0f;
 65     springArm->bEnableCameraLag = true;
 66     springArm->CameraLagSpeed = 100.0f;
 67     CameraForward = FVector::ZeroVector;
 68     CameraRotator = FRotator::ZeroRotator;
 69 
 70     RootComponent = Body;
 71     spot->SetupAttachment(Body);
 72     head->SetupAttachment(spot);
 73     Paospot->SetupAttachment(head);
 74     Pao->SetupAttachment(Paospot);
 75     shootSpot->SetupAttachment(Pao);
 76     fire->SetupAttachment(shootSpot);
 77 
 78     springArm->SetupAttachment(head);
 79     myCamera->SetupAttachment(springArm);
 80 
 81 
 82     SpringArmBaseLocation = FVector(-35.0f, -8710.0f, 3315.0f);
 83     SpringArmBaseRotation = FRotator(10, -90, 0);
 84 
 85     CameraBaseLocation = FVector(5390, 0, 950);
 86     CameraBaseRotation = FRotator(0,-180, 0);
 87 
 88 
 89     spot->SetRelativeLocation(FVector(0,-240,138));
 90     head->SetRelativeLocation(FVector(0,1630,0));
 91     Paospot->SetRelativeLocation(FVector(0,235,2011));
 92     Pao->SetRelativeLocation(FVector(0,-300,-2036));
 93     Body->SetRelativeRotation(FRotator(0,-90,0));
 94 
 95     springArm->SetRelativeLocation(SpringArmBaseLocation);
 96     springArm->SetRelativeRotation(SpringArmBaseRotation);
 97     myCamera->SetRelativeLocation(CameraBaseLocation);
 98     myCamera->SetRelativeRotation(CameraBaseRotation);
 99 
100     shootSpot->SetRelativeLocation(FVector(-20,4615,2154));
101     shootSpot->SetRelativeRotation(FRotator(0,90,0));
102     //shootSpot->SetRelativeRotation();
103 
104 
105     auto fireSoundAsset = ConstructorHelpers::FObjectFinder<USoundWave>(TEXT("SoundWave'/Game/BP/fireSound.fireSound'"));
106     if (fireSoundAsset.Succeeded()) {
107         fireSound = fireSoundAsset.Object;
108     }
109 
110 }
111 
112 // Called when the game starts or when spawned
113 void ATank::BeginPlay()
114 {
115     Super::BeginPlay();
116     AutoPossessPlayer = EAutoReceiveInput::Player0;
117 
118     Body->SetMassOverrideInKg(NAME_None, 500000.0f);
119     head->SetMassOverrideInKg(NAME_None, 100000.0f);
120     Pao->SetMassOverrideInKg(NAME_None, 30000.0f);
121 
122     Body->SetSimulatePhysics(true);
123     Body->SetEnableGravity(true);
124     Body->SetCollisionObjectType(ECC_Vehicle);
125 
126 }
127 
128 // Called every frame
129 void ATank::Tick(float DeltaTime)
130 {
131     Super::Tick(DeltaTime);
132 
133     AddActorLocalOffset(KeyInput);
134     AddActorLocalRotation(turnning);
135 
136 
137     if (HeadYawing.Yaw) {
138         rorationTemp = spot->GetRelativeRotation();
139         HeadYawing.Yaw *= RotationSpeed;
140         HeadYawing.Pitch = 0;
141         HeadYawing.Roll = 0;
142         FRotator temp = rorationTemp + HeadYawing;
143         //spot->SetRelativeRotation(temp);
144         spot->AddRelativeRotation(HeadYawing);
145 
146 
147     }
148 
149 
150     if (PaoRotator.Roll) {
151 
152         FRotator temp = Paospot->GetRelativeRotation();
153         PaoRotator.Pitch = 0;
154         PaoRotator.Yaw = 0;
155 
156         temp = temp + PaoRotator;
157 
158         temp.Roll = FMath::Clamp(temp.Roll, -2.0f, 10.0f);
159 
160         Paospot->SetRelativeRotation(temp);
161     }
162 }
163 
164 // Called to bind functionality to input
165 void ATank::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
166 {
167     Super::SetupPlayerInputComponent(PlayerInputComponent);
168 
169     PlayerInputComponent->BindAxis("forward",this,&ATank::forward);
170     PlayerInputComponent->BindAxis("turnHead",this,&ATank::turnHead);
171     PlayerInputComponent->BindAxis("turnPao",this,&ATank::turnPao);
172     PlayerInputComponent->BindAxis("Turning",this,&ATank::Turning);
173     PlayerInputComponent->BindAction("attack", IE_Pressed, this, &ATank::attack);
174 
175 }
176 
177 
178 
179 
180 void ATank::forward(float value)
181 {
182     KeyInput.Y = value * moveSpeed * 2;
183 }
184 
185 
186 void ATank::turnHead(float value)
187 {
188     value = FMath::Clamp(value, -1.0f, 1.0f);
189 
190     HeadYawing.Yaw = value;
191     HeadYawing.Pitch = 0.0f;
192     HeadYawing.Roll = 0.0f;
193 
194 }
195 
196 
197 void ATank::turnPao(float value)
198 {
199     PaoRotator.Roll = value;
200     PaoRotator.Pitch = 0;
201     PaoRotator.Yaw = 0;
202 }
203 
204 
205 
206 
207 void ATank::Turning(float value)
208 {
209     turnning.Yaw = value * moveSpeed * 0.05;
210     turnning.Pitch = 0.0f;
211     turnning.Roll = 0.0f;
212     
213 }
214 
215 void ATank::attack()
216 {
217 
218     FTransform shootSpotLocation = shootSpot->GetComponentTransform();
219 
220     AMissle* missle = GetWorld()->SpawnActor<AMissle>(AMissle::StaticClass(), shootSpotLocation);
221 
222     missle->moves->SetVelocityInLocalSpace(shootSpotLocation.GetLocation().ForwardVector * 80000);
223     missle->moves->Activate(true);
224     fire->Activate(true);
225     UGameplayStatics::PlaySoundAtLocation(GetWorld(),fireSound, shootSpot->GetRelativeLocation());
226 
227     UAudioComponent *ac = UGameplayStatics::SpawnSoundAttached(soundcue, shootSpot); 
228     ac->Activate(true);
229 }

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值