GameStateManager

This commit is contained in:
k8w
2021-12-28 22:45:50 +08:00
parent b9b6be0c7f
commit 5a6ae0b679
22 changed files with 887 additions and 815 deletions

View File

@@ -1,17 +1,17 @@
import { ApiCall, ApiCallWs } from "tsrpc";
import { ApiCallWs } from "tsrpc";
import { ReqLogin, ResLogin } from "../shared/protocols/PtlLogin";
let nextPlayerId = 1;
export async function ApiLogin(call: ApiCallWs<ReqLogin, ResLogin>) {
let playerId = nextPlayerId++;
call.conn.currentUser = {
id: playerId,
nickname: call.req.nickname
}
call.succ({
playerId: playerId
currentUser: call.conn.currentUser
})
}

View File

@@ -11,5 +11,7 @@ export async function ApiJoinRoom(call: ApiCallWs<ReqJoinRoom, ResJoinRoom>) {
return call.error(op.errMsg);
}
call.succ({});
call.succ({
roomState: room.state
});
}

View File

@@ -7,15 +7,10 @@ import { GameSystemState } from "../shared/game/GameSystemState";
import { MsgGameInput } from "../shared/protocols/game/client/MsgGameInput";
import { ServiceType } from "../shared/protocols/serviceProto";
import { CurrentUser } from "../shared/types/CurrentUser";
import { RoomState } from "../shared/types/RoomState";
const MAX_ROOM_USER = 2;
export interface RoomState {
id: uint;
players: { id: uint, nickname: string, isReady: boolean }[];
status: 'wait' | 'ready' | 'start';
}
/**
* 服务端 - 房间 - 逻辑系统
*/

View File

@@ -78,11 +78,11 @@ export class GameSystem {
}
// 子弹碰撞,抵消
case 'BulletHit': {
let player = this.state.players.find(v => v.id === input.player.id);
let enemy = this.state.enemies.find(v => v.id === input.enemy.id);
let player = this.state.players.find(v => v.id === input.playerId);
let enemy = this.state.enemies.find(v => v.id === input.enemyId);
if (player && enemy) {
player.bullets.removeOne(v => v.id === input.player.bulletId);
enemy.bullets.removeOne(v => v.id === input.enemy.bulletId);
player.bullets.removeOne(v => v.id === input.playerBulletId);
enemy.bullets.removeOne(v => v.id === input.enemyBulletId);
}
break;
}

View File

