123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- import { AnimationComponent, ButtonComponent, director, EventTouch, find, instantiate, LabelComponent, LayoutComponent, loader, ModelComponent, Node, Prefab, Quat, SpriteComponent, SpriteFrame, SystemEventType, Touch, Vec2, view, _decorator } from 'cc';
- import BufferManager from '../../../engines/buffers/BufferManager';
- import IBuffer from '../../../engines/buffers/IBuffer';
- import { GUIManager } from '../../../engines/gui/GUIManager';
- import { GUIMediator } from '../../../engines/gui/GUIMediator';
- import { DataModelEventType } from '../../../engines/models/DataModelEventType';
- import { NoticeManager } from '../../../engines/notices/NoticeManager';
- import { SoundManager } from '../../../engines/sounds/SoundManager';
- import StringUtils from '../../../engines/utils/StringUtils';
- import { LiangLiangSDK } from '../../../libs/liangliangSDK';
- import { PlatformManager } from '../../../Platform/PlatformManager';
- import { branchIdType } from '../../../Platform/WeChat/branchIdType';
- import { WeChatPlatform } from '../../../Platform/WeChat/WeChatPlatform';
- import GameConfigManager from '../../models/GameConfigManager';
- import { GameModel } from '../../models/GameModel';
- import { GamePropertys } from '../../models/GamePropertys';
- import { UIConst } from '../UIConst';
- import { WeaponCellListView } from './WeaponCellListView';
- const { ccclass, property } = _decorator;
- @ccclass('PrepareMediator')
- export class PrepareMediator extends GUIMediator {
- @property({
- type:AnimationComponent
- })
- DropBoxAnimation:AnimationComponent=null;
- @property({
- type: Node
- })
- HandTip: Node = null;
- @property({
- type: Node
- })
- bufferNode: Node = null;
- @property({
- type: LabelComponent
- })
- glodLabel: LabelComponent = null;
- @property({
- type: LabelComponent
- })
- glodLabel1: LabelComponent = null;
- @property({
- type: LabelComponent
- })
- diamondLabel: LabelComponent = null;
- @property({
- type: LabelComponent
- })
- gunNameLabel: LabelComponent = null;
- @property({
- type: LabelComponent
- })
- levelLabel: LabelComponent = null;
- @property({
- type: LabelComponent
- })
- weaponInfoLabel: LabelComponent = null;
- /**
- * 商城按钮
- */
- @property({
- type: ButtonComponent
- })
- shopButton: ButtonComponent = null;
- /**
- * 自动合成文本
- */
- @property({
- type: LabelComponent
- })
- autoSynthesisLabel: LabelComponent = null;
- /**
- * 快捷购买武器按钮
- */
- @property({
- type: ButtonComponent
- })
- quickBuyButton: ButtonComponent = null;
- /**
- * 快捷购买武器ICON
- */
- @property({
- type: SpriteComponent
- })
- quickBuyWeaponIcon: SpriteComponent = null;
- /**
- * 快捷购买武器价格
- */
- @property({
- type: LabelComponent
- })
- quickBuyWeaponPriceLabel: LabelComponent = null;
- /**
- * 武器拖拽Icon
- */
- @property({
- type: SpriteComponent
- })
- weaponDragIcon: SpriteComponent = null;
- /**
- * 删除按钮节点(用于拖拽判断)
- */
- @property({
- type: Node
- })
- deleteWeaponNode: Node = null;
- /**
- * 装配武器节点(用于拖拽判断)
- */
- @property({
- type: Node
- })
- equipWeaponNode: Node = null;
- /**
- * 装配格子预制体
- */
- @property({
- type: Prefab
- })
- WeaponCellPrefab: Prefab = null;
- /**
- * 武器列表
- */
- @property({
- type: LayoutComponent
- })
- weaponList: LayoutComponent = null;
- private modelView: ModelComponent;
- private modelPrefab: Prefab;
- private prefabInstance: Node;
- private prefabModelComponent: ModelComponent;
- private weaponCellListView: WeaponCellListView;
- private isNew:boolean=true;
- private get isNewPlayer():boolean{
- return this.isNew&&GameModel.single.isNewPlayer;
- }
- onLoad(): void {
- this.weaponCellListView = new WeaponCellListView(this);
- }
- OnShow(data?: any): void {
- //清空试用栅栏
- GameModel.single.trialFenceId=-1;
- GameModel.single.trialWeaponId=-1;
- GameModel.single.useSuperWeapon = false;
- super.OnShow(data);
- this.RefreshGunModel();
- this.RefreshGlod();
- this.RefreshDiamond();
- this.RefreshLevel();
- this.RefreshQuickBuy();
- this.AddEvents();
- this.weaponCellListView.OnShow();
- SoundManager.single.PlayMusic("sounds/main");
- if(this.isNewPlayer){
- this.HandTip.active=true;
- }else{
- this.HandTip.active=false;
- }
- if(LiangLiangSDK.CanWuChu() == true){
- GUIManager.single.Show(UIConst.ADVERTISING_UI)
- // PlatformManager.showBanner();
- }else{
- PlatformManager.showBanner();
- }
- //签到如果有奖励的话
- if(GameModel.single.GetSignAwardIndex()>=0){
- GUIManager.single.Show(UIConst.SIGN_UI);
- // this.HideSelf();
- }
- }
- /**
- * 初始化枪的模型
- */
- private RefreshGunModel(): void {
- if (this.modelView == null) {
- let modelNode: Node = find("ModelView", this.node);
- this.modelView = modelNode.getComponent(ModelComponent);
- }
- if (this.modelPrefab) {
- this.modelView.mesh = null;
- let target=this.modelView.materials[0];
- target.setProperty("mainTexture", null);
- this.prefabInstance.destroy();
- this.prefabInstance=null;
- let deps:string[]=loader.getDependsRecursively(this.modelPrefab);
- loader.release(deps);
- this.modelPrefab=null;
- }
- //武器为空
- if(GameModel.single.currentWeaponId<0){
- this.modelView.node.active = false;
- this.gunNameLabel.string="";
- this.weaponInfoLabel.string="";
- return;
- }else{
- this.modelView.node.active = true;
- let weaponConfig: any = GameConfigManager.GetWeaponConfig(GameModel.single.currentWeaponId);
- loader.loadRes(weaponConfig.prefab, Prefab, (err, prefab) => {
- if (err) {
- console.error("加载武器出错");
- }
- this.modelPrefab = prefab;
- this.prefabInstance = instantiate(prefab);
- this.prefabModelComponent = this.prefabInstance.getComponentInChildren(ModelComponent);
- //更换贴图
- let source = this.prefabModelComponent.materials[0];
- let target = this.modelView.materials[0];
- let texture = source.getProperty("mainTexture");
- target.setProperty("mainTexture", texture);
- //更换模型
- this.modelView.mesh = this.prefabModelComponent.mesh;
- });
- if (this.gunNameLabel != null) {
- this.gunNameLabel.string = weaponConfig.name;
- }
- //描述
- this.weaponInfoLabel.string=weaponConfig.desc;
- }
-
- }
- OnHide(): void {
- this.RemoveEvents();
- this.modelView.mesh = null;
- let target=this.modelView.materials[0];
- target.setProperty("mainTexture", null);
- if (this.prefabInstance) {
- this.prefabInstance.destroy();
- }
- if (this.modelPrefab != null) {
- var deps = loader.getDependsRecursively(this.modelPrefab);
- loader.release(deps);
- }
- this.modelView.node.active = false;
- this.weaponCellListView.onHide();
- }
- private AddEvents(): void {
- this.equipWeaponNode.on(Node.EventType.TOUCH_START, this.TouchStartHandler, this);
- this.equipWeaponNode.on(Node.EventType.TOUCH_MOVE, this.TouchMoveHandler, this);
- this.equipWeaponNode.on(Node.EventType.TOUCH_END, this.TouchEndHandler, this);
- GameModel.single.AddEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged, 0);
- this.quickBuyButton.node.on(ButtonComponent.EventType.CLICK, this.QickBuyButtonClickHandler, this);
- this.shopButton.node.on(ButtonComponent.EventType.CLICK, this.ShopButtonClickHandler, this);
- }
- private RemoveEvents(): void {
- this.equipWeaponNode.off(SystemEventType.TOUCH_START, this.TouchStartHandler, this);
- this.equipWeaponNode.off(SystemEventType.TOUCH_MOVE, this.TouchMoveHandler, this);
- this.equipWeaponNode.off(SystemEventType.TOUCH_END, this.TouchEndHandler, this);
- GameModel.single.RemoveEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged);
- this.quickBuyButton.node.on(ButtonComponent.EventType.CLICK, this.QickBuyButtonClickHandler, this);
- }
- private GameModelPropertyChanged(key: string): void {
- switch (key) {
- case GamePropertys.gold:
- this.CallNextFrame(this.RefreshGlod.bind(this));
- this.CallNextFrame(this.RefreshQuickBuy.bind(this));
- break;
- case GamePropertys.diamond:
- this.CallNextFrame(this.RefreshDiamond.bind(this));
- break;
- case GamePropertys.currentWeaponCellId:
- this.CallNextFrame(this.RefreshGunModel.bind(this));
- break;
- case GamePropertys.currentLevel:
- this.CallNextFrame(this.RefreshLevel.bind(this));
- break;
- case GamePropertys.synthesisMaxWeaponId:
- this.CallNextFrame(this.RefreshQuickBuy.bind(this));
- break;
- case GamePropertys.WeaponCell:
- this.HandTip.active=false;
- this.isNew=false;
- break;
- }
- }
- private RefreshLevel(): void {
- this.levelLabel.string = "第" + GameModel.single.currentLevel.toString() + "关";
- }
- private RefreshGlod(): void {
- if (this.glodLabel != null) {
- this.glodLabel.string = StringUtils.numberUtilsEn(GameModel.single.gold);
- }
- this.glodLabel1.string = StringUtils.numberUtilsEn(GameModel.single.fullEarnings) + "/秒";
- }
- private RefreshDiamond(): void {
- if (this.diamondLabel != null) {
- this.diamondLabel.string = GameModel.single.diamond.toString();
- }
- }
- private weaponIcon: SpriteFrame;
- /**
- * 刷新快速购买按钮
- */
- private RefreshQuickBuy(): void {
- this.quickBuyWeaponIcon.spriteFrame = this.weaponIcon;
- //价格
- let price: number = GameModel.single.GetQuickBuyPrice();
- // if(GameModel.single.gold<price){
- // this.quickBuyWeaponPriceLabel.color.set(Color.RED);
- // }else{
- // this.quickBuyWeaponPriceLabel.color.set(Color.WHITE);
- // }
- this.quickBuyWeaponPriceLabel.string = StringUtils.numberUtilsEn(GameModel.single.GetQuickBuyPrice());
- let weaponConfig: any = GameConfigManager.GetWeaponConfig(GameModel.single.CurrentQuickBuyWeaponId);
- loader.loadRes(weaponConfig.icon + "/spriteFrame", SpriteFrame, (err: Error, asset: SpriteFrame) => {
- if (err != null) {
- console.error("加载武器ICON出错!");
- }
- this.quickBuyWeaponIcon.spriteFrame = this.weaponIcon = asset;
- })
- }
- /**
- * 购买武器
- */
- private QickBuyButtonClickHandler(): void {
- if (GameModel.single.FindWeaponEmptyCell() == null) {
- NoticeManager.ShowPrompt("没有武器槽位了!");
- return;
- }
- //价格
- let price: number = GameModel.single.GetQuickBuyPrice();
- //钱不够
- if (GameModel.single.gold < price) {
- NoticeManager.ShowPrompt("金币不足");
- return;
- }
- GameModel.single.BuyWeapon(0, GameModel.single.CurrentQuickBuyWeaponId);
- }
- private ShopButtonClickHandler(): void {
- GUIManager.single.Show(UIConst.SHOP_UI);
- this.HideSelf();
- }
- /**
- * 自动合成按钮点击
- */
- AutoSynthesisButtonClickHandler():void{
- GUIManager.single.Show(UIConst.AUTO_SYNTHETIC_UI);
- this.HideSelf();
- }
- /**
- * 加速Buffer;
- */
- AccelerateButtonClickHandler():void{
- GUIManager.single.Show(UIConst.ACCELERATE_UI);
- this.HideSelf();
- }
- /**
- * 兑换界面
- */
- ExchangeButtonClickHandler():void{
- GUIManager.single.Show(UIConst.EXCHANGE_UI);
- this.HideSelf();
- }
- /**
- * 签到
- */
- SignButtonClickHandler():void{
- GUIManager.single.Show(UIConst.SIGN_UI);
- }
- private startRotation: Quat = new Quat();
- private currentRotation: Quat = new Quat();
- private startPoint: Vec2 = new Vec2();
- private currentPoint: Vec2 = new Vec2();
- private TouchStartHandler(touch: Touch, touchEvent): void {
- let pos: Vec2 = touch.getUILocation();
- console.log(pos);
- this.startPoint.set(pos.x, pos.y);
- this.startRotation.x = this.modelView.node.rotation.x;
- this.startRotation.set(this.modelView.node.rotation.x, this.modelView.node.rotation.y, this.modelView.node.rotation.z, this.modelView.node.rotation.w);
- }
- private TouchMoveHandler(touch: EventTouch, touchEvent): void {
- let pos: Vec2 = touch.getUILocation();
- this.currentPoint.set(pos.x, pos.y);
- let dis: number = Vec2.distance(this.currentPoint, this.startPoint);
- let value: number = dis / view.getCanvasSize().width;
- if (this.currentPoint.x < this.startPoint.x) {
- value = -value;
- }
- Quat.rotateY(this.currentRotation, this.startRotation, value);
- Quat.normalize(this.currentRotation, this.currentRotation);
- this.modelView.node.setRotation(this.currentRotation);
- }
- private TouchEndHandler(...arg): void {
- console.log("拖拽模型结束");
- }
- StartGame(): void {
- if(GameModel.single.currentWeaponCell==null||GameModel.single.currentWeaponId<0){
- NoticeManager.ShowPrompt("请装配武器后再试!");
- return;
- }
- if(LiangLiangSDK.CanWuChu()){
- GUIManager.single.Show(UIConst.FREE_USE_GUN_UI);
- }else{
- GUIManager.single.Show(UIConst.FREE_FENCE_UI);
- }
- }
- /**
- * 免费金币
- */
- getAdGold(): void {
- PlatformManager.showRewardedVideo(()=>{
- GameModel.single.gold += GameModel.single.fullEarnings * 600;
- NoticeManager.ShowPrompt(`获得${StringUtils.numberUtilsEn(GameModel.single.fullEarnings * 600)}金币`);
- let weChat = PlatformManager.impl as WeChatPlatform;
- if(weChat instanceof WeChatPlatform){
- weChat.branchAnalytics(branchIdType.FreeGold)
- }
- },()=>{
- NoticeManager.ShowPrompt(`未看完广告,无奖励`);
- })
- }
- /**
- * 心跳
- * @param dt
- */
- update(dt:number):void{
- super.update(dt);
- //空投
- let currentTime:number=director.getCurrentTime();
- if(currentTime-GameModel.single.lastDropBoxTime>30000){
- this.DropBoxAnimation.node.active=true;
- this.DropBoxAnimation.play("KongtouTip2");
- GameModel.single.lastDropBoxTime=currentTime;
- }
- //自动合成Buffer剩余时间
- let buffers:IBuffer[]=BufferManager.GetBufferGroup("AutoSynthesis");
- if(buffers!=null&&buffers.length>0){
- let buffer:IBuffer=buffers[0];
- let endTime:number=buffer.GetTimeRemaining();
- this.autoSynthesisLabel.string=StringUtils.TimeFormatting(endTime,":",":",":",":",":","");
- }else{
- this.autoSynthesisLabel.string="自动合成";
- }
- //加速Buffer
- buffers=BufferManager.GetBufferGroup("Accelerate");
- if(buffers!=null&&buffers.length>0){
- this.bufferNode.active=true;
- }else{
- this.bufferNode.active=false;
- }
- }
- /**
- * 获取空投
- */
- GetDropBox():void{
- this.DropBoxAnimation.node.active=false;
- GUIManager.single.Show(UIConst.DROP_BOX_UI,false);
- this.HideSelf();
- }
- }
|