UE5.1.1C++从0开始(10.作业三)

news2024/9/20 20:43:30

这次作业量和之前的相比可能会有点大,我先整理一下这次的作业清单:

  1. 魔法子弹的飞行声音以及爆炸声音
  2. 给玩家增加受击的闪亮的效果,和立方体相同的那种
  3. 增加一个health max的变量,把widget里头的health节点更换为health max节点,同时给health设置一个上下限,上限是health max , 下限是0
  4. 给我们刚创建出的显示伤害的widget设置一个动画,在受击的时候播放出来,同时伤害的显示不能写死,用变量来表示
  5. 新建一个生命药水,可以恢复生命值,回复一次之后,10秒内不能再次使用,10秒后可再次使用。当玩家血量满的时候忽略玩家的交互
  6. 给玩家的手上附加一个粒子效果(这个我没看明白,没写)
  7. 增加一个摇晃摄像头的效果

一共7项作业,量可以说是非常大了。。。不过还是一个一个来做。

1.魔法子弹的飞行声音以及爆炸效果:

我把这个功能写到了SFatherMagicProjectile里头

SFatherMagicProjectile.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "SFatherMagicProjectile.generated.h"

UCLASS()
class ACTIONROGUELIKE_API ASFatherMagicProjectile : public AActor
{
	GENERATED_BODY()

public:
	UPROPERTY(EditAnywhere)
	class USphereComponent * SphereComp;

	UPROPERTY(EditAnywhere)
	class UProjectileMovementComponent * MovementComp;

	UPROPERTY(EditAnywhere)
	class UParticleSystemComponent * ParticleComp;
	
    //一个声音组件
	UPROPERTY(EditAnywhere)
	class UAudioComponent * AudioComp;
	
    //爆炸声
	UPROPERTY(EditAnywhere)
	class USoundBase * Sound;
	
public:	
	// Sets default values for this actor's properties
	ASFatherMagicProjectile();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UFUNCTION()
	virtual void OnComponentHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, FHitResult Hit);

	UFUNCTION()
		void OnComponentOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
};

SFatherMagicProjectile.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "SFatherMagicProjectile.h"
#include "Components/SphereComponent.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "Particles/ParticleSystemComponent.h"
#include "SAttributeComponent.h"
#include "Components/AudioComponent.h"
#include "Kismet/GameplayStatics.h"

// Sets default values
ASFatherMagicProjectile::ASFatherMagicProjectile()
{
 	// 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;

	FScriptDelegate OnHit;
	OnHit.BindUFunction(this, STATIC_FUNCTION_FNAME(TEXT("ASFatherMagicProjectile::OnComponentHit")));

	FScriptDelegate OnOverlap;
	OnOverlap.BindUFunction(this, STATIC_FUNCTION_FNAME(TEXT("ASFatherMagicProjectile::OnComponentOverlap")));

	SphereComp = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Comp"));
	SphereComp->SetCollisionProfileName("Projectile");
	SphereComp->SetSphereRadius(10.0f, true);
	SphereComp->OnComponentHit.Add(OnHit);
	SphereComp->OnComponentBeginOverlap.Add(OnOverlap);
	RootComponent = SphereComp;

	MovementComp = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("Movement Comp"));
	MovementComp->InitialSpeed = 1000.0f;
	MovementComp->bRotationFollowsVelocity = true;
	MovementComp->bInitialVelocityInLocalSpace = true;
	MovementComp->ProjectileGravityScale = 0.0f;

	ParticleComp = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("Particle Comp"));
	ParticleComp->SetupAttachment(SphereComp);

	AudioComp = CreateDefaultSubobject<UAudioComponent>(TEXT("Audio Comp"));
	
}

// Called when the game starts or when spawned
void ASFatherMagicProjectile::BeginPlay()
{
	Super::BeginPlay();

	APawn* Instigator_01 = AActor::GetInstigator();

	SphereComp->IgnoreActorWhenMoving(Instigator_01, true);

    //播放飞行声效
	AudioComp->Play(0.0f);
	
}

// Called every frame
void ASFatherMagicProjectile::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

void ASFatherMagicProjectile::OnComponentHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, FHitResult Hit)
{
	APawn* Instigator_01 = AActor::GetInstigator();

	if (Instigator_01!=OtherActor)
	{	
        //播放爆炸声效
		UGameplayStatics::PlaySoundAtLocation(GetWorld(),Sound , this->GetActorLocation(), 1.0f, 1.0f);
		GetWorld()->DestroyActor(this);
		DrawDebugSphere(GetWorld(), Hit.ImpactPoint, 10.0f, 16, FColor::Red, false, 2.0f, 0U, 1.0f);
		
	}
}

