Implement callback for accept_teleport
This commit is contained in:
parent
53fe25043d
commit
f79e0728e6
@ -60,6 +60,18 @@ defmodule Amethyst.API.Game do
|
||||
mod.player_position(self(), {x, y, z}, refs)
|
||||
end
|
||||
@doc """
|
||||
`accept_teleport/3` is called when a client accepts a teleportation as sent by the Synchronize Player Position
|
||||
packet (TODO: Teleport Player API). This lets you know that the client is now where you expect it to be.
|
||||
|
||||
- 'from' is the PID of the player's connection process (see `login/3`).
|
||||
- 'id' is the teleport ID (TODO: Teleport Player API)
|
||||
- 'state_refs' are your references (see `instantiate/1`)
|
||||
"""
|
||||
@callback accept_teleport(from :: pid(), id :: integer(), state_refs :: map()) :: :ok
|
||||
def accept_teleport(%{:mod => mod, :refs => refs}, id) do
|
||||
mod.accept_teleport(self(), id, refs)
|
||||
end
|
||||
@doc """
|
||||
Whether or not this game instance can be joined by a new player. This should include basic logic such as
|
||||
if joining makes sense, for instance if the game is full or if the game has already started.
|
||||
"""
|
||||
|
@ -64,9 +64,6 @@ defmodule Amethyst.ConnectionHandler do
|
||||
Logger.debug("Sending packet #{inspect(packet)}")
|
||||
send_packet(socket, connstate, packet, version)
|
||||
loop(socket, connstate, version, state)
|
||||
{:tp_done, id} ->
|
||||
# TODO: Implement a proper teleportation API
|
||||
Logger.debug("Accepted teleportation #{inspect(id)}")
|
||||
after 0 ->
|
||||
receive do
|
||||
{:packet, id, data} ->
|
||||
|
@ -62,14 +62,8 @@ defmodule Amethyst.ConnectionState.Play do
|
||||
Macros.defpacket_serverbound :confirm_teleportation, 0x00, 747, [teleport_id: :varint]
|
||||
|
||||
def handle(%{packet_type: :confirm_teleportation, teleport_id: id}, 747, state) do
|
||||
if state[:teleport_id] == id do
|
||||
# The client has accepted our teleportation, we can let the connection process know
|
||||
send(self(), {:tp_done, id})
|
||||
state |> Map.put(:teleport_id, nil)
|
||||
else
|
||||
# The client has not yet accepted our teleportation, we aren't done yet
|
||||
state
|
||||
end
|
||||
Amethyst.API.Game.accept_teleport(state[:game], id)
|
||||
:ok
|
||||
end
|
||||
|
||||
def disconnect(reason) do
|
||||
|
@ -21,6 +21,12 @@ defmodule Example.Game do
|
||||
:ok
|
||||
end
|
||||
|
||||
@impl true
|
||||
def accept_teleport(from, id, _state_refs) do
|
||||
Logger.info("Player at #{inspect(from)} accepted teleport #{inspect(id)}")
|
||||
:ok
|
||||
end
|
||||
|
||||
@impl true
|
||||
def joinable?(_refs) do
|
||||
true
|
||||
|
Loading…
Reference in New Issue
Block a user