teeworlds_network/examples/08_gui_snapshot.rb

78 lines
1.5 KiB
Ruby
Raw Normal View History

2022-11-19 11:39:58 +00:00
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'gosu'
require_relative '../lib/teeworlds_client'
class Entitiy
attr_reader :x, :y
def initialize(attr = {})
@x = attr[:x]
@y = attr[:y]
end
end
class Tee < Entitiy
attr_reader :w, :h
def initialize(attr = {})
@w = 32
@h = 32
super
end
end
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 500
class Gui < Gosu::Window
def initialize
2024-01-20 14:33:25 +00:00
super(WINDOW_WIDTH, WINDOW_HEIGHT)
2022-11-19 11:39:58 +00:00
self.caption = 'ruby teeworlds client'
@client = TeeworldsClient.new
@client.connect('localhost', 8303, detach: true)
@tees = {}
2022-11-19 12:19:22 +00:00
@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)
2022-11-19 11:39:58 +00:00
end
def update
@client.on_snapshot do |_, snap|
2022-11-19 12:19:22 +00:00
@tees = {}
2022-11-19 11:39:58 +00:00
snap.items.each do |item|
next unless item.instance_of?(NetObj::Character)
player = item.to_h
@tees[player[:id]] = Tee.new(player)
end
end
end
def center_around_tee(tee)
wc = WINDOW_WIDTH / 2
hc = WINDOW_HEIGHT / 2
x = -tee.x + wc
y = -tee.y + hc
Entitiy.new(x:, y:)
end
def draw
2022-11-19 12:19:22 +00:00
@background_image.draw(0, 0, 0)
2022-11-19 11:39:58 +00:00
return if @tees.empty?
2022-11-19 12:19:22 +00:00
own_tee = @tees[@client.local_client_id]
own_tee = @tees.first.last if own_tee.nil?
offset = center_around_tee(own_tee)
2024-01-20 14:37:18 +00:00
@tees.each_value do |tee|
2022-11-19 12:19:22 +00:00
@tee_image.draw(tee.x + offset.x, tee.y + offset.y)
2022-11-19 11:39:58 +00:00
end
end
end
Gui.new.show