amethyst/apps/example_game/lib/example/game.ex
Kodi Craft 47bb453178
All checks were successful
Build & Test / nix-build (push) Successful in 1m53s
"Implement" "chat"
2024-10-09 12:19:50 +02:00

80 lines
2.0 KiB
Elixir

defmodule Example.Game do
require Logger
@behaviour Amethyst.Game
@moduledoc """
Example game used for testing Amethyst.
"""
require Amethyst.NBT.Write
alias Amethyst.NBT
@impl true
def instantiate(supervisor) do
Logger.info("The supervisor for this game is at #{inspect(supervisor)}")
{:ok, %{}}
end
@impl true
def login(from, cfg, refs) do
Logger.info("Player logged in from #{inspect(from)}: #{inspect(cfg)}")
Logger.info("The refs for this game are #{inspect(refs)}")
{:accept, {0.0, 270.0, 0.0}, {0.0, 0.0}}
end
@impl true
@spec player_position(any(), {any(), any(), any()}, any()) :: :ok
def player_position(from, {x, y, z}, _refs) do
# Logger.info("Player at #{inspect(from)} moved to #{x}, #{y}, #{z}")
send(from, {:set_position, {x, y, z}})
:ok
end
@impl true
def player_rotation(_from, {_yaw, _pitch}, _refs) do
# Logger.info("Player at #{inspect(from)} rotated to #{yaw}, #{pitch}")
: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 chat(from, message, _state_refs) do
Logger.info("Player at #{inspect(from)} said: #{inspect(message)}")
send(from, {:send_packet, %{
packet_type: :system_chat_message,
content: NBT.Write.compound(%{"text" => NBT.Write.string("You said: #{message}")}),
overlay: false
}})
:ok
end
@impl true
def joinable?(_refs) do
true
end
@impl true
def chunk(_from, {cx, cz}, _state_refs) do
# Logger.info("Player at #{inspect(from)} wants to know chunk #{cx}, #{cz}")
(0..255) |> Enum.map(fn y ->
(0..15) |> Enum.map(fn z ->
(0..15) |> Enum.map(fn x ->
gx = cx * 16 + x
gz = cz * 16 + z
gy = y
if rem(gx, 4) == 0 && rem(gy, 4) == 0 && rem(gz, 4) == 0 do
{abs(rem(div(gx + gy + gz, 4), 300)), 7, 7}
else
{0, 7, 7}
end
end)
end)
end)
end
end