coin.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { _decorator, CircleCollider2D, Component, Director, director, Node, RigidBody2D, Size, Sprite, tween, UITransform, Vec2, Vec3 } from 'cc';
  2. import { model_content_item_data } from '../data';
  3. import { car } from './car';
  4. import { tools } from '../tools';
  5. import { base } from './base';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('coin')
  8. export class coin extends base {
  9. private mCar:car = null;
  10. private isMove:boolean = false;
  11. public init(data:model_content_item_data,car:car){
  12. this.mCar = car
  13. this.mData = data
  14. this.node.position = new Vec3(this.mData.x,this.mData.y,this.node.position.z)
  15. this.node.getComponent(UITransform).contentSize = new Size(this.mData.w,this.mData.h)
  16. this.node.getComponent(CircleCollider2D).radius = this.mData.w*0.5
  17. this.unscheduleAllCallbacks()
  18. this.schedule(this.on_update,0.2)
  19. this.getComponent(RigidBody2D).wakeUp()
  20. }
  21. protected ReSetNode(){
  22. this.node.position = Vec3.ZERO
  23. this.isMove = false
  24. }
  25. public removeSelf(){
  26. let self = this;
  27. director.once(Director.EVENT_AFTER_DRAW,()=>{
  28. self.mCar.getGame().removeCoinForList(this.node)
  29. self.mCar.getGame().getPoolManager().putCoin(this.node)
  30. })
  31. }
  32. public moveToTarget(target_pos:Vec3){
  33. // this.node.getComponent(CircleCollider2D).enabled = false
  34. this.node.getComponent(RigidBody2D).sleep()
  35. tween(this.node).to(0.2,{position:target_pos}).call(this.removeSelf.bind(this)).start()
  36. }
  37. protected on_update(dt: number): void {
  38. if(this.mCar.getXiStatus()&&!this.isMove){
  39. let selfPos = this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
  40. let dis = Vec3.distance(this.mCar.node.position, this.mCar.node.parent.getComponent(UITransform).convertToNodeSpaceAR(selfPos))
  41. let carPos = this.mCar.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.mCar.node.position)
  42. if(dis<=tools.game_config.buff_xi_range){
  43. this.isMove = true
  44. this.unschedule(this.on_update)
  45. this.moveToTarget(this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(carPos))
  46. }
  47. }
  48. }
  49. }