diff --git a/examples/08_gui_snapshot.rb b/examples/08_gui_snapshot.rb index f15dc33..29402d5 100755 --- a/examples/08_gui_snapshot.rb +++ b/examples/08_gui_snapshot.rb @@ -33,10 +33,17 @@ class Gui < Gosu::Window @client = TeeworldsClient.new @client.connect('localhost', 8303, detach: true) @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 def update @client.on_snapshot do |_, snap| + @tees = {} snap.items.each do |item| next unless item.instance_of?(NetObj::Character) @@ -55,17 +62,14 @@ class Gui < Gosu::Window end def draw + @background_image.draw(0, 0, 0) 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| - draw_rect( - tee.x + offset.x, - tee.y + offset.y, - tee.w, - tee.h, - Gosu::Color.argb(0xff_00ff00) - ) + @tee_image.draw(tee.x + offset.x, tee.y + offset.y) end end end diff --git a/examples/img/default.png b/examples/img/default.png new file mode 100644 index 0000000..edd3f80 Binary files /dev/null and b/examples/img/default.png differ diff --git a/examples/img/jungle.png b/examples/img/jungle.png new file mode 100644 index 0000000..f4b6be8 Binary files /dev/null and b/examples/img/jungle.png differ diff --git a/lib/game_client.rb b/lib/game_client.rb index b1a1a40..12954fb 100644 --- a/lib/game_client.rb +++ b/lib/game_client.rb @@ -105,6 +105,10 @@ class GameClient return if call_hook(:client_info, context).nil? 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 end @@ -157,7 +161,7 @@ class GameClient u = SnapshotUnpacker.new snapshot = u.snap_single(chunk) - return if snapshot.game_tick.nil? + return if snapshot.nil? context = Context.new(nil, chunk:) return if call_hook(:snapshot, context, snapshot).nil? diff --git a/lib/models/player.rb b/lib/models/player.rb index 67bbcc2..c4c1ce6 100644 --- a/lib/models/player.rb +++ b/lib/models/player.rb @@ -5,7 +5,7 @@ class Player def initialize(data = {}) @id = data[:id] || -1 - @local = data[:local] || 0 + @local = data[:local] == 1 @team = data[:team] || 0 @name = data[:name] || '(connecting..)' @clan = data[:clan] || '' @@ -17,6 +17,10 @@ class Player @score = data[:score] || 0 end + def local? + @local + end + def set_start_info(start_info) raise "expected: StartInfo got: #{start_info.class}" unless start_info.instance_of?(StartInfo) diff --git a/lib/snapshot/unpacker.rb b/lib/snapshot/unpacker.rb index 5f6f8a6..ac68cf9 100644 --- a/lib/snapshot/unpacker.rb +++ b/lib/snapshot/unpacker.rb @@ -64,10 +64,12 @@ class SnapshotUnpacker return unless msg_id == NETMSG_SNAPSINGLE - puts ">>> snap #{snap_name} (#{msg_id})" - puts " id=#{msg_id} game_tick=#{game_tick} delta_tick=#{delta_tick}" - puts " num_parts=#{num_parts} part=#{part} crc=#{crc} part_size=#{part_size}" - puts "\n header:" + if @verbose + puts ">>> snap #{snap_name} (#{msg_id})" + puts " id=#{msg_id} game_tick=#{game_tick} delta_tick=#{delta_tick}" + puts " num_parts=#{num_parts} part=#{part} crc=#{crc} part_size=#{part_size}" + puts "\n header:" + end header = [] notes = [] @@ -79,11 +81,13 @@ class SnapshotUnpacker header += parsed[:raw] end - hexdump_lines(header.pack('C*'), 1, notes, legend: :inline).each do |hex| - puts " #{hex}" + if @verbose + hexdump_lines(header.pack('C*'), 1, notes, legend: :inline).each do |hex| + puts " #{hex}" + end + puts "\n payload:" end - puts "\n payload:" notes = [] data = u.get_raw @@ -176,9 +180,11 @@ class SnapshotUnpacker id_parsed = u.parsed.last end - # hexdump_lines(data.pack('C*'), 1, notes, legend: :inline).each do |hex| - # puts " #{hex}" - # end + if @verbose + hexdump_lines(data.pack('C*'), 1, notes, legend: :inline).each do |hex| + puts " #{hex}" + end + end exit 1 if invalid diff --git a/lib/teeworlds_client.rb b/lib/teeworlds_client.rb index 639e736..30df9a5 100644 --- a/lib/teeworlds_client.rb +++ b/lib/teeworlds_client.rb @@ -16,13 +16,14 @@ require_relative 'game_client' class TeeworldsClient attr_reader :state, :hooks, :game_client - attr_accessor :rcon_authed + attr_accessor :rcon_authed, :local_client_id def initialize(options = {}) @verbose = options[:verbose] || false @state = NET_CONNSTATE_OFFLINE @ip = 'localhost' @port = 8303 + @local_client_id = 0 @hooks = { chat: [], map_change: [],