简单的PVP

This commit is contained in:
DESKTOP-5RP3AKU\Jisol 2023-11-21 01:57:40 +08:00
parent 1aa5900294
commit 074cfbd5bd
18 changed files with 3218 additions and 158 deletions

View File

@ -26,10 +26,10 @@ import { JAPI, JAPIConfig } from "../../extensions/ngame/assets/ngame/util/JAPI"
import { AppData } from "./AppData";
import AppAction from "./AppAction";
// 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 = `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`

View File

@ -6,8 +6,15 @@ import { GUI } from "../ui/UIConfig";
import BaseAction from "./BaseAction";
export interface GPVPStart{
leftTactical:string;
rightTactical:string;
leftTactical:string; //左边玩家的阵法
leftPets:{[key:number]:string}; //左边玩家的宠物
rightTactical:string; //右边玩家的阵法
rightPets:{[key:number]:string}; //左边玩家的宠物
}
export interface GPVPText{
text:string
}
export default class PVPAction extends BaseAction {
@ -16,6 +23,7 @@ export default class PVPAction extends BaseAction {
app.socket.on(GAction.C_MODE_PVP_START_WAIT,this.onModePVPStartWait,this);
app.socket.on(GAction.C_MODE_PVP_END_WAIT,this.onModePVPEndWait,this);
app.socket.on(GAction.C_MODE_PVP_START,this.onModePVPStart,this,GActionType.GPVPStart);
app.socket.on(GAction.C_MODE_PVP_MESSAGE,this.onModePVPMessage,this,GActionType.GPVPText);
}
//PVP开始等待
@ -33,7 +41,13 @@ export default class PVPAction extends BaseAction {
//开始PVP
onModePVPStart(info:GPVPStart){
console.log("开始PVP",info);
GBattleModeManager.getIns().Open(BattleMode.PVP,true);
GBattleModeManager.getIns().Open(BattleMode.PVP,true,info);
}
//提示
onModePVPMessage(info:GPVPText){
console.log("提示PVP",info);
app.layer.Open(GUI.Tips,{text:info.text})
}
}

View File

@ -5,12 +5,15 @@ import { JNFrameInfo } from "../../../extensions/ngame/assets/ngame/sync/frame/J
import { Node } from "cc";
import { Vec3 } from "cc";
import { Camera } from "cc";
import GBattleModeManager from "./GBattleModeManager";
export default class GBaseMode<T> extends GObject<T> {
export default class GBaseMode<T,DT> extends GObject<T> {
//场景相机
camera:Camera;
//模式数据
data:DT;
get scene():Node{
return this.node;
@ -33,6 +36,11 @@ export default class GBaseMode<T> extends GObject<T> {
}
}
//结束场景
Close(){
GBattleModeManager.getIns().Close();
}
onSyncUpdate(dt: number,frame:JNFrameInfo, input?: T) {
//重置 场景中的层级

View File

@ -46,6 +46,12 @@ export default class GBattleModeManager extends Singleton {
//默认模式
default:BattleMode = BattleMode.OnHook; //默认无限模式
//当前帧不切换模式
frameNoSwitch:boolean = false;
//模式数据
data:any;
//初始化管理器
async onInit(info:GBattleModeInfo){
@ -62,10 +68,12 @@ export default class GBattleModeManager extends Singleton {
}
//打开指定模式
async Open(mode:BattleMode = null,isAuto:boolean = false){
async Open(mode:BattleMode = null,isAuto:boolean = false,data:any = this.data){
this.data = data;
if(!this.current && mode == null){
await this.Open(this.default,true);
await this.Open(this.default,true,data);
return;
}else if(mode == null){
return;
@ -85,8 +93,7 @@ export default class GBattleModeManager extends Singleton {
//主动调用场景销毁
app.sync.onReset();
this.current = null;
await this.Open();
this.frameNoSwitch = true;
}
@ -108,6 +115,7 @@ export default class GBattleModeManager extends Singleton {
if(!this.isInit || this.current == null) return;
let mode = instantiate(this.modes[this.current]);
mode.getComponent(GBaseMode).camera = this.camera;
mode.getComponent(GBaseMode).data = this.data;
this.root.addChild(mode)
}
@ -124,6 +132,13 @@ export default class GBattleModeManager extends Singleton {
app.sync.update(dt);
//自动推帧
this.onAutoFrame(dt);
//如果当前模式是空则默认模式
if(this.current == null && !this.frameNoSwitch){
this.Open();
}
this.frameNoSwitch = false;
}
//自动推帧

View File

@ -43,15 +43,13 @@ export enum GOnHookModePlayerEnum{
export interface GOnHookInfo{
//阵法
tactical: GTactical;
//宠物列表
roles: TB.TbGRole[];
}
/**
* ( 使 API)
*/
@ccclass('GOnHookMode')
export default class GOnHookMode extends GBaseMode<{}>{
export default class GOnHookMode extends GBaseMode<{},{}>{
@property(Prefab)
rolePrefab: Prefab = null;
@ -139,8 +137,8 @@ export default class GOnHookMode extends GBaseMode<{}>{
this.map3.init(app.battleRes.maps[60001][2],1,app.battleRes.maps[60001][1].width * scale,1048 * scale);
this.onUpdateMap(0);
this.playerInfo = { tactical: GTactical.getTactical().setOffset(this.playerPos), roles: GRoleUtil.getGRoles([]) };
this.enemyInfo = { tactical: GTactical.getTactical(true).setOffset(this.enemyPos), roles: GRoleUtil.getGRoles([]) };
this.playerInfo = { tactical: GTactical.getTactical().setOffset(this.playerPos) };
this.enemyInfo = { tactical: GTactical.getTactical(true).setOffset(this.enemyPos) };
this.onUpdatePlayerPet();
@ -379,8 +377,10 @@ export default class GOnHookMode extends GBaseMode<{}>{
//角色受击回调
onHitBack(role:GRoleDefault,hit:number){
if(!role.get()) return;
//添加受击显示
app.event.emit(ModeRenderEvent.HIT,role,hit);
app.event.emit(ModeRenderEvent.HIT,role.v2World.clone(),hit);
}

View File

@ -8,6 +8,12 @@ import { GRoleUtil } from "../entity/GRole";
import GRoleDefault from "../base/role/GRoleDefault";
import { v3 } from "cc";
import { TB } from "../../../resources/config/data/schema";
import JNFrameTime from "../../../../extensions/ngame/assets/ngame/sync/frame/game/time/JNFrameTime";
import GBattleModeManager from "../GBattleModeManager";
import { app, TD } from "../../App";
import { ModeRenderEvent } from "../../ui/Consts/Game/ModeRender";
import { GPVPStart } from "../../action/PVPAction";
import { PlayerPetOV } from "../../consts/API";
const { ccclass, property } = _decorator;
//PVP 角色
@ -28,7 +34,7 @@ export interface GPVPModePlayerInfo{
* PVP
*/
@ccclass('GPVPMode')
export default class GPVPMode extends GBaseMode<{}>{
export default class GPVPMode extends GBaseMode<{},GPVPStart>{
@property(Prefab)
rolePrefab: Prefab = null;
@ -51,11 +57,48 @@ export default class GPVPMode extends GBaseMode<{}>{
//敌方位置
enemyPos: Vec2 = new Vec2(400,0);
//是否结束游戏
isEndGame:boolean = false;
get scene():Node{
return this.objects;
}
onLoad(){
//整理GPVPStart数据
//宠物Id列表
let leftTactical:number[] = JSON.parse(this.data.leftTactical);
let rightTactical:number[] = JSON.parse(this.data.rightTactical);
//宠物列表
let leftTbs:TB.TbGRole[] = JSON.parse(this.data.leftTactical);
let rightTbs:TB.TbGRole[] = JSON.parse(this.data.rightTactical);
//玩家宠物信息
for (let index = 0; index < leftTactical.length; index++) {
const petId = leftTactical[index];
if(petId != 0){
let pet:PlayerPetOV = JSON.parse(this.data.leftPets[petId]);
leftTbs[index] = TD.TbGRole.get(pet.petTbId);
}else{
leftTbs[index] = null;
}
}
for (let index = 0; index < rightTactical.length; index++) {
const petId = rightTactical[index];
if(petId != 0){
let pet:PlayerPetOV = JSON.parse(this.data.leftPets[petId]);
rightTbs[index] = TD.TbGRole.get(pet.petTbId);
}else{
rightTbs[index] = null;
}
}
this.playerInfo = { tactical: GTactical.getTactical().setOffset(this.playerPos), roles: leftTbs };
this.enemyInfo = { tactical: GTactical.getTactical(true).setOffset(this.enemyPos), roles: rightTbs };
super.onLoad();
}
@ -68,13 +111,10 @@ export default class GPVPMode extends GBaseMode<{}>{
//初始化战斗
console.log("GPVPMode 模式初始化");
this.playerInfo = { tactical: GTactical.getTactical().setOffset(this.playerPos), roles: GRoleUtil.getGRoles([10004,10001,10004,10002,10003,10003]) };
this.enemyInfo = { tactical: GTactical.getTactical(true).setOffset(this.enemyPos), roles: GRoleUtil.getGRoles([10002,10002,10001,10003,10004,10003]) };
//生成玩家
this.playerInfo.roles.forEach((info,index) => this.onGenRole(GPVPModePlayerEnum.PLAYER,index+1,info))
this.enemyInfo.roles.forEach((info,index) => this.onGenRole(GPVPModePlayerEnum.ENEMY,index+1,info))
//生成宠物
this.playerInfo.roles.forEach((info,index) => info && this.onGenRole(GPVPModePlayerEnum.PLAYER,index+1,info))
this.enemyInfo.roles.forEach((info,index) => info && this.onGenRole(GPVPModePlayerEnum.ENEMY,index+1,info))
}
@ -94,6 +134,11 @@ export default class GPVPMode extends GBaseMode<{}>{
return this.getEnumy(entity,type);
}
//绑定受击回调
entity.addHitCallback(this.onHitBack.bind(this));
//绑定死亡回调
entity.addKillBackEvent(this.onRoleKillBack.bind(this))
this.addGObject(entity,tactical.getPosition(index));
this.getOnesRole(type).push(entity);
@ -111,6 +156,12 @@ export default class GPVPMode extends GBaseMode<{}>{
if(type == GPVPModePlayerEnum.ENEMY) return this.enemyRoles;
}
//获取存活的宠物
getOnesRoleAlive(type: GPVPModePlayerEnum):GRoleDefault[]{
if(type == GPVPModePlayerEnum.PLAYER) return this.playerRoles.filter(role => !!role.get());
if(type == GPVPModePlayerEnum.ENEMY) return this.enemyRoles.filter(role => !!role.get());
}
//获取敌人
getEnumy(player:GRoleDefault,type:GPVPModePlayerEnum):GRoleDefault{
@ -119,19 +170,65 @@ export default class GPVPMode extends GBaseMode<{}>{
if(type == GPVPModePlayerEnum.ENEMY) enumyOnes = GPVPModePlayerEnum.PLAYER
//获取敌人
let roles = this.getOnesRole(enumyOnes);
let roles = this.getOnesRoleAlive(enumyOnes);
//返回敌人
//获取我在第几排
let playerXY = player.tactical.getXY(player.tacticalIndex);
//通过排数获取最近的敌人
let sort = roles.filter(role => !!role.get()).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;
}
}
//角色死亡回调
onRoleKillBack(role:GRoleDefault){
//死亡销毁
JNFrameTime.getInstance().setTimeout(() => {
if(role.isValid)
role.node.destroy()
},3000)
this.onUpdateEndState();
}
//刷新结束状态
onUpdateEndState(){
//如果已经结束则返回
if(this.isEndGame) return;
//判断是否有队伍都死亡
if(this.getOnesRoleAlive(GPVPModePlayerEnum.PLAYER).length == 0 || this.getOnesRoleAlive(GPVPModePlayerEnum.ENEMY).length == 0){
this.isEndGame = true;
//结束游戏
JNFrameTime.getInstance().setTimeout(() => {
this.Close();
},3000)
}
}
//角色受击回调
onHitBack(role:GRoleDefault,hit:number){
if(!role.get()) return;
//添加受击显示
app.event.emit(ModeRenderEvent.HIT,role.v2World.clone(),hit);
}

View File

@ -15,5 +15,6 @@ export enum GAction {
C_MODE_PVP_END = 3005, //PVP结束
C_MODE_PVP_START_WAIT = 3006, //开始等待PVP开始
C_MODE_PVP_END_WAIT = 3007, //结束等待PVP开始
C_MODE_PVP_MESSAGE = 3008, //PVP 消息通知
}

View File

@ -13,5 +13,6 @@ export enum GActionType {
/*************** 游戏模式 : PVP **************/
GPVPStart = "GPVPStart", //PVP 开始
GPVPText = "GPVPText", //PVP 提示
}

View File

@ -7,6 +7,7 @@ import { Prefab } from 'cc';
import { instantiate } from 'cc';
import { v3 } from 'cc';
import { ModeRenderHitText } from './ModeRenderHitText';
import { Vec2 } from 'cc';
const { ccclass, property } = _decorator;
export enum ModeRenderEvent{
@ -34,11 +35,9 @@ export class ModeRender extends Component {
}
//受击
onHit(role:GRoleDefault,hit:number){
onHit(pos:Vec2,hit:number){
if(!role.get()) return;
let rolePos = GData.WorldCanvas.camera.worldToScreen(role.node.worldPosition);
let rolePos = GData.WorldCanvas.camera.worldToScreen(v3(pos.x,pos.y));
let hitNode = instantiate(this.hitPrefab);

View File

@ -16,9 +16,6 @@ export class MainView extends JNGLayerBase {
onJNLoad(data?: any): void {
//打开默认模式
GBattleModeManager.getIns().Open();
//发送消息
ChatData.getIns().onSend({
message:`${PlayerData.getIns().data.playerId} 加入游戏`

@ -1 +1 @@
Subproject commit ddba756071599f88801c2001235b59b152117740
Subproject commit 275c3cf5d8f108a4ca9d6f22dabdd1d8b853c84c

View File

@ -6,7 +6,14 @@ option java_package = "cn.jisol.ngame.proto";
//PVP
message GPVPStart {
string leftTactical = 1; //
string rightTactical = 2; //
map<int64, string> leftPets = 2; // key:Id value Id
string rightTactical = 3; //
map<int64, string> rightPets = 4; // key:Id value Id
}
//PVP
message GPVPText {
string text = 1; //
}

View File

@ -16,5 +16,6 @@ public interface GActionEnum {
int C_MODE_PVP_END = 3005; //PVP结束
int C_MODE_PVP_START_WAIT = 3006; //开始等待PVP开始
int C_MODE_PVP_END_WAIT = 3007; //结束等待PVP开始
int C_MODE_PVP_MESSAGE = 3008; //PVP 消息通知
}

View File

@ -1,6 +1,9 @@
package cn.jisol.game.actions.onhook;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import cn.jisol.game.actions.GActionEnum;
import cn.jisol.game.controller.game.GPlayerPetController;
import cn.jisol.game.controller.game.GPlayerTacticalController;
import cn.jisol.game.entity.game.PlayerTactical;
import cn.jisol.game.network.client.GClient;
@ -89,18 +92,51 @@ public class GPVPAction {
}
//获取双方的阵型
GPlayerTacticalController tactical = SpringBeanUtils.getBean(GPlayerTacticalController.class);
GPlayerTacticalController tacticalController = SpringBeanUtils.getBean(GPlayerTacticalController.class);
GPlayerPetController petController = SpringBeanUtils.getBean(GPlayerPetController.class);
String leftTactical = tacticalController.getInfo(client1.player).data.getTacticalData();
String rightTactical = tacticalController.getInfo(client2.player).data.getTacticalData();
//如果有人阵法是默认 不开始
if (leftTactical.equals(tacticalController.InitTactical) || rightTactical.equals(tacticalController.InitTactical)){
if(leftTactical.equals(tacticalController.InitTactical)){
client1.invoke(GActionEnum.C_MODE_PVP_END_WAIT);
client1.invoke(GActionEnum.C_MODE_PVP_MESSAGE, GPVPMessage.GPVPText.newBuilder().setText("你的阵法没有宠物~").build());
}else{
//加入回列表
pool.addFirst(client1);
}
if(rightTactical.equals(tacticalController.InitTactical)){
client2.invoke(GActionEnum.C_MODE_PVP_END_WAIT);
client2.invoke(GActionEnum.C_MODE_PVP_MESSAGE, GPVPMessage.GPVPText.newBuilder().setText("你的阵法没有宠物~").build());
}else{
//加入回列表
pool.addFirst(client2);
}
return;
}
//构建匹配信息
GPVPMessage.GPVPStart info = GPVPMessage.GPVPStart.newBuilder()
.setLeftTactical(tactical.getInfo(client1.player).data.getTacticalData())
.setRightTactical(tactical.getInfo(client2.player).data.getTacticalData())
.build();
GPVPMessage.GPVPStart.Builder builder = GPVPMessage.GPVPStart.newBuilder()
.setLeftTactical(leftTactical)
.setRightTactical(rightTactical);
//构建玩家宠物列表
petController.getPetList(client1.player).data.forEach(pet -> {
builder.putLeftPets(pet.getPetId(), JSONUtil.toJsonStr(pet));
});
petController.getPetList(client2.player).data.forEach(pet -> {
builder.putLeftPets(pet.getPetId(), JSONUtil.toJsonStr(pet));
});
//取消等待
client1.invoke(GActionEnum.C_MODE_PVP_END_WAIT);
client2.invoke(GActionEnum.C_MODE_PVP_END_WAIT);
GPVPMessage.GPVPStart info = builder.build();
//PVP 开始
client1.invoke(GActionEnum.C_MODE_PVP_START, info);
client2.invoke(GActionEnum.C_MODE_PVP_START, info);

View File

@ -29,6 +29,9 @@ public class GPlayerTacticalController {
@Autowired
PlayerTacticalService playerTacticalService;
//默认阵法
public final String InitTactical = "[0,0,0,0,0,0,0,0,0]";
//获取玩家阵法
@ApiImplicitParams({})
@ApiOperation(value = "获取玩家阵法")
@ -49,7 +52,7 @@ public class GPlayerTacticalController {
//如果没有阵法则默认一个阵法
if(Objects.isNull(info.getTacticalData())){
info.setTacticalData("[0,0,0,0,0,0,0,0,0]");
info.setTacticalData(InitTactical);
}
return NewsContext.onSuccess("获取成功",info);

View File

@ -6,7 +6,14 @@ option java_package = "cn.jisol.ngame.proto";
//PVP
message GPVPStart {
string leftTactical = 1; //
string rightTactical = 2; //
map<int64, string> leftPets = 2; // key:Id value Id
string rightTactical = 3; //
map<int64, string> rightPets = 4; // key:Id value Id
}
//PVP
message GPVPText {
string text = 1; //
}