wall.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { _decorator, BoxCollider2D, CircleCollider2D, Component, Label, Node, Size, Sprite, UITransform, Vec2, Vec3 } from 'cc';
  2. import { model_content_item_data } from '../data';
  3. import { base } from './base';
  4. import { car } from './car';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('wall')
  7. export class wall extends base {
  8. private active_node:boolean = false
  9. private mCar:car = null;
  10. public init(data:model_content_item_data,car:car){
  11. this.mData = data
  12. this.active_node = false
  13. this.mCar = car
  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(BoxCollider2D).size = new Size(this.mData.w,this.mData.h)
  17. }
  18. protected ReSetNode(){
  19. this.node.position = Vec3.ZERO
  20. }
  21. getIsActive(){
  22. return this.active_node
  23. }
  24. getIsCanCollider(){
  25. let box_world=this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
  26. let p = this.mCar.node.parent.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  27. return Vec3.distance(p,this.mCar.node.position)<500
  28. }
  29. updateBoxShowStatus(){
  30. this.node.getComponent(Sprite).enabled = this.active_node
  31. }
  32. protected lateUpdate(dt: number): void {
  33. if(this.mCar!=null){
  34. let box_world=this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
  35. let p = this.mCar.node.parent.getComponent(UITransform).convertToNodeSpaceAR(box_world)
  36. if(p.y<(this.mCar.node.position.y+1500)&&p.y>this.mCar.node.position.y&&!this.active_node){
  37. this.active_node = true
  38. }
  39. if(p.y<(this.mCar.node.position.y-1000)&&this.active_node){
  40. this.active_node = false
  41. }
  42. this.updateBoxShowStatus()
  43. }
  44. }
  45. }