Cocos 距离判断和小兵的攻击判定

2023-11-07 14:04

本文主要是介绍Cocos 距离判断和小兵的攻击判定,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 最近在学习cocos 和ts 遇到的问题和如何解决问题都会发表博客记录一下。

距离判断

let j = new Vec2(this.target.position.x - this.node.position.x, this.target.position.y - this.node.position.y);

let dis = Math.sqrt(j.x * j.x + j.y * j.y)

枚举的外部类型定义:

@property({type:Enum(HitAction)})   //外部定义枚举类型


import { _decorator, Component, debug, Enum, find, Node, Vec2, Vec3 } from 'cc';
import { HitAction } from '../Struct/GameStruct';
const { ccclass, property } = _decorator;@ccclass('EnemyRole')
export class EnemyRole extends Component {target: Node;   //目标speed: number = 20;   //速度@property({type:Enum(HitAction)})   //外部定义枚举类型hitAction: HitAction|null = null;canHit: boolean = true;start() {this.target = find('Canvas/Target');// console.log(this.target.name);// console.log(this.target.position);this.hitAction = HitAction.one_xiaoBing;//this.Attack();}update(deltaTime: number) {// [4]this.node.translate(new Vec3(0, deltaTime * this.speed, 0));let j = new Vec2(this.target.position.x - this.node.position.x, this.target.position.y - this.node.position.y);let dis = Math.sqrt(j.x * j.x + j.y * j.y)// console.log("距离=" + dis);//枚举判断是哪种兵//移动逻辑switch (this.hitAction) {case HitAction.one_xiaoBing:case HitAction.one_QiShi:case HitAction.one_DunBing:if (dis <= 150) {this.speed = 0;//开启攻击防御塔行为if (this.canHit) {this.Attack();this.canHit = false;}}break;case HitAction.one_SheShou:case HitAction.one_PaoBing:if (dis <= 200) {this.speed = 0;//开启攻击防御塔行为if (this.canHit) {this.Attack();this.canHit = false;}}break;default:break;}}
//攻击逻辑Attack() {console.log("进");if (this.canHit) {switch (this.hitAction) {case HitAction.one_xiaoBing:case HitAction.one_QiShi:case HitAction.one_DunBing://每隔2秒执行一次this.schedule(function () {console.log("进gong!!!!!!!!!!!!!!");}, 2);break;case HitAction.one_SheShou:case HitAction.one_PaoBing:this.schedule(function () {console.log("进gong!!!!!!!!!!!!!!");}, 3);break;default:break;}}}
}

数据存储:


import { _decorator, Component, Node, sys } from 'cc';
import BaseSingleClass from '../Single/BaseSingleClass';
const { ccclass, property } = _decorator;//用于存储数据
@ccclass('JsonMgr')
export class JsonMgr<T> extends BaseSingleClass {start () {}//数据保存SetJson(name:string,item?:T){let str=JSON.stringify(item);  //把要改变的泛型转换为字符串(序列化)sys.localStorage.setItem(name,str);   //保存数据}SetJsonOne(name:string,str:string){sys.localStorage.setItem(name,str);   //保存数据}   // GetJson(name:string){//     sys.localStorage.getItem(name);   //获取数据// }//获取数据, 获取到Json内的值(对象)//let s=Object.assign(new Person(),JSON.parse(YYYY.bbb)); //console.log(s.id);//console.log(s.age);//console.log(s.name);
}
export default class BaseSingleClass{public static getInstance<T extends{}>(this:new()=>T) :T{if(!(<any>this).instance){(<any>this).instance=new this();}return (<any>this).instance}public static recycle():void{(<any>this).instance=null}
}

根据修改需求,先对小兵进行进攻,在进攻防御塔


