class Object

Constants

CLIENT_PACKAGE_LEN
CLIENT_PCK_TYPE
CMD_LEN

networking

DEBUG
FULLHD_X
GAME_VERSION

update GAME_VERSION on network protocol changes

KEY_A
KEY_C
KEY_D
KEY_DOWN
KEY_H
KEY_J
KEY_K
KEY_L
KEY_LEFT
KEY_M
KEY_Q
KEY_RIGHT
KEY_S
KEY_T
KEY_UP
KEY_W
MAX_CLIENTS
MAX_TICK_SPEED
MAX_TIMEOUT
MOUSE_RADIUS
NAME_LEN
NET_CLIENT
NET_ERR
NET_ERR_BAN
NET_ERR_CLIENT_OUTDATED
NET_ERR_DISCONNECT
NET_ERR_FULL
NET_ERR_KICK
NET_ERR_SERVER_OUTDATED
NET_INT_BASE
NET_INT_OFFSET
NET_MAX_INT
NET_MIN_INT
PLAYER_ID
PLAYER_PACKAGE_LEN
SERVER_PACKAGE_LEN
SERVER_PCK_TYPE
SPAWN_X
SPAWN_Y
SPEED
STATE_CONNECTING
STATE_ERROR
STATE_INGAME
STATE_MENU
STATE_OFFLINE
STATE_REC_PLAYBACK
TILE_SIZE

game

UI_SCALE
WINDOW_SIZE_X
WINDOW_SIZE_Y

Public Instance Methods

draw_scoreboard(win_size_x, win_size_y, players, font, debug) click to toggle source
# File lib/client/scoreboard.rb, line 2
def draw_scoreboard(win_size_x, win_size_y, players, font, debug)
    # TODO: do not compute those every frame
    padX = win_size_x / 3
    sizeX = win_size_x / 3
    padY = win_size_y / 6
    sizeY = win_size_y / 3
    slot_height = sizeY / MAX_CLIENTS
    text_scale = slot_height / 15
    # background
    draw_rect(padX, padY, sizeX, sizeY+3, 0xaa000000)
    # left border
    draw_rect(padX, padY, 3, sizeY+3, 0xaa000000)
    # right border
    draw_rect(padX + sizeX - 3, padY, 3, sizeY+3, 0xaa000000)
    (0..MAX_CLIENTS).each do |i|
        # row borders
        draw_rect(padX + 3, padY + (i * slot_height), sizeX - 6, 3, 0xaa000000)
    end
    players.each_with_index do | player, i |
        score_offset = text_scale * 10 * player.score.to_s.length
        dbg = 0
        if debug
            dbg += 25
            score_offset += 25
            font.draw_text(player.id, padX + 5, padY + (i * slot_height), 0, text_scale, text_scale, 0xFF00FF00)
        end
        font.draw_text(player.name, dbg + padX + 5, padY + (i * slot_height), 0, text_scale, text_scale)
        font.draw_text(player.score, dbg + padX + sizeX - score_offset, padY + (i * slot_height), 0, text_scale, text_scale)
    end
end
get_frame_time() click to toggle source
# File lib/client/gui.rb, line 34
def get_frame_time
  diff = Time.now - $time_point
  $time_point = Time.now
  return diff
end
net_error(err) click to toggle source
# File lib/share/network.rb, line 144
def net_error(err)
  raise "NetError: #{err}"
  exit 1
end
net_pack_bigint(int, size) click to toggle source

Converts a integer to multi character network string

@param [Integer, net_pack_int] int decimal based number @param [Integer] size max length of the network string @return [String] the int converted to base NET_INT_BASE

# File lib/share/network.rb, line 103
def net_pack_bigint(int, size)
  sum = ""
  div = size - 1
  (size - 1).times do
    buf = int / ((NET_MAX_INT+1) ** div)
    sum += net_pack_int(buf)
    int = int % ((NET_MAX_INT+1) ** div)
  end
  sum += net_pack_int(int)
  # TODO: check reminder and so on
  # throw and error when int is too big for size
  int = int / NET_MAX_INT
  sum
end
net_pack_int(int) click to toggle source

Converts a integer to single character network string

the base of the network is NET_INT_BASE so the number 93 is the last single character number represented as '~'

@param [Integer, chr] int decimal based number @return [String] the int converted to base NET_INT_BASE

# File lib/share/network.rb, line 76
def net_pack_int(int)
  net_error "#{__method__}: '#{int}' is too low allowed range #{NET_MIN_INT}-#{NET_MAX_INT}" if int < NET_MIN_INT
  net_error "#{__method__}: '#{int}' is too high allowed range #{NET_MIN_INT}-#{NET_MAX_INT}" if int > NET_MAX_INT
  int = int + NET_INT_OFFSET
  int.chr
end
net_unpack_bigint(net_int) click to toggle source

Converts a multi character network string to integer

@param [String, net_unpack_int] net_int network packed int @return [Integer] the net_int converted to decimal based number

# File lib/share/network.rb, line 124
def net_unpack_bigint(net_int)
  sum = 0
  net_int.chars.reverse.each_with_index do |c, i|
    if i.zero?
      sum = net_unpack_int(c)
    else
      sum += net_unpack_int(c) * i * (NET_MAX_INT+1)
    end
  end
  sum
end
net_unpack_int(net_int) click to toggle source

Converts a single character network string to integer

the base of the network is NET_INT_BASE so the number 93 is the last single character number represented as '~'

@param [String, ord] net_int network packed string @return [Integer] the net_int converted to decimal based number

# File lib/share/network.rb, line 92
def net_unpack_int(net_int)
  net_int.ord - NET_INT_OFFSET
end
player_strs_to_objects(player_strs) click to toggle source
# File lib/client/test.rb, line 11
def player_strs_to_objects(player_strs)
  players = []
  player_strs.each do |player_str|
    id = player_str[0..1].to_i
    x = player_str[2..4].to_i
    y = player_str[5..7].to_i
    # puts "id: #{id} x: #{x} y: #{y}"
    players << Player.new(id, x, y) unless id.zero?
  end
  players
end
save_read(socket, size) click to toggle source
# File lib/share/network.rb, line 136
def save_read(socket, size)
  begin
    return socket.read_nonblock(size)
  rescue IO::WaitReadable
    return ''
  end
end
server_package_to_player_array(data) click to toggle source
# File lib/client/test.rb, line 23
def server_package_to_player_array(data)
  # /(?<count>\d{2})(?<player>(?<id>\d{2})(?<x>\d{3})(?<y>\d{3}))/
  slots = data[0..1].to_i # save slots
  data = data[2..-1] # cut slots off
  players = server_package_to_player_strs(slots, data)
  # puts players
  player_strs_to_objects(players)
end
server_package_to_player_strs(slots, data) click to toggle source
# File lib/client/test.rb, line 3
def server_package_to_player_strs(slots, data)
  players = []
  slots.times do |index|
    players[index] = data[index * 8..index * 8 + 7]
  end
  players
end