攻击力升级
升级攻击力需要能量,本节课我们需要完善获取能量的逻辑,以及完善攻击力升级逻辑。
1.获取能量
当角色攻击怪物时,造成的伤害可以1:1兑换为能量,我们可以在攻击函数添加该逻辑
PlayerModuleC脚本:
主要添加了两行代码
① 造成伤害后,将伤害值累加到当前能量属性上
② 能量发生改变,调用freshUI刷新UpgradeUI的显示内容
ts
export class PlayerModuleC extends ModuleC<PlayerModuleS, PlayerModuleData>{
//省略代码
......
public atk() {
// 范围检测
let result = QueryUtil.sphereOverlap(this.localPlayer.character.worldTransform.position, 100, false)
// 筛选出怪物
for (let obj of result) {
if (obj instanceof Character) {
continue
}
if (obj.tag == "Monster") {
// 让怪物受伤
let scripts = obj.getScripts()
for (let script of scripts) {
if (script instanceof MonsterScirpt) {
let damage = script.hurt(this._nowAtk)
this._nowEnergy += damage
}
}
}
}
this.server.net_playAnim()
// 播放音效
SoundService.playSound("209818", 1)
this.freshUI()
}
private freshUI() {
this._upgradeUI.freshInfo(this._nowEnergy, this._nowAtk, this._nowUpgrade)
}
}
export class PlayerModuleC extends ModuleC<PlayerModuleS, PlayerModuleData>{
//省略代码
......
public atk() {
// 范围检测
let result = QueryUtil.sphereOverlap(this.localPlayer.character.worldTransform.position, 100, false)
// 筛选出怪物
for (let obj of result) {
if (obj instanceof Character) {
continue
}
if (obj.tag == "Monster") {
// 让怪物受伤
let scripts = obj.getScripts()
for (let script of scripts) {
if (script instanceof MonsterScirpt) {
let damage = script.hurt(this._nowAtk)
this._nowEnergy += damage
}
}
}
}
this.server.net_playAnim()
// 播放音效
SoundService.playSound("209818", 1)
this.freshUI()
}
private freshUI() {
this._upgradeUI.freshInfo(this._nowEnergy, this._nowAtk, this._nowUpgrade)
}
}
2.升级函数
每升一级,就让角色的攻击力增加10%,同时将下一级升级所需的能量值设置为攻击力的10倍。
向 PlayerModuleC脚本 中添加升级函数upgrade
在这部分逻辑中,我还添加了播放升级特效以及升级音效的逻辑,大家记得要将对应的资源拖入优先加载,否则资源播放会失败。
ts
export class PlayerModuleC extends ModuleC<PlayerModuleS, PlayerModuleData>{
// 省略代码
......
public upgrade() {
// 判断能量是否足够进行升级
if (this._nowEnergy >= this._nowUpgrade) {
// 扣除能量
this._nowEnergy -= this._nowUpgrade
// 每次升级提示10%攻击力
this._nowAtk = Math.round(this._nowAtk * 1.1)
// 下一次升级所需要的能量等于当前攻击力的十倍
this._nowUpgrade = this._nowAtk * 10
this.freshUI()
// 播放升级特效
EffectService.playOnGameObject("160498", this.localPlayer.character, { slotType: HumanoidSlotType.Root })
// 播放音效
SoundService.playSound("169179", 1)
}
}
}
export class PlayerModuleC extends ModuleC<PlayerModuleS, PlayerModuleData>{
// 省略代码
......
public upgrade() {
// 判断能量是否足够进行升级
if (this._nowEnergy >= this._nowUpgrade) {
// 扣除能量
this._nowEnergy -= this._nowUpgrade
// 每次升级提示10%攻击力
this._nowAtk = Math.round(this._nowAtk * 1.1)
// 下一次升级所需要的能量等于当前攻击力的十倍
this._nowUpgrade = this._nowAtk * 10
this.freshUI()
// 播放升级特效
EffectService.playOnGameObject("160498", this.localPlayer.character, { slotType: HumanoidSlotType.Root })
// 播放音效
SoundService.playSound("169179", 1)
}
}
}
3.绑定升级按钮
当玩家点击UpgradeUI的升级按钮时,就调用upgrade函数来进行升级
UptradeUI脚本
ts
import { PlayerModuleC } from "../Module/PlayerModuleC";
import UpgradeUI_Generate from "../ui-generate/UpgradeUI_generate";
export default class UpgradeUI extends UpgradeUI_Generate {
protected onAwake(): void {
// 给按钮添加点击事件
this.mUpgrade_btn.onPressed.add(() => {
// 处理升级逻辑
ModuleService.getModule(PlayerModuleC).upgrade()
})
}
/**
* 刷新UI内容
* @param energy 当前能量
* @param atk 当前攻击力
* @param upgrade 升级所需能量
*/
public freshInfo(energy: number, atk: number, upgrade: number) {
this.mEnergy_txt.text = energy.toFixed(0)
this.mAtk_txt.text = atk.toFixed(0)
this.mUpgrade_txt.text = `升级(${upgrade.toFixed(0)})`
}
}
import { PlayerModuleC } from "../Module/PlayerModuleC";
import UpgradeUI_Generate from "../ui-generate/UpgradeUI_generate";
export default class UpgradeUI extends UpgradeUI_Generate {
protected onAwake(): void {
// 给按钮添加点击事件
this.mUpgrade_btn.onPressed.add(() => {
// 处理升级逻辑
ModuleService.getModule(PlayerModuleC).upgrade()
})
}
/**
* 刷新UI内容
* @param energy 当前能量
* @param atk 当前攻击力
* @param upgrade 升级所需能量
*/
public freshInfo(energy: number, atk: number, upgrade: number) {
this.mEnergy_txt.text = energy.toFixed(0)
this.mAtk_txt.text = atk.toFixed(0)
this.mUpgrade_txt.text = `升级(${upgrade.toFixed(0)})`
}
}