import { _decorator, Component, Enum, find, Node, Vec2, Vec3 } from 'cc';
import { HitAction, IsTarget, IsTargetFriend, friendTimes } from '../Struct/GameStruct';
import { EnemyRole } from './EnemyRole';
import { SingleMgr } from '../Managers/SingleMgr';
const { ccclass, property } = _decorator;@ccclass('FriendRole')
export class FriendRole extends Component {target: Node;   //目标speed: number = 30;   //行径速度@property({ type: Enum(HitAction) })   //外部定义枚举类型hitAction: HitAction | null = null;@property({ type: Enum(IsTargetFriend) })   //判断生成友军还是敌人public isTargetfriend: IsTargetFriend | null = null;@property({ type: Enum(IsTarget) })   //判断进攻敌人还是主城public isTarget: IsTarget | null = null;canHit: boolean = true;// 身体区域的坐标点private _bodyPolygonPoints: Array<Vec2> = [];private _timeID: number = 0;// 获取碰撞区域getPlayerCollider(): Array<Vec2> {return this._bodyPolygonPoints;}start() {this.isTarget = IsTarget.bigEnemy;    //初始目标为防御塔this.FindTarget();  //第一次初始化目标}update(deltaTime: number) {this.EnemyOrFriend();//持续更新,判断场景内是否有敌人进行,攻击目标的切换//看向防御塔位置this.LookIt();this.node.translate(new Vec3(0, deltaTime * this.speed, 0));    //移动//this.DisTance();}//攻击逻辑Attack() {console.log("friend进");if (this.canHit) {switch (this.hitAction) {case HitAction.one_xiaoBing:case HitAction.one_QiShi:case HitAction.one_DunBing://每隔2秒执行一次this.schedule(friendTimes, 2);    //v是break;case HitAction.one_SheShou:case HitAction.one_PaoBing://每隔3秒执行一次this.schedule(function () {console.log("Friend远程进gong!!!!!!!!!!!!!!");}, 3);break;default:break;}}}//始终看向防御塔(通过目标进行改变)LookIt() {var dx = this.node.position.x - this.target.position.x;var dy = this.node.position.y - this.target.position.y;var dir = new Vec2(dx, dy);let radian = Math.atan2(dir.y, dir.x);var angle = radian / Math.PI * 180;this.node.angle = angle + 90;}//攻击目标的切换转变(防御塔和敌人AI)//返回目标的名称FindTarget(): Node {switch (this.isTarget) {case IsTarget.enemy:this.target = find('Canvas/enemy');  //敌人break;case IsTarget.bigEnemy:this.target = find('Canvas/Target');    //目标主城break;default:break;}return this.target;}//敌人和主城的切换EnemyOrFriend() {//let targetss= this.FindTarget();let n_pos = this.node.position;   //自身位置let pGameManager = find("Canvas/enemy"); //身上挂载EnemyRole脚本let tGameManager = find('Canvas/Target');//敌人存在时if (pGameManager) {this.isTarget = IsTarget.enemy;this.FindTarget();let dis = Vec2.distance(n_pos, pGameManager.position);    //目标位置和自身位置之间的距离if (dis <= 60) {this.speed = 0;//进攻逻辑}else {this.speed = 30;//跟随进攻逻辑}}else {//主城存在时if (tGameManager) {this.isTarget = IsTarget.bigEnemy;this.FindTarget();//查找目标//console.log("target叫"+this.target);this.speed = 20;this.DisTance();    //范围判定}else {//胜利}}}//距离判断DisTance() {//距离判断let j = new Vec2(this.target.position.x - this.node.position.x, this.target.position.y - this.node.position.y);//console.log()let dis = Math.sqrt(j.x * j.x + j.y * j.y)//console.log("到"+this.target.name+"距离=" + dis);//枚举判断是哪种兵//移动逻辑switch (this.hitAction) {case HitAction.one_xiaoBing:case HitAction.one_QiShi:case HitAction.one_DunBing:if (dis <= 100) {//console.log("进了吗");this.speed = 0;//开启攻击防御塔行为if (this.canHit) {this.Attack();this.canHit = false;}}break;case HitAction.one_SheShou:case HitAction.one_PaoBing:if (dis <= 120) {this.speed = 0;//开启攻击防御塔行为if (this.canHit) {this.Attack();this.canHit = false;}}break;default:break;}}
}

