'use strict'
const SERVER_UPDATE_RATE = 10.0
class Entity {
constructor(id) {
this.entity_id = id
this.position = [0, 0]
this.velocity = [1, 1] this.position_buffer = []
}
apply_input(input) {
this.position[0] += this.velocity[0] * input.press_time
this.position[1] += this.velocity[1] * input.press_time
}
interpolate(render_timestamp) {
let buffer = this.position_buffer
while (buffer.length >= 2 && buffer[1].time <= render_timestamp) {
buffer.shift()
}
if (buffer.length >= 2 && buffer[0].time <= render_timestamp && render_timestamp <= buffer[1].time) {
let p0 = buffer[0].position
let p1 = buffer[1].position
let t0 = buffer[0].time
let t1 = buffer[1].time
this.position[0] = p0[0] + (p1[0] - p0[0]) * (render_timestamp - t0) / (t1 - t0)
this.position[1] = p0[1] + (p1[1] - p0[1]) * (render_timestamp - t0) / (t1 - t0)
}
}
}
class Client {
constructor(network) {
this.entities = {}
this.key_left = false
this.key_right = false
this.network = network
this.server = null
this.lag = 0
this.entity_id = null
this.client_side_prediction = false
this.server_reconciliation = false
this.input_sequence_number = 0
this.pending_inputs = []
this.entity_interpolation = false
}
update() {
if (this.entity_id === null) {
return }
if (this.entity_interpolation) {
this.interpolate_entities()
}
}
process_inputs() {
let now_ts = +new Date()
let last_ts = this.last_ts || now_ts
this.last_ts = now_ts
let dt_sec = (now_ts - last_ts) / 1000.0
let press_time
if (this.key_right) {
press_time = dt_sec
} else if (this.key_left) {
press_time = -dt_sec
} else {
return
}
this.server.network.send(this.lag, {
press_time: press_time,
input_sequence_number: this.input_sequence_number++,
entity_id: this.entity_id,
})
if (this.client_side_prediction) {
this.entities[this.entity_id].apply_input(input)
}
this.pending_inputs.push(input)
}
process_server_messages() {
while (true) {
let message = this.network.receive()
if (!message) {
break
}
process_world_state_list(message)
}
}
process_world_state_list(message) {
for (let i = 0; i < message.length; i++) {
let state = message[i]
this.process_server_message(state)
}
}
process_server_message(state, last_processed_input) {
if (!this.entities[state.entity_id]) {
this.entities[state.entity_id] = new Entity(state.entity_id)
}
let entity = this.entities[state.entity_id]
if (state.entity_id === this.entity_id) {
entity.position = state.position
if (this.server_reconciliation) {
let j = 0
while (j < this.pending_inputs.length) {
let input = this.pending_inputs[j]
if (input.input_sequence_number <= last_processed_input) {
this.pending_inputs.splice(j, 1)
} else {
entity.applyInput(input)
j++
}
}
} else {
this.pending_inputs = []
}
} else {
if (!this.entity_interpolation) {
entity.position = state.position
} else {
let timestamp = +new Date()
entity.position_buffer.push({
time: timestamp,
position: state.position,
})
}
}
}
interpolate_entities() {
let now = +new Date()
let render_timestamp = now - (1000.0 / SERVER_UPDATE_RATE)
for (let k in this.entities) {
let entity = this.entities[k]
if (entity.entity_id !== this.entity_id) {
entity.interpolate(render_timestamp)
}
}
}
}