Add images to gui client

This commit is contained in:
ChillerDragon 2022-11-19 13:19:22 +01:00
parent dc72204140
commit 1794051342
7 changed files with 40 additions and 21 deletions

View file

@ -33,10 +33,17 @@ class Gui < Gosu::Window
@client = TeeworldsClient.new @client = TeeworldsClient.new
@client.connect('localhost', 8303, detach: true) @client.connect('localhost', 8303, detach: true)
@tees = {} @tees = {}
@background_image = Gosu::Image.new(img('jungle.png'))
@tee_image = Gosu::Image.new(img('default.png'))
end
def img(path)
File.join(File.dirname(__FILE__), 'img/', path)
end end
def update def update
@client.on_snapshot do |_, snap| @client.on_snapshot do |_, snap|
@tees = {}
snap.items.each do |item| snap.items.each do |item|
next unless item.instance_of?(NetObj::Character) next unless item.instance_of?(NetObj::Character)
@ -55,17 +62,14 @@ class Gui < Gosu::Window
end end
def draw def draw
@background_image.draw(0, 0, 0)
return if @tees.empty? return if @tees.empty?
offset = center_around_tee(@tees.first.last) own_tee = @tees[@client.local_client_id]
own_tee = @tees.first.last if own_tee.nil?
offset = center_around_tee(own_tee)
@tees.each do |_id, tee| @tees.each do |_id, tee|
draw_rect( @tee_image.draw(tee.x + offset.x, tee.y + offset.y)
tee.x + offset.x,
tee.y + offset.y,
tee.w,
tee.h,
Gosu::Color.argb(0xff_00ff00)
)
end end
end end
end end

BIN
examples/img/default.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
examples/img/jungle.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 196 KiB

View file

@ -105,6 +105,10 @@ class GameClient
return if call_hook(:client_info, context).nil? return if call_hook(:client_info, context).nil?
player = context.data[:player] player = context.data[:player]
if player.local?
@client.local_client_id = player.id
puts "Our client id is #{@client.local_client_id}"
end
@players[player.id] = player @players[player.id] = player
end end
@ -157,7 +161,7 @@ class GameClient
u = SnapshotUnpacker.new u = SnapshotUnpacker.new
snapshot = u.snap_single(chunk) snapshot = u.snap_single(chunk)
return if snapshot.game_tick.nil? return if snapshot.nil?
context = Context.new(nil, chunk:) context = Context.new(nil, chunk:)
return if call_hook(:snapshot, context, snapshot).nil? return if call_hook(:snapshot, context, snapshot).nil?

View file

@ -5,7 +5,7 @@ class Player
def initialize(data = {}) def initialize(data = {})
@id = data[:id] || -1 @id = data[:id] || -1
@local = data[:local] || 0 @local = data[:local] == 1
@team = data[:team] || 0 @team = data[:team] || 0
@name = data[:name] || '(connecting..)' @name = data[:name] || '(connecting..)'
@clan = data[:clan] || '' @clan = data[:clan] || ''
@ -17,6 +17,10 @@ class Player
@score = data[:score] || 0 @score = data[:score] || 0
end end
def local?
@local
end
def set_start_info(start_info) def set_start_info(start_info)
raise "expected: StartInfo got: #{start_info.class}" unless start_info.instance_of?(StartInfo) raise "expected: StartInfo got: #{start_info.class}" unless start_info.instance_of?(StartInfo)

View file

@ -64,10 +64,12 @@ class SnapshotUnpacker
return unless msg_id == NETMSG_SNAPSINGLE return unless msg_id == NETMSG_SNAPSINGLE
puts ">>> snap #{snap_name} (#{msg_id})" if @verbose
puts " id=#{msg_id} game_tick=#{game_tick} delta_tick=#{delta_tick}" puts ">>> snap #{snap_name} (#{msg_id})"
puts " num_parts=#{num_parts} part=#{part} crc=#{crc} part_size=#{part_size}" puts " id=#{msg_id} game_tick=#{game_tick} delta_tick=#{delta_tick}"
puts "\n header:" puts " num_parts=#{num_parts} part=#{part} crc=#{crc} part_size=#{part_size}"
puts "\n header:"
end
header = [] header = []
notes = [] notes = []
@ -79,11 +81,13 @@ class SnapshotUnpacker
header += parsed[:raw] header += parsed[:raw]
end end
hexdump_lines(header.pack('C*'), 1, notes, legend: :inline).each do |hex| if @verbose
puts " #{hex}" hexdump_lines(header.pack('C*'), 1, notes, legend: :inline).each do |hex|
puts " #{hex}"
end
puts "\n payload:"
end end
puts "\n payload:"
notes = [] notes = []
data = u.get_raw data = u.get_raw
@ -176,9 +180,11 @@ class SnapshotUnpacker
id_parsed = u.parsed.last id_parsed = u.parsed.last
end end
# hexdump_lines(data.pack('C*'), 1, notes, legend: :inline).each do |hex| if @verbose
# puts " #{hex}" hexdump_lines(data.pack('C*'), 1, notes, legend: :inline).each do |hex|
# end puts " #{hex}"
end
end
exit 1 if invalid exit 1 if invalid

View file

@ -16,13 +16,14 @@ require_relative 'game_client'
class TeeworldsClient class TeeworldsClient
attr_reader :state, :hooks, :game_client attr_reader :state, :hooks, :game_client
attr_accessor :rcon_authed attr_accessor :rcon_authed, :local_client_id
def initialize(options = {}) def initialize(options = {})
@verbose = options[:verbose] || false @verbose = options[:verbose] || false
@state = NET_CONNSTATE_OFFLINE @state = NET_CONNSTATE_OFFLINE
@ip = 'localhost' @ip = 'localhost'
@port = 8303 @port = 8303
@local_client_id = 0
@hooks = { @hooks = {
chat: [], chat: [],
map_change: [], map_change: [],