FDialogBox.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import i18n from "../../../../i18n/i18n";
  2. import FDialogAttr, { DialogAttrContent } from "./FDialogAttr";
  3. /**
  4. * 战斗中对话框
  5. */
  6. const {ccclass, property} = cc._decorator;
  7. /**
  8. * 对话内容
  9. */
  10. export interface DialogContent{
  11. name:string,
  12. name1:string,
  13. icon:string,
  14. content:string
  15. }
  16. @ccclass
  17. export default class FDialogBox extends cc.Component {
  18. @property(cc.Label)
  19. mName: cc.Label = null;
  20. @property(cc.Label)
  21. mName1: cc.Label = null;
  22. @property(cc.Label)
  23. mContent: cc.Label = null;
  24. @property(cc.Sprite)
  25. mIcon: cc.Sprite = null;
  26. @property(cc.Node)
  27. mDialogNode: cc.Node = null;//对话框节点
  28. @property(cc.Node)
  29. mAttrNode: cc.Node = null;//属性选择节点
  30. @property(cc.Node)
  31. mAttrContent: cc.Node = null;
  32. @property(cc.Prefab)
  33. mAttrPerfab: cc.Prefab = null;
  34. /**
  35. * 下标
  36. */
  37. private tmpDialogIndex = 0;
  38. /**
  39. * 临时的对话信息
  40. */
  41. private tmpDialogs:Array<DialogContent> = null;
  42. /**
  43. * 对话的回调
  44. */
  45. private callback:()=>void;
  46. /**
  47. * 选择属性的回调
  48. */
  49. private attrCallback:(type:DialogAttrContent)=>void;
  50. public onclick(){
  51. this.tmpDialogIndex ++;
  52. if(this.tmpDialogs && this.tmpDialogIndex < this.tmpDialogs.length){
  53. let data = this.tmpDialogs[this.tmpDialogIndex];
  54. this.setDialog(data);
  55. }else{
  56. if(this.callback){
  57. this.callback();
  58. this.callback = null;
  59. }
  60. }
  61. }
  62. public setCallback(callback:()=>void){
  63. this.callback = callback;
  64. }
  65. public setData(tmpDialogs:Array<DialogContent>){
  66. this.mDialogNode.active = true;
  67. this.mAttrNode.active = false;
  68. this.tmpDialogIndex = 0;
  69. this.tmpDialogs = tmpDialogs;
  70. let data = this.tmpDialogs[0];
  71. this.setDialog(data);
  72. }
  73. /**
  74. * 设置对话内容
  75. * @param data
  76. */
  77. private setDialog(data:DialogContent){
  78. this.mName.string = i18n.t(data.name)
  79. this.mName1.string = i18n.t(data.name1)
  80. this.mContent.string = i18n.t(data.content)
  81. cc.resources.load('icon/npc/'+data.icon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
  82. if(err){
  83. cc.error(err);
  84. }else{
  85. this.mIcon.spriteFrame = spriteFrame;
  86. }
  87. } );
  88. }
  89. public setAttrCallback(callback:(data)=>void){
  90. this.attrCallback = callback;
  91. }
  92. public setAttr(datas:Array<DialogAttrContent>){
  93. this.mDialogNode.active = false;
  94. this.mAttrNode.active = true;
  95. for (let i = 0; i < datas.length; i++) {
  96. const element = datas[i];
  97. let node = cc.instantiate(this.mAttrPerfab);
  98. node.parent = this.mAttrContent;
  99. let attr = node.getComponent(FDialogAttr);
  100. attr.setVaule(element);
  101. attr.setCallback((da:FDialogAttr)=>{
  102. if(this.attrCallback){
  103. this.attrCallback(da.attr);
  104. }
  105. });
  106. }
  107. }
  108. }