import { _decorator, BoxCollider2D, CircleCollider2D, Collider2D, Component, Contact2DType, Director, director, IPhysics2DContact, Label, math, Node, PolygonCollider2D, Rect, RigidBody, RigidBody2D, Sprite, tween, Tween, UITransform, Vec2, Vec3, View } from 'cc'; import { wall } from './wall'; import { box } from './box'; import { coin } from './coin'; import { tools } from '../tools'; import { SdkUtil } from '../sdkUtil'; import { game } from './game'; import { DirType, model_content_item_data } from '../data'; import { config } from '../config'; import { imageCacheManager } from '../manager/imageCacheManager'; import { userDataManager } from '../manager/userDataManager'; const { ccclass, property } = _decorator; @ccclass('car') export class car extends Component { private offsetY:number = 0; private origin_pos_y:number = -300; private map:Node = null; private mDir:Vec3 = Vec3.ZERO; private mOldDir:Vec3 = Vec3.ZERO; private mGameOldDir:Vec3 = Vec3.ZERO; private mDis:number = 0; private isAction:boolean = false; private speed:number = 700; @property(CircleCollider2D) car_collider:CircleCollider2D = null; @property(Node) lab_hp:Node = null; private hp:number = 0 private scores:number = 0 private over_cb = null; private update_scores_cb = null; private game_manager:game = null; private buff_xi_status:boolean = false; private buff_dun_status:boolean = false; private buff_xing_status:boolean = false; private speed_bei:number = 1; @property(Number) move_speed_y:Number = 700; @property(Number) move_speed_x:Number = 700; @property(Boolean) is_open_jiaozheng:Boolean = true; private carDir:DirType = DirType.NONE private canvas_width:number = 1080; private isStopY:boolean = false private _curRingPos_1: Vec3 = new Vec3();//当前圆圈坐标 @property(Node) spr:Node = null public init(m:Node,cb,cb1,g:game){ this.spr.getComponent(Sprite).spriteFrame = imageCacheManager.getGameCarImageById(userDataManager.user_car_list.default_car_id) this.origin_pos_y = this.node.position.y this.game_manager = g this.over_cb = cb this.update_scores_cb = cb1 this.map = m; this.isAction = false; this.mOldDir = this.node.position this.buff_xi_status = false; this.buff_dun_status = false; this.buff_xing_status = false; this.isStopY = false; this.canvas_width = View.instance.getVisibleSize().width // let cd = this.node.getComponent(BoxCollider2D) // cd.off(Contact2DType.BEGIN_CONTACT) // cd.on(Contact2DType.BEGIN_CONTACT,this.onColliderCoin.bind(this)) // cd.off(Contact2DType.END_CONTACT) // cd.on(Contact2DType.END_CONTACT,this.onColliderCoin.bind(this)) // cd.off(Contact2DType.PRE_SOLVE) // cd.on(Contact2DType.PRE_SOLVE,this.onColliderWall.bind(this)) // this.car_collider.off(Contact2DType.BEGIN_CONTACT) // this.car_collider.on(Contact2DType.BEGIN_CONTACT,this.onColliderCoin.bind(this)) // this.car_collider.off(Contact2DType.END_CONTACT) // this.car_collider.on(Contact2DType.END_CONTACT,this.onColliderCoin.bind(this)) // this.car_collider.off(Contact2DType.POST_SOLVE) // this.car_collider.on(Contact2DType.POST_SOLVE,(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null)=>{ // // console.log("this.car_collider",otherCollider.node.name) // if(otherCollider.node.name.endsWith("fku")){ // if(otherCollider.node.parent.getComponent(box)!=null){ // otherCollider.node.parent.getComponent(box).hurt(this.shake.bind(this),this.buff_dun_status) // } // } // if(otherCollider.node.name.endsWith("wall")){ // // this.node.getComponent(RigidBody2D).linearVelocity = new Vec2(this.move_speed_x.valueOf(),this.move_speed_y.valueOf()) // } // },this) this.move_speed_y = tools.game_config.car_origin_speed this.hp = tools.game_config.car_init_num // this.hp = 100000 this.scores = 0 this.updateHp() this.updateScores() this.node.getComponent(RigidBody2D).gravityScale = -3 this.node.getComponent(RigidBody2D).linearDamping = 1 // this.schedule(this.check.bind(this),0.01) // circle.radius = tools.game_config.buff_xi_range // circle.radius = 400 // circle.off(Contact2DType.BEGIN_CONTACT) // circle.on(Contact2DType.BEGIN_CONTACT,(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null)=>{ // if(otherCollider.node.name.endsWith("coin")){ // if(otherCollider.node.getComponent(coin)!=null){ // otherCollider.node.getComponent(coin).moveToTarget(otherCollider.node.parent.getComponent(UITransform).convertToNodeSpaceAR(this.node.position)) // this.hp+=tools.randomCoin() // this.updateHp() // } // } // }) this.unschedule(this.addSpeed) this.schedule(this.addSpeed.bind(this),tools.game_config.car_add_speed_time) } addSpeed(){ let speed = this.move_speed_y.valueOf()+(tools.game_config.car_add_speed_num) if(speed{ // // this.move_speed_y = 0 // }) // tween(this.node).delay(0.1).call(()=>{ // // this.isAction = false; // // this.node.position = new Vec3(this.node.position.x, this.node.position.y+8) // // this.node.getComponent(RigidBody2D).wakeUp() // // this.node.getComponent(RigidBody2D).linearVelocity = new Vec2(this.move_speed_x.valueOf(),30) // }).start() Tween.stopAllByTarget(this.lab_hp) tween(this.lab_hp).delay(0.02).call(()=>{ this.isStopY = false}).start() if(this.hp<0){ Tween.stopAllByTarget(this.node) // this.node.getComponent(RigidBody2D).gravityScale = 0 // this.node.getComponent(RigidBody2D).linearDamping = 0 this.over_cb() }else{ if(data!=null){ switch (data.buff_type) { case config.buff_type.dun: // console.log("获取buff 盾") this.buff_dun() break; case config.buff_type.xing: // console.log("获取buff 星") this.buff_xing() break; case config.buff_type.xi: // console.log("获取buff 吸") this.buff_xi() break; } } } } public buff_xi(){ this.buff_xi_status = true; this.addSpeedNum(tools.game_config.buff_xi_add_speed) this.unschedule(this.cancelScheduleBuffXi) director.once(Director.EVENT_AFTER_DRAW,()=>{ this.scheduleOnce(this.cancelScheduleBuffXi.bind(this),tools.game_config.buff_xi) this.game_manager.getBuffShowView().showBuffXi() }) } public cancelScheduleBuffXi(){ this.buff_xi_status = false this.game_manager.getBuffShowView().hideBuffXi() } public buff_xing(){ this.buff_xing_status = true; this.addSpeedNum(tools.game_config.buff_xi_add_speed) this.unschedule(this.cancelScheduleBuffXi) director.once(Director.EVENT_AFTER_DRAW,()=>{ this.scheduleOnce(this.cancelScheduleBuffXing.bind(this),tools.game_config.buff_xing) this.game_manager.getBuffShowView().showBuffXing() }) } public cancelScheduleBuffXing(){ this.buff_xing_status = false this.game_manager.getBuffShowView().hideBuffXing() } public buff_dun(){ this.buff_dun_status = true; this.addSpeedNum(tools.game_config.buff_dun_add_speed) this.unschedule(this.cancelScheduleBuffDun) director.once(Director.EVENT_AFTER_DRAW,()=>{ this.scheduleOnce(this.cancelScheduleBuffDun.bind(this),tools.game_config.buff_dun) this.game_manager.getBuffShowView().showBuffDun() }) } public cancelScheduleBuffDun(){ this.buff_dun_status = false this.game_manager.getBuffShowView().hideBuffDun() } public setMoveSpeedX(speed_x:number){ // this.move_speed_x = speed_x } check(){ } protected update(dt: number): void { if(this.map!=null&&this.hp>=0){ let x = 0 let y = this.isStopY?this.node.position.y:this.node.position.y+this.move_speed_y.valueOf()*dt; let self_pos = new Vec3(this.node.position.x,this.node.position.y) let dis = Math.abs(self_pos.x-this.mDir.x) if(dis>1){ // console.log("dis",this.move_speed_x) x = self_pos.subtract(this.mDir).normalize().x*this.move_speed_x.valueOf()*dt*this.speed_bei; } x = this.node.position.x-x; let size = this.node.getComponent(UITransform).contentSize; let rect = new Rect(x-size.width*0.5,y-size.height*0.5,size.width,size.height) // let rect = this.node.getComponent(UITransform).getBoundingBox() let list = this.game_manager.carCollider(rect) if(list.length>0){ for (let index = 0; index < list.length; index++) { const element = list[index]; if(element.collider_dir==config.collider_dir.top){ y = this.node.position.y if(element.type==0){ this.onColliderBox(element.node) }else if(element.type==1){ x-=element.moveOffset // console.log("carCollider wall",element) } }else{ // x = this.node.position.x if(element.type==2){ this.onColliderCoin(element.node) } } } } let left = this.canvas_width*0.5*-1+90*0.5; let right = this.canvas_width*0.5-90*0.5; if(this.carDir==DirType.LEFT){ let left_x = this.game_manager.getLeftObstacles(new Rect(this.node.position.x-size.width*0.5,this.node.position.y-size.height*0.5,size.width,size.height)) // console.log("left_x",left_x) if(left_x!=null&&xright_x){ x = right_x } if(x>=right){ x = this.node.position.x } } // this._curRingPos_1.set(0, 0, 0); // Vec3.lerp(this._curRingPos_1, this.node.position, new Vec3(x,y), 50 * dt); // this.node.setPosition(this._curRingPos_1); this.node.setPosition(x,y) this.offsetY =Math.abs(this.origin_pos_y-y) let dir = new Vec3(this.map.position.x,-this.offsetY,this.map.position.z) this.map.position = dir } } public setDir(dir:DirType){ // if(this.carDir!=dir){ // this.node.getComponent(RigidBody2D).linearVelocity = new Vec2(0,this.move_speed_y.valueOf()) // } this.carDir = dir } public setDisDir(dis:number){ this.speed_bei = dis / 10 if(this.carDir==DirType.LEFT){ this.mDir = new Vec3(this.node.position.x-dis,this.node.position.y) } if(this.carDir==DirType.RIGHT){ this.mDir = new Vec3(this.node.position.x+dis,this.node.position.y) } } public setDis(dis:number){ this.setSpeed(new Vec3(dis)) // this.mDis = dis } public getDis(){ return this.mDis } public getCarDir(){ return this.carDir } public setSpeed(dir:Vec3){ // let dis = Math.abs(this.node.position.x - dir.x) // if(dis>200){ // dis = 500 // } // this.mDis = dis; let dis = Math.abs(this.mOldDir.x - dir.x) // let is_char = false // if(this.carDir===DirType.LEFT){ // if(dir.x>this.node.position.x){ // is_char = true // } // }else if(this.carDir===DirType.RIGHT){ // if(dir.xthis.node.position.x){ // dis = Math.abs(this.mGameOldDir.x - dir.x) // } // }else if(this.carDir===DirType.RIGHT){ // if(dir.x100&&dis<=200){ // // this.node.getComponent(RigidBody2D).linearVelocity = new Vec2(20*x,30) // // }else{ // // this.node.getComponent(RigidBody2D).linearVelocity = new Vec2(dis/200*35*x,25) // // } // // this.node.position = new Vec3(this.node.position.x+x*this.speed*dt,this.node.position.y,this.node.position.z) // } // }else{ // if(this.node.getComponent(RigidBody2D).linearVelocity.x!=0){ // this.node.getComponent(RigidBody2D).linearVelocity = new Vec2(0,this.move_speed_y.valueOf()) // } // } } // protected update(dt: number): void { // this.node.position = new Vec3(this.node.position.x,this.node.position.y+dt*500,this.node.position.z) // this.map.position = new Vec3(this.map.position.x,this.map.position.y-dt*500,this.map.position.z) // } }