-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAztecs.lhs
299 lines (260 loc) · 9.97 KB
/
Aztecs.lhs
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
\documentclass[sigplan,dvipsnames,nonacm]{acmart}\settopmatter{printfolios=true,printccs=false,printacmref=false}
\usepackage{listings}
%include polycode.fmt
\title{Aztecs: An Empirical Entity Component System (ECS) for Haskell}
\author{Matt Hunzinger}
\email{matt@@hunzinger.me}
\begin {document}
\definecolor{dkcyan}{rgb}{0.1, 0.3, 0.3}
\definecolor{dkgreen}{rgb}{0,0.3,0}
\definecolor{olive}{rgb}{0.5, 0.5, 0.0}
\definecolor{dkblue}{rgb}{0,0.1,0.5}
\definecolor{col:ln}{rgb} {0.1, 0.1, 0.7}
\definecolor{col:str}{rgb} {0.8, 0.0, 0.0}
\definecolor{col:db}{rgb} {0.9, 0.5, 0.0}
\definecolor{col:ours}{rgb}{0.0, 0.7, 0.0}
\definecolor{lightgreen}{RGB}{170, 255, 220}
\definecolor{darkbrown}{RGB}{121,37,0}
\colorlet{listing-comment}{gray}
\colorlet{operator-color}{darkbrown}
\lstdefinestyle{default}{
basicstyle=\ttfamily\fontsize{8.7}{9.5}\selectfont,
columns=fullflexible,
commentstyle=\sffamily\color{black!50!white},
escapechar=\#,
framexleftmargin=1em,
framexrightmargin=1ex,
keepspaces=true,
keywordstyle=\color{dkblue},
mathescape,
numbers=none,
numberblanklines=false,
numbersep=1.25em,
numberstyle=\relscale{0.8}\color{gray}\ttfamily,
showstringspaces=true,
stepnumber=1,
xleftmargin=1em
}
\lstdefinelanguage{custom-haskell}{
language=Haskell,
deletekeywords={lookup, delete, map, mapMaybe, Ord, Maybe, String, Just, Nothing, Int, Bool},
keywordstyle=[2]\color{dkgreen},
morekeywords=[2]{String, Map, Ord, Maybe, Int, Bool},
morekeywords=[2]{Name, Expression, ESummary, PosTree, Structure, HashCode, VarMap},
keywordstyle=[3]\color{dkcyan},
mathescape=false, % so that we can write infix $
escapechar=\%, % ... but still have a way to write math between % %
literate=%
{=}{{{\color{operator-color}=}}}1
{||}{{{\color{operator-color}||}}}1
{\\}{{{\color{operator-color}\textbackslash$\,\!$}}}1
{.}{{{\color{operator-color}.}}}1
{=>}{{{\color{operator-color}=>}}}1
{->}{{{\color{operator-color}->}}}1
{<-}{{{\color{operator-color}<-}}}1
{::}{{{\color{operator-color}::}}}1
}
\lstset{style=default}
% Environment for code snippets
\lstnewenvironment{code}[1][]
{\small\lstset{language=custom-haskell,#1}}
{}
%include haskell.fmt
\newcommand{\keyword}[1]{\textcolor{BlueViolet}{\textbf{#1}}}
\newcommand{\id}[1]{\textsf{\textsl{#1}}}
\newcommand{\varid}[1]{\textcolor{Sepia}{\id{#1}}}
\newcommand{\conid}[1]{\textcolor{OliveGreen}{\id{#1}}}
\newcommand{\tick}{\text{\textquoteright}}
\newcommand{\package}[1]{\textsf{#1}}
\setlength\mathindent{0em}
\renewcommand{\hscodestyle}{\small}
\begin{abstract}
An Entity Component System, or ECS,
is a modern approach to organizing your application state as a database,
providing patterns for data-oriented design and parallel processing.\par
An ECS is comprised of three main concepts:
\begin{itemize}
\item \textbf{Entities}: An \textit{entity} represents a general-purpose object. In a game engine
context, for example, every coarse game object is represented as an
entity.~\cite{ecsWiki} Aztecs represents entities as a unique integer
identifier, similar to other ECS implementations.
\item \textbf{Components}: A \textit{component} holds the data for a particular aspect of an entity.
For example, a zombie entity in a game might have a Health and a Transform component.
\item \textbf{Systems}: A \textit{system} is a pipeline that processes entities and their components.
\end{itemize}
\end{abstract}
\maketitle
\section{Introduction}
Aztecs implements an Entity Component System (ECS) in Haskell, providing a type-safe and composable DSL.
\begin{code}
import Aztecs.ECS
import Control.Monad
import Control.Monad.IO.Class
import Data.Function
\end{code}
\subsection{Components}
Components are the building blocks of entities in an ECS.
In Aztecs, a \texttt{Component} is a typeclass that defines its storage.
By default, components are stored in a simple list (i.e. \texttt{[Position]}).
\begin{figure}[H]
\begin{code}
newtype Position = Position Int deriving (Show)
instance Component Position
newtype Velocity = Velocity Int
instance Component Velocity
\end{code}
\caption{Defining a \texttt{Position} and \texttt{Velocity} component}
\end{figure}
\subsection{Queries}
Queries can be used to read and write components in a \texttt{World}.
A \texttt{Query} can be constructed from \texttt{Applicative} combinators.
\begin{figure}[H]
\begin{code}
move :: Query Position
move = fetch & zipFetchMap (\(Velocity v) (Position p) -> Position $ p + v)
\end{code}
\caption{Do notation}
\end{figure}
\subsection{Systems}
\begin{figure}[H]
\begin{code}
run :: SystemT IO ()
run = do
positions <- query move
liftIO $ print positions
\end{code}
\caption{System that queries all \texttt{Position} and \texttt{Velocity} components and applies an update.}
\end{figure}
\subsection{Access}
Full \texttt{Access} can be used to perform structural modifications to the \texttt{World},
as long as no systems are currently running.
\begin{figure}[H]
\begin{code}
app :: AccessT IO ()
app = do
_ <- spawn $ bundle (Position 0) <> bundle (Velocity 1)
forever $ system run
\end{code}
\caption{Access to setup an entity with a \texttt{Position} and \texttt{Velocity} component}
\end{figure}
\section{Implementation}
\subsection{Archetypes}
Archetypes are groups of unique component storages.\par
For example, if an entity \textbf{0} has a \textit{Health} component with value
\textbf{100} and \textit{Damage} component with value \textbf{50}, the
components would be stored together in an archetype, and each component would
be stored in its unique storage.
\begin{table}[H]
\centering
\caption{Archetype for \textit{Health} and \textit{Damage} components}\label{tab:nested}
\begin{tabular}{l rrr rrr rrr}
\toprule
\textbf{Component} & \textbf{Entity ID} & \textbf{Value} \\ \midrule
Health &
\begin{tabular}{l}
0 \\
1 \\
\end{tabular} &
\begin{tabular}{r}
100 \\
50 \\
\end{tabular} \\
Damage &
\begin{tabular}{l}
0 \\
1 \\
\end{tabular} &
\begin{tabular}{r}
10 \\
20 \\
\end{tabular} \\ \bottomrule
\end{tabular}
\end{table}
Now if we spawn an entity \textbf{1} that has a \textit{Health} component with
value \textbf{50} and \textit{Damage} component with value \textbf{20}, the
matching components would be stored together in the same archetype.
\begin{table}[H]
\centering
\caption{Archetype for \textit{Health} and \textit{Damage} components}\label{tab:nested}
\begin{tabular}{l rrr rrr rrr}
\toprule
\textbf{Component} & \textbf{Entity ID} & \textbf{Value} \\ \midrule
Health & 0 & 100 \\
Damage & 0 & 10 \\ \bottomrule
\end{tabular}
\end{table}
Alternatively, we can insert components into existing entities such as this example
procedure:
\begin{enumerate}
\item Spawn an entity with a \textit{Health} component
\begin{table}[H]
\centering
\caption{Archetype for \textit{Health} components}\label{tab:nested}
\begin{tabular}{l rrr rrr rrr}
\toprule
\textbf{Component} & \textbf{Entity ID} & \textbf{Value} \\ \midrule
Health & 0 & 100 \\ \bottomrule
\end{tabular}
\end{table}
\item Insert a \textit{Damage} component
\begin{table}[H]
\centering
\caption{Archetype for \textit{Health} and \textit{Damage} components}\label{tab:nested}
\begin{tabular}{l rrr rrr rrr}
\toprule
\textbf{Component} & \textbf{Entity ID} & \textbf{Value} \\ \midrule
Health & 0 & 100 \\
Damage & 0 & 10 \\ \bottomrule
\end{tabular}
\end{table}
\item Spawn another enitity with a \textit{Health} component
\begin{table}[H]
\centering
\caption{Archetype for \textit{Health} components}\label{tab:nested}
\begin{tabular}{l rrr rrr rrr}
\toprule
\textbf{Component} & \textbf{Entity ID} & \textbf{Value} \\ \midrule
Health & 1 & 50 \\ \bottomrule
\end{tabular}
\end{table}
\begin{table}[H]
\centering
\caption{Archetype for \textit{Health} and \textit{Damage} components}\label{tab:nested}
\begin{tabular}{l rrr rrr rrr}
\toprule
\textbf{Component} & \textbf{Entity ID} & \textbf{Value} \\ \midrule
Health & 0 & 100 \\
Damage & 0 & 10 \\ \bottomrule
\end{tabular}
\end{table}
\item Insert a \textit{Damage} component into entity \textbf{1}
\begin{table}[H]
\centering
\caption{Archetype for \textit{Health} and \textit{Damage} components}\label{tab:nested}
\begin{tabular}{l rrr rrr rrr}
\toprule
\textbf{Component} & \textbf{Entity ID} & \textbf{Value} \\ \midrule
Health &
\begin{tabular}{l}
0 \\
1 \\
\end{tabular} &
\begin{tabular}{r}
100 \\
50 \\
\end{tabular} \\
Damage &
\begin{tabular}{l}
0 \\
1 \\
\end{tabular} &
\begin{tabular}{r}
10 \\
20 \\
\end{tabular} \\ \bottomrule
\end{tabular}
\end{table}
\end{enumerate}
\bibliographystyle{plain}
\bibliography{references}
\end {document}