Skip to content

Commit

Permalink
Document on_snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
ChillerDragon committed Sep 17, 2023
1 parent c185d1a commit 32c3eb4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
7 changes: 7 additions & 0 deletions docs/v0.0.1/classes/Snapshot.md
Original file line number Diff line number Diff line change
@@ -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/).
18 changes: 14 additions & 4 deletions docs/v0.0.1/classes/TeeworldsClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,26 @@ client.connect('localhost', 8303, detach: false)

### <a name="on_snapshot"></a> #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)
Expand Down
1 change: 1 addition & 0 deletions lib/snapshot/unpacker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Snapshot
attr_accessor :game_tick, :items

def initialize(items)
@game_tick = 0
@items = items
end
end
Expand Down

0 comments on commit 32c3eb4

Please sign in to comment.