Card.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { _decorator, Animation, Component, Label, Node, Sprite } from 'cc';
  2. import { resMgr } from '../../Frames/ResourcesMgr';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('Card')
  5. export class Card extends Component {
  6. private _lock: Node = null;
  7. private _label: Node = null;
  8. private _sprite: Node = null;
  9. private _shadow: Node = null;
  10. private _oreCount: Node = null;
  11. private _consume: Node | null = null
  12. consumeCount: number = null;
  13. //是否可以点击
  14. clickable: boolean = true;
  15. //是否禁用
  16. disabled: boolean = false;
  17. private _shadow001: Node = null;
  18. protected onLoad(): void {
  19. this._lock = this.node.getChildByName("Lock");
  20. this._label = this.node.getChildByName("Label");
  21. this._sprite = this.node.getChildByName("Sprite");
  22. this._shadow = this.node.getChildByName("Shadow");
  23. this._consume = this.node.getChildByName("Count");
  24. this._shadow001 = this.node.getChildByName("Shadow-001");
  25. this._oreCount = this.node.parent.parent.getChildByPath("OreSpeed/_oreSpeed");
  26. }
  27. start() {
  28. //这个Card可以使用的条件:disabled === false; clickable === true
  29. const consumeCount = this._consume?.getComponent(Label);
  30. this.consumeCount = Number(consumeCount?.string);
  31. }
  32. protected update(dt: number): void {
  33. const strOreCount: string = this._oreCount.getComponent(Label).string;
  34. if (this.consumeCount) {
  35. if (Number(strOreCount) <= this.consumeCount) {
  36. //开启阴影
  37. this._shadow.active = true;
  38. //不可点击
  39. this.clickable = false;
  40. } else {
  41. //阴影关闭
  42. this._shadow.active = false;
  43. //可以点击
  44. this.clickable = true;
  45. }
  46. }
  47. }
  48. //冷却当中
  49. aniPlay() {
  50. this._shadow001.active = true;
  51. this.disabled = true;
  52. this._shadow001.getComponent(Animation).play();
  53. this._shadow001.getComponent(Animation).on(Animation.EventType.FINISHED,()=>{
  54. this.disabled = false;
  55. this._shadow001.active = false;
  56. })
  57. }
  58. aniStart() {
  59. //如何没被禁用,则动画开始播放的时候,开始禁用
  60. this.disabled = true;
  61. }
  62. aniEnd() {
  63. //如何被禁用,则动画结束播放的时候,不禁用
  64. this.disabled = false;
  65. this._shadow001.active = false;
  66. }
  67. setLock(b: boolean) {
  68. this._lock.active = b;
  69. }
  70. setlabel(b: boolean) {
  71. this._label.active = b;
  72. }
  73. setSprite(name: string) {
  74. this._sprite.getComponent(Sprite).spriteFrame = resMgr.getSpriteFrame(name);
  75. }
  76. }