From 32c3eb49193b4b8d5e14c4bf794ef74db128c896 Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Sun, 17 Sep 2023 12:59:38 +0200 Subject: [PATCH] Document on_snapshot --- docs/v0.0.1/classes/Snapshot.md | 7 +++++++ docs/v0.0.1/classes/TeeworldsClient.md | 18 ++++++++++++++---- lib/snapshot/unpacker.rb | 1 + 3 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 docs/v0.0.1/classes/Snapshot.md diff --git a/docs/v0.0.1/classes/Snapshot.md b/docs/v0.0.1/classes/Snapshot.md new file mode 100644 index 0000000..4b237d3 --- /dev/null +++ b/docs/v0.0.1/classes/Snapshot.md @@ -0,0 +1,7 @@ +# Snapshot + +### @game_tick [Integer] +### @items [Array#SnapItem] + +SnapItem can be any of the [event classes](../../../lib/snapshot/events/) +or any of the [item classes](../../../lib/snapshot/items/). diff --git a/docs/v0.0.1/classes/TeeworldsClient.md b/docs/v0.0.1/classes/TeeworldsClient.md index bbf4766..7f1208e 100644 --- a/docs/v0.0.1/classes/TeeworldsClient.md +++ b/docs/v0.0.1/classes/TeeworldsClient.md @@ -143,16 +143,26 @@ client.connect('localhost', 8303, detach: false) ### #on_snapshot(&block) -**Parameter: block [Block |[context](../classes/Context.md)|]** +**Parameter: block [Block |[context](../classes/Context.md), [Snapshot](../classes/Snapshot.md)|]** -TODO: generated documentation +context.message is nil but the block takes a second argument of type [Snapshot](../classes/Snapshot.md) + +By default when a snapshot is received the `GameClient::ack_game_tick` and `GameClient::pred_game_tick` +variables are updated. Those are crucial for a healthy connection to the server. So only call `context.cancel` +if you know what you are doing **Example:** ```ruby client = TeeworldsClient.new -client.on_snapshot do |context| - # TODO: generated documentation +client.on_snapshot do |_, snapshot| + snapshot.items.each do |item| + next unless item.instance_of?(NetObj::Character) + + p item.to_h + # => {:id=>0, :tick=>372118, :x=>1584, :y=>369, :vel_x=>0, :vel_y=>0, :angle=>0, :direction=>0, :jumped=>0, :hooked_player=>-1, :hook_state=>0, :hook_tick=>0, :hook_x=>1584, :hook_y=>369, :hook_dx=>0, :hook_dy=>0, :health=>0, :armor=>0, :ammo_count=>0, :weapon=>1, :emote=>0, :attack_tick=>0, :triggered_events=>0} + + end end client.connect('localhost', 8303, detach: false) diff --git a/lib/snapshot/unpacker.rb b/lib/snapshot/unpacker.rb index ffbee09..c8c0c5c 100644 --- a/lib/snapshot/unpacker.rb +++ b/lib/snapshot/unpacker.rb @@ -24,6 +24,7 @@ class Snapshot attr_accessor :game_tick, :items def initialize(items) + @game_tick = 0 @items = items end end