这篇关于Cocos 距离判断和小兵的攻击判定的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/364120

相关文章

Go语言中nil判断的注意事项(最新推荐)

《Go语言中nil判断的注意事项(最新推荐)》本文给大家介绍Go语言中nil判断的注意事项,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.接口变量的特殊行为2.nil的合法类型3.nil值的实用行为4.自定义类型与nil5.反射判断nil6.函数返回的

python判断文件是否存在常用的几种方式

《python判断文件是否存在常用的几种方式》在Python中我们在读写文件之前,首先要做的事情就是判断文件是否存在,否则很容易发生错误的情况,:本文主要介绍python判断文件是否存在常用的几种... 目录1. 使用 os.path.exists()2. 使用 os.path.isfile()3. 使用

Go语言如何判断两张图片的相似度

《Go语言如何判断两张图片的相似度》这篇文章主要为大家详细介绍了Go语言如何中实现判断两张图片的相似度的两种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 在介绍技术细节前,我们先来看看图片对比在哪些场景下可以用得到:图片去重:自动删除重复图片,为存储空间"瘦身"。想象你是一个

Python如何判断字符串中是否包含特殊字符并替换

《Python如何判断字符串中是否包含特殊字符并替换》这篇文章主要为大家详细介绍了如何使用Python实现判断字符串中是否包含特殊字符并使用空字符串替换掉,文中的示例代码讲解详细,感兴趣的小伙伴可以了... 目录python判断字符串中是否包含特殊字符方法一:使用正则表达式方法二:手动检查特定字符Pytho

Java计算经纬度距离的示例代码

《Java计算经纬度距离的示例代码》在Java中计算两个经纬度之间的距离,可以使用多种方法(代码示例均返回米为单位),文中整理了常用的5种方法,感兴趣的小伙伴可以了解一下... 目录1. Haversine公式(中等精度,推荐通用场景)2. 球面余弦定理(简单但精度较低)3. Vincenty公式(高精度,

判断PyTorch是GPU版还是CPU版的方法小结

《判断PyTorch是GPU版还是CPU版的方法小结》PyTorch作为当前最流行的深度学习框架之一,支持在CPU和GPU(NVIDIACUDA)上运行,所以对于深度学习开发者来说,正确识别PyTor... 目录前言为什么需要区分GPU和CPU版本?性能差异硬件要求如何检查PyTorch版本?方法1:使用命

Python如何精准判断某个进程是否在运行

《Python如何精准判断某个进程是否在运行》这篇文章主要为大家详细介绍了Python如何精准判断某个进程是否在运行,本文为大家整理了3种方法并进行了对比,有需要的小伙伴可以跟随小编一起学习一下... 目录一、为什么需要判断进程是否存在二、方法1:用psutil库(推荐)三、方法2:用os.system调用

Python实现特殊字符判断并去掉非字母和数字的特殊字符

《Python实现特殊字符判断并去掉非字母和数字的特殊字符》在Python中,可以通过多种方法来判断字符串中是否包含非字母、数字的特殊字符,并将这些特殊字符去掉,本文为大家整理了一些常用的,希望对大家... 目录1. 使用正则表达式判断字符串中是否包含特殊字符去掉字符串中的特殊字符2. 使用 str.isa

Python中判断对象是否为空的方法

《Python中判断对象是否为空的方法》在Python开发中,判断对象是否为“空”是高频操作,但看似简单的需求却暗藏玄机,从None到空容器,从零值到自定义对象的“假值”状态,不同场景下的“空”需要精... 目录一、python中的“空”值体系二、精准判定方法对比三、常见误区解析四、进阶处理技巧五、性能优化

web网络安全之跨站脚本攻击(XSS)详解

《web网络安全之跨站脚本攻击(XSS)详解》:本文主要介绍web网络安全之跨站脚本攻击(XSS)的相关资料,跨站脚本攻击XSS是一种常见的Web安全漏洞,攻击者通过注入恶意脚本诱使用户执行,可能... 目录前言XSS 的类型1. 存储型 XSS(Stored XSS)示例:危害:2. 反射型 XSS(Re