games/FullFire/assets/scripts/ui/SelectSceneViewCtr.ts

249 lines
8.5 KiB
TypeScript

import ViewBase from "./ViewBase";
import LDataCannonManager from "../datas/LDataCannonManager";
import CannonManager from "../manager/CannonManager";
import CannonCtr from "../CannonCtr";
import UIManager from "../manager/UIManager";
import Define from "../common/Define";
import Common from "../common/Common";
import SelectCannonItem from "../SelectCannonItem";
import UserInfo from "../UserInfo";
import WXHelper from "../common/WXHelper";
import GameManager from "../manager/GameManager";
import LDataCannon from "../datas/LDataCannon";
import SelectSceneItem from "../SelectSceneItem";
import LDataSceneManager from "../datas/LDataSceneManager";
import ResManager from "../manager/ResManager";
import LDataScene from "../datas/LDataScene";
import {TrackingManager, TrackingType} from "../Tracking/TrackingManager";
// Learn TypeScript:
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
const {ccclass, property} = cc._decorator;
class ScrollInfo{
index:number;
data:LDataScene;
offsetX:number;
distanceX:number;
constructor(index:number,offsetX:number,data:LDataScene){
this.index = index;
this.offsetX = offsetX;
this.data = data;
}
}
@ccclass
export default class SelectCannonViewCtr extends ViewBase {
@property(cc.ScrollView)
scrollView: cc.ScrollView = null;
@property(cc.Node)
nodeContent: cc.Node = null;
@property(cc.Node)
eventContent: cc.Node = null;
@property(cc.Node)
btn_back: cc.Node = null;
@property(cc.Node)
btn_video: cc.Node = null;
@property(cc.Node)
btn_use: cc.Node = null;
@property(cc.Node)
btn_share: cc.Node = null;
@property(cc.Node)
node_item: cc.Node = null;
maxCount:number = 0;
curCount:number = 0;
scrollInfoArr:ScrollInfo[] = [];
itemArr:SelectSceneItem[] = [];
isCanAutoScroll:boolean = false
scrollState:number = 0;
curSelectIndex:number;
isClickShare:boolean = false;
sceneId:number = 0;
refreshView(isFristRefresh:boolean = false){
this.isClickShare = false
this.scrollView.scrollTo(new cc.Vec2(0,150),0)
console.log("UserInfo.getCurSceneIndex() = " + UserInfo.getCurSceneIndex())
this.scrollToFitPos()
this.scrollToIndex(UserInfo.getCurSceneIndex(),UserInfo.getCurSceneIndex())
this.refreshItemState()
}
refreshItemState(){
for (let index = 0; index < this.itemArr.length; index++) {
this.itemArr[index].refreshItemInfoByData(LDataSceneManager.dataList[index]);
}
}
addEvent(){
Common.addClickEvent(this.eventContent,this.onClick.bind(this),false);
Common.addClickEvent(this.btn_back,this.onClick.bind(this));
Common.addClickEvent(this.btn_use,this.onClick.bind(this));
Common.addClickEvent(this.btn_video,this.onClick.bind(this));
Common.addClickEvent(this.btn_share,this.onClick.bind(this));
}
onClick(tag:string){
if(tag == "btn_back"){
UIManager.getInstance().showView(Define.viewMain)
UIManager.getInstance().showView(Define.viewUpgrade)
UIManager.getInstance().hideView(Define.viewSelectScene)
}else if(tag == "btn_use"){
UserInfo.setCurUseSceneId(this.scrollInfoArr[0].data.saveID)
GameManager.instance.changeSceneByIndex(UserInfo.getCurSceneIndex())
UIManager.getInstance().showView(Define.viewMain)
UIManager.getInstance().showView(Define.viewUpgrade)
UIManager.getInstance().hideView(Define.viewSelectScene)
}else if(tag == "btn_video"){
TrackingManager.send(TrackingType.RewardScene);
WXHelper.showVideo(function(state){
if(state == 1){
//播放成功奖励大炮
let data = this.scrollInfoArr[0].data
if(data != null && data.getmode == Define.cannonVideo){
UserInfo.addNewCannon(data.saveID,2);//获得一个新的场景
this.refreshButtonsState()
this.refreshItemState()
}
}
}.bind(this))
}else if(tag == "btn_share"){
GameManager.instance.wxHelper.shareAppMessage("")
this.isClickShare = true
}
}
initView(){
this.maxCount = LDataSceneManager.dataList.length
for (let index = 0; index < this.maxCount; index++) {
let itemNode = cc.instantiate(this.node_item)
itemNode.position = new cc.Vec2(index*213 + 320,-150)
itemNode.setParent(this.nodeContent)
this.scrollInfoArr.push(new ScrollInfo(index,itemNode.position.x,LDataSceneManager.GetDataById(index)))
let item:SelectSceneItem = itemNode.getComponent<SelectSceneItem>(SelectSceneItem);
this.itemArr.push(item);
}
this.createScene(this.curCount);
this.nodeContent.width = 213 * (this.maxCount + 2)
}
createScene(index){
ResManager.loadPrefab("prefabs/scenes/ui_scene"+LDataSceneManager.GetDataById(this.curCount).saveID,function(prefab){
let cannonNode = cc.instantiate(prefab);
cannonNode.setParent(this.nodeContent.children[index].children[1])
cannonNode.position = new cc.Vec2(0,112)
cannonNode.setScale(0.8)
this.curCount++
if(this.curCount < this.maxCount){
this.createScene(this.curCount)
}
}.bind(this));
}
//滑动到合适的位置
scrollToFitPos(){
let tempX = Math.abs(this.nodeContent.position.x)
console.log(tempX);
for (let index = 0; index < this.scrollInfoArr.length; index++) {
this.scrollInfoArr[index].distanceX = Math.abs(this.scrollInfoArr[index].offsetX - tempX)
}
this.scrollInfoArr.sort((a, b) => {
return a.distanceX - b.distanceX;
});
let tempIndex = this.scrollInfoArr[0].index;
let state:number = UserInfo.getCannnonState(this.scrollInfoArr[0].data.saveID);
if(state == 1){
UserInfo.addNewCannon(this.scrollInfoArr[0].data.saveID,2) //这里更改查看过的状态
this.refreshItemState()
}
this.scrollToIndex(tempIndex)
}
scrollToIndex(index,otherIndex:number = 0){
this.curSelectIndex = index
this.sceneId = this.scrollInfoArr[otherIndex].data.saveID
this.scrollView.scrollTo(new cc.Vec2(this.curSelectIndex/(LDataSceneManager.dataList.length - 1),0),1)
this.refreshButtonsState();
}
//刷新按键的状态
refreshButtonsState(){
this.btn_back.active = false
this.btn_use.active = false
this.btn_video.active = false
this.btn_share.active = false
if(UserInfo.isOwnCannonById(this.sceneId)){
if(UserInfo.curSceneId == this.sceneId){
this.btn_back.active = true
}else{
this.btn_use.active = true
}
}else{
let data = this.scrollInfoArr[0].data
if(data.getmode == Define.cannonVideo && WXHelper.isLoadVideoSuccessful){
this.btn_video.active = true
}else if(data.getmode == Define.cannonShare){
this.btn_share.active = true
}
else{
this.btn_back.active = true
}
}
}
update(dt){
if(this.scrollView.isScrolling()){
this.scrollState = 1
}else{
if(this.scrollState == 1){
this.scrollState = 0
this.scrollToFitPos()
}
}
}
executeEvent(eventTag:string = "defualt"){
let tempData:LDataScene = this.scrollInfoArr[0].data
if(tempData != null && tempData.getmode == Define.cannonShare){
UserInfo.addNewCannon(tempData.saveID,2);//获得一个新的大炮 分享获得大炮
this.refreshButtonsState()
this.refreshItemState()
}
}
}