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.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

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?
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?

View file

@ -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)

View file

@ -64,10 +64,12 @@ class SnapshotUnpacker
return unless msg_id == NETMSG_SNAPSINGLE
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
if @verbose
hexdump_lines(header.pack('C*'), 1, notes, legend: :inline).each do |hex|
puts " #{hex}"
end
puts "\n payload:"
end
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

View file

@ -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: [],