@@ -1,4 +1,4 @@
import { uint } from "tsrpc";
import { uint } from "tsrpc-proto";
import { PlayerState } from "./GameSystemState";
// 移动并攻击
@@ -34,14 +34,10 @@ export interface PlayerHurt {
// 子弹互相碰撞,双双消失
export interface BulletHit {
type: 'BulletHit',
player: {
id: uint,
bulletId: uint
},
enemy: {
id: uint,
bulletId: uint
},
playerId: uint,
playerBulletId: uint,
enemyId: uint,
enemyBulletId: uint
}
// 时间流逝

View File

@@ -1,4 +1,4 @@
import { uint } from "tsrpc";
import { CurrentUser } from "../types/CurrentUser";
import { BaseConf, BaseRequest, BaseResponse } from "./base";
export interface ReqLogin extends BaseRequest {
@@ -6,7 +6,7 @@ export interface ReqLogin extends BaseRequest {
}
export interface ResLogin extends BaseResponse {
playerId: uint
currentUser: CurrentUser
}
export const conf: BaseConf = {

View File

@@ -1,4 +1,5 @@
import { uint } from "tsrpc-proto";
import { RoomState } from "../../types/RoomState";
import { BaseConf, BaseRequest, BaseResponse } from "../base";
export interface ReqJoinRoom extends BaseRequest {
@@ -6,7 +7,7 @@ export interface ReqJoinRoom extends BaseRequest {
}
export interface ResJoinRoom extends BaseResponse {
roomState: RoomState;
}
export const conf: BaseConf = {

View File

@@ -1,4 +1,4 @@
import { RoomState } from "../../../../models/Room";
import { RoomState } from "../../../types/RoomState";
export interface MsgUpdateRoomState {
state: RoomState

View File

@@ -570,53 +570,35 @@ export const serviceProto: ServiceProto<ServiceType> = {
}
},
{
"id": 1,
"name": "player",
"id": 3,
"name": "playerId",
"type": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "id",
"type": {
"type": "Number",
"scalarType": "uint"
}
},
{
"id": 1,
"name": "bulletId",
"type": {
"type": "Number",
"scalarType": "uint"
}
}
]
"type": "Number",
"scalarType": "uint"
}
},
{
"id": 2,
"name": "enemy",
"id": 4,
"name": "playerBulletId",
"type": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "id",
"type": {
"type": "Number",
"scalarType": "uint"
}
},
{
"id": 1,
"name": "bulletId",
"type": {
"type": "Number",
"scalarType": "uint"
}
}
]
"type": "Number",
"scalarType": "uint"
}
},
{
"id": 5,
"name": "enemyId",
"type": {
"type": "Number",
"scalarType": "uint"
}
},
{
"id": 6,
"name": "enemyBulletId",
"type": {
"type": "Number",
"scalarType": "uint"
}
}
]
@@ -1143,11 +1125,11 @@ export const serviceProto: ServiceProto<ServiceType> = {
],
"properties": [
{
"id": 0,
"name": "playerId",
"id": 1,
"name": "currentUser",
"type": {
"type": "Number",
"scalarType": "uint"
"type": "Reference",
"target": "../types/CurrentUser/CurrentUser"
}
}
]
@@ -1155,6 +1137,26 @@ export const serviceProto: ServiceProto<ServiceType> = {
"base/BaseResponse": {
"type": "Interface"
},
"../types/CurrentUser/CurrentUser": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "id",
"type": {
"type": "Number",
"scalarType": "uint"
}
},
{
"id": 1,
"name": "nickname",
"type": {
"type": "String"
}
}
]
},
"room/PtlCreateRoom/ReqCreateRoom": {
"type": "Interface",
"extends": [
@@ -1245,55 +1247,19 @@ export const serviceProto: ServiceProto<ServiceType> = {
"target": "base/BaseResponse"
}
}
]
},
"room/PtlSetReady/ReqSetReady": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "base/BaseRequest"
}
}
],
"properties": [
{
"id": 0,
"name": "isReady",
"type": {
"type": "Boolean"
}
}
]
},
"room/PtlSetReady/ResSetReady": {
"type": "Interface",
"extends": [
{
"id": 0,
"name": "roomState",
"type": {
"type": "Reference",
"target": "base/BaseResponse"
"target": "../types/RoomState/RoomState"
}
}
]
},
"room/server/MsgUpdateRoomState/MsgUpdateRoomState": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "state",
"type": {
"type": "Reference",
"target": "../../models/Room/RoomState"
}
}
]
},
"../../models/Room/RoomState": {
"../types/RoomState/RoomState": {
"type": "Interface",
"properties": [
{
@@ -1305,7 +1271,7 @@ export const serviceProto: ServiceProto<ServiceType> = {
}
},
{
"id": 3,
"id": 1,
"name": "players",
"type": {
"type": "Array",
@@ -1369,6 +1335,52 @@ export const serviceProto: ServiceProto<ServiceType> = {
}
}
]
},
"room/PtlSetReady/ReqSetReady": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "base/BaseRequest"
}
}
],
"properties": [
{
"id": 0,
"name": "isReady",
"type": {
"type": "Boolean"
}
}
]
},
"room/PtlSetReady/ResSetReady": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "base/BaseResponse"
}
}
]
},
"room/server/MsgUpdateRoomState/MsgUpdateRoomState": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "state",
"type": {
"type": "Reference",
"target": "../types/RoomState/RoomState"
}
}
]
}
}
};

View File

@@ -0,0 +1,7 @@
import { uint } from "tsrpc-proto";
export interface RoomState {
id: uint;
players: { id: uint, nickname: string, isReady: boolean }[];
status: 'wait' | 'ready' | 'start';
}