RoleList.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { _decorator, Component, instantiate, Node, Sprite, UITransform, Vec2 } from 'cc';
  2. import { resMgr } from '../../Frames/ResourcesMgr';
  3. import { RoleCard } from './RoleCard';
  4. import { RoleData } from '../../DataItem/ItemData';
  5. import { dataMgr } from '../../Frames/DataManager';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('RoleList')
  8. export class RoleList extends Component {
  9. _content: Node = null;
  10. private _roleData: RoleData[] = null;
  11. private _characterSlot: Node = null;
  12. roleImgName: string = null;
  13. roleCardState: boolean = null;
  14. protected onLoad() {
  15. this._roleData = dataMgr.getAllDataByName("RoleCardData");
  16. this._content = this.node.getChildByPath("view/content");
  17. this._characterSlot = this.node.parent.getChildByPath('SelectTroops_Bottom/CharacterSlot');
  18. }
  19. protected start(): void {
  20. this._roleData.forEach((values, index) => {
  21. this._createRoleCard(index);
  22. })
  23. }
  24. //创建角色卡
  25. private _createRoleCard(id: number) {
  26. const roleCard: Node = instantiate(resMgr.getPrefab("RoleCard-Big"));
  27. roleCard.parent = this._content;
  28. roleCard.getComponent(RoleCard).init(id, this._roleData);
  29. }
  30. //打开阴影遮罩
  31. openShadow(pos: Vec2) {
  32. for (const roleCard of this._content.children) {
  33. const box = roleCard.getComponent(UITransform).getBoundingBoxToWorld();
  34. if (box.contains(pos)) {
  35. //获取 RoleList 节点下的子节点 content 的子节点的脚本
  36. const roleCardTS = roleCard.getComponent(RoleCard);
  37. //如果Shadow(阴影遮罩)是关闭的
  38. if (!roleCardTS.getShadowState()) {
  39. //遍历角色卡槽的子节点
  40. for (const card of this._characterSlot.children) {
  41. let sprite = card.getChildByName("Sprite").getComponent(Sprite);
  42. let label = card.getChildByName("Label");
  43. if(label.active){
  44. return;
  45. }
  46. //如果角色卡槽没有角色图片 以及 "解锁卡槽" 节点的关闭的
  47. if (!sprite.spriteFrame && !label.active) {
  48. //打开Shadow
  49. roleCardTS.isOpenShadow(true);
  50. sprite.spriteFrame = resMgr.getSpriteFrame(roleCardTS.getRoleImgName());
  51. return;
  52. }
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }