ShopView.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. import { ui } from "../ui/layaMaxUI";
  2. import { ViewManager, ViewType } from "./ViewManager";
  3. import ShopItem from "../Tools/ShopItem";
  4. import GuangGao from "../Net/GuangGao";
  5. import { ArcherWorrior, ServerManager } from "../Net/ServerManager";
  6. import { AccountManager } from "../Net/AccountManager";
  7. import { WXSDK } from "../Net/WXSDK";
  8. import { AccountUser } from "../Net/PlayerData";
  9. import GamePool from "../Tools/GamePool";
  10. import { AssetManager } from "../Tools/AssetManager";
  11. import ResDic from "../Tools/ResDic";
  12. import { GameManager } from "../Game/GameManager";
  13. export enum SkinGetForType {
  14. Geted = 1,//已经拥有
  15. Bycoin = 2,//通过金币
  16. GetByVideo = 3,//通过视频
  17. GetByActivity = 4,//通过活动
  18. Using = -1,//使用中
  19. }
  20. export default class ShopView extends ui.SkinViewUI{
  21. private static ins:ShopView;
  22. constructor(){
  23. super();
  24. ShopView.ins = this;
  25. }
  26. public static get Instance():ShopView{
  27. if(ShopView.ins == null){
  28. console.log("new shopview");
  29. return new ShopView();
  30. }
  31. return ShopView.ins
  32. }
  33. //首次打开皮肤界面
  34. firstopen:boolean = true;
  35. equip_Cap:number;
  36. equip_Horse:number;
  37. showroleId:number = 0;
  38. private uiArr = [];
  39. private CharacterLightImage:Laya.Image;
  40. private HorseLightImage:Laya.Image;
  41. private GetByCoin:Laya.Box;
  42. private RequireCoinNum:Laya.Text;
  43. private GetByVideo:Laya.Box;
  44. private RequireVideoNum:Laya.Text;
  45. private GetByActive:Laya.Box;
  46. private Geted:Laya.Box;
  47. private Using:Laya.Text;
  48. private skinGetForType:SkinGetForType;
  49. public _data:any;
  50. //帽子数据源
  51. private CapArray = [];
  52. //马数据源
  53. private HorseArray = [];
  54. Show(){
  55. WXSDK.AldEvent("打开皮肤商店界面")
  56. let data = AccountManager.Instance.curplayerData.GainCollocation("theme");
  57. for (let i = 0; i < data.length; i++) {
  58. if(i<5){
  59. this.CapArray.push(data[i]);
  60. }
  61. else{
  62. this.HorseArray.push(data[i]);
  63. }
  64. }
  65. this.BackBtn.clickHandler =new Laya.Handler(this,this.ClickClose);
  66. this.CharacterBtn.clickHandler =new Laya.Handler(this,this.ClickCap);
  67. this.HorseBtn.clickHandler =new Laya.Handler(this,this.ClickHorse);
  68. this.GetBtn.clickHandler =new Laya.Handler(this,this.ClickGet);
  69. this.VideoGetCoinBtn.clickHandler = new Laya.Handler(this,this.VideoGet);
  70. this.CharacterLightImage = this.CharacterBtn.getChildByName("light") as Laya.Image;
  71. this.HorseLightImage = this.HorseBtn.getChildByName("light") as Laya.Image;
  72. this.GetByCoin = this.GetBtn.getChildByName("GetByCoin") as Laya.Box;
  73. this.RequireCoinNum = this.GetByCoin.getChildByName("CoinNum") as Laya.Text;
  74. this.GetByVideo = this.GetBtn.getChildByName("GetByVideo") as Laya.Box;
  75. this.RequireVideoNum = this.GetByVideo.getChildByName("VideoNum") as Laya.Text;
  76. this.GetByActive = this.GetBtn.getChildByName("GetByActive") as Laya.Box;
  77. this.Geted = this.GetBtn.getChildByName("Geted") as Laya.Box;
  78. this.Using = this.GetBtn.getChildByName("Using") as Laya.Text;
  79. this.uiArr.push(this.Using);
  80. this.uiArr.push(this.Geted);
  81. this.uiArr.push(this.GetByCoin);
  82. this.uiArr.push(this.GetByVideo);
  83. this.uiArr.push(this.GetByActive);
  84. this.equip_Horse = AccountManager.Instance.curplayerData.GetUser(AccountUser.equip_themes)["2"];
  85. this.equip_Cap = AccountManager.Instance.curplayerData.GetUser(AccountUser.equip_themes)["1"];
  86. this.showroleId = 1;
  87. this.ItemList.selectEnable = false;
  88. this.ItemList.vScrollBarSkin = "";
  89. this.ItemList.renderHandler = Laya.Handler.create(this, this.UpdateItem, null, false);
  90. this.ItemList.array = this.CapArray;
  91. this.ItemList.refresh();
  92. this.ShowButton(1);
  93. this.height = Laya.stage.height;
  94. Laya.stage.on(Laya.Event.RESIZE,this,()=>
  95. {
  96. this.height = Laya.stage.height;
  97. })
  98. }
  99. Close(){
  100. WXSDK.AldEvent("关闭皮肤商城界面");
  101. }
  102. ClickClose(){
  103. var equiphorseid = Number(AccountManager.Instance.curplayerData.GetUser(AccountUser.equip_themes)["2"]);
  104. var equipcap = Number(AccountManager.Instance.curplayerData.GetUser(AccountUser.equip_themes)["1"]);
  105. if(equipcap){
  106. if(equipcap != this.equip_Cap){
  107. GameManager.Instance.SetCap(equipcap);
  108. }
  109. }
  110. if(equiphorseid != this.equip_Horse){
  111. GameManager.Instance.SetHorse(equiphorseid);
  112. }
  113. ViewManager.Instance.ShowView(ViewType.MainView);
  114. }
  115. UpdateItem(cell: Laya.Box, index: number) {
  116. let item = cell.getComponent(ShopItem) as ShopItem;
  117. item.RefreshItem();
  118. }
  119. RefreshItem(){
  120. this.ItemList.refresh();
  121. }
  122. //点击帽子选项
  123. ClickCap(){
  124. this.firstopen = true;
  125. if(this.equip_Cap){
  126. this.showroleId = this.equip_Cap;
  127. }
  128. else{
  129. this.showroleId = 1;
  130. }
  131. this.ShowButton(1);
  132. this.CharacterLightImage.visible = true;
  133. this.HorseLightImage.visible = false;
  134. this.ItemList.vScrollBarSkin = "";
  135. this.ItemList.array = this.CapArray;
  136. this.ItemList.renderHandler = Laya.Handler.create(this,this.UpdateItem,null,false);
  137. }
  138. //点击马选项
  139. ClickHorse(){
  140. this.firstopen = true;
  141. this.showroleId = this.equip_Horse;
  142. this.ShowButton(2);
  143. this.CharacterLightImage.visible = false;
  144. this.HorseLightImage.visible = true;
  145. this.ItemList.vScrollBarSkin = "";
  146. this.ItemList.array = this.HorseArray;
  147. this.ItemList.renderHandler = Laya.Handler.create(this,this.UpdateItem,null,false);
  148. }
  149. RefreshButton(_skinGetForType: SkinGetForType) {
  150. this.skinGetForType = _skinGetForType;
  151. this.ShowUI(this.skinGetForType);
  152. }
  153. ShowButton(type:number){//1帽子 2.马
  154. if(type == 1){
  155. var equipcap = Number(AccountManager.Instance.curplayerData.GetUser(AccountUser.equip_themes)["1"]);
  156. var usingId = equipcap;
  157. let item = this.CapArray[0].require_type;
  158. this._data = this.CapArray[0];
  159. var _id = this._data.id;
  160. var hasequip = AccountManager.Instance.curplayerData.HasEquip(_id);
  161. var require_type = item;
  162. if (usingId == _id ) {
  163. this.skinGetForType = SkinGetForType.Using;
  164. this.RefreshButton(this.skinGetForType);
  165. return;
  166. }
  167. if (hasequip) {
  168. this.skinGetForType = SkinGetForType.Geted;
  169. this.RefreshButton(this.skinGetForType);
  170. return;
  171. }
  172. if (require_type == 2) {
  173. this.skinGetForType = SkinGetForType.Bycoin;
  174. this.RefreshButton(this.skinGetForType);
  175. return;
  176. }
  177. if (require_type == 4 ) {
  178. this.skinGetForType = SkinGetForType.GetByActivity;
  179. this.RefreshButton(this.skinGetForType);
  180. return;
  181. }
  182. if (require_type == 3) {
  183. this.skinGetForType = SkinGetForType.GetByVideo;
  184. this.RefreshButton(this.skinGetForType);
  185. return;
  186. }
  187. // if(equipcap){
  188. // this.GetByCoin.visible = false;
  189. // this.GetByVideo.visible = false;
  190. // this.GetByActive.visible = false;
  191. // this.Geted.visible = false;
  192. // this.Using.visible = true;
  193. // }
  194. // else{
  195. // switch (item) {
  196. // case 2:
  197. // this.skinGetForType = SkinGetForType.Bycoin;
  198. // this.RefreshButton(this.skinGetForType);
  199. // break;
  200. // case 3:
  201. // this.skinGetForType = SkinGetForType.GetByVideo;
  202. // this.RefreshButton(this.skinGetForType);
  203. // break;
  204. // }
  205. // this.GetByCoin.visible = false;
  206. // this.GetByVideo.visible = false;
  207. // this.GetByActive.visible = false;
  208. // this.Geted.visible = false;
  209. // this.Using.visible = false;
  210. // }
  211. }
  212. else{
  213. var equiphorse = Number(AccountManager.Instance.curplayerData.GetUser(AccountUser.equip_themes)["2"]);
  214. var usingId = equiphorse;
  215. let item = this.HorseArray[0].require_type;
  216. this._data = this.HorseArray[0];
  217. var _id = this._data.id;
  218. var hasequip = AccountManager.Instance.curplayerData.HasEquip(_id);
  219. var require_type = item;
  220. if (usingId == _id ) {
  221. this.skinGetForType = SkinGetForType.Using;
  222. this.RefreshButton(this.skinGetForType);
  223. return;
  224. }
  225. if (hasequip) {
  226. this.skinGetForType = SkinGetForType.Geted;
  227. this.RefreshButton(this.skinGetForType);
  228. return;
  229. }
  230. if (require_type == 2) {
  231. this.skinGetForType = SkinGetForType.Bycoin;
  232. this.RefreshButton(this.skinGetForType);
  233. return;
  234. }
  235. if (require_type == 4 ) {
  236. this.skinGetForType = SkinGetForType.GetByActivity;
  237. this.RefreshButton(this.skinGetForType);
  238. return;
  239. }
  240. if (require_type == 3) {
  241. this.skinGetForType = SkinGetForType.GetByVideo;
  242. this.RefreshButton(this.skinGetForType);
  243. return;
  244. }
  245. }
  246. }
  247. //点击获得按钮
  248. ClickGet() {
  249. switch (this.skinGetForType) {
  250. case SkinGetForType.Bycoin:
  251. var money = Number(AccountManager.Instance.curplayerData.GainAccount("money"));
  252. if (money < Number(this._data.require_num)) {
  253. WXSDK.ShowTxt("金币不足--")
  254. }
  255. else {
  256. this.Buy();
  257. }
  258. console.log("金币--")
  259. break;
  260. case SkinGetForType.GetByActivity:
  261. WXSDK.ShowTxt("暂未开放!")
  262. console.log("活动获取--")
  263. break;
  264. case SkinGetForType.GetByVideo:
  265. console.log("视频获取--")
  266. this.VideoGet();
  267. break;
  268. case SkinGetForType.Geted:
  269. // console.log("已经拥有--")
  270. this.Use();
  271. break;
  272. }
  273. }
  274. ShowUI(_type: SkinGetForType) {
  275. console.log("所需类型--", _type);
  276. if(_type == SkinGetForType.Using){
  277. for (let index = 0; index < this.uiArr.length; index++) {
  278. if(index == 0){
  279. this.uiArr[index].visible = true;
  280. }
  281. else{
  282. this.uiArr[index].visible = false;
  283. }
  284. }
  285. return
  286. }
  287. this.Using.visible = false;
  288. for (let index = 1; index < this.uiArr.length; index++) {
  289. if (index == _type) {
  290. this.uiArr[index].visible = true;
  291. } else {
  292. this.uiArr[index].visible = false;
  293. }
  294. }
  295. // if(_type == SkinGetForType.Geted)return
  296. switch (_type) {
  297. case SkinGetForType.Bycoin:
  298. this.RequireCoinNum.text = this._data.require_num.toString();
  299. break;
  300. case SkinGetForType.GetByVideo:
  301. this.RequireVideoNum.text = AccountManager.Instance.curplayerData.GetProgress(this._data.id) + "/" + this._data.require_num.toString();
  302. break;
  303. }
  304. }
  305. //视频获取
  306. VideoGet() {
  307. GuangGao.Instance.loadVideo(isLoaded => {
  308. if (isLoaded) {
  309. GuangGao.Instance.showVideo(isOk => {
  310. if (isOk) {
  311. WXSDK.AldEvent("皮肤界面看视频得金币成功");
  312. var getkey = ArcherWorrior.vd;
  313. let param = [{ "reward_item": "1000" }, { "reward_num": "200" }, { "t": AccountManager.Instance.ServerTime.toString() }];
  314. ServerManager.Instance.SendHttp(getkey, param, Laya.Handler.create(this, this.VideoGetSuccess), Laya.Handler.create(this, this.VideoGetFail));
  315. }
  316. });
  317. }
  318. else {
  319. WXSDK.fengxiangtupian(isOk => {
  320. if (isOk) {
  321. WXSDK.AldEvent("皮肤界面看视频得金币成功");
  322. var getkey = ArcherWorrior.vd;
  323. let param = [{ "reward_item": "1000" }, { "reward_num": "200" }, { "t": AccountManager.Instance.ServerTime.toString() }];
  324. ServerManager.Instance.SendHttp(getkey, param, Laya.Handler.create(this, this.VideoGetSuccess), Laya.Handler.create(this, this.VideoGetFail));
  325. }
  326. });
  327. }
  328. })
  329. }
  330. //视频获取成功
  331. VideoGetSuccess(_data) {
  332. this.RefreshItem();
  333. console.log("LookVideo->suc:" + JSON.stringify(_data));
  334. }
  335. //视频获取失败
  336. VideoGetFail(_data) {
  337. console.log("LookVideo->fail:" + JSON.stringify(_data));
  338. }
  339. //使用装备
  340. Use() {
  341. var agrs = [{ "id": this._data.id }];
  342. ServerManager.Instance.SendHttp(ArcherWorrior.WearEquip, agrs, Laya.Handler.create(this, this.UseSuccess), Laya.Handler.create(this, this.UseFail));
  343. }
  344. //使用成功
  345. UseSuccess(data) {
  346. this.RefreshItem();
  347. // this.ShowButton();
  348. this.RefreshButton(SkinGetForType.Using);
  349. WXSDK.ShowTxt("装备成功!");
  350. console.log("装备suc:" + JSON.stringify(data));
  351. }
  352. //使用失败
  353. UseFail(data) {
  354. console.log(data);
  355. // console.log("装备err:" + JSON.stringify(data));
  356. }
  357. //购买
  358. Buy() {
  359. var agrs = [{ "id": this._data.id.toString() }];
  360. // var agrs = [{ "id": this._data.id }];
  361. ServerManager.Instance.SendHttp(ArcherWorrior.BuyEquip, agrs, Laya.Handler.create(this, this.BuySuccess), Laya.Handler.create(this, this.BuyFail));
  362. }
  363. //购买成功
  364. BuySuccess(data) {
  365. WXSDK.AldEvent("购买"+this._data.name+"成功");
  366. this.RefreshItem();
  367. this.RefreshButton(SkinGetForType.Using);
  368. // this.ShowUI(SkinGetForType.Geted);
  369. console.log("购买suc:" + JSON.stringify(data));
  370. }
  371. //购买失败
  372. BuyFail(data) {
  373. console.log("购买fail:" + JSON.stringify(data));
  374. // this.instance.RefreshItem();
  375. }
  376. }
  377. // 展示人物皮肤界面scene类
  378. export class ShopScene {
  379. private static ins: ShopScene;
  380. constructor() { ShopScene.ins = this; }
  381. public static get Instance(): ShopScene {
  382. if (ShopScene.ins) {
  383. return ShopScene.ins;
  384. }
  385. else {
  386. return new ShopScene();
  387. }
  388. }
  389. public first: boolean = true;
  390. public _shopscene: Laya.Scene3D;
  391. public _player: Laya.Sprite3D;
  392. public _motor: Laya.Sprite3D;
  393. public _camera: Laya.Camera;
  394. public _position: Laya.Vector3;
  395. public _sprite: Laya.Sprite3D;
  396. public _motorsprite: Laya.Sprite3D;
  397. public motordic: ResDic = new ResDic();
  398. public playernode: Laya.Sprite3D;
  399. public motornode: Laya.Sprite3D;
  400. public CreateMoto(_motorindex: number, _image?: Laya.Image,trial = null) {//type1:moto
  401. if (!this._shopscene || this._shopscene.destroyed || this._shopscene == null) {
  402. this.first = true;
  403. this._shopscene = new Laya.Scene3D();
  404. this._camera = new Laya.Camera();
  405. if(trial){
  406. this._camera.transform.position = new Laya.Vector3(0, 0.7, 3);
  407. this._camera.clearFlag = 2;
  408. this._camera.fieldOfView = 40;
  409. }
  410. else{
  411. this._camera.transform.position = new Laya.Vector3(0, 0.6, 3.5);
  412. this._camera.clearFlag = 2;
  413. this._camera.fieldOfView = 30;
  414. }
  415. this._shopscene.addChild(this._camera);
  416. let _light = new Laya.DirectionLight();
  417. _light.intensity = 0.5;
  418. this._shopscene.addChild(_light);
  419. //拿到玩家空节点
  420. var player = GamePool.Instance.GetModel(AssetManager.Instance.Horse.get(_motorindex), "");
  421. // this._sprite =Laya.Sprite3D.instantiate(player);
  422. this._shopscene.addChild(player);
  423. _image.addChild(this._shopscene);
  424. player.transform.position = new Laya.Vector3(0, 0, -2);
  425. this._position = new Laya.Vector3(0, 0.01, 0);
  426. }
  427. if (_image) {
  428. this._camera.viewport = new Laya.Viewport(_image.x, _image.y, _image.width, _image.height);
  429. }
  430. if (this._motor) {
  431. this._motor.active = false;
  432. this._motor.removeSelf();
  433. this._motor = null;
  434. }
  435. // var go;
  436. // if (this.motordic.has(_motorindex)) {
  437. // go = this.motordic.get(_motorindex);
  438. // go.active = true;
  439. // } else {
  440. // //////需更改
  441. // var motormodel = GamePools.Instance.GetModel(AssetManager.Instance.Motor.get(_motorindex), "");
  442. // motormodel.transform.position = new Laya.Vector3(0, 0, 0);
  443. // var jiasu = motormodel.getChildAt(0). getChildByName("putongjiasu") as Laya.Sprite3D;
  444. // var chaojijiasu = motormodel.getChildAt(0).getChildByName("chaojijiasu") as Laya.Sprite3D;
  445. // var suduxian = motormodel.getChildAt(0).getChildByName("speedline") as Laya.Sprite3D;
  446. // jiasu.active = false;
  447. // chaojijiasu.active = false;
  448. // suduxian.active = false;
  449. // go = Sprite3D.instantiate(motormodel);
  450. // this.motordic.set(_motorindex, go);
  451. // }
  452. // this._motor = go
  453. // if (this._motor) {
  454. // this.motornode.addChild(this._motor);
  455. // }
  456. if (!this.first) return;
  457. this.first = false;
  458. Laya.timer.frameLoop(1, this, this.Rotation,[player]);
  459. }
  460. Rotation(model:Laya.Sprite3D) {
  461. // if (!this._sprite) return;
  462. // if(this._sprite.destroyed)return;
  463. model.transform.rotate(this._position);
  464. }
  465. CloseShop() {
  466. Laya.timer.clear(this, this.Rotation);
  467. if (this._motor) {
  468. this._motor.active = false;
  469. this._motor.removeSelf();
  470. }
  471. if (this._player) {
  472. this._player.active = false;
  473. this._player.removeSelf();
  474. }
  475. if (this._shopscene) {
  476. this._shopscene.destroy(true);
  477. }
  478. this._shopscene = null;
  479. this._motor = null;
  480. this._sprite = null;
  481. this._player = null;
  482. this.first = true;
  483. }
  484. }