342 lines
12 KiB
TypeScript
342 lines
12 KiB
TypeScript
import Define from "./Define";
|
||
import ButtonEffect from "./ButtonEffect";
|
||
import UserInfo from "../UserInfo";
|
||
import LDataCannon from "../datas/LDataCannon";
|
||
|
||
|
||
// Learn TypeScript:
|
||
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
|
||
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
|
||
// Learn Attribute:
|
||
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
|
||
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
|
||
// Learn life-cycle callbacks:
|
||
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
|
||
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
|
||
|
||
|
||
export default class Common {
|
||
|
||
|
||
public static nodeUiRoot:cc.Node;
|
||
public static nodeGameRoot:cc.Node;
|
||
|
||
public static nodeBallRoot:cc.Node;
|
||
public static nodeGoldRoot:cc.Node;
|
||
public static nodePrompt:cc.Node;
|
||
public static nodeAddGold:cc.Node;
|
||
|
||
public static nodeSoundRoot:cc.Node;
|
||
|
||
|
||
|
||
public static nodeParticleEffects:cc.Node;
|
||
public static nodeSounds:cc.Node;
|
||
|
||
public static addPowerEffect:cc.Node;
|
||
|
||
public static addPower:number = 1;
|
||
|
||
public static pauseServer:boolean = false
|
||
|
||
//每秒发射的数量/s
|
||
public static shootCount:number = 3;
|
||
public static bulletWidth:number = 25;
|
||
public static isFirstCreatStone:boolean = true;
|
||
//是否正在游戏中
|
||
public static isPlaying:boolean = false;
|
||
//是否暂停
|
||
public static isPause:boolean = false;
|
||
//是否游戏失败
|
||
public static isGameOver:boolean = false;
|
||
//是否开启声音
|
||
public static isOpenSound:boolean = true;
|
||
//是否可以复活
|
||
public static isCanRevive = true;
|
||
|
||
public static isGod:boolean = false;
|
||
//离线期间的奖励
|
||
public static offLineRewardCount:number = 0;
|
||
|
||
public static isShowGoldFlyEffect:boolean = false;
|
||
//是否是在主界面进行的登录
|
||
public static isInMainViewLogin:boolean = true;
|
||
|
||
//当前使用的id
|
||
public static curUseCannonId:number = 0
|
||
public static curDataCannon:LDataCannon;
|
||
|
||
//游戏中玩家的数据
|
||
public static curLevelScore:number = 0; //当前关卡的分数
|
||
public static maxLevelScore:number = 0; //当前关卡需要通关的分数
|
||
public static totalShowScore:number = 0; //统计显示分数 每次攻击球涨一分
|
||
public static curLevel:number = 0; //当前关卡数
|
||
public static curLevelGold:number = 0; //当前关卡收集的钱
|
||
public static lastShowScore:number = 0; //上次的分数
|
||
|
||
public static resetDatas(){
|
||
this.curLevel = UserInfo.curLevel;
|
||
this.curLevelScore = 0;
|
||
this.curLevelGold = UserInfo.curGold;
|
||
this.totalShowScore = this.lastShowScore;
|
||
this.isFirstCreatStone = true;
|
||
this.isCanRevive = true;
|
||
this.isGod = false;
|
||
this.addPower = 1
|
||
}
|
||
|
||
public static getRandom(min:number,maxr:number):number{ // 0 2 返回 0 1
|
||
|
||
return Math.floor(Math.random() *(maxr - min) + min);
|
||
}
|
||
|
||
public static isCollision(circleNode:cc.Node,rectNode:cc.Node,radius:number):boolean{
|
||
|
||
//圆形中心与矩形中心的相对坐标
|
||
var x = circleNode.x - rectNode.x;
|
||
var y = circleNode.y - rectNode.y;
|
||
|
||
var minX = Math.min(x, rectNode.getContentSize().width /2);
|
||
var maxX = Math.max(minX, -rectNode.getContentSize().width/2);
|
||
var minY = Math.min(y, rectNode.getContentSize().height/2);
|
||
var maxY = Math.max(minY, -rectNode.getContentSize().height/2);
|
||
|
||
if((maxX - x) * (maxX - x) + (maxY - y) * (maxY - y) <= radius * radius)
|
||
{
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
//获得子弹偏移
|
||
public static getBulletOffsetX(row:number,index:number):number{
|
||
|
||
if(row%2 == 0) //是偶数
|
||
{
|
||
let count = Math.floor(row/2); // 2 0 1 2 3
|
||
if(count > index)
|
||
{
|
||
return 16 * -1 * (count - index);
|
||
}
|
||
else{
|
||
return 16 * 1 * (row - index);
|
||
}
|
||
}
|
||
else{ //基数
|
||
let count = Math.floor(row/2);
|
||
|
||
if(count == index){
|
||
return 0
|
||
}
|
||
else if(count > index)
|
||
{
|
||
return 25 * -1 * (count - index);
|
||
}
|
||
else{
|
||
return 25 * 1 * (row - index);
|
||
}
|
||
}
|
||
}
|
||
|
||
private static calculateCubicBezierPoint(t:number, p0:cc.Vec2, p1:cc.Vec2, p2:cc.Vec2):cc.Vec2
|
||
{
|
||
var u:number = 1 - t;
|
||
var tt:number = t * t;
|
||
var uu:number = u * u;
|
||
|
||
var p:cc.Vec2 = new cc.Vec2(uu * p0.x, uu * p0.y);
|
||
|
||
var t1:number = 2 * u * t;
|
||
|
||
var t2:cc.Vec2 = new cc.Vec2(t1 * p1.x, t1 * p1.y);
|
||
|
||
var t3:cc.Vec2 = new cc.Vec2(tt * p2.x, tt * p2.y);
|
||
|
||
p = new cc.Vec2(p.x + t2.x + t3.x, p.y+t2.y + t3.y);
|
||
|
||
return p;
|
||
}
|
||
//获得一个曲线的路径
|
||
public static getBeizerList( startPoint:cc.Vec2, controlPoint:cc.Vec2, endPoint:cc.Vec2,segmentNum:number):cc.Vec2[]
|
||
{
|
||
let arr:cc.Vec2[] = [];
|
||
|
||
for (var i:number = 1; i <= segmentNum; i++)
|
||
{
|
||
var t:number = i / segmentNum;
|
||
var pixel:cc.Vec2 = this.calculateCubicBezierPoint(t, startPoint,controlPoint, endPoint);
|
||
arr.push(pixel);
|
||
}
|
||
return arr;
|
||
}
|
||
/*
|
||
当最大分数≥200时,超过的200的分数全部使用【ID9】的颜色,
|
||
200以下的分数分为10个阶段,阶段数字越大使用的颜色ID越大。
|
||
|
||
50≤最大分数<200时,同样分为10个阶段,根据数字大小进行ID使用。数字越大使用ID颜色越大
|
||
使用id0--id9的颜色
|
||
|
||
10≤最大分数分数<50时,使用分数/10,分5个阶段,根据分数大小进行颜色划分
|
||
阶段数字越大,使用的颜色ID越大。可选为全部颜色【ID0-5】
|
||
|
||
1≤最大分数<10时,使用最大分数/3.可选颜色为【ID0-ID3】
|
||
阶段数字越大,使用的颜色ID越大
|
||
*/
|
||
|
||
public static getStoneColor(levelMaxScore:number,score:number):cc.Color{
|
||
let colorIndex = 0;
|
||
if(levelMaxScore >= 200){
|
||
if(score >= 200){
|
||
colorIndex = 9;
|
||
}else{
|
||
colorIndex = Math.ceil(score/20) - 1;
|
||
}
|
||
}else if(levelMaxScore < 200 && levelMaxScore >= 50){
|
||
let tempValue = levelMaxScore /10
|
||
colorIndex = Math.ceil(score/tempValue) - 1;
|
||
}else if(levelMaxScore < 50 && levelMaxScore >= 10){
|
||
let tempValue = levelMaxScore /5
|
||
colorIndex = Math.ceil(score/tempValue) - 1
|
||
} else if(levelMaxScore < 10 && levelMaxScore >= 1){
|
||
let tempValue = levelMaxScore /3
|
||
colorIndex = Math.ceil(score/tempValue) - 1
|
||
}
|
||
return Define.ballColorRGBArr[colorIndex];
|
||
}
|
||
//添加点击事件
|
||
public static addClickEvent(node:cc.Node,call:Function,isClickEffect:boolean = true){
|
||
if(node != null){
|
||
let effect = node.addComponent(ButtonEffect);
|
||
effect.setClickCallBack(call,isClickEffect);
|
||
}
|
||
}
|
||
//快速排序
|
||
public static quickSort(arr:number[]):number[]{
|
||
if(arr.length <= 1){
|
||
return arr;
|
||
}
|
||
var pivotIndex = Math.floor(arr.length/2);
|
||
var pivot = arr.splice(pivotIndex,1)[0];
|
||
var left = [] as number[];
|
||
var right = [] as number[];
|
||
|
||
for(var i =0;i<arr.length;i++){
|
||
if (arr[i] < pivot) {
|
||
left.push(arr[i]);
|
||
} else {
|
||
right.push(arr[i]);
|
||
}
|
||
}
|
||
return this.quickSort(left).concat([pivot], this.quickSort(right));
|
||
}
|
||
//左右移动的动作
|
||
public static actionLeftRightRotate(node:cc.Node,time:number = 0.2,range:number = 5,delayTime:number = 2){
|
||
let actionLeftRotate = cc.rotateTo(time,range)
|
||
let actionRightRotate = cc.rotateTo(time,range*-1)
|
||
let actionCenterRotate = cc.rotateTo(time,0)
|
||
|
||
node.runAction( cc.repeatForever(
|
||
cc.sequence(
|
||
actionLeftRotate,
|
||
actionRightRotate,
|
||
actionLeftRotate,
|
||
actionRightRotate,
|
||
actionCenterRotate,
|
||
cc.delayTime(delayTime),
|
||
))
|
||
);
|
||
}
|
||
//动作闪烁红色 白色 用于游戏结束
|
||
public static actionTinkColor(node:cc.Node,callBack:Function = null,count:number = 5,tinkSpeed:number = 0.1){
|
||
let action = cc.sequence(cc.tintTo(tinkSpeed,255,0,0),cc.tintTo(tinkSpeed,255,255,255))
|
||
node.runAction(cc.sequence(cc.repeat(action,count),cc.callFunc(function(){
|
||
if(callBack != null){
|
||
callBack();
|
||
}
|
||
}.bind(this))));
|
||
}
|
||
//大小缩放
|
||
public static actionBigSmall(node:cc.Node,scale1:number,scale2:number){
|
||
let action = cc.sequence(cc.scaleTo(1.2,scale1),cc.scaleTo(1,scale2))
|
||
node.runAction(cc.repeatForever(action));
|
||
}
|
||
|
||
public static showPrompt(str:string,offsetY:number = 450){
|
||
this.nodePrompt.stopAllActions();
|
||
this.nodePrompt.active = true;
|
||
this.nodePrompt.position = new cc.Vec2(320,offsetY);
|
||
this.nodePrompt.children[0].getComponent<cc.Label>(cc.Label).string = str;
|
||
this.nodePrompt.setScale(1,0);
|
||
this.nodePrompt.runAction(cc.sequence(cc.scaleTo(0.2,1,1),cc.delayTime(1),cc.scaleTo(0.2,1,0),cc.callFunc(function(){
|
||
this.nodePrompt.active = false;
|
||
}.bind(this))),
|
||
)
|
||
}
|
||
|
||
//获得显示的数字
|
||
public static getShowNumber(num:number):string{
|
||
|
||
let suffix:string = "";
|
||
|
||
if(num >= 10000){
|
||
num = Math.floor((num * 0.0001)*100)/100;
|
||
suffix = "w";
|
||
}else if(num >= 1000){
|
||
num = Math.floor((num * 0.001)*100)/100;
|
||
suffix = "k";
|
||
}
|
||
|
||
let tt = Number(num).toFixed(1).toString();
|
||
|
||
if (tt.search(".") != -1){
|
||
while(tt.substr(tt.length - 1,1) == "0"){
|
||
tt = tt.substr(0,tt.length-1);
|
||
}
|
||
if(tt.substr(tt.length - 1,1) == "."){
|
||
tt = tt.substr(0,tt.length-1);
|
||
}
|
||
}
|
||
let showNumber = tt + suffix;
|
||
return showNumber;
|
||
}
|
||
|
||
//判断当前日期为当月第几周
|
||
public static getMonthWeek(a, b, c):number{
|
||
//a = d = 当前日期
|
||
//b = 6 - w = 当前周的还有几天过完(不算今天)
|
||
//a + b 的和在除以7 就是当天是当前月份的第几周
|
||
var date = new Date(a, parseInt(b) - 1, c), w = date.getDay(), d = date.getDate();
|
||
return Math.ceil((d + 6 - w) / 7);
|
||
}
|
||
//判断当前日期为当年第几周
|
||
public static getYearWeek(a, b, c):number{
|
||
//date1是当前日期
|
||
//date2是当年第一天
|
||
//d是当前日期是今年第多少天
|
||
//用d + 当前年的第一天的周差距的和在除以7就是本年第几周
|
||
var date1 = new Date(a, parseInt(b) - 1, c), date2 = new Date(a, 0, 1),
|
||
d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000);
|
||
return Math.ceil((d + ((date2.getDay() + 1) - 1)) / 7);
|
||
}
|
||
|
||
static compile(code):string{
|
||
var str =String.fromCharCode(code.charCodeAt(0)+code.length);
|
||
for(var i=1;i<code.length;i++){
|
||
str+=String.fromCharCode(code.charCodeAt(i)+code.charCodeAt(i-1));
|
||
}
|
||
return str;
|
||
}
|
||
|
||
static uncompile(code):string{
|
||
var c=String.fromCharCode(code.charCodeAt(0)-code.length);
|
||
for(var i=1;i<code.length;i++){
|
||
c+=String.fromCharCode(code.charCodeAt(i)-c.charCodeAt(i-1));
|
||
}
|
||
return c;
|
||
}
|
||
|
||
}
|