123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- 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<tools.game_config.car_add_speed_max){
- this.move_speed_y=speed
- }
- }
- addSpeedNum(num:number){
- let speed = this.move_speed_y.valueOf()+(num*0.01*this.move_speed_y.valueOf())
- if(speed<tools.game_config.car_add_speed_max){
- this.move_speed_y=speed
- }
- }
- onColliderCoin(coin_node:Node){
- if(coin_node.getComponent(coin)!=null){
- coin_node.getComponent(coin).removeSelf()
- this.hp+=tools.randomCoin()
- this.updateHp()
- }
- }
- onColliderBox(box_node:Node){
- if(box_node.getComponent(box)!=null){
- box_node.getComponent(box).hurt(this.shake.bind(this),this.buff_dun_status)
- }
- }
-
-
- public getIsOpenJZ():boolean{
- return this.is_open_jiaozheng.valueOf()
- }
- public getGame(){
- return this.game_manager
- }
- public getXiStatus(){
- return this.buff_xi_status
- }
- private updateScores(){
- if(this.update_scores_cb!=null){
- this.update_scores_cb(this.scores)
- }
- }
- private updateHp(){
- this.lab_hp.getComponent(Label).string = this.hp+""
- }
- public getScores(){
- return this.scores
- }
- private shake(data:model_content_item_data,isKill:boolean,scores:number){
- // Tween.stopAllByTarget(this.node)
- this.hp-=1;
- this.updateHp()
- // this.isAction = true;
- if(isKill){
- this.scores += ( scores*(this.buff_xing_status?2:1) )
- }else{
- this.scores+=( 1*(this.buff_xing_status?2:1) );
- }
- // this.move_speed_y = 0
- this.isStopY = true
- this.updateScores()
- // this.node.getComponent(RigidBody2D).sleep()
- SdkUtil.vibrateShort()
- // this.node.position = new Vec3(this.node.position.x, this.node.position.y-8)
- // this.node.getComponent(RigidBody2D).linearVelocity = new Vec2(0,-2.5)
- // director.once(Director.EVENT_AFTER_DRAW,()=>{
- // // 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&&x<left_x){
- x = left_x
- }
- if(x<=left){
- x = this.node.position.x
- }
- }
- if(this.carDir==DirType.RIGHT){
- let right_x = this.game_manager.getRightObstacles(new Rect(this.node.position.x-size.width*0.5,this.node.position.y-size.height*0.5,size.width,size.height))
- // console.log("right_x",right_x)
- if(right_x!=null&&x>right_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.x<this.node.position.x){
- // is_char = true
- // }
-
- // }
- // if(is_char){
- // dis = Math.abs(this.mGameOldDir.x - dir.x)
- // }else{
- // this.mGameOldDir = dir
- // }
-
- this.mOldDir = this.node.position
- // dis*=2
- // console.log("this.mDis",dis)
- this.mDis = dis;
- // if(this.mDir.x!=0){
- // //与目标位置的距离进行速度算法 - 距离越长 速度越快
- // if(!this.isAction){
- // let dis = Math.abs(this.mOldDir.x - dir.x)
- // if(this.carDir===DirType.LEFT){
- // if(dir.x>this.node.position.x){
- // dis = Math.abs(this.mGameOldDir.x - dir.x)
- // }
- // }else if(this.carDir===DirType.RIGHT){
- // if(dir.x<this.node.position.x){
- // dis = Math.abs(this.mGameOldDir.x - dir.x)
- // }
-
- // }
-
- // this.mDis = dis;
- // // if(dis<=100){
- // // this.node.getComponent(RigidBody2D).linearVelocity = new Vec2(10*x,30)
- // // }else if(dis>100&&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)
- // }
- }
|