优化若干代码
This commit is contained in:
@@ -1,38 +1,38 @@
|
||||
import { Canvas, error, log, ResolutionPolicy, sys, view, _decorator } from 'cc'
|
||||
import { _decorator, Canvas, error, log, view } from 'cc'
|
||||
import DialogBase from '../uicomponent/DialogBase'
|
||||
const { ccclass, property } = _decorator
|
||||
|
||||
import { Logger } from './Logger'
|
||||
const { ccclass, property } = _decorator
|
||||
|
||||
@ccclass('AdapterHelper')
|
||||
export default class AdapterHelper {
|
||||
public static winSizeWidth: number
|
||||
public static winSizeHeiht: number
|
||||
public static fixApdater() {
|
||||
log('v3.6没找到接口修改 fitHeight、fitWidth, 先在项目里写死fitHeight=true')
|
||||
return
|
||||
let framesize = view.getFrameSize()
|
||||
if (!this.winSizeWidth) {
|
||||
this.winSizeWidth = screen.width
|
||||
this.winSizeHeiht = screen.height
|
||||
}
|
||||
let designsize = view.getDesignResolutionSize()
|
||||
let canvas: Canvas = DialogBase.GetRootCanvas().getComponent(Canvas)
|
||||
public static winSizeWidth: number
|
||||
public static winSizeHeiht: number
|
||||
|
||||
let ratio: number = framesize.height / framesize.width
|
||||
let designRatio: number = designsize.height / designsize.width
|
||||
if (ratio > designRatio) {
|
||||
//canvas.fitHeight = false;
|
||||
//canvas.fitWidth = true;
|
||||
error(
|
||||
'v3.6没找到接口修改 fitHeight、fitWidth, 先在项目里写死fitHeight=true'
|
||||
)
|
||||
} else {
|
||||
//canvas.fitHeight = true;
|
||||
//canvas.fitWidth = false;
|
||||
error(
|
||||
'v3.6没找到接口修改 fitHeight、fitWidth, 先在项目里写死fitHeight=true'
|
||||
)
|
||||
}
|
||||
}
|
||||
public static fixApdater() {
|
||||
log('v3.6没找到接口修改 fitHeight、fitWidth, 先在项目里写死fitHeight=true')
|
||||
return
|
||||
let framesize = view.getFrameSize()
|
||||
if (!this.winSizeWidth) {
|
||||
this.winSizeWidth = screen.width
|
||||
this.winSizeHeiht = screen.height
|
||||
}
|
||||
let designsize = view.getDesignResolutionSize()
|
||||
let canvas: Canvas = DialogBase.GetRootCanvas().getComponent(Canvas)
|
||||
|
||||
let ratio: number = framesize.height / framesize.width
|
||||
let designRatio: number = designsize.height / designsize.width
|
||||
if (ratio > designRatio) {
|
||||
//canvas.fitHeight = false;
|
||||
//canvas.fitWidth = true;
|
||||
error(
|
||||
'v3.6没找到接口修改 fitHeight、fitWidth, 先在项目里写死fitHeight=true'
|
||||
)
|
||||
} else {
|
||||
//canvas.fitHeight = true;
|
||||
//canvas.fitWidth = false;
|
||||
error(
|
||||
'v3.6没找到接口修改 fitHeight、fitWidth, 先在项目里写死fitHeight=true'
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,52 +1,48 @@
|
||||
import { _decorator } from 'cc'
|
||||
const { ccclass, property } = _decorator
|
||||
|
||||
import { Logger } from './Logger'
|
||||
const { ccclass, property } = _decorator
|
||||
|
||||
@ccclass('BitUtil')
|
||||
export default class BitUtil {
|
||||
//index是二进制从右到左
|
||||
public static isBitSet(value: number, index: number): boolean {
|
||||
let str: string = value.toString(2)
|
||||
if (parseInt(str[str.length - 1 - index]) == 1) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
//index是二进制从右到左
|
||||
public static isBitSet(value: number, index: number): boolean {
|
||||
let str: string = value.toString(2)
|
||||
return parseInt(str[str.length - 1 - index]) == 1
|
||||
}
|
||||
|
||||
//从右到左计算
|
||||
public static setBitValue(value: number, index: number): number {
|
||||
let newValue: number = value
|
||||
let str: string = value.toString(2)
|
||||
let newStr: string = ''
|
||||
let maxIndex = Math.max(str.length - 1, index)
|
||||
for (let i = 0; i <= maxIndex; i++) {
|
||||
if (index == i) {
|
||||
newStr = '1' + newStr
|
||||
} else {
|
||||
if (str[i] == undefined) {
|
||||
newStr = '0' + newStr
|
||||
} else {
|
||||
newStr = str[i] + newStr
|
||||
}
|
||||
}
|
||||
}
|
||||
newValue = parseInt(newStr, 2)
|
||||
return newValue
|
||||
}
|
||||
//从右到左计算
|
||||
public static setBitValue(value: number, index: number): number {
|
||||
let newValue: number = value
|
||||
let str: string = value.toString(2)
|
||||
let newStr: string = ''
|
||||
let maxIndex = Math.max(str.length - 1, index)
|
||||
for (let i = 0; i <= maxIndex; i++) {
|
||||
if (index == i) {
|
||||
newStr = '1' + newStr
|
||||
} else {
|
||||
if (str[i] == undefined) {
|
||||
newStr = '0' + newStr
|
||||
} else {
|
||||
newStr = str[i] + newStr
|
||||
}
|
||||
}
|
||||
}
|
||||
newValue = parseInt(newStr, 2)
|
||||
return newValue
|
||||
}
|
||||
|
||||
public static clearBitValue(value: number, index: number) {
|
||||
let newValue: number = value
|
||||
let str: string = value.toString(2)
|
||||
let newStr: string = ''
|
||||
for (let i = str.length - 1; i >= 0; i--) {
|
||||
if (index == str.length - 1 - i) {
|
||||
newStr = '0' + newStr
|
||||
} else {
|
||||
newStr = str[i] + newStr
|
||||
}
|
||||
}
|
||||
newValue = parseInt(newStr, 2)
|
||||
return newValue
|
||||
}
|
||||
public static clearBitValue(value: number, index: number) {
|
||||
let newValue: number = value
|
||||
let str: string = value.toString(2)
|
||||
let newStr: string = ''
|
||||
for (let i = str.length - 1; i >= 0; i--) {
|
||||
if (index == str.length - 1 - i) {
|
||||
newStr = '0' + newStr
|
||||
} else {
|
||||
newStr = str[i] + newStr
|
||||
}
|
||||
}
|
||||
newValue = parseInt(newStr, 2)
|
||||
return newValue
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { _decorator, Color } from 'cc'
|
||||
|
||||
const { ccclass, property } = _decorator
|
||||
|
||||
@ccclass('ColorHelper')
|
||||
export default class ColorHelper {
|
||||
public static getColor(hexStr: string): Color {
|
||||
let color: Color = Color.BLACK
|
||||
return color.fromHEX(hexStr)
|
||||
}
|
||||
public static getColor(hexStr: string): Color {
|
||||
let color: Color = Color.BLACK
|
||||
return color.fromHEX(hexStr)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,139 +1,136 @@
|
||||
import { _decorator } from 'cc'
|
||||
import { Logger } from './Logger'
|
||||
|
||||
export default class DateUtil {
|
||||
public static formatNumStr(num: number) {
|
||||
let str = '' + num
|
||||
if (num < 10) {
|
||||
str = '0' + num
|
||||
}
|
||||
return str
|
||||
}
|
||||
public static formatNumStr(num: number) {
|
||||
let str = '' + num
|
||||
if (num < 10) {
|
||||
str = '0' + num
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
public static formateYearMonthDayStr(timestamp: number) {
|
||||
let date: Date = new Date(timestamp)
|
||||
return (
|
||||
date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
|
||||
)
|
||||
}
|
||||
public static formateYearMonthDayStr(timestamp: number) {
|
||||
let date: Date = new Date(timestamp)
|
||||
return (
|
||||
date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
|
||||
)
|
||||
}
|
||||
|
||||
public static formateMonthDayStr(timestamp: number) {
|
||||
let date: Date = new Date(timestamp)
|
||||
return date.getMonth() + 1 + '月' + date.getDate() + '日'
|
||||
}
|
||||
public static formateMonthDayStr(timestamp: number) {
|
||||
let date: Date = new Date(timestamp)
|
||||
return date.getMonth() + 1 + '月' + date.getDate() + '日'
|
||||
}
|
||||
|
||||
// timestamp:1453094034000 2018-1-31 19:53:44
|
||||
//根据时间戳返回 2018-1-31 19:53:44
|
||||
public static formatDateStr(timestamp: number) {
|
||||
let date: Date = new Date(timestamp)
|
||||
return (
|
||||
date.getFullYear() +
|
||||
'-' +
|
||||
(date.getMonth() + 1) +
|
||||
'-' +
|
||||
date.getDate() +
|
||||
' ' +
|
||||
this.formatNumStr(date.getHours()) +
|
||||
':' +
|
||||
this.formatNumStr(date.getMinutes()) +
|
||||
':' +
|
||||
this.formatNumStr(date.getSeconds())
|
||||
)
|
||||
}
|
||||
// timestamp:1453094034000 2018-1-31 19:53:44
|
||||
//根据时间戳返回 2018-1-31 19:53:44
|
||||
public static formatDateStr(timestamp: number) {
|
||||
let date: Date = new Date(timestamp)
|
||||
return (
|
||||
date.getFullYear() +
|
||||
'-' +
|
||||
(date.getMonth() + 1) +
|
||||
'-' +
|
||||
date.getDate() +
|
||||
' ' +
|
||||
this.formatNumStr(date.getHours()) +
|
||||
':' +
|
||||
this.formatNumStr(date.getMinutes()) +
|
||||
':' +
|
||||
this.formatNumStr(date.getSeconds())
|
||||
)
|
||||
}
|
||||
|
||||
// timestamp:1453094034000 2018-1-31-19-53-44
|
||||
//根据时间戳返回 2018-1-31-19-53-44
|
||||
public static formatDateStr2(timestamp: number) {
|
||||
let date: Date = new Date(timestamp)
|
||||
return (
|
||||
date.getFullYear() +
|
||||
'-' +
|
||||
(date.getMonth() + 1) +
|
||||
'-' +
|
||||
date.getDate() +
|
||||
'-' +
|
||||
this.formatNumStr(date.getHours()) +
|
||||
'-' +
|
||||
this.formatNumStr(date.getMinutes()) +
|
||||
'-' +
|
||||
this.formatNumStr(date.getSeconds())
|
||||
)
|
||||
}
|
||||
// timestamp:1453094034000 2018-1-31-19-53-44
|
||||
//根据时间戳返回 2018-1-31-19-53-44
|
||||
public static formatDateStr2(timestamp: number) {
|
||||
let date: Date = new Date(timestamp)
|
||||
return (
|
||||
date.getFullYear() +
|
||||
'-' +
|
||||
(date.getMonth() + 1) +
|
||||
'-' +
|
||||
date.getDate() +
|
||||
'-' +
|
||||
this.formatNumStr(date.getHours()) +
|
||||
'-' +
|
||||
this.formatNumStr(date.getMinutes()) +
|
||||
'-' +
|
||||
this.formatNumStr(date.getSeconds())
|
||||
)
|
||||
}
|
||||
|
||||
// timestamp:1453094034000 2018-1-31
|
||||
//根据时间戳返回 2018-1-31
|
||||
public static formatDateStr3(timestamp: number) {
|
||||
let date: Date = new Date(timestamp)
|
||||
return (
|
||||
date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
|
||||
)
|
||||
}
|
||||
// timestamp:1453094034000 2018-1-31
|
||||
//根据时间戳返回 2018-1-31
|
||||
public static formatDateStr3(timestamp: number) {
|
||||
let date: Date = new Date(timestamp)
|
||||
return (
|
||||
date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
|
||||
)
|
||||
}
|
||||
|
||||
// timestamp:1453094034000
|
||||
//根据时间戳返回 19:53
|
||||
public static formatHourMinStr(timestamp: number) {
|
||||
let date: Date = new Date(timestamp)
|
||||
return (
|
||||
this.formatNumStr(date.getHours()) +
|
||||
':' +
|
||||
this.formatNumStr(date.getMinutes())
|
||||
)
|
||||
}
|
||||
// timestamp:1453094034000
|
||||
//根据时间戳返回 19:53
|
||||
public static formatHourMinStr(timestamp: number) {
|
||||
let date: Date = new Date(timestamp)
|
||||
return (
|
||||
this.formatNumStr(date.getHours()) +
|
||||
':' +
|
||||
this.formatNumStr(date.getMinutes())
|
||||
)
|
||||
}
|
||||
|
||||
// timestamp:1453094034000
|
||||
//根据时间戳返回 19:53:11
|
||||
public static formatHourMinSecondStr(timestamp: number) {
|
||||
let date: Date = new Date(timestamp)
|
||||
return (
|
||||
this.formatNumStr(date.getHours()) +
|
||||
':' +
|
||||
this.formatNumStr(date.getMinutes()) +
|
||||
':' +
|
||||
this.formatNumStr(date.getSeconds())
|
||||
)
|
||||
}
|
||||
// timestamp:1453094034000
|
||||
//根据时间戳返回 19:53:11
|
||||
public static formatHourMinSecondStr(timestamp: number) {
|
||||
let date: Date = new Date(timestamp)
|
||||
return (
|
||||
this.formatNumStr(date.getHours()) +
|
||||
':' +
|
||||
this.formatNumStr(date.getMinutes()) +
|
||||
':' +
|
||||
this.formatNumStr(date.getSeconds())
|
||||
)
|
||||
}
|
||||
|
||||
public static now(): number {
|
||||
let date: Date = new Date()
|
||||
return date.getTime()
|
||||
}
|
||||
public static now(): number {
|
||||
let date: Date = new Date()
|
||||
return date.getTime()
|
||||
}
|
||||
|
||||
public static betweenTime(startTime: number, endTime: number) {
|
||||
let date: Date = new Date()
|
||||
if (date.getTime() >= startTime && date.getTime() <= endTime) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
public static betweenTime(startTime: number, endTime: number) {
|
||||
let date: Date = new Date()
|
||||
if (date.getTime() >= startTime && date.getTime() <= endTime) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
//根据时间戳返回 1天19:53:11
|
||||
public static formatLeftTime(timestamp: number) {
|
||||
let result: string = ''
|
||||
let day: number = Math.floor(timestamp / (1000 * 60 * 60 * 24))
|
||||
let hour: number = Math.floor(timestamp / (1000 * 60 * 60)) % 24
|
||||
let min: number = Math.floor(timestamp / (1000 * 60)) % 60
|
||||
let second: number = Math.floor(timestamp / 1000) % 60
|
||||
result =
|
||||
day +
|
||||
'天' +
|
||||
this.formatNumStr(hour) +
|
||||
':' +
|
||||
this.formatNumStr(min) +
|
||||
':' +
|
||||
this.formatNumStr(second)
|
||||
return result
|
||||
}
|
||||
//根据时间戳返回 1天19:53:11
|
||||
public static formatLeftTime(timestamp: number) {
|
||||
let result: string = ''
|
||||
let day: number = Math.floor(timestamp / (1000 * 60 * 60 * 24))
|
||||
let hour: number = Math.floor(timestamp / (1000 * 60 * 60)) % 24
|
||||
let min: number = Math.floor(timestamp / (1000 * 60)) % 60
|
||||
let second: number = Math.floor(timestamp / 1000) % 60
|
||||
result =
|
||||
day +
|
||||
'天' +
|
||||
this.formatNumStr(hour) +
|
||||
':' +
|
||||
this.formatNumStr(min) +
|
||||
':' +
|
||||
this.formatNumStr(second)
|
||||
return result
|
||||
}
|
||||
|
||||
public static isToday(dateTime: number): boolean {
|
||||
let nowDate: Date = new Date()
|
||||
let checkDate: Date = new Date(dateTime)
|
||||
if (
|
||||
checkDate.getFullYear() == nowDate.getFullYear() &&
|
||||
checkDate.getMonth() == nowDate.getMonth() &&
|
||||
checkDate.getDate() == nowDate.getDate()
|
||||
) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
public static isToday(dateTime: number): boolean {
|
||||
let nowDate: Date = new Date()
|
||||
let checkDate: Date = new Date(dateTime)
|
||||
if (
|
||||
checkDate.getFullYear() == nowDate.getFullYear() &&
|
||||
checkDate.getMonth() == nowDate.getMonth() &&
|
||||
checkDate.getDate() == nowDate.getDate()
|
||||
) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,131 +1,132 @@
|
||||
import { _decorator, Node, Color, Button, Component, Slider } from 'cc'
|
||||
import { Logger } from './Logger'
|
||||
import { Button, Color, Component, Node, Slider } from 'cc'
|
||||
import ColorHelper from './ColorHelper'
|
||||
|
||||
export class HaoEvent {
|
||||
public callback: Function
|
||||
public caller: any
|
||||
public isStop: boolean
|
||||
constructor(callback: Function, caller: any) {
|
||||
this.callback = callback
|
||||
this.caller = caller
|
||||
this.isStop = false
|
||||
}
|
||||
public callback: Function
|
||||
public caller: any
|
||||
public isStop: boolean
|
||||
|
||||
constructor(callback: Function, caller: any) {
|
||||
this.callback = callback
|
||||
this.caller = caller
|
||||
this.isStop = false
|
||||
}
|
||||
}
|
||||
|
||||
export default class EventManager {
|
||||
public static instance: EventManager = new EventManager()
|
||||
public static instance: EventManager = new EventManager()
|
||||
|
||||
private callbackList = {}
|
||||
private callbackList = {}
|
||||
|
||||
public constructor() {}
|
||||
public constructor() {
|
||||
}
|
||||
|
||||
//注册事件
|
||||
public addListener(eventName: string, callback: Function, caller: any) {
|
||||
if (this.callbackList[eventName]) {
|
||||
let eventList: Array<HaoEvent> = this.callbackList[eventName]
|
||||
//不同元件才放入,相同元件覆蓋
|
||||
let add: boolean = true
|
||||
for (let i = 0; i < eventList.length; i++) {
|
||||
let event: HaoEvent = eventList[i]
|
||||
if (caller === event.caller) {
|
||||
add = false
|
||||
}
|
||||
}
|
||||
if (add) {
|
||||
eventList.push(new HaoEvent(callback, caller))
|
||||
this.callbackList[eventName] = eventList
|
||||
}
|
||||
} else {
|
||||
// this.callbackList[eventName] = [[callback, caller]];
|
||||
this.callbackList[eventName] = [new HaoEvent(callback, caller)]
|
||||
}
|
||||
}
|
||||
//注册事件
|
||||
public addListener(eventName: string, callback: Function, caller: any) {
|
||||
if (this.callbackList[eventName]) {
|
||||
let eventList: Array<HaoEvent> = this.callbackList[eventName]
|
||||
//不同元件才放入,相同元件覆蓋
|
||||
let add: boolean = true
|
||||
for (let i = 0; i < eventList.length; i++) {
|
||||
let event: HaoEvent = eventList[i]
|
||||
if (caller === event.caller) {
|
||||
add = false
|
||||
}
|
||||
}
|
||||
if (add) {
|
||||
eventList.push(new HaoEvent(callback, caller))
|
||||
this.callbackList[eventName] = eventList
|
||||
}
|
||||
} else {
|
||||
// this.callbackList[eventName] = [[callback, caller]];
|
||||
this.callbackList[eventName] = [new HaoEvent(callback, caller)]
|
||||
}
|
||||
}
|
||||
|
||||
public removeListener(eventName: string, callback: Function) {
|
||||
if (this.callbackList[eventName]) {
|
||||
for (let i = this.callbackList[eventName].length - 1; i >= 0; i--) {
|
||||
let event: HaoEvent = this.callbackList[eventName][i]
|
||||
if (event.callback == callback) {
|
||||
this.callbackList[eventName].splice(i, 1)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public removeListener(eventName: string, callback: Function) {
|
||||
if (this.callbackList[eventName]) {
|
||||
for (let i = this.callbackList[eventName].length - 1; i >= 0; i--) {
|
||||
let event: HaoEvent = this.callbackList[eventName][i]
|
||||
if (event.callback == callback) {
|
||||
this.callbackList[eventName].splice(i, 1)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public dispatchEvent(eventName, parameter?: any, ...restOfName: any[]) {
|
||||
let eventList: Array<HaoEvent> = this.callbackList[eventName]
|
||||
if (eventList) {
|
||||
for (let i = eventList.length - 1; i >= 0; i--) {
|
||||
let event: HaoEvent = eventList[i]
|
||||
event.callback.call(event.caller, event, parameter, ...restOfName)
|
||||
if (event.isStop) {
|
||||
break
|
||||
}
|
||||
}
|
||||
for (let i = eventList.length - 1; i >= 0; i--) {
|
||||
let event: HaoEvent = eventList[i]
|
||||
event.isStop = false
|
||||
}
|
||||
}
|
||||
}
|
||||
public dispatchEvent(eventName, parameter?: any, ...restOfName: any[]) {
|
||||
let eventList: Array<HaoEvent> = this.callbackList[eventName]
|
||||
if (eventList) {
|
||||
for (let i = eventList.length - 1; i >= 0; i--) {
|
||||
let event: HaoEvent = eventList[i]
|
||||
event.callback.call(event.caller, event, parameter, ...restOfName)
|
||||
if (event.isStop) {
|
||||
break
|
||||
}
|
||||
}
|
||||
for (let i = eventList.length - 1; i >= 0; i--) {
|
||||
let event: HaoEvent = eventList[i]
|
||||
event.isStop = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public addBtnEvent(
|
||||
parentNode: Node,
|
||||
objectNode: Node,
|
||||
scriptName: string,
|
||||
eventName: string,
|
||||
data: any = null
|
||||
) {
|
||||
var btn: Button = objectNode.addComponent(Button)
|
||||
var clickEventHandler = new Component.EventHandler()
|
||||
clickEventHandler.target = parentNode //这个 node 节点是你的事件处理代码组件所属的节点
|
||||
clickEventHandler.component = scriptName //这个是代码文件名
|
||||
clickEventHandler.handler = eventName
|
||||
clickEventHandler.customEventData = data
|
||||
btn.clickEvents.push(clickEventHandler)
|
||||
this.addBtnEffect(objectNode)
|
||||
}
|
||||
public addBtnEvent(
|
||||
parentNode: Node,
|
||||
objectNode: Node,
|
||||
scriptName: string,
|
||||
eventName: string,
|
||||
data: any = null
|
||||
) {
|
||||
var btn: Button = objectNode.addComponent(Button)
|
||||
var clickEventHandler = new Component.EventHandler()
|
||||
clickEventHandler.target = parentNode //这个 node 节点是你的事件处理代码组件所属的节点
|
||||
clickEventHandler.component = scriptName //这个是代码文件名
|
||||
clickEventHandler.handler = eventName
|
||||
clickEventHandler.customEventData = data
|
||||
btn.clickEvents.push(clickEventHandler)
|
||||
this.addBtnEffect(objectNode)
|
||||
}
|
||||
|
||||
public removeBtnEvent(objectNode: Node) {
|
||||
objectNode.removeComponent(Button)
|
||||
}
|
||||
public removeBtnEvent(objectNode: Node) {
|
||||
objectNode.removeComponent(Button)
|
||||
}
|
||||
|
||||
public removeBtnEffect(objectNode: Node) {
|
||||
var b = objectNode.getComponent(Button)
|
||||
b.transition = Button.Transition.NONE
|
||||
}
|
||||
public removeBtnEffect(objectNode: Node) {
|
||||
var b = objectNode.getComponent(Button)
|
||||
b.transition = Button.Transition.NONE
|
||||
}
|
||||
|
||||
public addBtnEffect(objectNode: Node, scale: number = 1.1) {
|
||||
var b = objectNode.getComponent(Button)
|
||||
b.transition = Button.Transition.SCALE
|
||||
b.zoomScale = scale
|
||||
}
|
||||
public addBtnEffect(objectNode: Node, scale: number = 1.1) {
|
||||
var b = objectNode.getComponent(Button)
|
||||
b.transition = Button.Transition.SCALE
|
||||
b.zoomScale = scale
|
||||
}
|
||||
|
||||
public addBtnEffect_color(
|
||||
objectNode: Node,
|
||||
normalC: Color = ColorHelper.getColor('#FFFFFF'),
|
||||
pressC: Color = ColorHelper.getColor('#C0C0C0')
|
||||
) {
|
||||
var b = objectNode.getComponent(Button)
|
||||
b.transition = Button.Transition.COLOR
|
||||
b.normalColor = normalC
|
||||
b.pressedColor = pressC
|
||||
}
|
||||
public addBtnEffect_color(
|
||||
objectNode: Node,
|
||||
normalC: Color = ColorHelper.getColor('#FFFFFF'),
|
||||
pressC: Color = ColorHelper.getColor('#C0C0C0')
|
||||
) {
|
||||
var b = objectNode.getComponent(Button)
|
||||
b.transition = Button.Transition.COLOR
|
||||
b.normalColor = normalC
|
||||
b.pressedColor = pressC
|
||||
}
|
||||
|
||||
public addSliderEvent(
|
||||
parentNode: Node,
|
||||
objectNode: Node,
|
||||
EventName: string,
|
||||
data: any
|
||||
) {
|
||||
var b = objectNode.getComponent(Slider)
|
||||
var clickEventHandler = new Component.EventHandler()
|
||||
clickEventHandler.target = parentNode //这个 node 节点是你的事件处理代码组件所属的节点
|
||||
clickEventHandler.component = parentNode.name //这个是代码文件名
|
||||
clickEventHandler.handler = EventName
|
||||
clickEventHandler.customEventData = data
|
||||
b.slideEvents.push(clickEventHandler)
|
||||
}
|
||||
public addSliderEvent(
|
||||
parentNode: Node,
|
||||
objectNode: Node,
|
||||
EventName: string,
|
||||
data: any
|
||||
) {
|
||||
var b = objectNode.getComponent(Slider)
|
||||
var clickEventHandler = new Component.EventHandler()
|
||||
clickEventHandler.target = parentNode //这个 node 节点是你的事件处理代码组件所属的节点
|
||||
clickEventHandler.component = parentNode.name //这个是代码文件名
|
||||
clickEventHandler.handler = EventName
|
||||
clickEventHandler.customEventData = data
|
||||
b.slideEvents.push(clickEventHandler)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { _decorator } from 'cc'
|
||||
export default class Grid {
|
||||
public row: number
|
||||
public col: number
|
||||
constructor(row: number, col) {
|
||||
this.row = row
|
||||
this.col = col
|
||||
}
|
||||
public row: number
|
||||
public col: number
|
||||
|
||||
public static init(row: number, col: number) {
|
||||
return new Grid(row, col)
|
||||
}
|
||||
constructor(row: number, col: number) {
|
||||
this.row = row
|
||||
this.col = col
|
||||
}
|
||||
|
||||
public static init(row: number, col: number) {
|
||||
return new Grid(row, col)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,34 +1,31 @@
|
||||
import { _decorator } from 'cc'
|
||||
import { Logger } from './Logger'
|
||||
|
||||
export default class HaoEncrypt {
|
||||
public static encode(str: string) {
|
||||
let result: string = ''
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
//遍历字符串
|
||||
let code: number = str.charCodeAt(i) // //逐个提取每个字符,并获取Unicode编码值
|
||||
if (i % 2 == 0) {
|
||||
code += 2
|
||||
} else {
|
||||
code += 1
|
||||
}
|
||||
result += String.fromCharCode(code)
|
||||
}
|
||||
return result
|
||||
}
|
||||
public static encode(str: string) {
|
||||
let result: string = ''
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
//遍历字符串
|
||||
let code: number = str.charCodeAt(i) // //逐个提取每个字符,并获取Unicode编码值
|
||||
if (i % 2 == 0) {
|
||||
code += 2
|
||||
} else {
|
||||
code += 1
|
||||
}
|
||||
result += String.fromCharCode(code)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
public static decode(str: string) {
|
||||
let result: string = ''
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
//遍历字符串
|
||||
let code: number = str.charCodeAt(i) // //逐个提取每个字符,并获取Unicode编码值
|
||||
if (i % 2 == 0) {
|
||||
code -= 2
|
||||
} else {
|
||||
code -= 1
|
||||
}
|
||||
result += String.fromCharCode(code)
|
||||
}
|
||||
return result
|
||||
}
|
||||
public static decode(str: string) {
|
||||
let result: string = ''
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
//遍历字符串
|
||||
let code: number = str.charCodeAt(i) // //逐个提取每个字符,并获取Unicode编码值
|
||||
if (i % 2 == 0) {
|
||||
code -= 2
|
||||
} else {
|
||||
code -= 1
|
||||
}
|
||||
result += String.fromCharCode(code)
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { sys, _decorator, native, System } from 'cc'
|
||||
import { native, sys } from 'cc'
|
||||
import { Logger } from './Logger'
|
||||
import EventManager from './EventManager'
|
||||
import VersionManager from './VersionManager'
|
||||
@@ -7,326 +7,315 @@ import ResourcePreload from '../../game/utils/ResourcePreload'
|
||||
import CommonTips from '../uicomponent/CommonTips'
|
||||
|
||||
export default class HotUpdate {
|
||||
public static Event_CheckUpdate: string = 'Event_CheckUpdate'
|
||||
public static Event_On_Progress: string = 'HotUpdate_Event_On_Progress'
|
||||
public static Event_On_NeedUpdate: string = 'HotUpdate_Event_On_NeedUpdate'
|
||||
public static Event_Finish_Update: string = 'HotUpdate_Event_Finish'
|
||||
public static Event_On_ALREADY_UP_TO_DATE: string =
|
||||
'HotUpdate_Event_On_ALREADY_UP_TO_DATE'
|
||||
public static Event_On_Fail_Update: string = 'HotUpdate_Event_On_Fail_Update'
|
||||
private _am: any
|
||||
private _checkListener
|
||||
private storagePath: string
|
||||
private manifestUrl: string
|
||||
private localBigVersion: number
|
||||
private remoteBigVersion: number
|
||||
public needUpdate: boolean = false
|
||||
public isUpdating: boolean
|
||||
public isFinishUpdate: boolean
|
||||
public isCheck: boolean
|
||||
private key: string
|
||||
private hotupdateIndex: number
|
||||
constructor() {}
|
||||
public init(
|
||||
index: number,
|
||||
key: string = 'Code-remote-asset',
|
||||
manifestUrl: string
|
||||
) {
|
||||
if (sys.isNative) {
|
||||
this.hotupdateIndex = index
|
||||
this.key = key
|
||||
this.manifestUrl = manifestUrl
|
||||
if (false) {
|
||||
//暂时不想修 fileUtils 这个报错
|
||||
//暂时注释
|
||||
// this.storagePath = jsb.fileUtils.getWritablePath() + key;
|
||||
// if (!jsb.fileUtils.isDirectoryExist(this.storagePath)) {
|
||||
// jsb.fileUtils.createDirectory(this.storagePath)
|
||||
// }
|
||||
} else {
|
||||
this.storagePath = '获取this.storagePath报错了'
|
||||
}
|
||||
public static Event_CheckUpdate: string = 'Event_CheckUpdate'
|
||||
public static Event_On_Progress: string = 'HotUpdate_Event_On_Progress'
|
||||
public static Event_On_NeedUpdate: string = 'HotUpdate_Event_On_NeedUpdate'
|
||||
public static Event_Finish_Update: string = 'HotUpdate_Event_Finish'
|
||||
public static Event_On_ALREADY_UP_TO_DATE: string =
|
||||
'HotUpdate_Event_On_ALREADY_UP_TO_DATE'
|
||||
public static Event_On_Fail_Update: string = 'HotUpdate_Event_On_Fail_Update'
|
||||
private _am: any
|
||||
private _checkListener: null
|
||||
private storagePath: string
|
||||
private manifestUrl: string
|
||||
private localBigVersion: number
|
||||
private remoteBigVersion: number
|
||||
public needUpdate: boolean = false
|
||||
public isUpdating: boolean
|
||||
public isFinishUpdate: boolean
|
||||
public isCheck: boolean
|
||||
private key: string
|
||||
private hotupdateIndex: number
|
||||
|
||||
Logger.log(this, 'init removeDirectory=', this.storagePath + '_temp')
|
||||
}
|
||||
this.needUpdate = false
|
||||
this.isUpdating = false
|
||||
this.isFinishUpdate = false
|
||||
this.isCheck = false
|
||||
}
|
||||
constructor() {
|
||||
}
|
||||
|
||||
private jumpToPack() {
|
||||
let url: string
|
||||
if (sys.isNative) {
|
||||
if (sys.os == sys.OS.ANDROID) {
|
||||
url = VersionManager.instance.apkStoreUrl
|
||||
} else if (sys.os == sys.OS.IOS) {
|
||||
url = VersionManager.instance.iosStoreUrl
|
||||
}
|
||||
}
|
||||
Logger.info(
|
||||
this,
|
||||
'jumpToPack==androidurl===',
|
||||
VersionManager.instance.apkStoreUrl
|
||||
)
|
||||
Logger.info(
|
||||
this,
|
||||
'jumpToPack==iosStoreUrl===',
|
||||
VersionManager.instance.iosStoreUrl
|
||||
)
|
||||
Logger.info(this, 'jumpToPack=====', url)
|
||||
sys.openURL(url)
|
||||
// cc.game.end();
|
||||
}
|
||||
public init(
|
||||
index: number,
|
||||
key: string = 'Code-remote-asset',
|
||||
manifestUrl: string
|
||||
) {
|
||||
if (sys.isNative) {
|
||||
this.hotupdateIndex = index
|
||||
this.key = key
|
||||
this.manifestUrl = manifestUrl
|
||||
this.storagePath = '获取this.storagePath报错了'
|
||||
|
||||
//显示强制更新,即更细包面板
|
||||
private showPackUpdateDialog() {
|
||||
CommonTips.showMsg(
|
||||
'有新的版本需要更新,下载后请先卸载,以前的版本,再安装!'
|
||||
)
|
||||
this.jumpToPack()
|
||||
this.showPackUpdateDialog()
|
||||
}
|
||||
Logger.log(this, 'init removeDirectory=', this.storagePath + '_temp')
|
||||
}
|
||||
this.needUpdate = false
|
||||
this.isUpdating = false
|
||||
this.isFinishUpdate = false
|
||||
this.isCheck = false
|
||||
}
|
||||
|
||||
private checkCb(event) {
|
||||
Logger.log(this, 'checkCb Code: =================' + event.getEventCode())
|
||||
switch (event.getEventCode()) {
|
||||
case native.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
|
||||
Logger.info(this, 'No local manifest file found, hot update skipped.')
|
||||
this.failUpdate()
|
||||
break
|
||||
case native.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
|
||||
case native.EventAssetsManager.ERROR_PARSE_MANIFEST:
|
||||
Logger.info(this, 'Fail to download manifest file, hot update skipped.')
|
||||
this.failUpdate()
|
||||
break
|
||||
case native.EventAssetsManager.ALREADY_UP_TO_DATE:
|
||||
Logger.info(this, 'Already up to date with the latest remote version.')
|
||||
this.alreadyUpToDate()
|
||||
break
|
||||
case native.EventAssetsManager.NEW_VERSION_FOUND:
|
||||
Logger.info(
|
||||
this,
|
||||
'new version found, please try to update.',
|
||||
this.localBigVersion,
|
||||
this.remoteBigVersion
|
||||
)
|
||||
if (
|
||||
this.key == VersionManager.Config_Key[0] &&
|
||||
this.localBigVersion < this.remoteBigVersion
|
||||
) {
|
||||
//更新大版本
|
||||
Logger.info(
|
||||
this,
|
||||
'new version found, please try to update======packupdate=',
|
||||
this.localBigVersion,
|
||||
this.remoteBigVersion
|
||||
)
|
||||
this.showPackUpdateDialog()
|
||||
} else {
|
||||
Logger.info(
|
||||
this,
|
||||
'new version found, please try to update======hotupdate=',
|
||||
this.localBigVersion,
|
||||
this.remoteBigVersion
|
||||
)
|
||||
// this._am.update();
|
||||
this.needUpdate = true
|
||||
EventManager.instance.dispatchEvent(
|
||||
HotUpdate.Event_On_NeedUpdate,
|
||||
this.key
|
||||
)
|
||||
}
|
||||
break
|
||||
case native.EventAssetsManager.UPDATE_PROGRESSION:
|
||||
// var currentPercent = event.getPercent();
|
||||
// var totalPercent = event.getPercentByFile();
|
||||
// var fileprocess = event.getDownloadedFiles() + ' / ' + event.getTotalFiles();
|
||||
// var byteprocess = event.getDownloadedBytes() + ' / ' + event.getTotalBytes();
|
||||
Logger.info(
|
||||
this,
|
||||
'UPDATE_PROGRESSION2222==========',
|
||||
this.key,
|
||||
event.getDownloadedBytes(),
|
||||
event.getTotalBytes()
|
||||
)
|
||||
if (event.getTotalBytes() > 0) {
|
||||
EventManager.instance.dispatchEvent(
|
||||
HotUpdate.Event_On_Progress,
|
||||
event.getDownloadedBytes(),
|
||||
event.getTotalBytes(),
|
||||
this.key
|
||||
)
|
||||
}
|
||||
break
|
||||
case native.EventAssetsManager.UPDATE_FINISHED:
|
||||
Logger.info(this, 'UPDATE_FINISHED==============')
|
||||
this.finishUpdate(true)
|
||||
break
|
||||
case native.EventAssetsManager.UPDATE_FAILED:
|
||||
Logger.warn(this, 'Update failed==========', event.getMessage())
|
||||
this.failUpdate()
|
||||
break
|
||||
case native.EventAssetsManager.ERROR_UPDATING:
|
||||
let fullFilePath: string = this.storagePath + '/' + event.getAssetId()
|
||||
let tempFilePath: string =
|
||||
this.storagePath + '_temp/' + event.getAssetId()
|
||||
Logger.warn(this, 'fullFilePath====', fullFilePath)
|
||||
Logger.warn(this, 'tempFilePath====', tempFilePath)
|
||||
// jsb.fileUtils.removeFile(tempFilePath);
|
||||
Logger.warn(
|
||||
this,
|
||||
'ERROR_UPDATING=============',
|
||||
event.getAssetId(),
|
||||
event.getMessage()
|
||||
)
|
||||
this.failUpdate()
|
||||
break
|
||||
default:
|
||||
// this.failUpdate();
|
||||
return
|
||||
}
|
||||
}
|
||||
private jumpToPack() {
|
||||
let url: string
|
||||
if (sys.isNative) {
|
||||
if (sys.os == sys.OS.ANDROID) {
|
||||
url = VersionManager.instance.apkStoreUrl
|
||||
} else if (sys.os == sys.OS.IOS) {
|
||||
url = VersionManager.instance.iosStoreUrl
|
||||
}
|
||||
}
|
||||
Logger.info(
|
||||
this,
|
||||
'jumpToPack==androidurl===',
|
||||
VersionManager.instance.apkStoreUrl
|
||||
)
|
||||
Logger.info(
|
||||
this,
|
||||
'jumpToPack==iosStoreUrl===',
|
||||
VersionManager.instance.iosStoreUrl
|
||||
)
|
||||
Logger.info(this, 'jumpToPack=====', url)
|
||||
sys.openURL(url)
|
||||
// cc.game.end();
|
||||
}
|
||||
|
||||
public checkUpdate() {
|
||||
if (this.isUpdating || this.isCheck) {
|
||||
Logger.log(this, 'Checking or updating ...')
|
||||
return
|
||||
}
|
||||
let hotupdateUrlKey: string =
|
||||
VersionManager.Config_Url_Key[this.hotupdateIndex]
|
||||
Logger.log(this, 'checkoutUpdate=====', this.manifestUrl, hotupdateUrlKey)
|
||||
if (!this._am) {
|
||||
this._am = new native.AssetsManager(
|
||||
'',
|
||||
this.storagePath,
|
||||
this.versionCompareHandle.bind(this)
|
||||
)
|
||||
}
|
||||
// this._am.setMaxConcurrentTask(1);
|
||||
let manifestStr: string = ManifestConfig.getManifestStr(hotupdateUrlKey)
|
||||
Logger.log(this, 'checkUpdate=======manifestStr=======', manifestStr)
|
||||
let manifest = new native.Manifest(manifestStr, this.storagePath)
|
||||
this._am.setVerifyCallback(function (filePath, asset) {
|
||||
return true
|
||||
// var md5 = calculateMD5(filePath);
|
||||
// if (md5 === asset.md5)
|
||||
// return true;
|
||||
// else
|
||||
// return false;
|
||||
})
|
||||
this._am.setEventCallback(this.checkCb.bind(this))
|
||||
// 设置事件回调
|
||||
this.isCheck = true
|
||||
this._am.loadLocalManifest(manifest, this.storagePath)
|
||||
this._am.checkUpdate()
|
||||
}
|
||||
//显示强制更新,即更细包面板
|
||||
private showPackUpdateDialog() {
|
||||
CommonTips.showMsg(
|
||||
'有新的版本需要更新,下载后请先卸载,以前的版本,再安装!'
|
||||
)
|
||||
this.jumpToPack()
|
||||
this.showPackUpdateDialog()
|
||||
}
|
||||
|
||||
/**
|
||||
* @param versionA 本地版本 1.0.0
|
||||
* @param versionB 服务器版本 1.0.1
|
||||
* @param return -1需要更新 不用更新
|
||||
*/
|
||||
private versionCompareHandle(versionA, versionB) {
|
||||
var vA = versionA.split('.')
|
||||
var vB = versionB.split('.')
|
||||
Logger.log(
|
||||
this,
|
||||
'versionCompareHandle======',
|
||||
this.key,
|
||||
VersionManager.Config_Key[0]
|
||||
)
|
||||
if (this.key == VersionManager.Config_Key[0]) {
|
||||
Logger.log(this, 'versionCompareHandle22===', versionA, versionB)
|
||||
VersionManager.instance.nowVersion = versionA
|
||||
VersionManager.instance.targetVersion = versionB
|
||||
}
|
||||
this.localBigVersion = parseInt(vA[0])
|
||||
this.remoteBigVersion = parseInt(vB[0])
|
||||
for (var i = 0; i < vA.length; ++i) {
|
||||
var a = parseInt(vA[i])
|
||||
var b = parseInt(vB[i] || 0)
|
||||
if (a === b) {
|
||||
continue
|
||||
} else {
|
||||
return a - b
|
||||
}
|
||||
}
|
||||
if (vB.length > vA.length) {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
private checkCb(event: any) {
|
||||
Logger.log(this, 'checkCb Code: =================' + event.getEventCode())
|
||||
switch (event.getEventCode()) {
|
||||
case native.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
|
||||
Logger.info(this, 'No local manifest file found, hot update skipped.')
|
||||
this.failUpdate()
|
||||
break
|
||||
case native.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
|
||||
case native.EventAssetsManager.ERROR_PARSE_MANIFEST:
|
||||
Logger.info(this, 'Fail to download manifest file, hot update skipped.')
|
||||
this.failUpdate()
|
||||
break
|
||||
case native.EventAssetsManager.ALREADY_UP_TO_DATE:
|
||||
Logger.info(this, 'Already up to date with the latest remote version.')
|
||||
this.alreadyUpToDate()
|
||||
break
|
||||
case native.EventAssetsManager.NEW_VERSION_FOUND:
|
||||
Logger.info(
|
||||
this,
|
||||
'new version found, please try to update.',
|
||||
this.localBigVersion,
|
||||
this.remoteBigVersion
|
||||
)
|
||||
if (
|
||||
this.key == VersionManager.Config_Key[0] &&
|
||||
this.localBigVersion < this.remoteBigVersion
|
||||
) {
|
||||
//更新大版本
|
||||
Logger.info(
|
||||
this,
|
||||
'new version found, please try to update======packupdate=',
|
||||
this.localBigVersion,
|
||||
this.remoteBigVersion
|
||||
)
|
||||
this.showPackUpdateDialog()
|
||||
} else {
|
||||
Logger.info(
|
||||
this,
|
||||
'new version found, please try to update======hotupdate=',
|
||||
this.localBigVersion,
|
||||
this.remoteBigVersion
|
||||
)
|
||||
// this._am.update();
|
||||
this.needUpdate = true
|
||||
EventManager.instance.dispatchEvent(
|
||||
HotUpdate.Event_On_NeedUpdate,
|
||||
this.key
|
||||
)
|
||||
}
|
||||
break
|
||||
case native.EventAssetsManager.UPDATE_PROGRESSION:
|
||||
// var currentPercent = event.getPercent();
|
||||
// var totalPercent = event.getPercentByFile();
|
||||
// var fileprocess = event.getDownloadedFiles() + ' / ' + event.getTotalFiles();
|
||||
// var byteprocess = event.getDownloadedBytes() + ' / ' + event.getTotalBytes();
|
||||
Logger.info(
|
||||
this,
|
||||
'UPDATE_PROGRESSION2222==========',
|
||||
this.key,
|
||||
event.getDownloadedBytes(),
|
||||
event.getTotalBytes()
|
||||
)
|
||||
if (event.getTotalBytes() > 0) {
|
||||
EventManager.instance.dispatchEvent(
|
||||
HotUpdate.Event_On_Progress,
|
||||
event.getDownloadedBytes(),
|
||||
event.getTotalBytes(),
|
||||
this.key
|
||||
)
|
||||
}
|
||||
break
|
||||
case native.EventAssetsManager.UPDATE_FINISHED:
|
||||
Logger.info(this, 'UPDATE_FINISHED==============')
|
||||
this.finishUpdate(true)
|
||||
break
|
||||
case native.EventAssetsManager.UPDATE_FAILED:
|
||||
Logger.warn(this, 'Update failed==========', event.getMessage())
|
||||
this.failUpdate()
|
||||
break
|
||||
case native.EventAssetsManager.ERROR_UPDATING:
|
||||
let fullFilePath: string = this.storagePath + '/' + event.getAssetId()
|
||||
let tempFilePath: string =
|
||||
this.storagePath + '_temp/' + event.getAssetId()
|
||||
Logger.warn(this, 'fullFilePath====', fullFilePath)
|
||||
Logger.warn(this, 'tempFilePath====', tempFilePath)
|
||||
// jsb.fileUtils.removeFile(tempFilePath);
|
||||
Logger.warn(
|
||||
this,
|
||||
'ERROR_UPDATING=============',
|
||||
event.getAssetId(),
|
||||
event.getMessage()
|
||||
)
|
||||
this.failUpdate()
|
||||
break
|
||||
default:
|
||||
// this.failUpdate();
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
public startUpdate() {
|
||||
if (this.isUpdating) return
|
||||
let localManifest = this._am.getLocalManifest()
|
||||
let remoteManifest = this._am.getRemoteManifest()
|
||||
Logger.log(this, 'startUpdate111===', localManifest.getVersionFileUrl())
|
||||
Logger.log(this, 'startUpdate2222===', localManifest.getManifestFileUrl())
|
||||
Logger.log(this, 'startUpdate3333===', remoteManifest.getVersionFileUrl())
|
||||
Logger.log(this, 'startUpdate4444===', remoteManifest.getManifestFileUrl())
|
||||
this.isUpdating = true
|
||||
EventManager.instance.dispatchEvent(
|
||||
HotUpdate.Event_On_Progress,
|
||||
0,
|
||||
100,
|
||||
this.key
|
||||
)
|
||||
this._am.update()
|
||||
}
|
||||
public checkUpdate() {
|
||||
if (this.isUpdating || this.isCheck) {
|
||||
Logger.log(this, 'Checking or updating ...')
|
||||
return
|
||||
}
|
||||
let hotupdateUrlKey: string =
|
||||
VersionManager.Config_Url_Key[this.hotupdateIndex]
|
||||
Logger.log(this, 'checkoutUpdate=====', this.manifestUrl, hotupdateUrlKey)
|
||||
if (!this._am) {
|
||||
this._am = new native.AssetsManager(
|
||||
'',
|
||||
this.storagePath,
|
||||
this.versionCompareHandle.bind(this)
|
||||
)
|
||||
}
|
||||
// this._am.setMaxConcurrentTask(1);
|
||||
let manifestStr: string = ManifestConfig.getManifestStr(hotupdateUrlKey)
|
||||
Logger.log(this, 'checkUpdate=======manifestStr=======', manifestStr)
|
||||
let manifest = new native.Manifest(manifestStr, this.storagePath)
|
||||
this._am.setVerifyCallback(function(filePath, asset) {
|
||||
return true
|
||||
// var md5 = calculateMD5(filePath);
|
||||
// if (md5 === asset.md5)
|
||||
// return true;
|
||||
// else
|
||||
// return false;
|
||||
})
|
||||
this._am.setEventCallback(this.checkCb.bind(this))
|
||||
// 设置事件回调
|
||||
this.isCheck = true
|
||||
this._am.loadLocalManifest(manifest, this.storagePath)
|
||||
this._am.checkUpdate()
|
||||
}
|
||||
|
||||
public disposeUpdate() {
|
||||
if (this._am) {
|
||||
this._am.setVerifyCallback(null)
|
||||
this._am.setEventCallback(null)
|
||||
}
|
||||
this._am = null
|
||||
this._checkListener = null
|
||||
this.isUpdating = false
|
||||
this.needUpdate = false
|
||||
}
|
||||
/**
|
||||
* @param versionA 本地版本 1.0.0
|
||||
* @param versionB 服务器版本 1.0.1
|
||||
*/
|
||||
private versionCompareHandle(versionA: string, versionB: string) {
|
||||
const vA = versionA.split('.')
|
||||
const vB = versionB.split('.')
|
||||
Logger.log(
|
||||
this,
|
||||
'versionCompareHandle======',
|
||||
this.key,
|
||||
VersionManager.Config_Key[0]
|
||||
)
|
||||
if (this.key == VersionManager.Config_Key[0]) {
|
||||
Logger.log(this, 'versionCompareHandle22===', versionA, versionB)
|
||||
VersionManager.instance.nowVersion = versionA
|
||||
VersionManager.instance.targetVersion = versionB
|
||||
}
|
||||
this.localBigVersion = parseInt(vA[0])
|
||||
this.remoteBigVersion = parseInt(vB[0])
|
||||
for (let i = 0; i < vA.length; ++i) {
|
||||
const a = parseInt(vA[i])
|
||||
const b = parseInt(vB[i] || '0')
|
||||
if (a !== b) return a - b
|
||||
}
|
||||
if (vB.length > vA.length) {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
private failUpdate() {
|
||||
this.disposeUpdate()
|
||||
this.isCheck = false
|
||||
EventManager.instance.dispatchEvent(
|
||||
HotUpdate.Event_On_Fail_Update,
|
||||
this.key
|
||||
)
|
||||
}
|
||||
public startUpdate() {
|
||||
if (this.isUpdating) return
|
||||
let localManifest = this._am.getLocalManifest()
|
||||
let remoteManifest = this._am.getRemoteManifest()
|
||||
Logger.log(this, 'startUpdate111===', localManifest.getVersionFileUrl())
|
||||
Logger.log(this, 'startUpdate2222===', localManifest.getManifestFileUrl())
|
||||
Logger.log(this, 'startUpdate3333===', remoteManifest.getVersionFileUrl())
|
||||
Logger.log(this, 'startUpdate4444===', remoteManifest.getManifestFileUrl())
|
||||
this.isUpdating = true
|
||||
EventManager.instance.dispatchEvent(
|
||||
HotUpdate.Event_On_Progress,
|
||||
0,
|
||||
100,
|
||||
this.key
|
||||
)
|
||||
this._am.update()
|
||||
}
|
||||
|
||||
private alreadyUpToDate() {
|
||||
this.disposeUpdate()
|
||||
this.isFinishUpdate = true
|
||||
EventManager.instance.dispatchEvent(
|
||||
HotUpdate.Event_On_ALREADY_UP_TO_DATE,
|
||||
this.key
|
||||
)
|
||||
}
|
||||
public disposeUpdate() {
|
||||
if (this._am) {
|
||||
this._am.setVerifyCallback(null)
|
||||
this._am.setEventCallback(null)
|
||||
}
|
||||
this._am = null
|
||||
this._checkListener = null
|
||||
this.isUpdating = false
|
||||
this.needUpdate = false
|
||||
}
|
||||
|
||||
private finishUpdate(needRestart: boolean) {
|
||||
Logger.info(this, '更新完成=====', needRestart)
|
||||
this.disposeUpdate()
|
||||
this.isFinishUpdate = true
|
||||
EventManager.instance.dispatchEvent(
|
||||
HotUpdate.Event_Finish_Update,
|
||||
this.key,
|
||||
needRestart
|
||||
)
|
||||
if (false && needRestart) {
|
||||
//暂时不想修 fileUtils 这个报错
|
||||
var searchPaths = '' //jsb.fileUtils.getSearchPaths();暂时注释
|
||||
Logger.info(this, '更新完成====searchPaths======', searchPaths)
|
||||
sys.localStorage.setItem(
|
||||
'HotUpdateSearchPaths',
|
||||
JSON.stringify(searchPaths)
|
||||
)
|
||||
//jsb.fileUtils.setSearchPaths(searchPaths);暂时注释
|
||||
if (this.key == VersionManager.Config_Key[0]) {
|
||||
ResourcePreload.instance.restartGame()
|
||||
}
|
||||
}
|
||||
}
|
||||
private failUpdate() {
|
||||
this.disposeUpdate()
|
||||
this.isCheck = false
|
||||
EventManager.instance.dispatchEvent(
|
||||
HotUpdate.Event_On_Fail_Update,
|
||||
this.key
|
||||
)
|
||||
}
|
||||
|
||||
private alreadyUpToDate() {
|
||||
this.disposeUpdate()
|
||||
this.isFinishUpdate = true
|
||||
EventManager.instance.dispatchEvent(
|
||||
HotUpdate.Event_On_ALREADY_UP_TO_DATE,
|
||||
this.key
|
||||
)
|
||||
}
|
||||
|
||||
private finishUpdate(needRestart: boolean) {
|
||||
Logger.info(this, '更新完成=====', needRestart)
|
||||
this.disposeUpdate()
|
||||
this.isFinishUpdate = true
|
||||
EventManager.instance.dispatchEvent(
|
||||
HotUpdate.Event_Finish_Update,
|
||||
this.key,
|
||||
needRestart
|
||||
)
|
||||
if (false && needRestart) {
|
||||
//暂时不想修 fileUtils 这个报错
|
||||
var searchPaths = '' //jsb.fileUtils.getSearchPaths();暂时注释
|
||||
Logger.info(this, '更新完成====searchPaths======', searchPaths)
|
||||
sys.localStorage.setItem(
|
||||
'HotUpdateSearchPaths',
|
||||
JSON.stringify(searchPaths)
|
||||
)
|
||||
//jsb.fileUtils.setSearchPaths(searchPaths);暂时注释
|
||||
if (this.key == VersionManager.Config_Key[0]) {
|
||||
ResourcePreload.instance.restartGame()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,160 +1,160 @@
|
||||
import { _decorator } from 'cc'
|
||||
const { ccclass } = _decorator
|
||||
|
||||
import LoadingPrefab from '../uicomponent/LoadingPrefab'
|
||||
import VersionManager from './VersionManager'
|
||||
import { Logger } from './Logger'
|
||||
|
||||
const { ccclass } = _decorator
|
||||
|
||||
@ccclass('HttpClient')
|
||||
export default class HttpClient {
|
||||
public static instance: HttpClient //= new HttpClient();
|
||||
public static instance: HttpClient //= new HttpClient();
|
||||
|
||||
//example
|
||||
// HttpClient.instance.request("http://localhost:8080/haohttp/test", ()=>{
|
||||
// console.log("http 请求 end=============");
|
||||
// }, {"nickName":"jhao", "hh":1, "id":9527});
|
||||
//example
|
||||
// HttpClient.instance.request("http://localhost:8080/haohttp/test", ()=>{
|
||||
// console.log("http 请求 end=============");
|
||||
// }, {"nickName":"jhao", "hh":1, "id":9527});
|
||||
|
||||
private methodType: string = 'GET'
|
||||
private methodType: string = 'GET'
|
||||
|
||||
private responseType: XMLHttpRequestResponseType = 'json'
|
||||
private responseType: XMLHttpRequestResponseType = 'json'
|
||||
|
||||
private xhr: XMLHttpRequest
|
||||
private xhr: XMLHttpRequest
|
||||
|
||||
// --GET or POST
|
||||
public setMethod(method: string = 'GET') {
|
||||
this.methodType = method
|
||||
}
|
||||
// --GET or POST
|
||||
public setMethod(method: string = 'GET') {
|
||||
this.methodType = method
|
||||
}
|
||||
|
||||
public setParams(paramsObj: object): string {
|
||||
let resParams = ''
|
||||
let nowIndex = 1
|
||||
for (const key in paramsObj) {
|
||||
if (paramsObj.hasOwnProperty(key)) {
|
||||
if (nowIndex == 1) {
|
||||
resParams += key + '=' + paramsObj[key]
|
||||
} else {
|
||||
resParams += '&' + key + '=' + paramsObj[key]
|
||||
}
|
||||
nowIndex += 1
|
||||
}
|
||||
}
|
||||
Logger.log(this, 'resParam===============', resParams)
|
||||
return resParams
|
||||
}
|
||||
public setParams(paramsObj: object): string {
|
||||
let resParams = ''
|
||||
let nowIndex = 1
|
||||
for (const key in paramsObj) {
|
||||
if (paramsObj.hasOwnProperty(key)) {
|
||||
if (nowIndex == 1) {
|
||||
resParams += key + '=' + paramsObj[key]
|
||||
} else {
|
||||
resParams += '&' + key + '=' + paramsObj[key]
|
||||
}
|
||||
nowIndex += 1
|
||||
}
|
||||
}
|
||||
Logger.log(this, 'resParam===============', resParams)
|
||||
return resParams
|
||||
}
|
||||
|
||||
public setResponseType(responseType: XMLHttpRequestResponseType) {
|
||||
this.responseType = responseType
|
||||
}
|
||||
public setResponseType(responseType: XMLHttpRequestResponseType) {
|
||||
this.responseType = responseType
|
||||
}
|
||||
|
||||
public setContentType() {}
|
||||
public setContentType() {
|
||||
}
|
||||
|
||||
public request(
|
||||
url: string,
|
||||
callback: Function,
|
||||
params: any = null,
|
||||
timeOut: number = 5 * 1000
|
||||
) {
|
||||
if (params && this.methodType == 'GET') {
|
||||
let getParams: string = this.setParams(params)
|
||||
// getParams = StringUtil:encodeURI(params)
|
||||
getParams = encodeURI(getParams)
|
||||
url += '?' + getParams
|
||||
}
|
||||
this.xhr = new XMLHttpRequest() // http请求 fget
|
||||
//this.xhr = cc.loader.getXMLHttpRequest();
|
||||
let xhr: XMLHttpRequest = this.xhr
|
||||
xhr.responseType = this.responseType
|
||||
xhr.timeout = timeOut
|
||||
// xhr.setRequestHeader("Content-Type", "text/plain");
|
||||
xhr.onreadystatechange = () => {
|
||||
Logger.log(
|
||||
this,
|
||||
'status======',
|
||||
xhr.status,
|
||||
xhr.readyState,
|
||||
xhr.statusText
|
||||
)
|
||||
// if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
|
||||
if (xhr.readyState == 4 && xhr.status == 200) {
|
||||
let response = xhr.response
|
||||
Logger.log(this, 'http response1============', xhr)
|
||||
try {
|
||||
let testJson = JSON.stringify(response)
|
||||
Logger.log(this, 'http response json============', testJson)
|
||||
if (callback) {
|
||||
callback(true, response)
|
||||
callback = null
|
||||
}
|
||||
} catch (error) {
|
||||
Logger.error(this, 'http response json=====error=======', error)
|
||||
if (callback) {
|
||||
callback(false)
|
||||
callback = null
|
||||
}
|
||||
}
|
||||
} else if (xhr.readyState == 4 && xhr.status == 301) {
|
||||
//域名转移
|
||||
Logger.log(
|
||||
this,
|
||||
'http response222============',
|
||||
xhr.getResponseHeader('Location')
|
||||
)
|
||||
// console.log("http response333============", xhr.getAllResponseHeaders());
|
||||
if (HttpClient.instance == null) HttpClient.instance = new HttpClient()
|
||||
HttpClient.instance.request(xhr.getResponseHeader('Location'), callback)
|
||||
} else if (xhr.readyState == 4 && xhr.status == 404) {
|
||||
Logger.log(this, 'http onError============')
|
||||
if (callback) {
|
||||
callback(false)
|
||||
callback = null
|
||||
}
|
||||
} else {
|
||||
Logger.log(
|
||||
this,
|
||||
'onreadystatechange else====',
|
||||
xhr.status,
|
||||
xhr.readyState,
|
||||
xhr.response
|
||||
)
|
||||
if (xhr.readyState == 4) {
|
||||
Logger.log(this, 'http onError else============')
|
||||
if (callback) {
|
||||
callback(false)
|
||||
callback = null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
xhr.onprogress = () => {
|
||||
Logger.log(
|
||||
this,
|
||||
'http onprogress===',
|
||||
xhr.status,
|
||||
xhr.readyState,
|
||||
xhr.response
|
||||
)
|
||||
}
|
||||
xhr.onerror = () => {
|
||||
Logger.log(this, 'http onError============')
|
||||
if (callback) {
|
||||
callback(false)
|
||||
callback = null
|
||||
}
|
||||
}
|
||||
xhr.ontimeout = () => {
|
||||
Logger.log(this, 'http ontimeout============')
|
||||
if (callback) {
|
||||
callback(false)
|
||||
callback = null
|
||||
}
|
||||
}
|
||||
Logger.log(this, 'http request==============', url)
|
||||
Logger.log(this, 'http request======method========', this.methodType)
|
||||
Logger.log(this, 'http request======params========', params)
|
||||
xhr.open(this.methodType, url, true)
|
||||
xhr.setRequestHeader('content-type', 'text/plain;charset=UTF-8')
|
||||
xhr.send(params)
|
||||
}
|
||||
public request(
|
||||
url: string,
|
||||
callback: Function,
|
||||
params: any = null,
|
||||
timeOut: number = 5 * 1000
|
||||
) {
|
||||
if (params && this.methodType == 'GET') {
|
||||
let getParams: string = this.setParams(params)
|
||||
// getParams = StringUtil:encodeURI(params)
|
||||
getParams = encodeURI(getParams)
|
||||
url += '?' + getParams
|
||||
}
|
||||
this.xhr = new XMLHttpRequest() // http请求 fget
|
||||
//this.xhr = cc.loader.getXMLHttpRequest();
|
||||
let xhr: XMLHttpRequest = this.xhr
|
||||
xhr.responseType = this.responseType
|
||||
xhr.timeout = timeOut
|
||||
// xhr.setRequestHeader("Content-Type", "text/plain");
|
||||
xhr.onreadystatechange = () => {
|
||||
Logger.log(
|
||||
this,
|
||||
'status======',
|
||||
xhr.status,
|
||||
xhr.readyState,
|
||||
xhr.statusText
|
||||
)
|
||||
// if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
|
||||
if (xhr.readyState == 4 && xhr.status == 200) {
|
||||
let response = xhr.response
|
||||
Logger.log(this, 'http response1============', xhr)
|
||||
try {
|
||||
let testJson = JSON.stringify(response)
|
||||
Logger.log(this, 'http response json============', testJson)
|
||||
if (callback) {
|
||||
callback(true, response)
|
||||
callback = null
|
||||
}
|
||||
} catch (error) {
|
||||
Logger.error(this, 'http response json=====error=======', error)
|
||||
if (callback) {
|
||||
callback(false)
|
||||
callback = null
|
||||
}
|
||||
}
|
||||
} else if (xhr.readyState == 4 && xhr.status == 301) {
|
||||
//域名转移
|
||||
Logger.log(
|
||||
this,
|
||||
'http response222============',
|
||||
xhr.getResponseHeader('Location')
|
||||
)
|
||||
// console.log("http response333============", xhr.getAllResponseHeaders());
|
||||
if (HttpClient.instance == null) HttpClient.instance = new HttpClient()
|
||||
HttpClient.instance.request(xhr.getResponseHeader('Location'), callback)
|
||||
} else if (xhr.readyState == 4 && xhr.status == 404) {
|
||||
Logger.log(this, 'http onError============')
|
||||
if (callback) {
|
||||
callback(false)
|
||||
callback = null
|
||||
}
|
||||
} else {
|
||||
Logger.log(
|
||||
this,
|
||||
'onreadystatechange else====',
|
||||
xhr.status,
|
||||
xhr.readyState,
|
||||
xhr.response
|
||||
)
|
||||
if (xhr.readyState == 4) {
|
||||
Logger.log(this, 'http onError else============')
|
||||
if (callback) {
|
||||
callback(false)
|
||||
callback = null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
xhr.onprogress = () => {
|
||||
Logger.log(
|
||||
this,
|
||||
'http onprogress===',
|
||||
xhr.status,
|
||||
xhr.readyState,
|
||||
xhr.response
|
||||
)
|
||||
}
|
||||
xhr.onerror = () => {
|
||||
Logger.log(this, 'http onError============')
|
||||
if (callback) {
|
||||
callback(false)
|
||||
callback = null
|
||||
}
|
||||
}
|
||||
xhr.ontimeout = () => {
|
||||
Logger.log(this, 'http ontimeout============')
|
||||
if (callback) {
|
||||
callback(false)
|
||||
callback = null
|
||||
}
|
||||
}
|
||||
Logger.log(this, 'http request==============', url)
|
||||
Logger.log(this, 'http request======method========', this.methodType)
|
||||
Logger.log(this, 'http request======params========', params)
|
||||
xhr.open(this.methodType, url, true)
|
||||
xhr.setRequestHeader('content-type', 'text/plain;charset=UTF-8')
|
||||
xhr.send(params)
|
||||
}
|
||||
|
||||
public getInfo(callback: Function = null) {}
|
||||
public getInfo(callback: Function = null) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,64 +1,63 @@
|
||||
import { sys, _decorator } from 'cc'
|
||||
import { Logger } from './Logger'
|
||||
import { sys } from 'cc'
|
||||
|
||||
export default class LocalStorage {
|
||||
public static GamePreFlag: string = 'fengshen-game-HaoLocalStorage'
|
||||
public static GamePreFlag: string = 'fengshen-game-HaoLocalStorage'
|
||||
|
||||
public static setItem(key: string, value: string): void {
|
||||
sys.localStorage.setItem(LocalStorage.GamePreFlag + key, value)
|
||||
}
|
||||
public static setItem(key: string, value: string): void {
|
||||
sys.localStorage.setItem(LocalStorage.GamePreFlag + key, value)
|
||||
}
|
||||
|
||||
public static getItem(key: string): string {
|
||||
return sys.localStorage.getItem(LocalStorage.GamePreFlag + key)
|
||||
}
|
||||
public static getItem(key: string): string {
|
||||
return sys.localStorage.getItem(LocalStorage.GamePreFlag + key)
|
||||
}
|
||||
|
||||
public static removeItem(key: string): void {
|
||||
sys.localStorage.removeItem(LocalStorage.GamePreFlag + key)
|
||||
}
|
||||
public static removeItem(key: string): void {
|
||||
sys.localStorage.removeItem(LocalStorage.GamePreFlag + key)
|
||||
}
|
||||
|
||||
public static getInt(key: string): number {
|
||||
let tempValue: string = LocalStorage.getItem(key)
|
||||
let result: number = 0
|
||||
if (tempValue) {
|
||||
result = parseInt(tempValue)
|
||||
}
|
||||
return result
|
||||
}
|
||||
public static getInt(key: string): number {
|
||||
let tempValue: string = LocalStorage.getItem(key)
|
||||
let result: number = 0
|
||||
if (tempValue) {
|
||||
result = parseInt(tempValue)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
public static setInt(key: string, value: number): void {
|
||||
LocalStorage.setItem(key, value.toString())
|
||||
}
|
||||
public static setInt(key: string, value: number): void {
|
||||
LocalStorage.setItem(key, value.toString())
|
||||
}
|
||||
|
||||
public static getFloat(key: string): number {
|
||||
let tempValue: string = LocalStorage.getItem(key)
|
||||
let result: number = 0
|
||||
if (tempValue) {
|
||||
result = parseFloat(tempValue)
|
||||
}
|
||||
return result
|
||||
}
|
||||
public static getFloat(key: string): number {
|
||||
let tempValue: string = LocalStorage.getItem(key)
|
||||
let result: number = 0
|
||||
if (tempValue) {
|
||||
result = parseFloat(tempValue)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
public static setFloat(key: string, value: number): void {
|
||||
LocalStorage.setItem(key, value.toString())
|
||||
}
|
||||
public static setFloat(key: string, value: number): void {
|
||||
LocalStorage.setItem(key, value.toString())
|
||||
}
|
||||
|
||||
public static getBoolean(key: string): boolean {
|
||||
let temp: number = LocalStorage.getInt(key)
|
||||
if (temp == 1) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
public static getBoolean(key: string): boolean {
|
||||
let temp: number = LocalStorage.getInt(key)
|
||||
if (temp == 1) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
public static setBoolean(key: string, value: boolean) {
|
||||
if (value) {
|
||||
LocalStorage.setInt(key, 1)
|
||||
} else {
|
||||
LocalStorage.setInt(key, 0)
|
||||
}
|
||||
}
|
||||
public static setBoolean(key: string, value: boolean) {
|
||||
if (value) {
|
||||
LocalStorage.setInt(key, 1)
|
||||
} else {
|
||||
LocalStorage.setInt(key, 0)
|
||||
}
|
||||
}
|
||||
|
||||
public static clear() {
|
||||
sys.localStorage.clear()
|
||||
}
|
||||
public static clear() {
|
||||
sys.localStorage.clear()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,133 +1,138 @@
|
||||
import { error, _decorator } from 'cc'
|
||||
class LOG_LEVEL_TYPES {
|
||||
public static DEBUG = 0
|
||||
public static LOG = 1
|
||||
public static INFO = 2
|
||||
public static WARN = 3
|
||||
public static ERROR = 4
|
||||
public static DEBUG = 0
|
||||
public static LOG = 1
|
||||
public static INFO = 2
|
||||
public static WARN = 3
|
||||
public static ERROR = 4
|
||||
}
|
||||
|
||||
const Log_Level_Names: Array<string> = ['debug', 'log', 'info', 'warn', 'error']
|
||||
|
||||
export class Logger {
|
||||
public static tag: string = '[HaoJslog]' //可以设置当前游戏的前缀
|
||||
public static LEVEL: number = LOG_LEVEL_TYPES.WARN //当前Logger等级
|
||||
public static Log_Color_Config: Array<string> = [
|
||||
'color:#890;font-size:10px;',
|
||||
'color:#000;font-size:11px;',
|
||||
'color:#09f;font-size:12px;',
|
||||
'color:#f90;font-size:13px;',
|
||||
'color:#f00;font-size:15px;',
|
||||
]
|
||||
private static Terminal_Log: boolean = false
|
||||
public static formatNow() {
|
||||
let date: Date = new Date() //后端返回的时间戳是秒
|
||||
return (
|
||||
date.getFullYear() +
|
||||
'-' +
|
||||
(date.getMonth() + 1) +
|
||||
'-' +
|
||||
date.getDate() +
|
||||
' ' +
|
||||
date.getHours() +
|
||||
':' +
|
||||
date.getMinutes() +
|
||||
':' +
|
||||
date.getSeconds() +
|
||||
':' +
|
||||
date.getMilliseconds()
|
||||
)
|
||||
}
|
||||
private static getLogPreKey(nowLevel: number): string {
|
||||
let str: string =
|
||||
'[' +
|
||||
Logger.formatNow() +
|
||||
'] ' +
|
||||
Logger.tag +
|
||||
' [' +
|
||||
Log_Level_Names[nowLevel] +
|
||||
'] '
|
||||
return str
|
||||
}
|
||||
public static debug(...params: any) {
|
||||
if (Logger.LEVEL > LOG_LEVEL_TYPES.DEBUG) {
|
||||
return
|
||||
}
|
||||
let str: string = this.getLogPreKey(LOG_LEVEL_TYPES.DEBUG)
|
||||
let fileStr: string = str + params.join(' ')
|
||||
// LogErrorFileUtil.debug(fileStr);
|
||||
if (this.Terminal_Log) {
|
||||
console.log(
|
||||
'%c' + str,
|
||||
this.Log_Color_Config[LOG_LEVEL_TYPES.DEBUG],
|
||||
...params
|
||||
)
|
||||
} else {
|
||||
console.info(fileStr)
|
||||
}
|
||||
}
|
||||
public static log(...params: any) {
|
||||
if (Logger.LEVEL > LOG_LEVEL_TYPES.LOG) {
|
||||
return
|
||||
}
|
||||
let str: string = this.getLogPreKey(LOG_LEVEL_TYPES.LOG)
|
||||
let fileStr: string = str + params.join(' ')
|
||||
// LogErrorFileUtil.log(fileStr);
|
||||
if (this.Terminal_Log) {
|
||||
console.log(
|
||||
'%c' + str,
|
||||
this.Log_Color_Config[LOG_LEVEL_TYPES.DEBUG],
|
||||
...params
|
||||
)
|
||||
} else {
|
||||
console.info(fileStr) //console.log(str, ...params)
|
||||
}
|
||||
}
|
||||
public static info(...params: any) {
|
||||
if (Logger.LEVEL > LOG_LEVEL_TYPES.INFO) {
|
||||
return
|
||||
}
|
||||
let str: string = this.getLogPreKey(LOG_LEVEL_TYPES.INFO)
|
||||
let fileStr: string = str + params.join(' ')
|
||||
if (this.Terminal_Log) {
|
||||
console.info(
|
||||
'%c' + str,
|
||||
this.Log_Color_Config[LOG_LEVEL_TYPES.DEBUG],
|
||||
...params
|
||||
)
|
||||
} else {
|
||||
console.info(fileStr)
|
||||
}
|
||||
}
|
||||
public static warn(...params: any) {
|
||||
if (Logger.LEVEL > LOG_LEVEL_TYPES.WARN) {
|
||||
return
|
||||
}
|
||||
let str: string = this.getLogPreKey(LOG_LEVEL_TYPES.WARN)
|
||||
let fileStr: string = str + params.join(' ')
|
||||
if (this.Terminal_Log) {
|
||||
console.warn(
|
||||
'%c' + str,
|
||||
this.Log_Color_Config[LOG_LEVEL_TYPES.DEBUG],
|
||||
...params
|
||||
)
|
||||
} else {
|
||||
console.warn(fileStr)
|
||||
}
|
||||
}
|
||||
public static error(...params: any) {
|
||||
if (Logger.LEVEL > LOG_LEVEL_TYPES.ERROR) {
|
||||
return
|
||||
}
|
||||
let str: string = this.getLogPreKey(LOG_LEVEL_TYPES.ERROR)
|
||||
let fileStr: string = str + params.join(' ')
|
||||
if (this.Terminal_Log) {
|
||||
console.error(
|
||||
'%c' + str,
|
||||
this.Log_Color_Config[LOG_LEVEL_TYPES.DEBUG],
|
||||
...params
|
||||
)
|
||||
} else {
|
||||
console.error(fileStr)
|
||||
}
|
||||
}
|
||||
public static tag: string = '[HaoJslog]' //可以设置当前游戏的前缀
|
||||
public static LEVEL: number = LOG_LEVEL_TYPES.WARN //当前Logger等级
|
||||
public static Log_Color_Config: Array<string> = [
|
||||
'color:#890;font-size:10px;',
|
||||
'color:#000;font-size:11px;',
|
||||
'color:#09f;font-size:12px;',
|
||||
'color:#f90;font-size:13px;',
|
||||
'color:#f00;font-size:15px;'
|
||||
]
|
||||
private static Terminal_Log: boolean = false
|
||||
|
||||
public static formatNow() {
|
||||
let date: Date = new Date() //后端返回的时间戳是秒
|
||||
return (
|
||||
date.getFullYear() +
|
||||
'-' +
|
||||
(date.getMonth() + 1) +
|
||||
'-' +
|
||||
date.getDate() +
|
||||
' ' +
|
||||
date.getHours() +
|
||||
':' +
|
||||
date.getMinutes() +
|
||||
':' +
|
||||
date.getSeconds() +
|
||||
':' +
|
||||
date.getMilliseconds()
|
||||
)
|
||||
}
|
||||
|
||||
private static getLogPreKey(nowLevel: number): string {
|
||||
return '[' +
|
||||
Logger.formatNow() +
|
||||
'] ' +
|
||||
Logger.tag +
|
||||
' [' +
|
||||
Log_Level_Names[nowLevel] +
|
||||
'] '
|
||||
}
|
||||
|
||||
public static debug(...params: any) {
|
||||
if (Logger.LEVEL > LOG_LEVEL_TYPES.DEBUG) {
|
||||
return
|
||||
}
|
||||
let str: string = this.getLogPreKey(LOG_LEVEL_TYPES.DEBUG)
|
||||
let fileStr: string = str + params.join(' ')
|
||||
// LogErrorFileUtil.debug(fileStr);
|
||||
if (this.Terminal_Log) {
|
||||
console.log(
|
||||
'%c' + str,
|
||||
this.Log_Color_Config[LOG_LEVEL_TYPES.DEBUG],
|
||||
...params
|
||||
)
|
||||
} else {
|
||||
console.info(fileStr)
|
||||
}
|
||||
}
|
||||
|
||||
public static log(...params: any) {
|
||||
if (Logger.LEVEL > LOG_LEVEL_TYPES.LOG) {
|
||||
return
|
||||
}
|
||||
let str: string = this.getLogPreKey(LOG_LEVEL_TYPES.LOG)
|
||||
let fileStr: string = str + params.join(' ')
|
||||
// LogErrorFileUtil.log(fileStr);
|
||||
if (this.Terminal_Log) {
|
||||
console.log(
|
||||
'%c' + str,
|
||||
this.Log_Color_Config[LOG_LEVEL_TYPES.DEBUG],
|
||||
...params
|
||||
)
|
||||
} else {
|
||||
console.info(fileStr) //console.log(str, ...params)
|
||||
}
|
||||
}
|
||||
|
||||
public static info(...params: any) {
|
||||
if (Logger.LEVEL > LOG_LEVEL_TYPES.INFO) {
|
||||
return
|
||||
}
|
||||
let str: string = this.getLogPreKey(LOG_LEVEL_TYPES.INFO)
|
||||
let fileStr: string = str + params.join(' ')
|
||||
if (this.Terminal_Log) {
|
||||
console.info(
|
||||
'%c' + str,
|
||||
this.Log_Color_Config[LOG_LEVEL_TYPES.DEBUG],
|
||||
...params
|
||||
)
|
||||
} else {
|
||||
console.info(fileStr)
|
||||
}
|
||||
}
|
||||
|
||||
public static warn(...params: any) {
|
||||
if (Logger.LEVEL > LOG_LEVEL_TYPES.WARN) {
|
||||
return
|
||||
}
|
||||
let str: string = this.getLogPreKey(LOG_LEVEL_TYPES.WARN)
|
||||
let fileStr: string = str + params.join(' ')
|
||||
if (this.Terminal_Log) {
|
||||
console.warn(
|
||||
'%c' + str,
|
||||
this.Log_Color_Config[LOG_LEVEL_TYPES.DEBUG],
|
||||
...params
|
||||
)
|
||||
} else {
|
||||
console.warn(fileStr)
|
||||
}
|
||||
}
|
||||
|
||||
public static error(...params: any) {
|
||||
if (Logger.LEVEL > LOG_LEVEL_TYPES.ERROR) {
|
||||
return
|
||||
}
|
||||
let str: string = this.getLogPreKey(LOG_LEVEL_TYPES.ERROR)
|
||||
let fileStr: string = str + params.join(' ')
|
||||
if (this.Terminal_Log) {
|
||||
console.error(
|
||||
'%c' + str,
|
||||
this.Log_Color_Config[LOG_LEVEL_TYPES.DEBUG],
|
||||
...params
|
||||
)
|
||||
} else {
|
||||
console.error(fileStr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,61 +1,64 @@
|
||||
import { _decorator, Vec2 } from 'cc'
|
||||
import { Vec2 } from 'cc'
|
||||
|
||||
export default class MathUtils {
|
||||
/**
|
||||
* 2个点之前的直线距离
|
||||
* @param p1
|
||||
* @param p2
|
||||
*/
|
||||
public static distance(x1: number, y1: number, x2: number, y2: number) {
|
||||
// 设两点A(X1,Y1),B(X2,Y2)
|
||||
// 距离D=(X2-X1)的平方+(Y2-Y1)平方的和开平方
|
||||
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2))
|
||||
}
|
||||
/**
|
||||
* 2个点之前的直线距离
|
||||
* @param x1
|
||||
* @param y1
|
||||
* @param x2
|
||||
* @param y2
|
||||
*/
|
||||
public static distance(x1: number, y1: number, x2: number, y2: number) {
|
||||
// 设两点A(X1,Y1),B(X2,Y2)
|
||||
// 距离D=(X2-X1)的平方+(Y2-Y1)平方的和开平方
|
||||
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2))
|
||||
}
|
||||
|
||||
/**
|
||||
* 2点间的向量
|
||||
* @param p1
|
||||
* @param p2
|
||||
*/
|
||||
public static sub(p1: Vec2, p2: Vec2) {
|
||||
return new Vec2(p1.x - p2.x, p1.y - p2.y)
|
||||
}
|
||||
/**
|
||||
* 2点间的向量
|
||||
* @param p1
|
||||
* @param p2
|
||||
*/
|
||||
public static sub(p1: Vec2, p2: Vec2) {
|
||||
return new Vec2(p1.x - p2.x, p1.y - p2.y)
|
||||
}
|
||||
|
||||
/**
|
||||
* 弧度转角度
|
||||
* @param radians
|
||||
*/
|
||||
public static radiansToDegrees(radians: number) {
|
||||
return (180 / Math.PI) * radians
|
||||
}
|
||||
/**
|
||||
* 弧度转角度
|
||||
* @param radians
|
||||
*/
|
||||
public static radiansToDegrees(radians: number) {
|
||||
return (180 / Math.PI) * radians
|
||||
}
|
||||
|
||||
/**
|
||||
* 角度转弧度
|
||||
* @param degrees
|
||||
*/
|
||||
public static degreesToRadians(degrees: number) {
|
||||
return (Math.PI * degrees) / 180
|
||||
}
|
||||
/**
|
||||
* 角度转弧度
|
||||
* @param degrees
|
||||
*/
|
||||
public static degreesToRadians(degrees: number) {
|
||||
return (Math.PI * degrees) / 180
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回2点间的弧度
|
||||
* @param startP
|
||||
* @param endP
|
||||
*/
|
||||
public static p2pRad(startP: Vec2, endP: Vec2) {
|
||||
let rad: number = Math.atan2(endP.y - startP.y, endP.x - startP.x)
|
||||
return rad
|
||||
}
|
||||
/**
|
||||
* 返回2点间的弧度
|
||||
* @param startP
|
||||
* @param endP
|
||||
*/
|
||||
public static p2pRad(startP: Vec2, endP: Vec2) {
|
||||
let rad: number = Math.atan2(endP.y - startP.y, endP.x - startP.x)
|
||||
return rad
|
||||
}
|
||||
|
||||
/**
|
||||
* 针对捕鱼鱼的方向特定实现的鱼方向转换
|
||||
* @param rot
|
||||
*/
|
||||
public static rotation2Fish(rot: number) {
|
||||
if (rot >= 0 && rot <= 180) {
|
||||
rot = 180 - rot
|
||||
} else {
|
||||
rot = -180 - rot
|
||||
}
|
||||
return rot
|
||||
}
|
||||
/**
|
||||
* 针对捕鱼鱼的方向特定实现的鱼方向转换
|
||||
* @param rot
|
||||
*/
|
||||
public static rotation2Fish(rot: number) {
|
||||
if (rot >= 0 && rot <= 180) {
|
||||
rot = 180 - rot
|
||||
} else {
|
||||
rot = -180 - rot
|
||||
}
|
||||
return rot
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,62 +1,62 @@
|
||||
import { _decorator, Node, Vec3, Vec2 } from 'cc'
|
||||
import { Logger } from './Logger'
|
||||
import { Node, Vec2, Vec3 } from 'cc'
|
||||
import MathUtils from './MathUtils'
|
||||
|
||||
export class MoveHelper {
|
||||
public static _vec3: Vec3 = new Vec3()
|
||||
public static _vec2_0: Vec2 = new Vec2()
|
||||
public static _vec2_1: Vec2 = new Vec2()
|
||||
public static moveNode(
|
||||
moveNode: Node,
|
||||
speed: number,
|
||||
tx: number,
|
||||
ty: number,
|
||||
minSpeed: number = 0.01
|
||||
) {
|
||||
let isMoving: boolean = false
|
||||
let times: number = 0
|
||||
moveNode.getPosition(MoveHelper._vec3)
|
||||
MoveHelper._vec2_0.x = MoveHelper._vec3.x
|
||||
MoveHelper._vec2_0.y = MoveHelper._vec3.y
|
||||
MoveHelper._vec2_1.x = tx
|
||||
MoveHelper._vec2_1.y = ty
|
||||
let rad: number = MathUtils.p2pRad(MoveHelper._vec2_0, MoveHelper._vec2_1)
|
||||
let speedX: number = speed * Math.cos(rad)
|
||||
let speedY: number = speed * Math.sin(rad)
|
||||
if (Math.abs(MoveHelper._vec3.x - tx) > minSpeed) {
|
||||
times = Math.floor(Math.abs(speedX / minSpeed))
|
||||
for (let i = 0; i < times; i++) {
|
||||
if (MoveHelper._vec3.x > tx) {
|
||||
MoveHelper._vec3.x -= minSpeed
|
||||
moveNode.setPosition(MoveHelper._vec3)
|
||||
} else {
|
||||
MoveHelper._vec3.x += minSpeed
|
||||
moveNode.setPosition(MoveHelper._vec3)
|
||||
}
|
||||
if (Math.abs(MoveHelper._vec3.x - tx) <= minSpeed * 2) {
|
||||
MoveHelper._vec3.x = tx
|
||||
moveNode.setPosition(MoveHelper._vec3)
|
||||
}
|
||||
}
|
||||
isMoving = true
|
||||
}
|
||||
if (Math.abs(MoveHelper._vec3.y - ty) > minSpeed) {
|
||||
times = Math.floor(Math.abs(speedY / minSpeed))
|
||||
for (let j = 0; j < times; j++) {
|
||||
if (MoveHelper._vec3.y > ty) {
|
||||
MoveHelper._vec3.y -= minSpeed
|
||||
moveNode.setPosition(MoveHelper._vec3)
|
||||
} else {
|
||||
MoveHelper._vec3.y += minSpeed
|
||||
moveNode.setPosition(MoveHelper._vec3)
|
||||
}
|
||||
if (Math.abs(MoveHelper._vec3.x - ty) <= minSpeed * 2) {
|
||||
MoveHelper._vec3.y = ty
|
||||
moveNode.setPosition(MoveHelper._vec3)
|
||||
}
|
||||
}
|
||||
isMoving = true
|
||||
}
|
||||
return isMoving
|
||||
}
|
||||
public static _vec3: Vec3 = new Vec3()
|
||||
public static _vec2_0: Vec2 = new Vec2()
|
||||
public static _vec2_1: Vec2 = new Vec2()
|
||||
|
||||
public static moveNode(
|
||||
moveNode: Node,
|
||||
speed: number,
|
||||
tx: number,
|
||||
ty: number,
|
||||
minSpeed: number = 0.01
|
||||
) {
|
||||
let isMoving: boolean = false
|
||||
let times: number = 0
|
||||
moveNode.getPosition(MoveHelper._vec3)
|
||||
MoveHelper._vec2_0.x = MoveHelper._vec3.x
|
||||
MoveHelper._vec2_0.y = MoveHelper._vec3.y
|
||||
MoveHelper._vec2_1.x = tx
|
||||
MoveHelper._vec2_1.y = ty
|
||||
let rad: number = MathUtils.p2pRad(MoveHelper._vec2_0, MoveHelper._vec2_1)
|
||||
let speedX: number = speed * Math.cos(rad)
|
||||
let speedY: number = speed * Math.sin(rad)
|
||||
if (Math.abs(MoveHelper._vec3.x - tx) > minSpeed) {
|
||||
times = Math.floor(Math.abs(speedX / minSpeed))
|
||||
for (let i = 0; i < times; i++) {
|
||||
if (MoveHelper._vec3.x > tx) {
|
||||
MoveHelper._vec3.x -= minSpeed
|
||||
moveNode.setPosition(MoveHelper._vec3)
|
||||
} else {
|
||||
MoveHelper._vec3.x += minSpeed
|
||||
moveNode.setPosition(MoveHelper._vec3)
|
||||
}
|
||||
if (Math.abs(MoveHelper._vec3.x - tx) <= minSpeed * 2) {
|
||||
MoveHelper._vec3.x = tx
|
||||
moveNode.setPosition(MoveHelper._vec3)
|
||||
}
|
||||
}
|
||||
isMoving = true
|
||||
}
|
||||
if (Math.abs(MoveHelper._vec3.y - ty) > minSpeed) {
|
||||
times = Math.floor(Math.abs(speedY / minSpeed))
|
||||
for (let j = 0; j < times; j++) {
|
||||
if (MoveHelper._vec3.y > ty) {
|
||||
MoveHelper._vec3.y -= minSpeed
|
||||
moveNode.setPosition(MoveHelper._vec3)
|
||||
} else {
|
||||
MoveHelper._vec3.y += minSpeed
|
||||
moveNode.setPosition(MoveHelper._vec3)
|
||||
}
|
||||
if (Math.abs(MoveHelper._vec3.x - ty) <= minSpeed * 2) {
|
||||
MoveHelper._vec3.y = ty
|
||||
moveNode.setPosition(MoveHelper._vec3)
|
||||
}
|
||||
}
|
||||
isMoving = true
|
||||
}
|
||||
return isMoving
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +1,30 @@
|
||||
import { AssetManager, Prefab, _decorator } from 'cc'
|
||||
const { ccclass, property } = _decorator
|
||||
|
||||
import { _decorator, AssetManager, Prefab } from 'cc'
|
||||
import { Logger } from './Logger'
|
||||
|
||||
const { ccclass, property } = _decorator
|
||||
|
||||
@ccclass('PrefabLoader')
|
||||
export default class PrefabLoader {
|
||||
private static isLoading: boolean = false
|
||||
private static isLoading: boolean = false
|
||||
|
||||
public static loadPrefab(url: string, callback: Function) {
|
||||
if (this.isLoading) return
|
||||
this.isLoading = true
|
||||
AssetManager.instance.resources.load(
|
||||
url,
|
||||
Prefab,
|
||||
(error: Error, loadedResource) => {
|
||||
if (error) {
|
||||
Logger.warn(this, '载入Prefab失败, 原因:', url, error.message)
|
||||
return
|
||||
}
|
||||
if (!(loadedResource instanceof Prefab)) {
|
||||
Logger.warn(this, '你载入的不是Prefab, 你做了什么事?')
|
||||
return
|
||||
}
|
||||
callback(loadedResource)
|
||||
this.isLoading = false
|
||||
}
|
||||
)
|
||||
}
|
||||
public static loadPrefab(url: string, callback: Function) {
|
||||
if (this.isLoading) return
|
||||
this.isLoading = true
|
||||
AssetManager.instance.resources.load(
|
||||
url,
|
||||
Prefab,
|
||||
(error: Error, loadedResource) => {
|
||||
if (error) {
|
||||
Logger.warn(this, '载入Prefab失败, 原因:', url, error.message)
|
||||
return
|
||||
}
|
||||
if (!(loadedResource instanceof Prefab)) {
|
||||
Logger.warn(this, '你载入的不是Prefab, 你做了什么事?')
|
||||
return
|
||||
}
|
||||
callback(loadedResource)
|
||||
this.isLoading = false
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,50 +1,49 @@
|
||||
import { Vec2, _decorator } from 'cc'
|
||||
import { Vec2 } from 'cc'
|
||||
|
||||
export default class RandomUtil {
|
||||
//随机minNum到maxNum的数字 (包含maxNum)
|
||||
public static nextInt(minNum: number, maxNum: number) {
|
||||
return Math.floor(Math.random() * (maxNum - minNum + 1) + minNum)
|
||||
}
|
||||
//随机minNum到maxNum的数字 (包含maxNum)
|
||||
public static nextInt(minNum: number, maxNum: number) {
|
||||
return Math.floor(Math.random() * (maxNum - minNum + 1) + minNum)
|
||||
}
|
||||
|
||||
public static nextNumber(minNum: number, maxNum: number) {
|
||||
return Math.random() * (maxNum - minNum) + minNum
|
||||
}
|
||||
public static nextNumber(minNum: number, maxNum: number) {
|
||||
return Math.random() * (maxNum - minNum) + minNum
|
||||
}
|
||||
|
||||
public static nextSign() {
|
||||
let temp = Math.random()
|
||||
if (temp < 0.5) {
|
||||
return 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
public static nextSign() {
|
||||
let temp = Math.random()
|
||||
if (temp < 0.5) {
|
||||
return 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
public static nextBoolean() {
|
||||
let temp = Math.random()
|
||||
if (temp < 0.5) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
public static nextBoolean() {
|
||||
let temp = Math.random()
|
||||
return temp < 0.5;
|
||||
|
||||
public static randomArr(nowArr: Array<any>, needNum: number) {
|
||||
let tempArr: Array<any> = nowArr.concat()
|
||||
let resultArr: Array<any> = []
|
||||
for (let index = 0; index < needNum; index++) {
|
||||
if (tempArr.length <= 0) {
|
||||
break
|
||||
}
|
||||
let randomIndex: number = RandomUtil.nextInt(0, tempArr.length - 1)
|
||||
resultArr.push(tempArr.splice(randomIndex, 1)[0])
|
||||
}
|
||||
return resultArr
|
||||
}
|
||||
}
|
||||
|
||||
public static randomItem(nowArr: Array<any>) {
|
||||
return this.randomArr(nowArr, 1)[0]
|
||||
}
|
||||
public static randomArr(nowArr: Array<any>, needNum: number) {
|
||||
let tempArr: Array<any> = nowArr.concat()
|
||||
let resultArr: Array<any> = []
|
||||
for (let index = 0; index < needNum; index++) {
|
||||
if (tempArr.length <= 0) {
|
||||
break
|
||||
}
|
||||
let randomIndex: number = RandomUtil.nextInt(0, tempArr.length - 1)
|
||||
resultArr.push(tempArr.splice(randomIndex, 1)[0])
|
||||
}
|
||||
return resultArr
|
||||
}
|
||||
|
||||
public static randomP(left: number, right: number, up: number, down: number) {
|
||||
let randomX: number = RandomUtil.nextNumber(left, right)
|
||||
let randomY: number = RandomUtil.nextNumber(up, down)
|
||||
return new Vec2(randomX, randomY)
|
||||
}
|
||||
public static randomItem(nowArr: Array<any>) {
|
||||
return this.randomArr(nowArr, 1)[0]
|
||||
}
|
||||
|
||||
public static randomP(left: number, right: number, up: number, down: number) {
|
||||
let randomX: number = RandomUtil.nextNumber(left, right)
|
||||
let randomY: number = RandomUtil.nextNumber(up, down)
|
||||
return new Vec2(randomX, randomY)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,436 +1,428 @@
|
||||
import {
|
||||
_decorator,
|
||||
Node,
|
||||
Material,
|
||||
Color,
|
||||
UIRenderer,
|
||||
Vec2,
|
||||
UITransform,
|
||||
} from 'cc'
|
||||
import { Color, Material, Node, UIRenderer, UITransform, Vec2 } from 'cc'
|
||||
import ShaderMaterialPrefab from '../../game/prefab/ShaderMaterialPrefab'
|
||||
import { Logger } from './Logger'
|
||||
|
||||
export default class ShaderHelper {
|
||||
/**
|
||||
* 清除所有shader
|
||||
* @param showNode
|
||||
* @param material
|
||||
*/
|
||||
public static clearAllEffect(
|
||||
showNode: Node,
|
||||
material: Material = ShaderMaterialPrefab.instance.getComponent(
|
||||
ShaderMaterialPrefab
|
||||
).default
|
||||
) {
|
||||
showNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
showNode.children.forEach((childNode) => {
|
||||
childNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 清除所有shader
|
||||
* @param showNode
|
||||
* @param material
|
||||
*/
|
||||
public static clearAllEffect(
|
||||
showNode: Node,
|
||||
material: Material = ShaderMaterialPrefab.instance.getComponent(
|
||||
ShaderMaterialPrefab
|
||||
).default
|
||||
) {
|
||||
showNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
showNode.children.forEach((childNode) => {
|
||||
childNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置图片灰白程度
|
||||
* @param showNode
|
||||
* @param material
|
||||
* @param grayLevel [0.0, 1.0]
|
||||
*/
|
||||
public static setGrayEffect(
|
||||
showNode: Node,
|
||||
grayLevel: number = 1,
|
||||
material: Material = ShaderMaterialPrefab.instance.getComponent(
|
||||
ShaderMaterialPrefab
|
||||
).grayMaterial
|
||||
) {
|
||||
showNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
material.setProperty('grayLevel', grayLevel)
|
||||
renderComponent.setMaterial(material, 0)
|
||||
})
|
||||
showNode.children.forEach((childNode) => {
|
||||
childNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
material.setProperty('grayLevel', grayLevel)
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 设置图片灰白程度
|
||||
* @param showNode
|
||||
* @param grayLevel
|
||||
* @param material
|
||||
*/
|
||||
public static setGrayEffect(
|
||||
showNode: Node,
|
||||
grayLevel: number = 1,
|
||||
material: Material = ShaderMaterialPrefab.instance.getComponent(
|
||||
ShaderMaterialPrefab
|
||||
).grayMaterial
|
||||
) {
|
||||
showNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
material.setProperty('grayLevel', grayLevel)
|
||||
renderComponent.setMaterial(material, 0)
|
||||
})
|
||||
showNode.children.forEach((childNode) => {
|
||||
childNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
material.setProperty('grayLevel', grayLevel)
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 播放变灰过程动画
|
||||
*/
|
||||
public static showGrayMv(showNode: Node) {
|
||||
let grayValue: number = 0.5
|
||||
let intervalId = setInterval(() => {
|
||||
grayValue += 0.01
|
||||
if (grayValue >= 1) {
|
||||
grayValue = 1
|
||||
clearInterval(intervalId)
|
||||
}
|
||||
if (showNode) {
|
||||
ShaderHelper.setGrayEffect(showNode, grayValue)
|
||||
}
|
||||
}, 1)
|
||||
}
|
||||
/**
|
||||
* 播放变灰过程动画
|
||||
*/
|
||||
public static showGrayMv(showNode: Node) {
|
||||
let grayValue: number = 0.5
|
||||
let intervalId = setInterval(() => {
|
||||
grayValue += 0.01
|
||||
if (grayValue >= 1) {
|
||||
grayValue = 1
|
||||
clearInterval(intervalId)
|
||||
}
|
||||
if (showNode) {
|
||||
ShaderHelper.setGrayEffect(showNode, grayValue)
|
||||
}
|
||||
}, 1)
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置图片老化
|
||||
* @param showNode
|
||||
* @param grayLevel [0.0, 1.0]
|
||||
* @param material
|
||||
*/
|
||||
public static setOldPhotoEffect(
|
||||
showNode: Node,
|
||||
grayLevel: number = 1,
|
||||
material: Material = ShaderMaterialPrefab.instance.getComponent(
|
||||
ShaderMaterialPrefab
|
||||
).oldPhoto
|
||||
) {
|
||||
showNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
material.setProperty('oldLevel', grayLevel)
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
showNode.children.forEach((childNode) => {
|
||||
childNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
material.setProperty('oldLevel', grayLevel)
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 设置图片老化
|
||||
* @param showNode
|
||||
* @param grayLevel
|
||||
* @param material
|
||||
*/
|
||||
public static setOldPhotoEffect(
|
||||
showNode: Node,
|
||||
grayLevel: number = 1,
|
||||
material: Material = ShaderMaterialPrefab.instance.getComponent(
|
||||
ShaderMaterialPrefab
|
||||
).oldPhoto
|
||||
) {
|
||||
showNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
material.setProperty('oldLevel', grayLevel)
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
showNode.children.forEach((childNode) => {
|
||||
childNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
material.setProperty('oldLevel', grayLevel)
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 播放变灰过程动画
|
||||
*/
|
||||
public static showOldPhotoMv(showNode: Node) {
|
||||
let grayValue: number = 0
|
||||
let intervalId = setInterval(() => {
|
||||
grayValue += 0.01
|
||||
if (grayValue >= 1) {
|
||||
grayValue = 1
|
||||
clearInterval(intervalId)
|
||||
}
|
||||
if (showNode) {
|
||||
ShaderHelper.setOldPhotoEffect(showNode, grayValue)
|
||||
}
|
||||
}, 1)
|
||||
}
|
||||
/**
|
||||
* 播放变灰过程动画
|
||||
*/
|
||||
public static showOldPhotoMv(showNode: Node) {
|
||||
let grayValue: number = 0
|
||||
let intervalId = setInterval(() => {
|
||||
grayValue += 0.01
|
||||
if (grayValue >= 1) {
|
||||
grayValue = 1
|
||||
clearInterval(intervalId)
|
||||
}
|
||||
if (showNode) {
|
||||
ShaderHelper.setOldPhotoEffect(showNode, grayValue)
|
||||
}
|
||||
}, 1)
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加内发光特效
|
||||
* showNode:要增加特效的节点或者他的子节点
|
||||
* material:发光特效材质
|
||||
* materialParam: {}
|
||||
* materialParam.glowColor:cc.v4(r,g,b,a) 颜色rbga值的结构体
|
||||
* materialParam.glowColorSize:这里为约束一下值发光宽度值在 [0.0, 0.1] 因为 0.1+ 之后的效果可能不明显,也可以自己尝试修改,个人测试感觉0.01效果最佳
|
||||
* materialParam.glowThreshold:这里为约束一下值发光阈值值在 [0.0, 0.5] 因为 0.5+ 之后的效果可能就是其他效果,个人感觉0.1效果最佳
|
||||
*/
|
||||
public static setGlowInner(
|
||||
showNode: Node,
|
||||
materialParam: any,
|
||||
material: Material = ShaderMaterialPrefab.instance.getComponent(
|
||||
ShaderMaterialPrefab
|
||||
).glowInner
|
||||
) {
|
||||
showNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
material.setProperty('glowColor', materialParam.glowColor)
|
||||
material.setProperty('glowColorSize', materialParam.glowColorSize)
|
||||
material.setProperty('glowThreshold', materialParam.glowThreshold)
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
showNode.children.forEach((childNode) => {
|
||||
childNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
material.setProperty('glowColor', materialParam.glowColor)
|
||||
material.setProperty('glowColorSize', materialParam.glowColorSize)
|
||||
material.setProperty('glowThreshold', materialParam.glowThreshold)
|
||||
renderComponent.setMaterial(material, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 增加内发光特效
|
||||
* showNode:要增加特效的节点或者他的子节点
|
||||
* material:发光特效材质
|
||||
* materialParam: {}
|
||||
* materialParam.glowColor:cc.v4(r,g,b,a) 颜色rbga值的结构体
|
||||
* materialParam.glowColorSize:这里为约束一下值发光宽度值在 [0.0, 0.1] 因为 0.1+ 之后的效果可能不明显,也可以自己尝试修改,个人测试感觉0.01效果最佳
|
||||
* materialParam.glowThreshold:这里为约束一下值发光阈值值在 [0.0, 0.5] 因为 0.5+ 之后的效果可能就是其他效果,个人感觉0.1效果最佳
|
||||
*/
|
||||
public static setGlowInner(
|
||||
showNode: Node,
|
||||
materialParam: any,
|
||||
material: Material = ShaderMaterialPrefab.instance.getComponent(
|
||||
ShaderMaterialPrefab
|
||||
).glowInner
|
||||
) {
|
||||
showNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
material.setProperty('glowColor', materialParam.glowColor)
|
||||
material.setProperty('glowColorSize', materialParam.glowColorSize)
|
||||
material.setProperty('glowThreshold', materialParam.glowThreshold)
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
showNode.children.forEach((childNode) => {
|
||||
childNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
material.setProperty('glowColor', materialParam.glowColor)
|
||||
material.setProperty('glowColorSize', materialParam.glowColorSize)
|
||||
material.setProperty('glowThreshold', materialParam.glowThreshold)
|
||||
renderComponent.setMaterial(material, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置不同颜色的发光
|
||||
* @param showNode
|
||||
* @param color
|
||||
*/
|
||||
public static setCommonGlowInner(showNode: Node, color: Color = Color.WHITE) {
|
||||
this.setGlowInner(showNode, {
|
||||
glowColor: color,
|
||||
glowColorSize: 0.015,
|
||||
glowThreshold: 0.1,
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 设置不同颜色的发光
|
||||
* @param showNode
|
||||
* @param color
|
||||
*/
|
||||
public static setCommonGlowInner(showNode: Node, color: Color = Color.WHITE) {
|
||||
this.setGlowInner(showNode, {
|
||||
glowColor: color,
|
||||
glowColorSize: 0.015,
|
||||
glowThreshold: 0.1
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 播放被攻击闪烁过程动画
|
||||
*/
|
||||
public static showFlash(showNode: Node, totalFlashTimes: number = 1) {
|
||||
let timeCount: number = 0
|
||||
let color: Color = Color.WHITE
|
||||
let flashTimes: number = 0
|
||||
let intervalId = setInterval(() => {
|
||||
timeCount += 1
|
||||
if (timeCount % 50 == 0) {
|
||||
let tempCount: number = timeCount / 50
|
||||
if (tempCount % 2 == 0) {
|
||||
color.a = 100
|
||||
this.setGlowInner(showNode, {
|
||||
glowColor: color,
|
||||
glowColorSize: 0.1,
|
||||
glowThreshold: 0,
|
||||
})
|
||||
} else {
|
||||
flashTimes++
|
||||
this.setGlowInner(showNode, {
|
||||
glowColor: color,
|
||||
glowColorSize: 0,
|
||||
glowThreshold: 0,
|
||||
})
|
||||
if (flashTimes > totalFlashTimes) {
|
||||
clearInterval(intervalId)
|
||||
}
|
||||
}
|
||||
}
|
||||
}, 1)
|
||||
}
|
||||
/**
|
||||
* 播放被攻击闪烁过程动画
|
||||
*/
|
||||
public static showFlash(showNode: Node, totalFlashTimes: number = 1) {
|
||||
let timeCount: number = 0
|
||||
let color: Color = Color.WHITE
|
||||
let flashTimes: number = 0
|
||||
let intervalId = setInterval(() => {
|
||||
timeCount += 1
|
||||
if (timeCount % 50 == 0) {
|
||||
let tempCount: number = timeCount / 50
|
||||
if (tempCount % 2 == 0) {
|
||||
color.a = 100
|
||||
this.setGlowInner(showNode, {
|
||||
glowColor: color,
|
||||
glowColorSize: 0.1,
|
||||
glowThreshold: 0
|
||||
})
|
||||
} else {
|
||||
flashTimes++
|
||||
this.setGlowInner(showNode, {
|
||||
glowColor: color,
|
||||
glowColorSize: 0,
|
||||
glowThreshold: 0
|
||||
})
|
||||
if (flashTimes > totalFlashTimes) {
|
||||
clearInterval(intervalId)
|
||||
}
|
||||
}
|
||||
}
|
||||
}, 1)
|
||||
}
|
||||
|
||||
/**
|
||||
* 马赛克
|
||||
* @param showNode
|
||||
* @param materialParam
|
||||
* @param material
|
||||
*/
|
||||
public static setMosaic(
|
||||
showNode: Node,
|
||||
materialParam: any,
|
||||
material: Material = ShaderMaterialPrefab.instance.getComponent(
|
||||
ShaderMaterialPrefab
|
||||
).mosaic
|
||||
) {
|
||||
showNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
material.setProperty('xBlockCount', materialParam.xBlockCount)
|
||||
material.setProperty('yBlockCount', materialParam.yBlockCount)
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
showNode.children.forEach((childNode) => {
|
||||
childNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
material.setProperty('xBlockCount', materialParam.xBlockCount)
|
||||
material.setProperty('yBlockCount', materialParam.yBlockCount)
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 马赛克
|
||||
* @param showNode
|
||||
* @param materialParam
|
||||
* @param material
|
||||
*/
|
||||
public static setMosaic(
|
||||
showNode: Node,
|
||||
materialParam: any,
|
||||
material: Material = ShaderMaterialPrefab.instance.getComponent(
|
||||
ShaderMaterialPrefab
|
||||
).mosaic
|
||||
) {
|
||||
showNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
material.setProperty('xBlockCount', materialParam.xBlockCount)
|
||||
material.setProperty('yBlockCount', materialParam.yBlockCount)
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
showNode.children.forEach((childNode) => {
|
||||
childNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
material.setProperty('xBlockCount', materialParam.xBlockCount)
|
||||
material.setProperty('yBlockCount', materialParam.yBlockCount)
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 播放被攻击闪烁过程动画
|
||||
*/
|
||||
public static showMosaicMv(showNode: Node, callback: Function = null) {
|
||||
let masaicTimes: number = 500
|
||||
let intervalId = setInterval(() => {
|
||||
masaicTimes -= 2
|
||||
this.setMosaic(showNode, {
|
||||
xBlockCount: masaicTimes,
|
||||
yBlockCount: masaicTimes,
|
||||
})
|
||||
if (masaicTimes <= 30) {
|
||||
clearInterval(intervalId)
|
||||
if (callback) {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
}, 1)
|
||||
}
|
||||
/**
|
||||
* 播放被攻击闪烁过程动画
|
||||
*/
|
||||
public static showMosaicMv(showNode: Node, callback: Function = null) {
|
||||
let masaicTimes: number = 500
|
||||
let intervalId = setInterval(() => {
|
||||
masaicTimes -= 2
|
||||
this.setMosaic(showNode, {
|
||||
xBlockCount: masaicTimes,
|
||||
yBlockCount: masaicTimes
|
||||
})
|
||||
if (masaicTimes <= 30) {
|
||||
clearInterval(intervalId)
|
||||
if (callback) {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
}, 1)
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置圆角剪切
|
||||
* @param showNode
|
||||
* @param roundCornerRadius [0, 1]
|
||||
*/
|
||||
public static setRoundCornerCrop(
|
||||
showNode: Node,
|
||||
roundCornerRadius: number = 0.1,
|
||||
material: Material = ShaderMaterialPrefab.instance.getComponent(
|
||||
ShaderMaterialPrefab
|
||||
).roundCornerCrop
|
||||
) {
|
||||
showNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
// material.setProperty("roundCornerRadius", roundCornerRadius);
|
||||
material.setProperty('xRadius', roundCornerRadius)
|
||||
material.setProperty('yRadius', roundCornerRadius)
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
showNode.children.forEach((childNode) => {
|
||||
childNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
// material.setProperty("roundCornerRadius", roundCornerRadius);
|
||||
material.setProperty('xRadius', roundCornerRadius)
|
||||
material.setProperty('yRadius', roundCornerRadius)
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 设置圆角剪切
|
||||
* @param showNode
|
||||
* @param roundCornerRadius
|
||||
* @param material
|
||||
*/
|
||||
public static setRoundCornerCrop(
|
||||
showNode: Node,
|
||||
roundCornerRadius: number = 0.1,
|
||||
material: Material = ShaderMaterialPrefab.instance.getComponent(
|
||||
ShaderMaterialPrefab
|
||||
).roundCornerCrop
|
||||
) {
|
||||
showNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
// material.setProperty("roundCornerRadius", roundCornerRadius);
|
||||
material.setProperty('xRadius', roundCornerRadius)
|
||||
material.setProperty('yRadius', roundCornerRadius)
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
showNode.children.forEach((childNode) => {
|
||||
childNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
// material.setProperty("roundCornerRadius", roundCornerRadius);
|
||||
material.setProperty('xRadius', roundCornerRadius)
|
||||
material.setProperty('yRadius', roundCornerRadius)
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置闪光
|
||||
* @param showNode
|
||||
* @param lightColor 光颜色
|
||||
* @param lightWidth 光的宽度
|
||||
* @param lightAngle 光的角度
|
||||
* @param enableGradient
|
||||
* @param cropAlpha
|
||||
* @param enableFog
|
||||
* @param material
|
||||
*/
|
||||
public static setFlashLight(
|
||||
showNode: Node,
|
||||
lightColor: Color,
|
||||
lightWidth: number,
|
||||
lightAngle: number = 0,
|
||||
enableGradient: boolean = true,
|
||||
cropAlpha: boolean = true,
|
||||
enableFog: boolean = false,
|
||||
material: Material = ShaderMaterialPrefab.instance.getComponent(
|
||||
ShaderMaterialPrefab
|
||||
).flashLight
|
||||
) {
|
||||
showNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
material.setProperty('lightColor', lightColor)
|
||||
material.setProperty('lightWidth', lightWidth)
|
||||
material.setProperty('lightAngle', lightAngle)
|
||||
material.setProperty('enableGradient', enableGradient ? 1 : 0)
|
||||
material.setProperty('cropAlpha', cropAlpha ? 1 : 0)
|
||||
material.setProperty('enableFog', enableFog ? 1 : 0)
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
showNode.children.forEach((childNode) => {
|
||||
childNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
material.setProperty('lightColor', lightColor)
|
||||
material.setProperty('lightWidth', lightWidth)
|
||||
material.setProperty('lightAngle', lightAngle)
|
||||
material.setProperty('enableGradient', enableGradient ? 1 : 0)
|
||||
material.setProperty('cropAlpha', cropAlpha ? 1 : 0)
|
||||
material.setProperty('enableFog', enableFog ? 1 : 0)
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 设置闪光
|
||||
* @param showNode
|
||||
* @param lightColor 光颜色
|
||||
* @param lightWidth 光的宽度
|
||||
* @param lightAngle 光的角度
|
||||
* @param enableGradient
|
||||
* @param cropAlpha
|
||||
* @param enableFog
|
||||
* @param material
|
||||
*/
|
||||
public static setFlashLight(
|
||||
showNode: Node,
|
||||
lightColor: Color,
|
||||
lightWidth: number,
|
||||
lightAngle: number = 0,
|
||||
enableGradient: boolean = true,
|
||||
cropAlpha: boolean = true,
|
||||
enableFog: boolean = false,
|
||||
material: Material = ShaderMaterialPrefab.instance.getComponent(
|
||||
ShaderMaterialPrefab
|
||||
).flashLight
|
||||
) {
|
||||
showNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
material.setProperty('lightColor', lightColor)
|
||||
material.setProperty('lightWidth', lightWidth)
|
||||
material.setProperty('lightAngle', lightAngle)
|
||||
material.setProperty('enableGradient', enableGradient ? 1 : 0)
|
||||
material.setProperty('cropAlpha', cropAlpha ? 1 : 0)
|
||||
material.setProperty('enableFog', enableFog ? 1 : 0)
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
showNode.children.forEach((childNode) => {
|
||||
childNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
material.setProperty('lightColor', lightColor)
|
||||
material.setProperty('lightWidth', lightWidth)
|
||||
material.setProperty('lightAngle', lightAngle)
|
||||
material.setProperty('enableGradient', enableGradient ? 1 : 0)
|
||||
material.setProperty('cropAlpha', cropAlpha ? 1 : 0)
|
||||
material.setProperty('enableFog', enableFog ? 1 : 0)
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 玩家升级shader动画
|
||||
* @param showNode
|
||||
* @param callback
|
||||
*/
|
||||
public static showFlashLightMv(showNode: Node, callback: Function = null) {
|
||||
let nowClor: Color = new Color(0, 0, 0, 255)
|
||||
let colorIndex: number = 0
|
||||
let lightAngle: number = 0
|
||||
let intervalId = setInterval(() => {
|
||||
if (colorIndex == 0) {
|
||||
nowClor.r = nowClor.r + 2
|
||||
if (nowClor.r >= 255) {
|
||||
colorIndex += 1
|
||||
}
|
||||
} else if (colorIndex == 1) {
|
||||
nowClor.g = nowClor.g + 2
|
||||
if (nowClor.g >= 255) {
|
||||
colorIndex += 1
|
||||
}
|
||||
} else {
|
||||
nowClor.b = nowClor.b + 2
|
||||
if (nowClor.b >= 255) {
|
||||
clearInterval(intervalId)
|
||||
ShaderHelper.clearAllEffect(showNode)
|
||||
if (callback) {
|
||||
callback()
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
lightAngle += 1
|
||||
this.setFlashLight(showNode, nowClor, 1, lightAngle)
|
||||
}, 1)
|
||||
}
|
||||
/**
|
||||
* 玩家升级shader动画
|
||||
* @param showNode
|
||||
* @param callback
|
||||
*/
|
||||
public static showFlashLightMv(showNode: Node, callback: Function = null) {
|
||||
let nowClor: Color = new Color(0, 0, 0, 255)
|
||||
let colorIndex: number = 0
|
||||
let lightAngle: number = 0
|
||||
let intervalId = setInterval(() => {
|
||||
if (colorIndex == 0) {
|
||||
nowClor.r = nowClor.r + 2
|
||||
if (nowClor.r >= 255) {
|
||||
colorIndex += 1
|
||||
}
|
||||
} else if (colorIndex == 1) {
|
||||
nowClor.g = nowClor.g + 2
|
||||
if (nowClor.g >= 255) {
|
||||
colorIndex += 1
|
||||
}
|
||||
} else {
|
||||
nowClor.b = nowClor.b + 2
|
||||
if (nowClor.b >= 255) {
|
||||
clearInterval(intervalId)
|
||||
ShaderHelper.clearAllEffect(showNode)
|
||||
if (callback) {
|
||||
callback()
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
lightAngle += 1
|
||||
this.setFlashLight(showNode, nowClor, 1, lightAngle)
|
||||
}, 1)
|
||||
}
|
||||
|
||||
public static setFlag(
|
||||
showNode: Node,
|
||||
material: Material = ShaderMaterialPrefab.instance.getComponent(
|
||||
ShaderMaterialPrefab
|
||||
).flag
|
||||
) {
|
||||
showNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
showNode.children.forEach((childNode) => {
|
||||
childNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
public static setFlag(
|
||||
showNode: Node,
|
||||
material: Material = ShaderMaterialPrefab.instance.getComponent(
|
||||
ShaderMaterialPrefab
|
||||
).flag
|
||||
) {
|
||||
showNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
showNode.children.forEach((childNode) => {
|
||||
childNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置高斯模糊
|
||||
* @param showNode
|
||||
* @param material
|
||||
*/
|
||||
public static setGaussian(
|
||||
showNode: Node,
|
||||
material: Material = ShaderMaterialPrefab.instance.getComponent(
|
||||
ShaderMaterialPrefab
|
||||
).gaussian
|
||||
) {
|
||||
showNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
let tran = renderComponent.node.getComponent(UITransform)
|
||||
material.setProperty(
|
||||
'textureSize',
|
||||
new Vec2(tran.contentSize.width, tran.contentSize.height)
|
||||
)
|
||||
renderComponent.setMaterial(material, 0)
|
||||
})
|
||||
showNode.children.forEach((childNode) => {
|
||||
childNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
let tran = renderComponent.node.getComponent(UITransform)
|
||||
material.setProperty(
|
||||
'textureSize',
|
||||
new Vec2(tran.contentSize.width, tran.contentSize.height)
|
||||
)
|
||||
// material.setProperty("textureSize", cc.v2(showNode.width, showNode.height));
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 设置高斯模糊
|
||||
* @param showNode
|
||||
* @param material
|
||||
*/
|
||||
public static setGaussian(
|
||||
showNode: Node,
|
||||
material: Material = ShaderMaterialPrefab.instance.getComponent(
|
||||
ShaderMaterialPrefab
|
||||
).gaussian
|
||||
) {
|
||||
showNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
let tran = renderComponent.node.getComponent(UITransform)
|
||||
material.setProperty(
|
||||
'textureSize',
|
||||
new Vec2(tran.contentSize.width, tran.contentSize.height)
|
||||
)
|
||||
renderComponent.setMaterial(material, 0)
|
||||
})
|
||||
showNode.children.forEach((childNode) => {
|
||||
childNode
|
||||
.getComponents(UIRenderer)
|
||||
.forEach((renderComponent: UIRenderer) => {
|
||||
let tran = renderComponent.node.getComponent(UITransform)
|
||||
material.setProperty(
|
||||
'textureSize',
|
||||
new Vec2(tran.contentSize.width, tran.contentSize.height)
|
||||
)
|
||||
// material.setProperty("textureSize", cc.v2(showNode.width, showNode.height));
|
||||
renderComponent.setSharedMaterial(material, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,139 +1,140 @@
|
||||
import { sys, _decorator } from 'cc'
|
||||
import { sys } from 'cc'
|
||||
import ManifestConfig from '../config/ManifestConfig'
|
||||
import EventManager from './EventManager'
|
||||
import HotUpdate from './HotUpdate'
|
||||
|
||||
export default class VersionManager {
|
||||
public static instance: VersionManager = new VersionManager()
|
||||
public static instance: VersionManager = new VersionManager()
|
||||
|
||||
public static Config_Game_Name: Array<string> = ['游戏大厅']
|
||||
public static Config_Game_Name: Array<string> = ['游戏大厅']
|
||||
|
||||
//热更文件下载来后存放文件夹
|
||||
public static Config_Key: Array<string> = ['main-remote-asset']
|
||||
//热更文件下载来后存放文件夹
|
||||
public static Config_Key: Array<string> = ['main-remote-asset']
|
||||
|
||||
private static Config_ManifestName: string = 'project.manifest'
|
||||
private static Config_ManifestName: string = 'project.manifest'
|
||||
|
||||
public static Config_Url_Key: Array<string> = ['main']
|
||||
public static Config_Url_Key: Array<string> = ['main']
|
||||
|
||||
public iosStoreUrl: string = ''
|
||||
public apkStoreUrl: string = ''
|
||||
public iosStoreUrl: string = ''
|
||||
public apkStoreUrl: string = ''
|
||||
|
||||
public nowVersion: string = ManifestConfig.version //网页显示版本号,如果是热更会替换改值
|
||||
public targetVersion: string = '1.0.0'
|
||||
public nowVersion: string = ManifestConfig.version //网页显示版本号,如果是热更会替换改值
|
||||
public targetVersion: string = '1.0.0'
|
||||
|
||||
public isOpenHotUpdate: boolean = true //是否打开热更
|
||||
public isOpenHotUpdate: boolean = true //是否打开热更
|
||||
|
||||
private hotUpdateList: Array<HotUpdate> = []
|
||||
private hotUpdateList: Array<HotUpdate> = []
|
||||
|
||||
private noUpdateIndex: number = -1 //
|
||||
private noUpdateIndex: number = -1 //
|
||||
|
||||
public init() {
|
||||
this.reInitAll()
|
||||
}
|
||||
public reInitAll() {
|
||||
this.releaseAll()
|
||||
for (let i = 0; i < VersionManager.Config_Key.length; i++) {
|
||||
this.reInit(i)
|
||||
}
|
||||
}
|
||||
public init() {
|
||||
this.reInitAll()
|
||||
}
|
||||
|
||||
public releaseAll() {
|
||||
for (let i = 0; i < VersionManager.Config_Key.length; i++) {
|
||||
if (this.hotUpdateList[i]) {
|
||||
this.hotUpdateList[i].disposeUpdate()
|
||||
}
|
||||
}
|
||||
}
|
||||
public reInitAll() {
|
||||
this.releaseAll()
|
||||
for (let i = 0; i < VersionManager.Config_Key.length; i++) {
|
||||
this.reInit(i)
|
||||
}
|
||||
}
|
||||
|
||||
public reInit(index: number) {
|
||||
if (!this.hotUpdateList[index]) {
|
||||
this.hotUpdateList[index] = new HotUpdate()
|
||||
}
|
||||
this.hotUpdateList[index].init(
|
||||
index,
|
||||
VersionManager.Config_Key[index],
|
||||
VersionManager.Config_ManifestName
|
||||
)
|
||||
if (!this.isOpenHotUpdate) {
|
||||
this.hotUpdateList[index].isCheck = true
|
||||
this.hotUpdateList[index].isFinishUpdate = true
|
||||
}
|
||||
}
|
||||
public releaseAll() {
|
||||
for (let i = 0; i < VersionManager.Config_Key.length; i++) {
|
||||
if (this.hotUpdateList[i]) {
|
||||
this.hotUpdateList[i].disposeUpdate()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public checkUpdate(keyIndex: number) {
|
||||
if (keyIndex < this.hotUpdateList.length) {
|
||||
let hotUpdate: HotUpdate = this.hotUpdateList[keyIndex]
|
||||
if (sys.isNative) {
|
||||
if (keyIndex == this.noUpdateIndex) {
|
||||
//在大厅热更,不用子游戏热更了
|
||||
hotUpdate.isCheck = true
|
||||
hotUpdate.isFinishUpdate = true
|
||||
EventManager.instance.dispatchEvent(
|
||||
HotUpdate.Event_On_ALREADY_UP_TO_DATE,
|
||||
VersionManager.Config_Key[keyIndex]
|
||||
)
|
||||
} else {
|
||||
hotUpdate.checkUpdate()
|
||||
}
|
||||
} else {
|
||||
hotUpdate.isCheck = true
|
||||
hotUpdate.isFinishUpdate = true
|
||||
EventManager.instance.dispatchEvent(
|
||||
HotUpdate.Event_On_ALREADY_UP_TO_DATE,
|
||||
VersionManager.Config_Key[keyIndex]
|
||||
)
|
||||
}
|
||||
} else {
|
||||
EventManager.instance.dispatchEvent(
|
||||
HotUpdate.Event_On_ALREADY_UP_TO_DATE,
|
||||
VersionManager.Config_Key[keyIndex]
|
||||
)
|
||||
}
|
||||
}
|
||||
public reInit(index: number) {
|
||||
if (!this.hotUpdateList[index]) {
|
||||
this.hotUpdateList[index] = new HotUpdate()
|
||||
}
|
||||
this.hotUpdateList[index].init(
|
||||
index,
|
||||
VersionManager.Config_Key[index],
|
||||
VersionManager.Config_ManifestName
|
||||
)
|
||||
if (!this.isOpenHotUpdate) {
|
||||
this.hotUpdateList[index].isCheck = true
|
||||
this.hotUpdateList[index].isFinishUpdate = true
|
||||
}
|
||||
}
|
||||
|
||||
public startUpdate(keyIndex: number) {
|
||||
let hotUpdate: HotUpdate = this.hotUpdateList[keyIndex]
|
||||
hotUpdate.startUpdate()
|
||||
}
|
||||
public checkUpdate(keyIndex: number) {
|
||||
if (keyIndex < this.hotUpdateList.length) {
|
||||
let hotUpdate: HotUpdate = this.hotUpdateList[keyIndex]
|
||||
if (sys.isNative) {
|
||||
if (keyIndex == this.noUpdateIndex) {
|
||||
//在大厅热更,不用子游戏热更了
|
||||
hotUpdate.isCheck = true
|
||||
hotUpdate.isFinishUpdate = true
|
||||
EventManager.instance.dispatchEvent(
|
||||
HotUpdate.Event_On_ALREADY_UP_TO_DATE,
|
||||
VersionManager.Config_Key[keyIndex]
|
||||
)
|
||||
} else {
|
||||
hotUpdate.checkUpdate()
|
||||
}
|
||||
} else {
|
||||
hotUpdate.isCheck = true
|
||||
hotUpdate.isFinishUpdate = true
|
||||
EventManager.instance.dispatchEvent(
|
||||
HotUpdate.Event_On_ALREADY_UP_TO_DATE,
|
||||
VersionManager.Config_Key[keyIndex]
|
||||
)
|
||||
}
|
||||
} else {
|
||||
EventManager.instance.dispatchEvent(
|
||||
HotUpdate.Event_On_ALREADY_UP_TO_DATE,
|
||||
VersionManager.Config_Key[keyIndex]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
public isCheck(keyIndex: number) {
|
||||
if (keyIndex < this.hotUpdateList.length) {
|
||||
let hotUpdate: HotUpdate = this.hotUpdateList[keyIndex]
|
||||
if (keyIndex == this.noUpdateIndex) {
|
||||
return true
|
||||
}
|
||||
return hotUpdate.isCheck
|
||||
}
|
||||
return true
|
||||
}
|
||||
public startUpdate(keyIndex: number) {
|
||||
let hotUpdate: HotUpdate = this.hotUpdateList[keyIndex]
|
||||
hotUpdate.startUpdate()
|
||||
}
|
||||
|
||||
public needUpdate(keyIndex: number) {
|
||||
if (keyIndex < this.hotUpdateList.length) {
|
||||
let hotUpdate: HotUpdate = this.hotUpdateList[keyIndex]
|
||||
if (keyIndex == this.noUpdateIndex) {
|
||||
return false
|
||||
}
|
||||
return hotUpdate.needUpdate
|
||||
}
|
||||
return false
|
||||
}
|
||||
public isCheck(keyIndex: number) {
|
||||
if (keyIndex < this.hotUpdateList.length) {
|
||||
let hotUpdate: HotUpdate = this.hotUpdateList[keyIndex]
|
||||
if (keyIndex == this.noUpdateIndex) {
|
||||
return true
|
||||
}
|
||||
return hotUpdate.isCheck
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
public isUpdating(keyIndex: number) {
|
||||
if (keyIndex < this.hotUpdateList.length) {
|
||||
let hotUpdate: HotUpdate = this.hotUpdateList[keyIndex]
|
||||
return hotUpdate.isUpdating
|
||||
}
|
||||
return false
|
||||
}
|
||||
public needUpdate(keyIndex: number) {
|
||||
if (keyIndex < this.hotUpdateList.length) {
|
||||
let hotUpdate: HotUpdate = this.hotUpdateList[keyIndex]
|
||||
if (keyIndex == this.noUpdateIndex) {
|
||||
return false
|
||||
}
|
||||
return hotUpdate.needUpdate
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
public isFinishUpdate(keyIndex: number) {
|
||||
if (keyIndex < this.hotUpdateList.length) {
|
||||
let hotUpdate: HotUpdate = this.hotUpdateList[keyIndex]
|
||||
if (keyIndex == this.noUpdateIndex) {
|
||||
return true
|
||||
}
|
||||
return hotUpdate.isFinishUpdate
|
||||
}
|
||||
return true
|
||||
}
|
||||
public isUpdating(keyIndex: number) {
|
||||
if (keyIndex < this.hotUpdateList.length) {
|
||||
let hotUpdate: HotUpdate = this.hotUpdateList[keyIndex]
|
||||
return hotUpdate.isUpdating
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
public isFinishUpdate(keyIndex: number) {
|
||||
if (keyIndex < this.hotUpdateList.length) {
|
||||
let hotUpdate: HotUpdate = this.hotUpdateList[keyIndex]
|
||||
if (keyIndex == this.noUpdateIndex) {
|
||||
return true
|
||||
}
|
||||
return hotUpdate.isFinishUpdate
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user