void ASFatherMagicProjectile::OnComponentOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	APawn* Instigator_01 = AActor::GetInstigator();

	if (OtherActor)
	{
		USAttributeComponent* AttributeComp = Cast<USAttributeComponent>(OtherActor->GetComponentByClass(USAttributeComponent::StaticClass()));
		if (AttributeComp)
		{
			if (Instigator_01 != OtherActor)
			{
				UGameplayStatics::Play
					AtLocation(GetWorld(), Sound, this->GetActorLocation(), 1.0f, 1.0f);
				AttributeComp->ApplyHealthChange(-20.0f);
				GetWorld()->DestroyActor(this);
			}
		}

	}
}




2.给玩家增加受击的闪亮的效果,和立方体相同的那种

这个首先得对我们的Gideon的材质进行修改

请添加图片描述

使用MatLayerBlend_Emissive函数,然后把我们的MF_HitFlash给连上去就好,再之后就是玩家角色的代码更改

SCharacter.cpp

void ASCharacter::OnHealthChanged(AActor* InstigatorActor, USAttributeComponent* OwningComp, float NewHealth, float Delta)
{
    //这个函数做了更改,第一点是只有受伤才会触发闪亮的效果,加血的时候不需要触发这个效果
	if (NewHealth<0.0f)
	{
		this->GetMesh()->SetScalarParameterValueOnMaterials("TimeToHit", GetWorld()->TimeSeconds);
		if (Delta<0.0f)
		{
			APlayerController* PC = Cast<APlayerController>(GetController());
			DisableInput(PC);
		}
	}
	
}

3.增加一个health max的变量,把widget里头的health节点更换为health max节点,同时给health设置一个上下限,上限是health max , 下限是0

这个蛮简单的,更改如下

SAttributeComponent.cpp

bool USAttributeComponent::ApplyHealthChange(float Delta)
{
	
    //使用Clamp函数,Health的更改在Clamp函数的第一个参数那里显示了
	Health = FMath::Clamp(Health+Delta, 0, HealthMax);


	OnHealthChanged.Broadcast(nullptr,this,Health,Delta);
	return true;
}

4.给我们刚创建出的显示伤害的widget设置一个动画,在受击的时候播放出来,同时伤害的显示不能写死,用变量来表示

请添加图片描述

Cube的蓝图更改

请添加图片描述
至于动画怎么播放,看各位的设计了,这里是千人千面的。

5.新建一个生命药水,可以恢复生命值,回复一次之后,10秒内不能再次使用,10秒后可再次使用。当玩家血量满的时候忽略玩家的交互

这也没遇上啥大问题,这个的示例就是我们的可以开关的那个箱子

SHealBottle.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "SGameplayInterface.h"
#include "SHealBottle.generated.h"

UCLASS()
class ACTIONROGUELIKE_API ASHealBottle : public AActor, public ISGameplayInterface
{
	GENERATED_BODY()

	void Interact_Implementation(APawn* InstigatorPawn) override;

	UPROPERTY(VisibleAnywhere)
	class UStaticMeshComponent* MeshComp;

private:
	UFUNCTION()
	void RefreshBottle();

private:
	bool CanHeal;
	
	FTimerHandle Timer;

public:	
	// Sets default values for this actor's properties
	ASHealBottle();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};

SHealBottle.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "SHealBottle.h"
#include "Components/StaticMeshComponent.h"
#include "SAttributeComponent.h"

//接口函数的实现
void ASHealBottle::Interact_Implementation(APawn* InstigatorPawn)
{
	ASCharacter* InstigatorCharacter_01 = Cast<ASCharacter>(InstigatorPawn);
	USAttributeComponent* attributeComp_01 = InstigatorCharacter_01->AttributeComp;
	if (attributeComp_01->GetHealth()>=100||!CanHeal)
	{
		return;
	}
	attributeComp_01->ApplyHealthChange(10.0f);
	CanHeal = false;
	MeshComp->SetVisibility(false);
    //消失10秒
	GetWorldTimerManager().SetTimer(Timer,this, &ASHealBottle::RefreshBottle, 10.0f);
}

void ASHealBottle::RefreshBottle()
{
	CanHeal = true;
	MeshComp->SetVisibility(true);
}

// Sets default values
ASHealBottle::ASHealBottle()
{
 	// 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;

	MeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh Comp"));

	CanHeal = true;

}

// Called when the game starts or when spawned
void ASHealBottle::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void ASHealBottle::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}


7.增加一个摇晃摄像头的效果

SCharacter.cpp

void ASCharacter::PrimaryAttack()
{
	
    //获得控制器
	APlayerController* pc = Cast<APlayerController>(GetController());
	//播放摄像机的震荡
	pc->ClientStartCameraShake(CameraShakeComp);

	GetWorldTimerManager().SetTimer(TimerHandle_PrimaryAttack,this,&ASCharacter::PrimaryAttack_TimeElasped,0.2f);
	
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/565039.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

PythonOCR识别扫描版纯图PDF提取汉字的10大方法,力推RapidOCRPDF 可识别纯图PDF 加密签名的PDF 重点是开源免费,某些方面准确度比百度OCR高

下面实例都以下面的测试样例PDF为实验对象 非纯图可复制pdf 纯图PDF TOP1&#xff1a;RapidOCRPDF 可识别纯图PDF也能识别加密签名的PDF 重点是开源免费 https://github.com/RapidAI/RapidOCRPDF # 基于rapidocr_onnxruntime pip install rapidocr_pdf[onnxruntime]# 基于ra…

多体动力学:哈密尔顿原理的使用

参考资料&#xff1a; HAMILTON’S PRINCIPLE AND HAMILTON’S FORMULATION 原理已经讲得很详细了。如果看不到可以见我的资源&#xff0c;不用积分就可以下载。 步骤&#xff1a; 1.写出拉格朗日表达式 L T − V LT-V LT−V, T T T是系统的动能&#xff0c; V V V是系统的…

二进制安装1.26版本k8s(docker)

文章目录 前言准备工作准备4台虚拟机说明下载对应的二进制包初始化操作CentOS7配置yum源配置免密、修改hostname、关闭防火墙、selinux、关闭swap分区(方便后面进行其它操作)下载软件包并批量安装配置时间同步配置打开文件描述符添加ipvs模块和内核模块 Ubuntu配置apt源配置免密…

【数据结构】---堆排序+TOP-K问题(了解游戏排行底层原理)

文章目录 前言&#x1f31f;一、建堆的两种方式&#xff1a;&#x1f30f;1.1 向上调整建堆(堆排序)&#xff1a;&#x1f4ab;1.1.1 完整代码&#xff1a;&#x1f4ab;1.1.2 流程图(以小堆为例)&#xff1a;升序&#xff1a;建大堆&#x1f4ab;1.1.3 流程图(以小堆为例)&…

python调用海康视频汇聚平台API,获得所有摄像头设备编号、实时播放rtsp地址、回放rtsp地址

目录 1. 需求2. 思路3. 实现3.1. 确保服务器上安装视频汇聚平台&#xff08;iSecure Center&#xff09;3.2. 查看API网关是否安装成功3.3. 分配身份认证信息AK/SK3.4. 利用认证信息&#xff0c;python demo开发 1. 需求 海康视频汇聚平台(综合安防管理平台&#xff08;iSecur…

机器学习之集成学习

一、介绍 集成学习&#xff08;Ensemble Learning&#xff09;是一种机器学习技术&#xff0c;通过结合多个学习器&#xff08;例如决策树、神经网络、支持向量机等&#xff09;的预测结果&#xff0c;来达到更好的分类或回归预测性能。集成学习可以通过降低模型的方差&#x…

探究 Android 14 新功能之 TextView 搜索高亮和焦点移动~

前言 之前写的文章《Android 14 新功能之 HighLights》里&#xff0c;讲到 Android 14 里推出的 HighLights 新功能可以快速实现 TextView 文字的高亮效果&#xff0c;并支持动态更新。 本文将继续介绍 TextView 的另 2 处新功能&#xff1a; 使用 searchResultHighlight 等…

(转载)从0开始学matlab(第14天)—while循环结构

循环(loop) 是一种 matlab 结构&#xff0c;它允许我们多次执行一系列的语句。循环结构有两种基本形式 :while 循环和 for 循环。两者之间的最大不同在于代码的重复是如何控制的。在while 循环中&#xff0c;代码的重复的次数是不能确定的&#xff0c;只要满足用户定义的条件…

Metabase可视化BI系统部署安装及简单使用

metabase 是国外开源的一个可视化系统&#xff0c;语言使用了Clojure TypeScript。 Clojure&#xff08;/ˈkloʊʒər/&#xff09;是Lisp程式語言在Java平台上的現代、動態及函數式方言。来自维基百科。 我也是第一次看到这个语言&#xff0c;知道它是可以运行在Java环境上的…

脱离文档流和半脱离文档流(详解)

脱离文档流和半脱离文档流&#xff08;详解&#xff09;_脱离文档流是什么意思_大前端工程师的博客-CSDN博客 脱离文档流 脱离文档流是指&#xff0c;该元素脱离了文档中。不再占据默认分配的空间&#xff0c;它下面的元素会上去补位。 可以理解为脱离文档流的元素离开了它的座…

1720_Linux学习中的问题处理

全部学习汇总&#xff1a;GreyZhang/little_bits_of_linux: My notes on the trip of learning linux. (github.com) 这个有点学习的方法论的意思&#xff0c;画个滋味导图顺便整理一下。 遇到问题的时候&#xff0c;解决的方法大致有3中&#xff0c;而针对学习的建议有一部分是…

STL-queue和priority_queue的模拟实现

回顾 对于STL&#xff0c;我们已经知道了vector和list&#xff0c;而它们是STL中被称为六大组件之一的容器&#xff0c;我们还学习了模拟实现stack&#xff0c;而stack在STL中被称为六大组件之一的适配器&#xff0c;今天&#xff0c;我们来学习queue的模拟实现和priority_que…

分布式运用之ELK企业级日志分析系统

1.ELK的相关知识 1.1 ELK的概念与组件 ELK平台是一套完整的日志集中处理解决方案&#xff0c;将 ElasticSearch、Logstash 和 Kiabana 三个开源工具配合使用&#xff0c; 完成更强大的用户对日志的查询、排序、统计需求。 ElasticSearch&#xff1a; 是基于Lucene&#xff0…

Tomcat服务器、Servlet生命周期、上传下载文件、使用XHR请求数据、注解使用

文章目录 Servlet认识Tomcat服务器使用Maven创建Web项目创建Servlet探究Servlet的生命周期解读和使用HttpServletWebServlet注解详解使用POST请求完成登陆上传和下载文件下载文件上传文件 使用XHR请求数据重定向与请求转发重定向请求转发 ServletContext对象初始化参数 Servlet…

基于自营配送模式的车辆路径规划设计与实现_kaic

摘要 近年来&#xff0c;随着我国消费水平逐渐提升&#xff0c;消费者在网上购物的频率也越来越高&#xff0c;电商发展速度迅猛&#xff0c;加大了物流配送的压力&#xff0c;促使物流企业以更大的运力&#xff0c;更短的时间将货物送达。在货品的运输过程中&#xff0c;成本居…

TCP 连接未必都是用TCP协议沟通

确实&#xff0c;一般来说 TCP 连接是标准的 TCP 三次握手完成的&#xff1a; 1.客户端发送 SYN&#xff1b; 2.服务端收到 SYN 后&#xff0c;回复 SYNACK&#xff1b; 3.客户端收到 SYNACK 后&#xff0c;回复 ACK。 SYN 会在两端各发送一次&#xff0c;表示“我准备好了&…

MicroBlaze系列教程(9):xilisf串行Flash驱动库的使用

文章目录 1. xilisf库简介2. xilisf库函数3. xilisf配置4. xilisf应用示例工程下载本文是Xilinx MicroBlaze系列教程的第9篇文章。 1. xilisf库简介 xilisf库(Xilinx In-system and Serial Flash Library) 是Xilinx 提供的一款串行Flash驱动库,支持常用的 Atmel 、Intel、S…

Linux基本指令介绍

目录 前言 指令操作与图形化界面的对比 adduser passwd whoami ls指令 pwd指令 cd指令 touch指令 mkdir指令 rmdir指令 && rm 指令 man指令 cp指令 mv指令 cat&#xff08;显示文件内容&#xff08;全部&#xff09;&#xff0c;且不可修改的&#xff09;…

【1++的C++初阶】之模板

&#x1f44d;作者主页&#xff1a;进击的1 &#x1f929; 专栏链接&#xff1a;【1的C初阶】 文章目录 一&#xff0c;浅谈模板二&#xff0c;函数模板三&#xff0c;类模板 一&#xff0c;浅谈模板 在前面的文章【【1的C初阶】之C入门篇1】中我们对函数重载有了一定的认识&a…

Python-web开发学习笔记(3):Flask Demo,一个网站开发小案例

☕️ 推荐关注我的另一个专栏系列&#xff1a;大道至简&#xff0c;该系列收录了许多优质的人工智能算法博文&#xff0c;包括机器学习和自然语言处理等&#xff0c;持续更新中&#xff0c;欢迎各位关注~ 介绍完HTML标签&#xff0c;我们来搞一个简单的网站小案例&#xff0c;带…