geckosio / snapshot-interpolation

A Snapshot Interpolation library for Real-Time Multiplayer Games.
BSD 3-Clause "New" or "Revised" License
277 stars 25 forks source link

State without being an entity #4

Closed lucas-jones closed 4 years ago

lucas-jones commented 4 years ago

Amazing library. Currently everything in State has to be an Entity would be nice if objects could be synced.

Such as to sync: { gameState: 'ingame' | 'lobby'; timer: number; }

yandeu commented 4 years ago

Thanks :smiley:

But, I'm not exactly sure what you mean.

Can you give me an example of what you're trying to achieve?

lucas-jones commented 4 years ago

Sorry bad explanation - was late at night

export type State = Entity[]
export interface Entity {
  id: string
  [key: string]: Value
}

So State always has to be Entity[] example { id: 'heroBlue', x: 23, y: 14, z: 47 }, which has worked great for players. I'm wanting to sync data that isn't entity specific, such as timers, scores, etc.

It would be nice if we could have something like this:

export type State = Entity[] | { [key: string]: Value  }

const state: State = {
    players: [
        { id: 'heroBlue', x: 23, y: 14, z: 47 },
        { id: 'heroRed', x: 23, y: 14, z: 47 },
    ],
    timer: {
        timeLeft: 64,
    },
    score: {
        red: 0,
        blue: 2,
    }
}
lucas-jones commented 4 years ago

Hmmm maybe I'm mistaken. And this shouldn't be part of the snapshot-interpolation library. And i just include this data with the packet that includes the snapshot - as score / timer wouldn't need to be interpolated. My misunderstanding

yandeu commented 4 years ago

Yes, if it does not have to be interpolated, you do not need to add it inside the snapshot.

You could do this:

const snapshot = SI.snapshot.create({
  players: [
    { id: 0, x: 17.5, y: -1.1 },
    { id: 1, x: 5.8, y: 18.9 }
  ]
})

const data = {
  snapshot: snapshot,
  timer: {
    timeLeft: 64,
  },
  score: {
    red: 0,
    blue: 2,
  }
}

// send data to the client