Sync TICK_LENGTH constant between client & server

This commit is contained in:
Tangent Wantwight 2020-06-07 01:56:06 -04:00
parent d2fe79b55c
commit 3f07748abd
5 changed files with 12 additions and 8 deletions

View file

@ -1,5 +1,5 @@
export const INPUT_FREQUENCY = 33; // roughly 30fps
export const TICK_LENGTH = 33; // roughly 30fps
export interface LockstepProcessor<LocalInput, GlobalInput, State> {
compareInput(a: GlobalInput, b: GlobalInput): boolean;

View file

@ -3,7 +3,7 @@ import { DrawSet, Layer } from "../applet/Render";
import { ComponentSchema, Data as EcsData } from "../ecs/Components";
import { Component, copySparse, Join, StateForSchema, Store } from "../ecs/Data";
import { DumbMotion } from "../ecs/Location";
import { INPUT_FREQUENCY, LockstepProcessor } from "../ecs/Lockstep";
import { LockstepProcessor, TICK_LENGTH } from "../ecs/Lockstep";
import { Buttons } from "./Input";
export enum GamePhase {
@ -208,7 +208,7 @@ export class Engine implements LockstepProcessor<KeyName[], KeyName[][], Data> {
}
advanceState(state: Data, input: KeyName[][]) {
DumbMotion(state, INPUT_FREQUENCY);
DumbMotion(state, TICK_LENGTH);
Join(state, "playerControl", "location").forEach(([player, location]) => {
const playerInput = input[player.playerNumber];
if(playerInput) {

View file

@ -4,7 +4,7 @@ import animationFrames from "callbag-animation-frames";
import map from "callbag-map";
import pipe from "callbag-pipe";
import { INPUT_FREQUENCY, LockstepProcessor, LockstepState } from "../ecs/Lockstep";
import { LockstepProcessor, LockstepState, TICK_LENGTH } from "../ecs/Lockstep";
export const enum MessageTypes {
/**
@ -78,7 +78,7 @@ export abstract class LockstepClient<LocalInput, GlobalInput, State> {
this.serverTalkback = data as Server<LocalInput, GlobalInput, State>;
// kickoff input sender
setTimeout(this.sampleInput, INPUT_FREQUENCY);
setTimeout(this.sampleInput, TICK_LENGTH);
} else if (mode == 1) {
// server message
const message = data as ServerMessage<GlobalInput, State>;
@ -104,7 +104,7 @@ export abstract class LockstepClient<LocalInput, GlobalInput, State> {
this.state.addLocalInput(this.playerNumber, input);
this.serverTalkback(1, { t: MessageTypes.INPUT, i: input });
}
setTimeout(this.sampleInput, INPUT_FREQUENCY);
setTimeout(this.sampleInput, TICK_LENGTH);
}
};

View file

@ -2,7 +2,7 @@ import { Callbag } from "callbag";
import pipe from "callbag-pipe";
import share from "callbag-share";
import { INPUT_FREQUENCY } from "../ecs/Lockstep";
import { TICK_LENGTH } from "../ecs/Lockstep";
import {
catchTalkback,
defer,
@ -23,7 +23,7 @@ export class LoopbackServer<LocalInput, State> {
private inputBuffer: LocalInput[] = [];
private heartbeat = pipe(
interval(INPUT_FREQUENCY),
interval(TICK_LENGTH),
map(() => ({
t: MessageTypes.INPUT,
i: this.inputBuffer.slice()

View file

@ -1,8 +1,12 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::time::Duration;
pub mod server;
/// Roughly 30 fps
pub static TICK_LENGTH: Duration = Duration::from_millis(33);
pub type PlayerId = usize;
pub type PlayerInput = Value;
pub type GameState = Value;