aboutsummaryrefslogtreecommitdiffstats
path: root/lib/lkn/core/component.ex
blob: 51b5a63fbfdb9641774a0ecbd9f9ea69694f81b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#
# lkn.core: an entity-component-system (ecs) implementation for lyxan
# Copyright (C) 2017 Thomas Letan <contact@thomasletan.fr>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
defmodule Lkn.Core.Component do
  alias Lkn.Core.Specs

  @type state :: any

  @callback init_state(Lkn.Core.Entity.k) :: {:ok, state} | :error

  defmacro defcomponent(name, do: block) do
    state_type = quote do Lkn.Core.Component.state end
    key_type = quote do Lkn.Core.Component.k end
    key_to_name = quote do
      &(Lkn.Core.Name.component(&1, unquote(name)))
    end

    quote do
      defmodule unquote(name) do
        unquote(Specs.gen_server_from_specs(block, key_type, key_to_name, state_type))

        def system do
          @system
        end

        defmacro __using__(_) do
          quote do
            defmodule State do
              defstruct [
                :entity_key,
                :state,
              ]

              @type t :: %State{
                entity_key: Lkn.Core.Entity.k,
                state: Lkn.Core.Component.state,
              }
            end

            @behaviour unquote(__MODULE__)
            @behaviour Lkn.Core.Component

            use GenServer

            alias Lkn.Core.Entity
            alias Lkn.Core.Name
            alias Lkn.Core.Properties

            def specs do
              unquote(__MODULE__)
            end

            @spec start_link(Entity.k) :: GenServer.on_start
            def start_link(entity_key) do
              GenServer.start_link(__MODULE__, entity_key, name: Name.component(entity_key, unquote(__MODULE__)))
            end

            def init(entity_key) do
              {:ok, s} = init_state(entity_key)
              {:ok, %State{entity_key: entity_key, state: s}}
            end

            @spec read(Entity.k, Properties.prop) :: Option.t(Properties.value)
            defp read(entity_key, p) do
              Properties.read(entity_key, p)
            end

            @spec write(Entity.k, Properties.prop, Properties.value) :: :ok
            def write(entity_key, p, v) do
              Properties.write(entity_key, p, v)
            end

            def handle_cast({:spec, {name, args}}, state) do
              s = :erlang.apply(__MODULE__, name, [state.entity_key|args] ++ [state.state])

              {:noreply, %State{state|state: s}}
            end

            def handle_call({:spec, {name, args}}, _call, state) do
              {res, s} = :erlang.apply(__MODULE__, name, [state.entity_key|args] ++ [state.state])

              {:reply, res, %State{state|state: s}}
            end
          end
        end
      end
    end
  end
end