123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- import { _decorator, CircleCollider2D, Component, Director, director, Node, RigidBody2D, Size, Sprite, tween, UITransform, Vec2, Vec3 } from 'cc';
- import { model_content_item_data } from '../data';
- import { car } from './car';
- import { tools } from '../tools';
- import { base } from './base';
- const { ccclass, property } = _decorator;
- @ccclass('coin')
- export class coin extends base {
- private mCar:car = null;
- private isMove:boolean = false;
- public init(data:model_content_item_data,car:car){
- this.mCar = car
- this.mData = data
- this.node.position = new Vec3(this.mData.x,this.mData.y,this.node.position.z)
- this.node.getComponent(UITransform).contentSize = new Size(this.mData.w,this.mData.h)
- this.node.getComponent(CircleCollider2D).radius = this.mData.w*0.5
- this.unscheduleAllCallbacks()
- this.schedule(this.on_update,0.2)
- this.getComponent(RigidBody2D).wakeUp()
- }
- protected ReSetNode(){
- this.node.position = Vec3.ZERO
- this.isMove = false
- }
- public removeSelf(){
- let self = this;
- director.once(Director.EVENT_AFTER_DRAW,()=>{
- self.mCar.getGame().removeCoinForList(this.node)
- self.mCar.getGame().getPoolManager().putCoin(this.node)
- })
- }
- public moveToTarget(target_pos:Vec3){
- // this.node.getComponent(CircleCollider2D).enabled = false
- this.node.getComponent(RigidBody2D).sleep()
- tween(this.node).to(0.2,{position:target_pos}).call(this.removeSelf.bind(this)).start()
- }
- protected on_update(dt: number): void {
- if(this.mCar.getXiStatus()&&!this.isMove){
- let selfPos = this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
- let dis = Vec3.distance(this.mCar.node.position, this.mCar.node.parent.getComponent(UITransform).convertToNodeSpaceAR(selfPos))
- let carPos = this.mCar.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.mCar.node.position)
- if(dis<=tools.game_config.buff_xi_range){
- this.isMove = true
- this.unschedule(this.on_update)
- this.moveToTarget(this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(carPos))
- }
- }
- }
- }
|