Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions client/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {GameScreen} from './gamescreen';
import {GameSocket, MessageData, Entity} from './protocol';

class Game {
static tickPeriod: number = 1.0 / 60;
static TPS: number = 60;
static tickPeriod: number = 1.0 / Game.TPS;

transport: GameWSTransport = null;
socket: GameSocket = null;
Expand Down Expand Up @@ -66,7 +67,7 @@ class Game {
var maxDistanceY = this.canvas.height - this.welcomeMessage.size;
this.game.state.alivePlayers.forEach((player: Entity): void => {
if (player.direction != null) {
var move = player.direction.clone().multiplyScalar(this.welcomeMessage.speed);
var move = player.direction.clone().multiplyScalar(this.welcomeMessage.speed / Game.TPS);
var newpos = player.position.clone().add(move);
if (!this.game.state.alivePlayers.some((cmpPlayer: Entity) => {
return cmpPlayer.id != player.id && cmpPlayer.distanceSq(newpos) <= minPlayerDistanceSq;
Expand All @@ -81,8 +82,8 @@ class Game {
updateBulletStates(): void {
this.game.state.aliveBullets.forEach((bullet: Entity): void => {
if (bullet.direction != null) {
bullet.position.x += bullet.direction.x * this.welcomeMessage.bulletSpeed;
bullet.position.y += bullet.direction.y * this.welcomeMessage.bulletSpeed;
bullet.position.x += bullet.direction.x * this.welcomeMessage.bulletSpeed / Game.TPS;
bullet.position.y += bullet.direction.y * this.welcomeMessage.bulletSpeed / Game.TPS;
}
});
}
Expand Down Expand Up @@ -258,7 +259,7 @@ class Game {

context.restore();

window.requestAnimationFrame(this.frame);
setTimeout((): void => { window.requestAnimationFrame(this.frame); }, Game.tickPeriod * 1000 - delta);
}

//
Expand Down
14 changes: 8 additions & 6 deletions server/src/server/gamestate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ use rand::{thread_rng, Rng};
use self::super::Client;
use self::super::WebSocketEvent;

// TODO: substitute when doing config, see `mod.rs`
const TPS: u32 = 60;
static BULLET_RADIUS: f32 = 5.0;
static PLAYER_RADIUS: f32 = 10.0;
static BULLET_SPEED: f32 = 3.0;
static PLAYER_SPEED: f32 = 2.0;
static BULLET_SPEED: f32 = 3.0 * TPS as f32;
static PLAYER_SPEED: f32 = 2.0 * TPS as f32;
static MAP_HEIGHT: f32 = 500.0;
static MAP_WIDTH: f32 = 500.0;
static TICKS_BETWEEN_FULL_UPDATES: u32 = 600; // 10s @ 60FPS
static TICKS_BETWEEN_FULL_UPDATES: u32 = 10 * TPS;

/// The `GameState` contains the whole state of the game.
///
Expand Down Expand Up @@ -116,8 +118,8 @@ impl GameState {
let mut destroyed_players = Vec::new();

for (_, bullet) in &mut self.bullets {
bullet.bullet.x += bullet.bullet.move_x.unwrap_or(0.0) * BULLET_SPEED;
bullet.bullet.y += bullet.bullet.move_y.unwrap_or(0.0) * BULLET_SPEED;
bullet.bullet.x += bullet.bullet.move_x.unwrap_or(0.0) * BULLET_SPEED / TPS as f32;
bullet.bullet.y += bullet.bullet.move_y.unwrap_or(0.0) * BULLET_SPEED / TPS as f32;

if bullet.bullet.x < 0.0 || bullet.bullet.x > MAP_WIDTH || bullet.bullet.y < 0.0 ||
bullet.bullet.y > MAP_HEIGHT {
Expand Down Expand Up @@ -364,7 +366,7 @@ impl GameState {
///
/// Returns whether the player crashed into a wall during movement.
fn move_player(pos: &mut f32, mov: Option<f32>) -> bool {
let new_pos = *pos + mov.unwrap_or(0.0) * PLAYER_SPEED;
let new_pos = *pos + mov.unwrap_or(0.0) * PLAYER_SPEED / TPS as f32;
*pos = new_pos.max(PLAYER_RADIUS)
.min(MAP_WIDTH - PLAYER_RADIUS);

Expand Down
7 changes: 5 additions & 2 deletions server/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ use std::time::Duration;
pub use self::events::*;
pub use self::gamestate::GameState;

// TODO: substitute when doing config, see `gamestate.rs`
const TPS: f32 = 60.0;

/// The main listening loop for the server.
pub fn listen(host: &str,
port: u16,
Expand Down Expand Up @@ -61,11 +64,11 @@ pub fn listen(host: &str,

/// Spawns the main game loop in a separate thread and returns the handle therefor. Non-blocking.
///
/// The general idea for the game loop is to update the game state every 16 milliseconds (60 FPS), processing messages along the way.
/// The general idea for the game loop is to update the game state every 1/TPS milliseconds, processing messages along the way.
pub fn start_game_loop(game_messages: mpsc::Receiver<WebSocketEvent>,
cont: &Arc<RwLock<bool>>)
-> thread::JoinHandle<()> {
static ITER_LENGTH: u64 = 16 * 1000000; // 16 milliseconds
static ITER_LENGTH: u64 = (1.0 / TPS * 1000.0) as u64 * 1000000; // 1/TPS milliseconds

let cont = cont.clone();
thread::spawn(move || {
Expand Down