Commit 8020f9d6 authored by liujiaxin's avatar liujiaxin

feat: with backend interface

parent 8113a10a
This diff is collapsed.
This diff is collapsed.
......@@ -5,6 +5,7 @@ class Item {
public icon: string;//商品图片--可用type生成
public growthValue: number;//生长值
public cost: number;//费用
public cover: string;
public discount: number;//折扣
public num: number;//拥有数量
public levelLimite: number;//等级限制
......@@ -15,8 +16,9 @@ class Item {
this.icon = obj.icon;
this.growthValue = obj.growthValue;
this.cost = obj.cost;
this.discount = obj.discount;
this.num = obj.num;
this.cover = obj.cover;
this.discount = obj.discount || 1;
this.num = obj.amount;
this.levelLimite = obj.levelLimite;
}
}
......
......@@ -14,6 +14,9 @@ class Shop {
get list() {
return this._list;
}
getListByType(type: number) {
return this._list.filter(li => li.type == type)
}
}
export default Shop;
const shop = new Shop();
export default shop;
......@@ -5,6 +5,7 @@ class User {
public coin: number;
public level: number;
public growth: number;//当前成长值
public growthLevel: number;
public growthDaily: number;
public growthDailyMax: number;
public eatTime: number;
......@@ -20,12 +21,13 @@ class User {
this.coin = data.coin;
this.level = data.level;
this.growth = data.growth;
this.growthLevel = data.growthLevel;
this.growthDaily = data.growthDaily;
this.growthDailyMax = data.growthDailyMax;
this.eatTime = data.eatTime;
this.useFurniture = data.useFurniture;
this.useClothes = data.useClothes;
this.mood = 1;
this.mood = data.mood;
}
isDailyMax() {
return this.growthDailyMax >= this.growthDaily
......@@ -38,18 +40,18 @@ class User {
useCoin(val) {
this.coin -= val;
}
//当前等级成长值最大值
public get growthLevel(): number {
let max = 1;
for (let i = 1; i < 100; i++) {
let val = LEVEL[`level_${i}`].growth;
if (this.growth < val) {
max = val
break;
}
}
return max
}
// //当前等级成长值最大值
// public get growthLevel(): number {
// let max = 1;
// for (let i = 1; i < 100; i++) {
// let val = LEVEL[`level_${i}`].growth;
// if (this.growth < val) {
// max = val
// break;
// }
// }
// return max
// }
}
let user = new User();
export default user;
......
......@@ -2,25 +2,52 @@ import pg from "../pg";
import { ITEMS, USER } from "../config/config"
import user from "../model/user";
import kitchen from "../model/kitchen";
import shop from "../model/shop";
//获取信息,购买物品,使用物品(吃东西),穿戴衣服/更换家具
class Api {
static askUser() {
return new Promise((resolve, reject) => {
pg.http.send("GET", "http://www.baidu.com", {}).then((data: any) => {
let userInfo = USER;
user.parse(userInfo);
let kitchenInfo = ITEMS;
kitchen.parse(kitchenInfo);
resolve('');
pg.http.send("GET", `${globalThis.SERVER_HOST}/api/pets/info`, null).then((userInfo: any) => {
// let userInfo = USER;
try{
const mood = (Date.now() - userInfo.status.eat.time) > 4 * 60 * 60 * 1000 ? 1: 0; // 4 hours
const info = {
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) {
return new Promise((resolve, reject) => {
pg.http.send("GET", "http://www.baidu.com", {}).then((data: any) => {
user.addGrowth(data.growthValue);
kitchen.use(data.id);
resolve('');
pg.http.send("POST", `${globalThis.SERVER_HOST}/api/pets/use/item`, {item_id: data.id}).then((resp: any) => {
// user.addGrowth(data.growthValue);
// kitchen.use(data.id);
try {
resolve(resp);
} catch (e) {
reject(e)
}
}).catch(e => {
reject(e)
})
});
}
......@@ -28,10 +55,17 @@ class Api {
return new Promise((resolve, reject) => {
data.id;
data.type;
pg.http.send("GET", "http://www.baidu.com", {}).then((data: any) => {
user.useCoin(data.cost * data.discount);
kitchen.buy(data.id);
resolve('');
pg.http.send("POST", `${globalThis.SERVER_HOST}/api/pets/item/buy`, {item_id: data.id}).then((resp: any) => {
// user.useCoin(data.cost * data.discount);
// kitchen.buy(data.id);
try {
resolve(resp);
} catch (e) {
reject(e)
}
}).catch(e => {
reject(e)
})
});
}
......
......@@ -130,7 +130,7 @@ let pg = {
})
})
},
setNetImg(item, res) {
setNetImg(item, res, cache = true) {
return new Promise((resolve, reject) => {
if (!item) return pg.logger.w("图片更换失败,传入了错误的item");
let node = item.node ? item.node : item;
......@@ -139,7 +139,7 @@ let pg = {
return pg.logger.w("图片更换失败,传入了错误的res");
}
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 h = node.height;
node.active = false;//
......@@ -147,15 +147,30 @@ let pg = {
if (!cc.isValid(node)) return pg.logger.i("节点已销毁");
let nw = node.width = texture.width;
let nh = node.height = texture.height;
let component = node.getComponent(cc.Sprite);
let spriteFrame = new cc.SpriteFrame(texture);
component.spriteFrame = spriteFrame;
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(() => {
if (!cc.isValid(node)) return pg.logger.i("节点已销毁");
if (!node) return pg.logger.w("节点已销毁");
node.width = w;
node.height = h;
node.active = true;
// }, 30);
resolve({ w: nw, h: nh });
......@@ -317,36 +332,57 @@ let pg = {
http: {
//http访问
send: function (type, url, data) {
return new Promise((resolve) => {
return setTimeout(() => {
return resolve({ status: 200 });
}, 60);
return new Promise((resolve, reject) => {
// return setTimeout(() => {
// return resolve({ status: 200 });
// }, 60);
let xhr = cc.loader.getXMLHttpRequest();
xhr.timeout = 5000;
xhr.timeout = 30000;
xhr.responseType = "text";
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 = () => {
if (xhr.readyState !== 4) return;
if (xhr.status >= 200 && xhr.status < 300) {
try {
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);
} catch (e) {
reject(e)
}
}
else {
} else {
pg.logger.w("onerror->" + url + ';resp = '+ xhr.responseText);
reject(xhr.responseText)
}
};
xhr.onerror = (e) => {
pg.logger.w("onerror->" + url);
reject(e);
};
xhr.ontimeout = (e) => {
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 {
// this.onLoadEnd();
// next();
// };
this.onLoadEnd();
window["air"].hideAirClassLoading();
this.onLoadEnd().then(() => {
window["air"].hideAirClassLoading();
});
} else {
this.onLoadEnd();
}
......@@ -115,7 +117,7 @@ export class MyCocosSceneComponent extends cc.Component {
}
onLoadEnd() {
return new Promise((resolve, reject) => {});
}
......@@ -164,7 +166,7 @@ export class MyCocosSceneComponent extends cc.Component {
playAudioByUrl(audio_url, cb = null) {
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);
if (cb) {
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