GroupController.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import EventType from "./EventType";
  2. import Boy from "./Boy";
  3. const {ccclass, property} = cc._decorator;
  4. @ccclass
  5. export default class GroupController extends cc.Component {
  6. boyGroup:cc.Node;
  7. boxGroup:cc.Node;
  8. boxs:cc.Node[];
  9. gearGroup:cc.Node;
  10. bigSaw:cc.Node;
  11. trackGroup:cc.Node;
  12. tracks:cc.Node[];
  13. bloodGroup:cc.Node;
  14. lightGroup:cc.Node;
  15. lights:cc.Node[];
  16. lightDirection:number = 1;
  17. lightAngle:number = 0;
  18. lightSpeed:number = 10;
  19. lightMaxAngle:number = 25;
  20. onLoad(){
  21. window.gcr = this;
  22. this.boyGroup = this.node.getChildByName('BoyGroup');
  23. this.boxGroup = this.node.getChildByName('BoxGroup');
  24. this.boxs = this.boxGroup.children;
  25. this.gearGroup = this.node.getChildByName('GearGroup');
  26. this.bigSaw = this.gearGroup.getChildByName('BigSaw');
  27. this.bigSaw.addComponent(BigSaw);
  28. this.trackGroup = this.node.getChildByName('TrackGroup');
  29. this.tracks = this.trackGroup.children;
  30. this.tracks.forEach((track:cc.Node)=>{
  31. if(track.name.indexOf('Left')>-1){
  32. track.getChildByName('Root').addComponent(Track).setDirection(-1);
  33. }else if(track.name.indexOf('Right')>-1){
  34. track.getChildByName('Root').addComponent(Track).setDirection(1);
  35. }
  36. })
  37. this.bloodGroup = this.node.getChildByName('BloodGroup');
  38. this.lightGroup = this.node.getChildByName('LightGroup');
  39. this.lights = this.lightGroup.children;
  40. }
  41. update(dt: number) {
  42. //light animate
  43. if(this.lightAngle<-this.lightMaxAngle){
  44. this.lightDirection = 1;
  45. }else if(this.lightAngle>this.lightMaxAngle){
  46. this.lightDirection = -1;
  47. }
  48. this.lightAngle += dt*this.lightSpeed*this.lightDirection;
  49. this.lights.forEach((light:cc.Node)=>{
  50. light.angle = this.lightAngle;
  51. });
  52. }
  53. }
  54. class BigSaw extends cc.Component{
  55. onCollisionEnter(other:cc.Collider){
  56. if(other.node.group==EventType.GROUP_BOY){
  57. let boy = other.node.getComponent(Boy);
  58. if(boy&&boy.isValid){
  59. if (boy.isRunOnHost()) boy.uploadFrame_beKilled(100)
  60. }
  61. }
  62. }
  63. }
  64. class Track extends cc.Component{
  65. acc:number = 3000;
  66. direction:number = 0;
  67. setDirection(direction:number){
  68. this.direction = direction;
  69. }
  70. onCollisionEnter(other:cc.Collider){
  71. if(other.node.group==EventType.GROUP_BOY){
  72. let boy = other.node.getComponent(Boy);
  73. if(boy&&boy.isValid){
  74. if (boy.isRunOnHost()) boy.addAcc(cc.v2(this.direction*this.acc,0));
  75. }
  76. }
  77. }
  78. onCollisionExit(other:cc.Collider){
  79. if(other.node.group==EventType.GROUP_BOY){
  80. let boy = other.node.getComponent(Boy);
  81. if(boy&&boy.isValid){
  82. if (boy.isRunOnHost()) boy.subAcc(cc.v2(this.direction*this.acc,0));
  83. }
  84. }
  85. }
  86. }