Commit 8020f9d6 authored by liujiaxin's avatar liujiaxin

feat: with backend interface

parent 8113a10a
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -46,13 +46,33 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -46,13 +46,33 @@ export default class SceneComponent extends MyCocosSceneComponent {
} }
onLoadEnd() { onLoadEnd() {
globalThis.USER_TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE2Mzk2MjE3MTIsImV4cCI6MTY0MjIxMzcxMiwiaWQiOjQwMDY2LCJwYXNzd29yZCI6IjE5QTEwM0E3NUUzM0QzQTNCODg0QkQxNTUwMjhENTA1Iiwic2Nob29sX2lkIjoyODYsInBob25lIjoiMTMxMDAwMDAwMDAiLCJuYW1lIjoi5bCP546J5rWL6K-VMCIsInN0YXR1cyI6MCwidHlwZSI6MCwicm9sZXMiOjEsImV4cGlyZWQiOiIyMDIyLTAzLTE5VDE2OjAwOjAwLjAwMFoiLCJzY2hvb2xfbmFtZSI6Iue7mOeOqea1i-ivleeOr-Wig-WtpuagoSIsInNjaG9vbF90eXBlIjoyLCJjcmVhdGVvbiI6MTU3MDc2MTk3NDAwMCwiY2xhc3NfbmFtZSI6IuS4ieW5tDHnj60iLCJjbGFzc19pbmZvIjpbeyJjbGFzc19pZCI6MzkxNSwiY2xhc3NfbmFtZSI6IuS4ieW5tDHnj60ifV0sIm90aGVycyI6W119.4T-Lnsl25aJ-rvUOCDwShq4F5u3BbR86ZLPew4zyULU"
globalThis.CLIENT_TYPE = 'student'
globalThis.SERVER_HOST = 'http://192.168.3.127:3900'
// globalThis.SERVER_HOST = 'http://staging.ireadabc.com'
this.initGame(); this.initGame();
this.initView();
this.initEvent();
this.initTipSys();
// TODO 加载完成后的逻辑写在这里, 下面的代码仅供参考 // TODO 加载完成后的逻辑写在这里, 下面的代码仅供参考
Api.askUser().then(() => { this.fetchUserData();
return Promise.resolve()
}
fetchUserData() {
this.showWaitingLetters();
return Api.askUser().then(() => {
this.hideWaitingLetters()
this.initData(); this.initData();
this.initView(); }).catch((e) => {
this.initEvent(); this.hideWaitingLetters()
this.initTipSys(); this.openTipAlert({message: '网络错误,请返回重新进入',
callback: () => {
this.closeTipAlert();
this.fetchUserData();
}})
}) })
} }
initGame() { initGame() {
...@@ -68,12 +88,17 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -68,12 +88,17 @@ export default class SceneComponent extends MyCocosSceneComponent {
private home: cc.Node; private home: cc.Node;
private kitchen: cc.Node; private kitchen: cc.Node;
private shop: cc.Node; private shop: cc.Node;
private loading: cc.Node;
tws: any[] = [];
LoadingCount: number = 1;
initView() { initView() {
this.tws = [];
this.LoadingCount = 0;
this.home = pg.view.find(this, "home"); this.home = pg.view.find(this, "home");
this.kitchen = pg.view.find(this, "kitchen"); this.kitchen = pg.view.find(this, "kitchen");
this.shop = pg.view.find(this, "shop"); this.shop = pg.view.find(this, "shop");
this.elephant = pg.view.find(this.home, "elephant"); this.elephant = pg.view.find(this.home, "elephant");
this.loading = pg.view.find(this, "loading");
this.elephentState(0); this.elephentState(0);
} }
...@@ -116,12 +141,17 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -116,12 +141,17 @@ export default class SceneComponent extends MyCocosSceneComponent {
this.updateShopBtns(); this.updateShopBtns();
} }
onTouchShopHouse() { onTouchShopHouse() {
this.openTipEmpty(() => {
this.closeTipEmpty(); this.shopType = ITEM_TYPE.HOUSE;
}) const list = this.updateShopList();
// this.shopType = ITEM_TYPE.HOUSE;
// this.updateShopList(); if (!list.length) {
// this.updateShopBtns(); this.openTipEmpty(() => {
this.closeTipEmpty();
});
return;
}
this.updateShopBtns();
} }
onTouchShopClothes() { onTouchShopClothes() {
this.openTipEmpty(() => { this.openTipEmpty(() => {
...@@ -138,12 +168,16 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -138,12 +168,16 @@ export default class SceneComponent extends MyCocosSceneComponent {
updateShopList() { updateShopList() {
let scroll = pg.view.find(this.shop, "scrollview"); let scroll = pg.view.find(this.shop, "scrollview");
let svt = scroll.getComponent("svt"); let svt = scroll.getComponent("svt");
let list = kitchen.getListByType(this.shopType); let list = shop.getListByType(this.shopType);
svt.list = list; svt.list = list;
if (list.length == 0) {
return list;
}
svt.updateItem = this.updateShopItems; svt.updateItem = this.updateShopItems;
svt.target = this; svt.target = this;
svt.addNum = 4; svt.addNum = 4;
svt.flush(); svt.flush();
return list;
} }
updateShopBtns() { updateShopBtns() {
let btns = pg.view.find(this.shop, "btns"); let btns = pg.view.find(this.shop, "btns");
...@@ -165,44 +199,49 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -165,44 +199,49 @@ export default class SceneComponent extends MyCocosSceneComponent {
for (let i = 0; i < datas.length; i++) { for (let i = 0; i < datas.length; i++) {
let it = item.children[i]; let it = item.children[i];
let dt = datas[i]; let dt = datas[i];
this.updateShopItem(it, dt); if (dt) {
// console.log(dt.name);
this.updateShopItem(it, dt);
}
} }
} }
updateShopItem(item, data: Item) { updateShopItem(item, data: Item) {
item.active = true; item.active = true;
let growth = pg.view.find(item, "growth"); let growth = pg.view.find(item, "bg_food/growth");
let txt = pg.view.find(item, "txt"); let txt = pg.view.find(item, "bg_food/txt");
let name = pg.view.find(item, "name"); let name = pg.view.find(item, "bg_food/name");
let icon = pg.view.find(item, "icon"); let icon = pg.view.find(item, "bg_food/icon");
let discount = pg.view.find(item, "discount"); let discount = pg.view.find(item, "bg_food/discount");
let btn_buy = pg.view.find(item, "btn_buy"); let btn_buy = pg.view.find(item, "bg_food/btn_buy");
pg.view.setString(txt, `可增加 个成长值`) pg.view.setString(txt, `可增加 个成长值`)
pg.view.setString(growth, data.growthValue) pg.view.setString(growth, data.growthValue)
pg.view.setString(name, data.name) pg.view.setString(name, data.name)
icon.getComponent(cc.Sprite).spriteFrame = this[data.icon]; // console.log(data.cover);
pg.view.setNetImg(icon, data.cover, false)
// icon.getComponent(cc.Sprite).spriteFrame = this[data.icon];
let dis = Math.round((1 - data.discount) * 100); let dis = Math.round((1 - data.discount) * 100);
if (dis > 0) { if (dis > 0) {
pg.view.setString(discount, `${dis}%`) pg.view.setString(discount, `${dis}%`)
pg.view.visible(pg.view.find(item, "bg_sale"), true); pg.view.visible(pg.view.find(item, "bg_food/bg_sale"), true);
pg.view.visible(pg.view.find(item, "discount"), true); pg.view.visible(pg.view.find(item, "bg_food/discount"), true);
} else { } else {
pg.view.visible(pg.view.find(item, "bg_sale"), false); pg.view.visible(pg.view.find(item, "bg_food/bg_sale"), false);
pg.view.visible(pg.view.find(item, "discount"), false); pg.view.visible(pg.view.find(item, "bg_food/discount"), false);
} }
pg.view.setString(pg.view.find(btn_buy, "num"), data.cost) pg.view.setString(pg.view.find(btn_buy, "num"), data.cost)
pg.view.touchOn(btn_buy, this.onTouchShopItem, this); pg.view.touchOn(item, this.onTouchShopItem, this);
item.data = data; item.data = data;
} }
onTouchShopItem(e) { onTouchShopItem(e) {
let item = e.target.parent; let item = e.target; // .parent;
let data: Item = item.data; let data: Item = item.data;
if (data.type == 1) { if (data.type == 1) {
//弹出购买弹窗 //弹出购买弹窗
console.log(user)
if (user.coin >= data.cost * data.discount) { if (user.coin >= data.cost * data.discount) {
this.openTipNormal(data, () => { this.openTipNormal(data, () => {
this.buy(data); this.buy(data);
...@@ -226,10 +265,28 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -226,10 +265,28 @@ export default class SceneComponent extends MyCocosSceneComponent {
} }
} }
buy(data) { buy(data) {
Api.askBuyItem(data).then(() => { this.showWaitingLetters();
this.updateShopCoin(); Api.askBuyItem(data).then((resp) => {
this.openTipBuy(data, () => { this.fetchUserData().then(ud => {
this.closeTipBuy(); this.hideWaitingLetters(true)
this.updateShopCoin();
this.openTipBuy(data, () => {
this.closeTipBuy();
});
}).catch(e => {
this.hideWaitingLetters(true);
this.openTipAlert({message: '网络错误,请重试(err: 1)',
callback: () => {
this.closeTipAlert();
}
})
});
}).catch(e => {
this.hideWaitingLetters(true);
this.openTipAlert({message: '网络错误,请重试(err: 2)',
callback: () => {
this.closeTipAlert();
}
}) })
}) })
} }
...@@ -255,10 +312,13 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -255,10 +312,13 @@ export default class SceneComponent extends MyCocosSceneComponent {
for (let i = 0; i < datas.length; i++) { for (let i = 0; i < datas.length; i++) {
let it = item.children[i]; let it = item.children[i];
let dt = datas[i]; let dt = datas[i];
this.updateChickenItem(it, dt); if (dt) {
this.updateChickenItem(it, dt);
}
} }
} }
updateChickenItem(item, data: Item) { updateChickenItem(item, data: Item) {
// console.log(data);
item.active = true; item.active = true;
let growth = pg.view.find(item, "growth"); let growth = pg.view.find(item, "growth");
let name = pg.view.find(item, "name"); let name = pg.view.find(item, "name");
...@@ -267,7 +327,8 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -267,7 +327,8 @@ export default class SceneComponent extends MyCocosSceneComponent {
pg.view.setString(growth, `成长值+${data.growthValue}`) pg.view.setString(growth, `成长值+${data.growthValue}`)
pg.view.setString(name, data.name) pg.view.setString(name, data.name)
pg.view.setString(num, data.num) pg.view.setString(num, data.num)
icon.getComponent(cc.Sprite).spriteFrame = this[data.icon]; // icon.getComponent(cc.Sprite).spriteFrame = this[data.icon];
pg.view.setNetImg(icon, data.cover)
pg.view.touchOn(item, this.onTouchChickenItem, this); pg.view.touchOn(item, this.onTouchChickenItem, this);
item.data = data; item.data = data;
} }
...@@ -283,11 +344,30 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -283,11 +344,30 @@ export default class SceneComponent extends MyCocosSceneComponent {
} }
} }
eating(data) { eating(data) {
Api.askUseItem(data).then(() => {
this.elephentState(3);
this.onTouchKitchenClose();
})
//可以检测是否升级等---升级说明之类的 //可以检测是否升级等---升级说明之类的
this.showWaitingLetters();
Api.askUseItem(data).then((resp) => {
this.fetchUserData().then(ud => {
this.hideWaitingLetters(true);
this.elephentState(3);
this.onTouchKitchenClose();
}).catch(e => {
this.hideWaitingLetters(true);
this.openTipAlert({message: '网络错误,请重试(err: 1)',
callback: () => {
this.closeTipAlert();
}
})
});
}).catch(e => {
this.hideWaitingLetters(true);
this.openTipAlert({message: '网络错误,请重试(err: 2)',
callback: () => {
this.closeTipAlert();
}
})
})
} }
...@@ -426,23 +506,28 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -426,23 +506,28 @@ export default class SceneComponent extends MyCocosSceneComponent {
private tip_buy: cc.Node; private tip_buy: cc.Node;
private tip_fail: cc.Node; private tip_fail: cc.Node;
private tip_empty: cc.Node; private tip_empty: cc.Node;
private tip_alert: cc.Node;
private tipSuccess: Function; private tipSuccess: Function;
private tipCancel: Function; private tipCancel: Function;
private tipBuySuccess: Function; private tipBuySuccess: Function;
private tipFailSuccess: Function; private tipFailSuccess: Function;
private tipFailCancel: Function; private tipFailCancel: Function;
private tipEmptySuccess: Function; private tipEmptySuccess: Function;
private tipAlertCallback: Function;
initTipSys() { initTipSys() {
this.tip_normal = pg.view.find(this, "tip_normal"); this.tip_normal = pg.view.find(this, "tip_normal");
this.tip_buy = pg.view.find(this, "tip_buy"); this.tip_buy = pg.view.find(this, "tip_buy");
this.tip_fail = pg.view.find(this, "tip_fail"); this.tip_fail = pg.view.find(this, "tip_fail");
this.tip_empty = pg.view.find(this, "tip_empty"); this.tip_empty = pg.view.find(this, "tip_empty");
this.tip_alert = pg.view.find(this, "tip_alert");
pg.view.touchOn(pg.view.find(this.tip_normal, "btn_sure"), () => { this.tipSuccess && this.tipSuccess() }, this); pg.view.touchOn(pg.view.find(this.tip_normal, "btn_sure"), () => { this.tipSuccess && this.tipSuccess() }, this);
pg.view.touchOn(pg.view.find(this.tip_normal, "btn_cancel"), () => { this.tipCancel && this.tipCancel() }, this); pg.view.touchOn(pg.view.find(this.tip_normal, "btn_cancel"), () => { this.tipCancel && this.tipCancel() }, this);
pg.view.touchOn(pg.view.find(this.tip_buy, "btn_cancel"), () => { this.tipBuySuccess && this.tipBuySuccess() }, this); pg.view.touchOn(pg.view.find(this.tip_buy, "btn_cancel"), () => { this.tipBuySuccess && this.tipBuySuccess() }, this);
pg.view.touchOn(pg.view.find(this.tip_fail, "btn_sure"), () => { this.tipFailSuccess && this.tipFailSuccess() }, this); pg.view.touchOn(pg.view.find(this.tip_fail, "btn_sure"), () => { this.tipFailSuccess && this.tipFailSuccess() }, this);
pg.view.touchOn(pg.view.find(this.tip_fail, "btn_cancel"), () => { this.tipFailCancel && this.tipFailCancel() }, this); pg.view.touchOn(pg.view.find(this.tip_fail, "btn_cancel"), () => { this.tipFailCancel && this.tipFailCancel() }, this);
pg.view.touchOn(pg.view.find(this.tip_empty, "btn_cancel"), () => { this.tipEmptySuccess && this.tipEmptySuccess() }, this); pg.view.touchOn(pg.view.find(this.tip_empty, "btn_cancel"), () => { this.tipEmptySuccess && this.tipEmptySuccess() }, this);
pg.view.touchOn(pg.view.find(this.tip_alert, "btn_cancel"), () => { this.tipAlertCallback && this.tipAlertCallback() }, this);
} }
openTipNormal(data: Item, success, cancel) { openTipNormal(data: Item, success, cancel) {
this.tip_normal.active = true; this.tip_normal.active = true;
...@@ -451,7 +536,8 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -451,7 +536,8 @@ export default class SceneComponent extends MyCocosSceneComponent {
if (data) { if (data) {
pg.view.setString(pg.view.find(this.tip_normal, "desc"), `确定花${data.cost * data.discount}金币购买${data.name}吗?`); pg.view.setString(pg.view.find(this.tip_normal, "desc"), `确定花${data.cost * data.discount}金币购买${data.name}吗?`);
let icon = pg.view.find(this.tip_normal, "icon"); let icon = pg.view.find(this.tip_normal, "icon");
icon.getComponent(cc.Sprite).spriteFrame = this[data.icon]; // icon.getComponent(cc.Sprite).spriteFrame = this[data.icon];
pg.view.setNetImg(icon, data.cover)
} }
} }
closeTipNormal() { closeTipNormal() {
...@@ -474,8 +560,9 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -474,8 +560,9 @@ export default class SceneComponent extends MyCocosSceneComponent {
this.tip_buy.active = true; this.tip_buy.active = true;
this.tipBuySuccess = success; this.tipBuySuccess = success;
if (data) { if (data) {
let icon = pg.view.find(this.tip_normal, "icon"); let icon = pg.view.find(this.tip_buy, "icon");
icon.getComponent(cc.Sprite).spriteFrame = this[data.icon]; // icon.getComponent(cc.Sprite).spriteFrame = this[data.icon];
pg.view.setNetImg(icon, data.cover, false)
} }
} }
closeTipBuy() { closeTipBuy() {
...@@ -492,6 +579,94 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -492,6 +579,94 @@ export default class SceneComponent extends MyCocosSceneComponent {
} }
openTipAlert({message, btnText, callback}: {message?: string, btnText?: string, callback: (...args) => void}) {
if (message) {
const tipNode = this.tip_alert.getChildByName('tip_empty');
const messageNode = tipNode.getChildByName('message');
const lbl = messageNode.getComponent(cc.RichText);
lbl.string = `<color=#333333>${message}</color>`;
}
if (btnText) {
const btnNode = this.tip_alert.getChildByName('btn_ok');
const btnTxtNode = btnNode.getChildByName('txt');
const lbl = btnTxtNode.getComponent(cc.Label);
lbl.string = btnText;
}
this.tip_alert.active = true;
this.tipAlertCallback = callback;
}
closeTipAlert() {
this.tip_alert.active = false;
}
hideWaitingLetters(force = false) {
if (force) {
this.LoadingCount = 0;
} else {
this.LoadingCount--;
}
if (this.LoadingCount <= 0) {
this.LoadingCount = 0;
setTimeout(() => {
if (this.tws){
for (const tw of this.tws) {
tw.node.y = 0;
tw.tw.stop();
}
}
this.loading.active = false;
});
}
}
showWaitingLetters() {
this.LoadingCount += 1;
if (this.loading.active) {
return;
}
const colorList = this.getRainbowColorList();
const text = this.loading.getChildByName('text')
text.children.forEach((node, idx) => {
node.color = colorList[idx];
const tw = cc.tween(node)
.delay(idx / 4)
.to(0.3, { y: 30 }, { easing: 'sineOut' })
.to(0.3, { y: -30 }, { easing: 'sineIn' })
.delay((text.children.length - idx) / 4)
.union()
.repeatForever()
.start();
this.tws.push({
tw,
node
});
});
this.loading.active = true;
}
getRainbowColorList() {
return [
cc.color(255, 255, 0),
cc.color(128, 255, 0),
cc.color(0, 255, 0),
cc.color(0, 255, 128),
cc.color(0, 255, 255),
cc.color(0, 128, 255),
cc.color(0, 0, 255),
cc.color(128, 0, 255),
cc.color(255, 0, 255),
cc.color(255, 0, 128),
cc.color(255, 0, 0),
cc.color(255, 128, 0),
cc.color(255, 255, 0),
];
}
......
...@@ -5,6 +5,7 @@ class Item { ...@@ -5,6 +5,7 @@ class Item {
public icon: string;//商品图片--可用type生成 public icon: string;//商品图片--可用type生成
public growthValue: number;//生长值 public growthValue: number;//生长值
public cost: number;//费用 public cost: number;//费用
public cover: string;
public discount: number;//折扣 public discount: number;//折扣
public num: number;//拥有数量 public num: number;//拥有数量
public levelLimite: number;//等级限制 public levelLimite: number;//等级限制
...@@ -15,8 +16,9 @@ class Item { ...@@ -15,8 +16,9 @@ class Item {
this.icon = obj.icon; this.icon = obj.icon;
this.growthValue = obj.growthValue; this.growthValue = obj.growthValue;
this.cost = obj.cost; this.cost = obj.cost;
this.discount = obj.discount; this.cover = obj.cover;
this.num = obj.num; this.discount = obj.discount || 1;
this.num = obj.amount;
this.levelLimite = obj.levelLimite; this.levelLimite = obj.levelLimite;
} }
} }
......
...@@ -14,6 +14,9 @@ class Shop { ...@@ -14,6 +14,9 @@ class Shop {
get list() { get list() {
return this._list; return this._list;
} }
getListByType(type: number) {
return this._list.filter(li => li.type == type)
}
} }
const shop = new Shop();
export default Shop; export default shop;
...@@ -5,6 +5,7 @@ class User { ...@@ -5,6 +5,7 @@ class User {
public coin: number; public coin: number;
public level: number; public level: number;
public growth: number;//当前成长值 public growth: number;//当前成长值
public growthLevel: number;
public growthDaily: number; public growthDaily: number;
public growthDailyMax: number; public growthDailyMax: number;
public eatTime: number; public eatTime: number;
...@@ -20,12 +21,13 @@ class User { ...@@ -20,12 +21,13 @@ class User {
this.coin = data.coin; this.coin = data.coin;
this.level = data.level; this.level = data.level;
this.growth = data.growth; this.growth = data.growth;
this.growthLevel = data.growthLevel;
this.growthDaily = data.growthDaily; this.growthDaily = data.growthDaily;
this.growthDailyMax = data.growthDailyMax; this.growthDailyMax = data.growthDailyMax;
this.eatTime = data.eatTime; this.eatTime = data.eatTime;
this.useFurniture = data.useFurniture; this.useFurniture = data.useFurniture;
this.useClothes = data.useClothes; this.useClothes = data.useClothes;
this.mood = 1; this.mood = data.mood;
} }
isDailyMax() { isDailyMax() {
return this.growthDailyMax >= this.growthDaily return this.growthDailyMax >= this.growthDaily
...@@ -38,18 +40,18 @@ class User { ...@@ -38,18 +40,18 @@ class User {
useCoin(val) { useCoin(val) {
this.coin -= val; this.coin -= val;
} }
//当前等级成长值最大值 // //当前等级成长值最大值
public get growthLevel(): number { // public get growthLevel(): number {
let max = 1; // let max = 1;
for (let i = 1; i < 100; i++) { // for (let i = 1; i < 100; i++) {
let val = LEVEL[`level_${i}`].growth; // let val = LEVEL[`level_${i}`].growth;
if (this.growth < val) { // if (this.growth < val) {
max = val // max = val
break; // break;
} // }
} // }
return max // return max
} // }
} }
let user = new User(); let user = new User();
export default user; export default user;
......
...@@ -2,25 +2,52 @@ import pg from "../pg"; ...@@ -2,25 +2,52 @@ import pg from "../pg";
import { ITEMS, USER } from "../config/config" import { ITEMS, USER } from "../config/config"
import user from "../model/user"; import user from "../model/user";
import kitchen from "../model/kitchen"; import kitchen from "../model/kitchen";
import shop from "../model/shop";
//获取信息,购买物品,使用物品(吃东西),穿戴衣服/更换家具 //获取信息,购买物品,使用物品(吃东西),穿戴衣服/更换家具
class Api { class Api {
static askUser() { static askUser() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
pg.http.send("GET", "http://www.baidu.com", {}).then((data: any) => { pg.http.send("GET", `${globalThis.SERVER_HOST}/api/pets/info`, null).then((userInfo: any) => {
let userInfo = USER; // let userInfo = USER;
user.parse(userInfo); try{
let kitchenInfo = ITEMS; const mood = (Date.now() - userInfo.status.eat.time) > 4 * 60 * 60 * 1000 ? 1: 0; // 4 hours
kitchen.parse(kitchenInfo); const info = {
resolve(''); name: userInfo.name,
coin: userInfo.coins,
level: userInfo.level,
eatTime: userInfo.status.eat.time,
useFurniture: userInfo.furniture,
useClothes: userInfo.dress,
growth: userInfo.current_exp,
growthLevel: userInfo.need_exp,
mood
};
console.log(info);
user.parse(info);
// let kitchenInfo = ITEMS;
kitchen.parse(userInfo.my_items);
shop.parse(userInfo.shop_items);
resolve('');
} catch(e) {
reject(e)
}
}).catch(e => {
reject(e)
}) })
}); });
} }
static askUseItem(data) { static askUseItem(data) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
pg.http.send("GET", "http://www.baidu.com", {}).then((data: any) => { pg.http.send("POST", `${globalThis.SERVER_HOST}/api/pets/use/item`, {item_id: data.id}).then((resp: any) => {
user.addGrowth(data.growthValue); // user.addGrowth(data.growthValue);
kitchen.use(data.id); // kitchen.use(data.id);
resolve(''); try {
resolve(resp);
} catch (e) {
reject(e)
}
}).catch(e => {
reject(e)
}) })
}); });
} }
...@@ -28,10 +55,17 @@ class Api { ...@@ -28,10 +55,17 @@ class Api {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
data.id; data.id;
data.type; data.type;
pg.http.send("GET", "http://www.baidu.com", {}).then((data: any) => { pg.http.send("POST", `${globalThis.SERVER_HOST}/api/pets/item/buy`, {item_id: data.id}).then((resp: any) => {
user.useCoin(data.cost * data.discount); // user.useCoin(data.cost * data.discount);
kitchen.buy(data.id); // kitchen.buy(data.id);
resolve(''); try {
resolve(resp);
} catch (e) {
reject(e)
}
}).catch(e => {
reject(e)
}) })
}); });
} }
......
...@@ -130,7 +130,7 @@ let pg = { ...@@ -130,7 +130,7 @@ let pg = {
}) })
}) })
}, },
setNetImg(item, res) { setNetImg(item, res, cache = true) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!item) return pg.logger.w("图片更换失败,传入了错误的item"); if (!item) return pg.logger.w("图片更换失败,传入了错误的item");
let node = item.node ? item.node : item; let node = item.node ? item.node : item;
...@@ -139,7 +139,7 @@ let pg = { ...@@ -139,7 +139,7 @@ let pg = {
return pg.logger.w("图片更换失败,传入了错误的res"); return pg.logger.w("图片更换失败,传入了错误的res");
} }
if (!node) return pg.logger.w("图片更换失败,传入了错误的item"); if (!node) return pg.logger.w("图片更换失败,传入了错误的item");
if (node.net_url == res) return; if (cache && node.net_url == res) return;
let w = node.width; let w = node.width;
let h = node.height; let h = node.height;
node.active = false;// node.active = false;//
...@@ -147,15 +147,30 @@ let pg = { ...@@ -147,15 +147,30 @@ let pg = {
if (!cc.isValid(node)) return pg.logger.i("节点已销毁"); if (!cc.isValid(node)) return pg.logger.i("节点已销毁");
let nw = node.width = texture.width; let nw = node.width = texture.width;
let nh = node.height = texture.height; let nh = node.height = texture.height;
let component = node.getComponent(cc.Sprite);
let spriteFrame = new cc.SpriteFrame(texture); let spriteFrame = new cc.SpriteFrame(texture);
component.spriteFrame = spriteFrame;
node.net_url = res; node.net_url = res;
const cover = node.getChildByName('cover');
if (cover) {
const {width, height} = spriteFrame.getOriginalSize()
cover.getComponent(cc.Sprite).spriteFrame = spriteFrame;
cover.width = width;
cover.height = height;
const sx = node.width / width;
const sy = node.height / height;
const s = Math.min(sx, sy);
cover.scale = Math.round(s * 1000) / 1000;
} else {
let component = node.getComponent(cc.Sprite);
component.spriteFrame = spriteFrame;
node.width = w;
node.height = h;
}
// setTimeout(() => { // setTimeout(() => {
if (!cc.isValid(node)) return pg.logger.i("节点已销毁"); if (!cc.isValid(node)) return pg.logger.i("节点已销毁");
if (!node) return pg.logger.w("节点已销毁"); if (!node) return pg.logger.w("节点已销毁");
node.width = w;
node.height = h;
node.active = true; node.active = true;
// }, 30); // }, 30);
resolve({ w: nw, h: nh }); resolve({ w: nw, h: nh });
...@@ -317,36 +332,57 @@ let pg = { ...@@ -317,36 +332,57 @@ let pg = {
http: { http: {
//http访问 //http访问
send: function (type, url, data) { send: function (type, url, data) {
return new Promise((resolve) => { return new Promise((resolve, reject) => {
return setTimeout(() => { // return setTimeout(() => {
return resolve({ status: 200 }); // return resolve({ status: 200 });
}, 60); // }, 60);
let xhr = cc.loader.getXMLHttpRequest(); let xhr = cc.loader.getXMLHttpRequest();
xhr.timeout = 5000; xhr.timeout = 30000;
xhr.responseType = "text"; xhr.responseType = "text";
xhr.open(type, url, true); xhr.open(type, url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.setRequestHeader("token", globalThis.USER_TOKEN);
xhr.setRequestHeader("Client-Type", globalThis.CLIENT_TYPE);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = () => { xhr.onreadystatechange = () => {
if (xhr.readyState !== 4) return; if (xhr.readyState !== 4) return;
if (xhr.status >= 200 && xhr.status < 300) { if (xhr.status >= 200 && xhr.status < 300) {
try { try {
let resp = xhr.responseText; let resp = xhr.responseText;
pg.logger.d("resp->" + JSON.stringify(resp)); // pg.logger.d("resp->" + JSON.stringify(resp));
if (typeof resp === 'string') {
resp = JSON.parse(resp);
// @ts-ignore
if (resp.code !=200) {
// @ts-ignore
reject(resp.message)
return
}
// @ts-ignore
resp = resp.data;
}
resolve(resp); resolve(resp);
} catch (e) { } catch (e) {
reject(e)
} }
} } else {
else { pg.logger.w("onerror->" + url + ';resp = '+ xhr.responseText);
reject(xhr.responseText)
} }
}; };
xhr.onerror = (e) => { xhr.onerror = (e) => {
pg.logger.w("onerror->" + url); pg.logger.w("onerror->" + url);
reject(e);
}; };
xhr.ontimeout = (e) => { xhr.ontimeout = (e) => {
pg.logger.w("ontimeout->" + url); pg.logger.w("ontimeout->" + url);
reject(e);
}; };
xhr.send(data); if (data) {
xhr.send(JSON.stringify(data));
} else {
xhr.send();
}
}) })
} }
......
...@@ -95,8 +95,10 @@ export class MyCocosSceneComponent extends cc.Component { ...@@ -95,8 +95,10 @@ export class MyCocosSceneComponent extends cc.Component {
// this.onLoadEnd(); // this.onLoadEnd();
// next(); // next();
// }; // };
this.onLoadEnd(); this.onLoadEnd().then(() => {
window["air"].hideAirClassLoading(); window["air"].hideAirClassLoading();
});
} else { } else {
this.onLoadEnd(); this.onLoadEnd();
} }
...@@ -115,7 +117,7 @@ export class MyCocosSceneComponent extends cc.Component { ...@@ -115,7 +117,7 @@ export class MyCocosSceneComponent extends cc.Component {
} }
onLoadEnd() { onLoadEnd() {
return new Promise((resolve, reject) => {});
} }
...@@ -164,7 +166,7 @@ export class MyCocosSceneComponent extends cc.Component { ...@@ -164,7 +166,7 @@ export class MyCocosSceneComponent extends cc.Component {
playAudioByUrl(audio_url, cb = null) { playAudioByUrl(audio_url, cb = null) {
if (audio_url) { if (audio_url) {
cc.assetManager.loadRemote(audio_url, (err, audioClip) => { cc.assetManager.loadRemote(audio_url, (err, audioClip: cc.AudioClip) => {
const audioId = cc.audioEngine.play(audioClip, false, 0.8); const audioId = cc.audioEngine.play(audioClip, false, 0.8);
if (cb) { if (cb) {
cc.audioEngine.setFinishCallback(audioId, () => { cc.audioEngine.setFinishCallback(audioId, () => {
......
{
"ver": "1.1.2",
"uuid": "df38fb90-8e23-45a6-8a15-dbb6c2ef0a5f",
"isBundle": false,
"bundleName": "",
"priority": 1,
"compressionType": {},
"optimizeHotUpdate": {},
"inlineSpriteFrames": {},
"isRemoteBundle": {},
"subMetas": {}
}
\ No newline at end of file
{
"ver": "2.3.5",
"uuid": "c3393c60-0fc9-4915-91d9-485a561738ef",
"type": "sprite",
"wrapMode": "clamp",
"filterMode": "bilinear",
"premultiplyAlpha": false,
"genMipmaps": false,
"packable": true,
"width": 644,
"height": 317,
"platformSettings": {},
"subMetas": {
"bg_alert": {
"ver": "1.0.4",
"uuid": "1da38dd7-6872-4ddf-a558-19368fccb1d5",
"rawTextureUuid": "c3393c60-0fc9-4915-91d9-485a561738ef",
"trimType": "auto",
"trimThreshold": 1,
"rotated": false,
"offsetX": 0,
"offsetY": 0,
"trimX": 0,
"trimY": 0,
"width": 644,
"height": 317,
"rawWidth": 644,
"rawHeight": 317,
"borderTop": 0,
"borderBottom": 0,
"borderLeft": 0,
"borderRight": 0,
"subMetas": {}
}
}
}
\ No newline at end of file
{
"ver": "2.3.5",
"uuid": "ada08f28-6853-4b73-b40c-f890cf785859",
"type": "sprite",
"wrapMode": "clamp",
"filterMode": "bilinear",
"premultiplyAlpha": false,
"genMipmaps": false,
"packable": true,
"width": 186,
"height": 63,
"platformSettings": {},
"subMetas": {
"btn_alert_ok": {
"ver": "1.0.4",
"uuid": "5bc8143f-384b-4b62-8d62-846f1f6dc2ea",
"rawTextureUuid": "ada08f28-6853-4b73-b40c-f890cf785859",
"trimType": "auto",
"trimThreshold": 1,
"rotated": false,
"offsetX": 0,
"offsetY": 0,
"trimX": 0,
"trimY": 0,
"width": 186,
"height": 63,
"rawWidth": 186,
"rawHeight": 63,
"borderTop": 0,
"borderBottom": 0,
"borderLeft": 0,
"borderRight": 0,
"subMetas": {}
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment