mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-27 02:36:14 +00:00
资源刷新 以及 出售
This commit is contained in:
@@ -25,12 +25,12 @@ import { StorageData, StorageEnum } from "./consts/GData";
|
||||
import { JAPI, JAPIConfig } from "../../extensions/ngame/assets/ngame/util/JAPI";
|
||||
import { AppData } from "./AppData";
|
||||
|
||||
// let APIPath = `http://localhost:8080`
|
||||
// let WsPath = `ws://localhost:8080/websocket`
|
||||
let APIPath = `http://localhost:8080`
|
||||
let WsPath = `ws://localhost:8080/websocket`
|
||||
// let APIPath = `http://192.168.0.123:8080`
|
||||
// let WsPath = `ws://192.168.0.123:8080/websocket`
|
||||
let APIPath = `https://api.pet.jisol.cn`
|
||||
let WsPath = `wss://api.pet.jisol.cn/websocket`
|
||||
// let APIPath = `https://api.pet.jisol.cn`
|
||||
// let WsPath = `wss://api.pet.jisol.cn/websocket`
|
||||
|
||||
//重写UI
|
||||
class JNGLayer extends JNLayer{
|
||||
|
@@ -5,6 +5,7 @@ import ChatData from "./data/ChatData";
|
||||
import PlayerData from "./data/PlayerData";
|
||||
import PlayerPetData from "./data/PlayerPetData";
|
||||
import PlayerTacticalData from "./data/PlayerTacticalData";
|
||||
import ResourceData from "./data/ResourceData";
|
||||
|
||||
//数据类 (用于初始化游戏信息)
|
||||
export class AppData extends SystemBase{
|
||||
@@ -16,6 +17,7 @@ export class AppData extends SystemBase{
|
||||
PlayerPetData.getIns(), //玩家宠物信息
|
||||
ChatData.getIns(), //聊天
|
||||
PlayerTacticalData.getIns(), //阵法
|
||||
ResourceData.getIns(), //玩家资源
|
||||
];
|
||||
|
||||
async onInit(): Promise<any> {
|
||||
|
@@ -326,17 +326,25 @@ export default class GOnHookMode extends GBaseMode<{}>{
|
||||
//获取敌人
|
||||
let roles = this.getOnesRoleAlive(enumyOnes);
|
||||
|
||||
//返回敌人
|
||||
//获取我在第几排
|
||||
let playerXY = player.tactical.getXY(player.tacticalIndex);
|
||||
//通过排数获取最近的敌人
|
||||
let sort = roles.sort((enumy1,enumy2) => {
|
||||
let enumy1XY = enumy1.tactical.getXY(enumy1.tacticalIndex);
|
||||
let enumy2XY = enumy2.tactical.getXY(enumy2.tacticalIndex);
|
||||
return Math.abs((playerXY.y * 1000) - (enumy1XY.y * 1000)) + Math.abs((playerXY.x - enumy1XY.x)) -
|
||||
Math.abs((playerXY.y * 1000) - (enumy2XY.y * 1000)) + Math.abs((playerXY.x - enumy2XY.x))
|
||||
});
|
||||
return sort[0]
|
||||
//通过距离获取最近的敌人
|
||||
if(roles[0]){
|
||||
let len = Math.abs(Vec2.distance(player.v2World,roles[0].v2World));
|
||||
let enumy = roles[0];
|
||||
|
||||
for (let index = 0; index < roles.length; index++) {
|
||||
const role = roles[index];
|
||||
let tLen;
|
||||
if(tLen = Math.abs(Vec2.distance(player.v2World,role.v2World)) < len){
|
||||
enumy = role;
|
||||
len = tLen;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return enumy;
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@@ -1,9 +1,17 @@
|
||||
import { app } from "../App";
|
||||
import ResourceData from "../data/ResourceData";
|
||||
import { GUI } from "../ui/UIConfig";
|
||||
|
||||
//接受到JSON消息
|
||||
export const RData = (data:any,isTips:boolean = true) => {
|
||||
if(data.data.state == 200){
|
||||
//如果有 Resource 字段 表示要刷新资源
|
||||
if(data.data['resources']){
|
||||
//刷新资源
|
||||
data.data['resources'].forEach(res => {
|
||||
ResourceData.getIns().onUpdateOV(res.operation,res.resource);
|
||||
})
|
||||
}
|
||||
return data.data.data;
|
||||
}else{
|
||||
//弹出提示
|
||||
@@ -68,6 +76,13 @@ export interface PlayerTacticalOV{
|
||||
playerId:number, //玩家Id
|
||||
tacticalData:string, //阵法数据
|
||||
}
|
||||
//玩家资源
|
||||
export interface ResourceOV{
|
||||
resourceId:number; //资源Id
|
||||
playerId:number; //玩家Id
|
||||
resourceTbId:number; //资源配置表Id
|
||||
resourceValue:number; //资源数量
|
||||
}
|
||||
|
||||
export const API = {
|
||||
|
||||
@@ -86,5 +101,9 @@ export const API = {
|
||||
/********** 阵法接口 ******************/
|
||||
GetPlayerTactical: async () => RData(await app.api.get(`/game/tactical/get`),false) as PlayerTacticalOV, //获取玩家阵法
|
||||
SetPlayerTactical: async (data:PlayerTacticalOV) => RData(await app.api.post(`/game/tactical/set`,data),false) as PlayerTacticalOV, //更新玩家阵法
|
||||
|
||||
/********** 资源接口 ******************/
|
||||
GetPlayerResource: async () => RData(await app.api.get(`/game/resource/get`),false) as ResourceOV[], //获取玩家资源
|
||||
|
||||
}
|
||||
|
||||
|
@@ -10,5 +10,7 @@ export const GAPI = {
|
||||
GOnHookSpawnCreeps : async () => RProto(await app.api.get(`/game/mode/onHook/onSpawnCreeps`,{responseType:'arraybuffer'}),GActionType.GOnHookPets) as GOnHookPets,
|
||||
//捕捉野怪
|
||||
GOnHookCatchCreeps : async (creepId) => RData(await app.api.post(`/game/mode/onHook/onCatchCreeps/${creepId}`)) as PlayerPetOV,
|
||||
//出售野怪
|
||||
GOnHookSellCreeps : async (creepId) => RData(await app.api.post(`/game/mode/onHook/onSellCreeps/${creepId}`)),
|
||||
|
||||
}
|
57
JisolGameCocos/assets/script/data/ResourceData.ts
Normal file
57
JisolGameCocos/assets/script/data/ResourceData.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { app } from "../App";
|
||||
import { API, ResourceOV } from "../consts/API";
|
||||
import BaseData from "./BaseData";
|
||||
|
||||
export enum ResourceEvent{
|
||||
UPDATE = "ResourceEvent_UPDATE", //刷新资源
|
||||
}
|
||||
|
||||
//资源类型
|
||||
export enum ResourceType{
|
||||
Gold = 90001, //金币
|
||||
}
|
||||
|
||||
//玩家资源数据
|
||||
export default class ResourceData extends BaseData{
|
||||
|
||||
//资源数据 {资源Id:资源数量}
|
||||
data:{[key:number]:number} = {};
|
||||
|
||||
//初始化
|
||||
async onInit() {
|
||||
await this.onUpdateResource();
|
||||
}
|
||||
|
||||
//更新玩家资源
|
||||
async onUpdateResource(){
|
||||
|
||||
//请求资源
|
||||
let res = await API.GetPlayerResource();
|
||||
|
||||
//保存资源数量
|
||||
res.forEach(data => {
|
||||
this.data[data.resourceTbId] = data.resourceValue;
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
//获取指定资源的数量
|
||||
getValue(type:ResourceType){
|
||||
return this.data[type] || 0;
|
||||
}
|
||||
|
||||
//刷新返回资源
|
||||
onUpdateOV(operation:number,resource:ResourceOV){
|
||||
|
||||
if(operation == 0){
|
||||
//更新资源
|
||||
this.data[resource.resourceTbId] = resource.resourceValue;
|
||||
}
|
||||
|
||||
app.event.emit(ResourceEvent.UPDATE)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
9
JisolGameCocos/assets/script/data/ResourceData.ts.meta
Normal file
9
JisolGameCocos/assets/script/data/ResourceData.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "7d3c75f4-37fd-4009-baab-1fcc2430b739",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@@ -74,7 +74,7 @@ export default class GOnHookManager extends Singleton{
|
||||
|
||||
let index;
|
||||
if((index = this.killSreeps.indexOf(creeps)) < 0){
|
||||
app.layer.Open(GUI.Tips,{text:"不可捕捉该宠物"});
|
||||
app.layer.Open(GUI.Tips,{text:"不可捕捉该野怪"});
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -95,6 +95,25 @@ export default class GOnHookManager extends Singleton{
|
||||
|
||||
}
|
||||
|
||||
//出售野怪
|
||||
async onSellCreeps(creeps:GOnHookPet):Promise<boolean>{
|
||||
|
||||
let index;
|
||||
if((index = this.killSreeps.indexOf(creeps)) < 0){
|
||||
app.layer.Open(GUI.Tips,{text:"不可出售当前野怪"});
|
||||
return false;
|
||||
}
|
||||
|
||||
//删除
|
||||
this.killSreeps.splice(index,1);
|
||||
|
||||
//通知添加野怪被删除
|
||||
app.event.emit(GOnHookManagerEvent.DEL_KILL_SREEP,creeps);
|
||||
|
||||
return await GAPI.GOnHookSellCreeps(creeps.key);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
9
JisolGameCocos/assets/script/ui/Consts/Resource.meta
Normal file
9
JisolGameCocos/assets/script/ui/Consts/Resource.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "a2e6e211-4b26-493c-85ba-38c5a9f2c614",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
import { Label } from 'cc';
|
||||
import { _decorator, Component, Node } from 'cc';
|
||||
import ResourceData, { ResourceEvent, ResourceType } from '../../../data/ResourceData';
|
||||
import { Enum } from 'cc';
|
||||
import { app } from '../../../App';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('PlayerResourceShow')
|
||||
export class PlayerResourceShow extends Component {
|
||||
|
||||
|
||||
//显示的资源
|
||||
@property({type:Enum(ResourceType)})
|
||||
type:ResourceType = ResourceType.Gold;
|
||||
|
||||
//显示文本
|
||||
@property(Label)
|
||||
show:Label;
|
||||
|
||||
protected onLoad(): void {
|
||||
|
||||
//更新
|
||||
this.onUpdateView();
|
||||
//监听
|
||||
app.event.on(ResourceEvent.UPDATE,this.onUpdateView,this);
|
||||
|
||||
}
|
||||
|
||||
protected onDestroy(): void {
|
||||
app.event.off(ResourceEvent.UPDATE,this.onUpdateView,this);
|
||||
}
|
||||
|
||||
//刷新
|
||||
onUpdateView(){
|
||||
|
||||
//更新资源数量
|
||||
this.show.string = `${ResourceData.getIns().getValue(this.type)}`;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "dd6ffde0-3461-40cc-a793-9463cad639f1",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@@ -41,11 +41,13 @@ export class MainSreepsList extends Component {
|
||||
//添加野怪
|
||||
onAddSreep(sreep:GOnHookPet){
|
||||
this.views.addData(sreep);
|
||||
this.onUpdateView();
|
||||
}
|
||||
|
||||
//删除野怪
|
||||
onDelSreep(sreep:GOnHookPet){
|
||||
this.views.delData(sreep);
|
||||
this.onUpdateView();
|
||||
}
|
||||
|
||||
//刷新
|
||||
@@ -86,6 +88,22 @@ export class MainSreepsList extends Component {
|
||||
app.layer.Open(GUI.Tips,{text:"捕捉成功!"});
|
||||
|
||||
}
|
||||
|
||||
//野怪出售
|
||||
async onClickSell(){
|
||||
|
||||
//获取选中的野怪
|
||||
let item = this.views.getItems<MainSreepsIcon>()[this.index];
|
||||
|
||||
if(!item){
|
||||
app.layer.Open(GUI.Tips,{text:"请选择需要出售的宠物"});
|
||||
return;
|
||||
}
|
||||
|
||||
if(await GOnHookManager.getIns().onSellCreeps(item.data))
|
||||
app.layer.Open(GUI.Tips,{text:"出售成功!"});
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user