射箭准确度

This commit is contained in:
k8w 2021-12-03 01:00:29 +08:00
parent 22c9913ed6
commit c9ac4a6470
6 changed files with 31 additions and 13 deletions

View File

@ -49,8 +49,8 @@ export class GameSystem {
startPos: { ...player.pos },
startTime: this._state.now,
targetPos: {
x: player.pos.x + input.direction.x * gameConfig.arrowDistance,
y: player.pos.y + input.direction.y * gameConfig.arrowDistance
x: player.pos.x + input.offset.x,
y: player.pos.y + input.offset.y
},
targetTime: this._state.now + gameConfig.arrowFlyTime
});
@ -75,11 +75,11 @@ export class GameSystem {
// 伤害判定
let damagedPlayers = this._state.players.filter(v => {
// 不能伤害自己
if (v.id === arrow.fromPlayerId) {
return false;
}
// if (v.id === arrow.fromPlayerId) {
// return false;
// }
return (v.pos.x - arrow.targetPos.x) * (v.pos.x - arrow.targetPos.x) + (v.pos.y - arrow.targetPos.y) * (v.pos.y - arrow.targetPos.y) <= gameConfig.arrowDistance * gameConfig.arrowDistance
return (v.pos.x - arrow.targetPos.x) * (v.pos.x - arrow.targetPos.x) + (v.pos.y - arrow.targetPos.y) * (v.pos.y - arrow.targetPos.y) <= gameConfig.arrowAttackRadius * gameConfig.arrowAttackRadius
});
damagedPlayers.forEach(p => {
// 设置击晕状态
@ -112,7 +112,7 @@ export interface PlayerMove {
export interface PlayerAttack {
type: 'PlayerAttack',
playerId: number,
direction: { x: number, y: number },
offset: { x: number, y: number },
}
export interface PlayerJoin {
type: 'PlayerJoin',

View File

@ -9,6 +9,7 @@ export const gameConfig = {
moveSpeed: 10,
arrowFlyTime: 500,
arrowDistance: 7,
arrowDistance: 8,
arrowAttackRadius: 3,
arrowDizzyTime: 1000
}

View File

@ -17,7 +17,7 @@ export interface ServiceType {
}
export const serviceProto: ServiceProto<ServiceType> = {
"version": 1,
"version": 2,
"services": [
{
"id": 0,
@ -159,8 +159,8 @@ export const serviceProto: ServiceProto<ServiceType> = {
}
},
{
"id": 2,
"name": "direction",
"id": 3,
"name": "offset",
"type": {
"type": "Interface",
"properties": [

View File

@ -22,7 +22,6 @@ export class Arrow extends Component {
this._endPos.set(state.targetPos.x, 0, -state.targetPos.y);
}
updateState(state: ArrowState, now: number) {
let percent = MathUtil.limit((now - state.startTime) / (state.targetTime - state.startTime), 0, 1);

View File

@ -143,9 +143,10 @@ export class GameScene extends Component {
}
onBtnAttack() {
let offset = this._playerInstances[this.gameManager.selfPlayerId].node.forward.clone().normalize().multiplyScalar(gameConfig.arrowDistance);
this.gameManager.sendClientInput({
type: 'PlayerAttack',
direction: this._playerInstances[this.gameManager.selfPlayerId].node.forward
offset: { x: offset.x, y: -offset.z }
})
}

View File

@ -11,6 +11,7 @@ export class GameManager {
gameSystem = new GameSystem();
lastServerState: GameSystemState = this.gameSystem.state;
lastRecvSetverStateTime = 0;
selfPlayerId: number = -1;
lastSN = 0;
@ -67,6 +68,7 @@ export class GameManager {
this.gameSystem.reset(ret.res.gameState);
this.lastServerState = Object.merge(ret.res.gameState);
this.lastRecvSetverStateTime = Date.now();
this.selfPlayerId = ret.res.playerId;
}
@ -77,6 +79,7 @@ export class GameManager {
this.gameSystem.applyInput(input);
}
this.lastServerState = Object.merge({}, this.gameSystem.state);
this.lastRecvSetverStateTime = Date.now();
// 和解
let lastSn = frame.lastSn ?? -1;
@ -89,6 +92,13 @@ export class GameManager {
});
})
})
// 本地时间流逝(会被下一次服务器状态覆盖)
this.gameSystem.applyInput({
type: 'TimePast',
dt: Date.now() - this.lastRecvSetverStateTime
});
this.lastRecvSetverStateTime = Date.now();
}
pendingInputMsgs: MsgClientInput[] = [];
@ -109,6 +119,13 @@ export class GameManager {
...input,
playerId: this.selfPlayerId
});
// 本地时间流逝(会被下一次服务器状态覆盖)
// this.gameSystem.applyInput({
// type: 'TimePast',
// dt: Date.now() - this.lastRecvSetverStateTime
// });
// this.lastRecvSetverStateTime = Date.now();
}
}