-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.html
1614 lines (1199 loc) · 95.2 KB
/
game.html
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>A small space shooter in Clojure.</title>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"/>
<meta name="title" content="A small space shooter in Clojure."/>
<meta name="generator" content="Org-mode"/>
<meta name="generated" content="2013-11-05 23:18:40 SAST"/>
<meta name="author" content="Deon Moolman"/>
<meta name="description" content=""/>
<meta name="keywords" content=""/>
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
html { font-family: Times, serif; font-size: 12pt; }
.title { text-align: center; }
.todo { color: red; }
.done { color: green; }
.tag { background-color: #add8e6; font-weight:normal }
.target { }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.right {margin-left:auto; margin-right:0px; text-align:right;}
.left {margin-left:0px; margin-right:auto; text-align:left;}
.center {margin-left:auto; margin-right:auto; text-align:center;}
p.verse { margin-left: 3% }
pre {
border: 1pt solid #AEBDCC;
background-color: #F3F5F7;
padding: 5pt;
font-family: courier, monospace;
font-size: 90%;
overflow:auto;
}
table { border-collapse: collapse; }
td, th { vertical-align: top; }
th.right { text-align:center; }
th.left { text-align:center; }
th.center { text-align:center; }
td.right { text-align:right; }
td.left { text-align:left; }
td.center { text-align:center; }
dt { font-weight: bold; }
div.figure { padding: 0.5em; }
div.figure p { text-align: center; }
div.inlinetask {
padding:10px;
border:2px solid gray;
margin:10px;
background: #ffffcc;
}
textarea { overflow-x: auto; }
.linenr { font-size:smaller }
.code-highlighted {background-color:#ffff00;}
.org-info-js_info-navigation { border-style:none; }
#org-info-js_console-label { font-size:10px; font-weight:bold;
white-space:nowrap; }
.org-info-js_search-highlight {background-color:#ffff00; color:#000000;
font-weight:bold; }
/*]]>*/-->
</style>
<style type='text/css'>pre.src { background: #505050; border-radius: 15px; color: #AEBDCC } html { font-family: helvetica; font-size: 15px; text-align: center; margin: 5em 0 0 0; vertical-align: middle;} body { width: 760px; text-align: left; margin: 0 auto; } </style>
<script type="text/javascript">
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="preamble">
</div>
<div id="content">
<h1 class="title">A small space shooter in Clojure.</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#sec-1">1 Prelude</a></li>
<li><a href="#sec-2">2 Introduction</a></li>
<li><a href="#sec-3">3 Act one - OO Design.</a></li>
<li><a href="#sec-4">4 Act two - Functional approach</a></li>
<li><a href="#sec-5">5 Setup the project</a>
<ul>
<li><a href="#sec-5-1">5.1 Create the project</a></li>
<li><a href="#sec-5-2">5.2 Setup Dependencies</a></li>
<li><a href="#sec-5-3">5.3 Fire up the REPL</a></li>
</ul>
</li>
<li><a href="#sec-6">6 Setup the state handling</a>
<ul>
<li><a href="#sec-6-1">6.1 DB Helper functions</a></li>
<li><a href="#sec-6-2">6.2 Schema</a></li>
<li><a href="#sec-6-3">6.3 Spawn functions</a></li>
<li><a href="#sec-6-4">6.4 Try it out</a></li>
</ul>
</li>
<li><a href="#sec-7">7 Game loop</a>
<ul>
<li><a href="#sec-7-1">7.1 Setup</a></li>
<li><a href="#sec-7-2">7.2 Movement</a></li>
<li><a href="#sec-7-3">7.3 Collision handling</a></li>
<li><a href="#sec-7-4">7.4 Remove entities that have zero or lower health</a></li>
<li><a href="#sec-7-5">7.5 Finally, putting it all together</a></li>
</ul>
</li>
<li><a href="#sec-8">8 Visuals</a></li>
<li><a href="#sec-9">9 Bootstrap into the game</a></li>
<li><a href="#sec-10">10 Conclusions</a></li>
<li><a href="#sec-11">11 org-mode</a></li>
</ul>
</div>
</div>
<div id="outline-container-1" class="outline-2">
<h2 id="sec-1"><span class="section-number-2">1</span> Prelude</h2>
<div class="outline-text-2" id="text-1">
<p>
You can clone the repo for this project here :
<a href="https://github.com/cmdrdats/space-shooter">https://github.com/cmdrdats/space-shooter</a> or go to the rendered
version : <a href="http://cmdrdats.github.io/space-shooter">http://cmdrdats.github.io/space-shooter</a>
</p>
</div>
</div>
<div id="outline-container-2" class="outline-2">
<h2 id="sec-2"><span class="section-number-2">2</span> Introduction</h2>
<div class="outline-text-2" id="text-2">
<p>
So, at <a href="http://www.sugsa.org.za">SUGSA</a> this year, I was a a talk by <a href="http://www.twitter.com/hermanlintvelt">@hermanlintvelt</a>, titled
"GRASPing OO Design". He was speaking about general OO design
principles and I was amazed at the amount of stuff that I had,
essentially, unlearnt almost the instant I picked up Clojure.
</p>
<p>
He presented the concept of assigning responsibilities to classes in
OO and then proceeded to run through a fairly simple example of
designing a basic game - described as (with me paraphrasing):
</p>
<p>
"A Game, where the Player controls a Ship in a Level. The Ship has
Guns and a Gun can fire Bullets. There are Enemy Ships that also have
Guns that fire Bullets. Bullets hit Ships and cause damage. Ships have
health and if a Player Ship is destroyed, he loses the Game.
</p>
<p>
Furthermore, there is a Score Bar that keeps track of a Player Score,
along with which Level the Player is currently on."
</p>
<p>
There was also mention of sharing your score via social channels, and
perhaps a notion of lives, but for the purpose of this exercise I'm
just going to ignore those details.
</p>
<p>
My thought while doing the exercise of designing which classes should
exist and what their responsibilities should be was "What would this
look like, should I build it in a Functional language, like Clojure?"
</p>
</div>
</div>
<div id="outline-container-3" class="outline-2">
<h2 id="sec-3"><span class="section-number-2">3</span> Act one - OO Design.</h2>
<div class="outline-text-2" id="text-3">
<p>
So let's pull it apart first in OO terms :
</p>
<p>
<img src="oodesign.png" alt="oodesign.png" />
</p>
<p>
Here we see that a Game has Levels and a Player. The Level manages
all kinds of ships, the Player also has a handle to a Ship and that
any Ship can have Guns and Guns can own Bullets.
</p>
<p>
Ok, so maybe not the <span style="text-decoration:underline;">best</span> design, but short of actually implementing
this, it should do just fine.
</p>
</div>
</div>
<div id="outline-container-4" class="outline-2">
<h2 id="sec-4"><span class="section-number-2">4</span> Act two - Functional approach</h2>
<div class="outline-text-2" id="text-4">
<p>
The problem with the OO noun based approach, is that you are
conflating your data with behaviour. Your data should exist as
something you can store off and look at, as a snapshot at any point in
time. Having mutable objects underneath your feet mean that your code
gets kinda tricky to reason about.
</p>
<p>
What you really want is the classic approach to processing the
universe:
</p>
<p>
<img src="process.png" alt="process.png" />
</p>
<p>
You want to run your state through a function, take the result, thread
it through the next function, rinse, repeat. That's a great way of
thinking about things in the functional world.
</p>
<p>
So that got me to thinking about Component Entity Systems a little,
and then the thought of just having everything as an entity. You
basically end up with a list of hashmaps as your game state. Each
hashmap simply represents some game entity - and they can refer to
each other, a bit like foreign keys in a SQL database.
</p>
<p>
That gets a bit tricky in terms of querying your data - luckily, I
have just the tool for the job. Enter Datomic.
</p>
<p>
In a nutshell, Datomic maintains an attribute schema, which lets you
define arbitrary attributes that any entity might have and then a list
of entities which you can assign these arbitrary attributes and values
to.
</p>
<p>
Let's build some code in an attempt to clear this up
</p>
</div>
</div>
<div id="outline-container-5" class="outline-2">
<h2 id="sec-5"><span class="section-number-2">5</span> Setup the project</h2>
<div class="outline-text-2" id="text-5">
</div>
<div id="outline-container-5-1" class="outline-3">
<h3 id="sec-5-1"><span class="section-number-3">5.1</span> Create the project</h3>
<div class="outline-text-3" id="text-5-1">
<p>
The first thing we'll want to do is create a new project, so make sure
you have <a href="http://leiningen.org">Leiningen</a> installed and fire this off in your terminal:
</p>
<pre class="src src-sh">lein new space-shooter
</pre>
<p>
This will create you a space-shooter folder, you'll need to <b>cd</b> into
it for the rest of this article.
</p>
</div>
</div>
<div id="outline-container-5-2" class="outline-3">
<h3 id="sec-5-2"><span class="section-number-3">5.2</span> Setup Dependencies</h3>
<div class="outline-text-3" id="text-5-2">
<p>
Next up we open up the <code>project.clj</code> and setup the datomic and
datomic-schema dependencies. We'll throw <a href="https://github.com/quil/quil">Quil</a> in so long, since we'll
want to visualize stuff later on.
</p>
<pre class="src src-clojure"><span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">defproject</span> <span style="color: #8cd0d3; font-style: italic;">space-shooter</span> <span style="color: #CC9393;">"0.1.0-SNAPSHOT"</span>
<span style="color: #ddaa77; font-weight: bold;">:description</span> <span style="color: #CC9393;">"A simple space shooter implementation"</span>
<span style="color: #ddaa77; font-weight: bold;">:url</span> <span style="color: #CC9393;">"http://github.com/CmdrDats/space-shooter"</span>
<span style="color: #ddaa77; font-weight: bold;">:license</span> {<span style="color: #ddaa77; font-weight: bold;">:name</span> <span style="color: #CC9393;">"Eclipse Public License"</span>
<span style="color: #ddaa77; font-weight: bold;">:url</span> <span style="color: #CC9393;">"http://www.eclipse.org/legal/epl-v10.html"</span>}
<span style="color: #ddaa77; font-weight: bold;">:dependencies</span>
[[org.clojure/clojure <span style="color: #CC9393;">"1.5.1"</span>]
[com.datomic/datomic-free <span style="color: #CC9393;">"0.8.4254"</span>]
[datomic-schema <span style="color: #CC9393;">"1.0.2"</span>]
[quil <span style="color: #CC9393;">"1.6.0"</span>]]
<span style="color: #ddaa77; font-weight: bold;">:main</span> space-shooter.core<span style="color: #7f7f7f;">)</span>
</pre>
</div>
</div>
<div id="outline-container-5-3" class="outline-3">
<h3 id="sec-5-3"><span class="section-number-3">5.3</span> Fire up the REPL</h3>
<div class="outline-text-3" id="text-5-3">
<p>
Since we want to do REPL based development, let's get it running with:
</p>
<pre class="src src-sh">lein repl
</pre>
<p>
That should download the dependencies, give you an nrepl port
(something like 58621) and a => REPL prompt. You can connect into this
directly from your editor for your convenience, or just copy/paste the
code snippets below into the REPL directly.
</p>
</div>
</div>
</div>
<div id="outline-container-6" class="outline-2">
<h2 id="sec-6"><span class="section-number-2">6</span> Setup the state handling</h2>
<div class="outline-text-2" id="text-6">
<p>
Now that we have that sorted, let's open up <code>src/space_shooter/db.clj</code> -
This is where we'll define our attributes and add any utility
functions we might want to use against our datomic db.
</p>
<p>
First off, let's import the datomic-schema utility functions and the
datomic api:
</p>
<pre class="src src-clojure"><span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">ns</span> space-shooter.db
<span style="color: #7f7f7f;">(</span><span style="color: #ddaa77; font-weight: bold;">:use</span> [datomic-schema.schema <span style="color: #ddaa77; font-weight: bold;">:only</span> [defpart defschema fields]]<span style="color: #7f7f7f;">)</span>
<span style="color: #7f7f7f;">(</span><span style="color: #ddaa77; font-weight: bold;">:require</span> [datomic.api <span style="color: #ddaa77; font-weight: bold;">:as</span> d]<span style="color: #7f7f7f;">)</span>
<span style="color: #7f7f7f;">(</span><span style="color: #ddaa77; font-weight: bold;">:require</span> [datomic-schema.schema <span style="color: #ddaa77; font-weight: bold;">:as</span> s]<span style="color: #7f7f7f;">))</span>
</pre>
<p>
Then we want to setup a db-url, we'll just use the in memory db for
now. Additionally, we'll add a couple of helper functions that just
make it a bit quicker to do a few common things with the db.
</p>
</div>
<div id="outline-container-6-1" class="outline-3">
<h3 id="sec-6-1"><span class="section-number-3">6.1</span> DB Helper functions</h3>
<div class="outline-text-3" id="text-6-1">
<p>
Now to setup datomic and some helper functions around that
</p>
<pre class="src src-clojure"><span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">defonce</span> <span style="color: #8cd0d3; font-style: italic;">db-url</span> <span style="color: #CC9393;">"datomic:mem://testdb"</span><span style="color: #7f7f7f;">)</span>
</pre>
<p>
<code>db</code> just gives us the current value of the database at the latest
known point in time.
</p>
<pre class="src src-clojure"><span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">defn</span> <span style="color: #8cd0d3; font-style: italic;">db</span> [] <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">d/db</span> <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">d/connect</span> db-url<span style="color: #7f7f7f;">)))</span>
</pre>
<p>
<code>tx</code> will take a list of transactions and send them off to the
transactor for storing into the db
</p>
<pre class="src src-clojure"><span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">defn</span> <span style="color: #8cd0d3; font-style: italic;">tx</span> [t] <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">d/transact</span> <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">d/connect</span> db-url<span style="color: #7f7f7f;">)</span> t<span style="color: #7f7f7f;">))</span>
</pre>
<p>
<code>e</code> is just a helper function we can use at the repl over a list of
entity results to quickly visualise our entities
</p>
<pre class="src src-clojure"><span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">def</span> <span style="color: #8cd0d3; font-style: italic;">e</span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">comp</span> d/touch #<span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">d/entity</span> <span style="color: #7f7f7f;">(</span>db<span style="color: #7f7f7f;">)</span> %<span style="color: #7f7f7f;">)</span> first<span style="color: #7f7f7f;">))</span>
</pre>
<p>
One last thing we'll need is an addition transactor
function so that we don't end up overwriting accounting style values
(like health, for instance.) - This is taken straight from the <a href="https://github.com/Datomic/day-of-datomic/blob/master/resources/day-of-datomic/clojure-data-functions.edn">Day of Datomic</a> sample code.
</p>
<pre class="src src-clojure"><span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">def</span> <span style="color: #8cd0d3; font-style: italic;">tx-functions</span>
[{<span style="color: #ddaa77; font-weight: bold;">:db/id</span> #db/id [<span style="color: #ddaa77; font-weight: bold;">:db.part/user</span>]
<span style="color: #ddaa77; font-weight: bold;">:db/ident</span> <span style="color: #ddaa77; font-weight: bold;">:inc</span>
<span style="color: #ddaa77; font-weight: bold;">:db/doc</span> <span style="color: #CC9393;">"Data function that increments value of attribute a by amount."</span>
<span style="color: #ddaa77; font-weight: bold;">:db/fn</span> #db/fn
{<span style="color: #ddaa77; font-weight: bold;">:lang</span> <span style="color: #CC9393;">"clojure"</span>
<span style="color: #ddaa77; font-weight: bold;">:params</span> [db e a amount]
<span style="color: #ddaa77; font-weight: bold;">:code</span> [[<span style="color: #ddaa77; font-weight: bold;">:db/add</span> e a
<span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">-></span> <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">d/entity</span> db e<span style="color: #7f7f7f;">)</span> a <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">+</span> amount<span style="color: #7f7f7f;">))</span>]]}}]<span style="color: #7f7f7f;">)</span>
</pre>
</div>
</div>
<div id="outline-container-6-2" class="outline-3">
<h3 id="sec-6-2"><span class="section-number-3">6.2</span> Schema</h3>
<div class="outline-text-3" id="text-6-2">
<p>
Right, now we're ready to define the attributes for our game. The
first thing we want is a couple of global attributes we'll use on
everything, namely <code>uuid</code> and <code>type</code>, we'll namespace those under
<code>:entity</code>
</p>
<p>
We'll be using <a href="http://github.com/yuppiechef/datomic-schema">datomic-schema</a> to define the schema of our db, since
it's nice and concise.
</p>
<pre class="src src-clojure"><span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">defschema</span> <span style="color: #8cd0d3; font-style: italic;">entity</span>
<span style="color: #7f7f7f;">(</span>fields
[uuid <span style="color: #ddaa77; font-weight: bold;">:uuid</span>]
[type <span style="color: #ddaa77; font-weight: bold;">:keyword</span> <span style="color: #CC9393;">"The type of game entity"</span>]<span style="color: #7f7f7f;">))</span>
</pre>
<p>
Next up, we'll want to define some properties for all 'real' physical objects
in the game, like position, velocity, size. We'll assume very simple
rectangular bounding boxes for the collisions in this game. Also,
let's throw in the concept that another entity will 'own' this thing.
</p>
<p>
I think a good name for this will be <code>thing</code>, so that we don't confuse
it with an overloaded concept of <code>object</code>
</p>
<p>
We can assert that all <code>things</code> have health, and if they get to zero,
it will be destroyed. In the case of bullets, we'll overload that as
an amount of damage, for convenience.
</p>
<pre class="src src-clojure"><span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">defschema</span> <span style="color: #8cd0d3; font-style: italic;">thing</span>
<span style="color: #7f7f7f;">(</span>fields
[owner <span style="color: #ddaa77; font-weight: bold;">:ref</span>]
[posx <span style="color: #ddaa77; font-weight: bold;">:double</span>]
[posy <span style="color: #ddaa77; font-weight: bold;">:double</span>]
[velx <span style="color: #ddaa77; font-weight: bold;">:double</span>]
[vely <span style="color: #ddaa77; font-weight: bold;">:double</span>]
[width <span style="color: #ddaa77; font-weight: bold;">:double</span>]
[height <span style="color: #ddaa77; font-weight: bold;">:double</span>]
[health <span style="color: #ddaa77; font-weight: bold;">:long</span>]<span style="color: #7f7f7f;">))</span>
</pre>
<p>
This should be able to represent pretty much every <code>thing</code> in the
game. Now for some meta objects
</p>
<p>
We'll need some specific information about a player, like the name and score:
</p>
<pre class="src src-clojure"><span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">defschema</span> <span style="color: #8cd0d3; font-style: italic;">player</span>
<span style="color: #7f7f7f;">(</span>fields
[name <span style="color: #ddaa77; font-weight: bold;">:string</span>]
[score <span style="color: #ddaa77; font-weight: bold;">:long</span>]<span style="color: #7f7f7f;">))</span>
</pre>
<p>
The level is really just a configuration that we'll use to setup the
game to a certain state and spawn all the things in the game, so we
have no need for tracking that in our game state.
</p>
<p>
Similarly, the guns are just a configuration of the ship type, so
there's no reason to track that as state either. Bullets are just
small objects with velocity and 'health' with a bullet type.
</p>
<p>
The last part here is to create a helper function that will setup our
db, install our tx function and get the schema in
</p>
<pre class="src src-clojure"><span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">defn</span> <span style="color: #8cd0d3; font-style: italic;">setup-db</span> [& args]
<span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">d/create-database</span> db-url<span style="color: #7f7f7f;">)</span>
<span style="color: #7f7f7f;">(</span>tx <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">concat</span> tx-functions <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">s/build-schema</span> d/tempid<span style="color: #7f7f7f;">))))</span>
</pre>
</div>
</div>
<div id="outline-container-6-3" class="outline-3">
<h3 id="sec-6-3"><span class="section-number-3">6.3</span> Spawn functions</h3>
<div class="outline-text-3" id="text-6-3">
<p>
<code>new-ent</code> just sets up a transaction for a new entity by assigning it
a temporary id, a sequential UUID (better for indexing) and reminds
us to pick a type for our new entity in the game.
</p>
<pre class="src src-clojure"><span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">defn</span> <span style="color: #8cd0d3; font-style: italic;">new-ent</span> [type e]
<span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">assoc</span> e
<span style="color: #ddaa77; font-weight: bold;">:db/id</span> <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">d/tempid</span> <span style="color: #ddaa77; font-weight: bold;">:db.part/user</span><span style="color: #7f7f7f;">)</span>
<span style="color: #ddaa77; font-weight: bold;">:entity/uuid</span> <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">d/squuid</span><span style="color: #7f7f7f;">)</span>
<span style="color: #ddaa77; font-weight: bold;">:entity/type</span> type<span style="color: #7f7f7f;">))</span>
</pre>
<p>
To make it a little easier to spawn <code>thing</code>'s in the game, let's define
a spawning function:
</p>
<pre class="src src-clojure"><span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">defn</span> <span style="color: #8cd0d3; font-style: italic;">spawn</span> [type health owner [posx posy] [width height] [velx vely]]
<span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">let</span> [e {<span style="color: #ddaa77; font-weight: bold;">:thing/posx</span> posx <span style="color: #ddaa77; font-weight: bold;">:thing/posy</span> posy
<span style="color: #ddaa77; font-weight: bold;">:thing/velx</span> velx <span style="color: #ddaa77; font-weight: bold;">:thing/vely</span> vely
<span style="color: #ddaa77; font-weight: bold;">:thing/width</span> width <span style="color: #ddaa77; font-weight: bold;">:thing/height</span> height
<span style="color: #ddaa77; font-weight: bold;">:thing/health</span> health}
e <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">if</span> owner <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">assoc</span> e <span style="color: #ddaa77; font-weight: bold;">:thing/owner</span> owner<span style="color: #7f7f7f;">)</span> e<span style="color: #7f7f7f;">)</span>]
<span style="color: #7f7f7f;">(</span>new-ent type e<span style="color: #7f7f7f;">)))</span>
</pre>
</div>
</div>
<div id="outline-container-6-4" class="outline-3">
<h3 id="sec-6-4"><span class="section-number-3">6.4</span> Try it out</h3>
<div class="outline-text-3" id="text-6-4">
<p>
Now we've defined a bunch of stuff, lets see what playing with the
game state actually does. Make sure you've evaluated all of the above
code in the REPL and you're currently in the space-shooter.db
namespace (just type <code>(ns space-shooter.db)</code> if you're not)
</p>
<p>
<b>NOTE:</b> the part after the => is what you enter in each of the
following examples, the bit after that will be a sample of the
response you receive.
</p>
<p>
Let's define me as a player:
</p>
<pre class="src src-clojure"> => <span style="color: #7f7f7f;">(</span>new-ent {<span style="color: #ddaa77; font-weight: bold;">:player/name</span> <span style="color: #CC9393;">"Deon"</span> <span style="color: #ddaa77; font-weight: bold;">:player/score</span> 0} <span style="color: #ddaa77; font-weight: bold;">:player</span><span style="color: #7f7f7f;">)</span>
{<span style="color: #ddaa77; font-weight: bold;">:entity/type</span> <span style="color: #ddaa77; font-weight: bold;">:player</span>,
<span style="color: #ddaa77; font-weight: bold;">:entity/uuid</span> #uuid <span style="color: #CC9393;">"527740ce-962f-49dd-9978-36e385980f4c"</span>,
<span style="color: #ddaa77; font-weight: bold;">:db/id</span> #db/id[<span style="color: #ddaa77; font-weight: bold;">:db.part/user</span> -1000000],
<span style="color: #ddaa77; font-weight: bold;">:player/score</span> 0,
<span style="color: #ddaa77; font-weight: bold;">:player/name</span> <span style="color: #CC9393;">"Deon"</span>}
</pre>
<p>
That's neat, but it hasn't done anything in the db yet - we're happy
with it though, so let's store it off:
</p>
<pre class="src src-clojure">=> <span style="color: #7f7f7f;">(</span>tx [*1]<span style="color: #7f7f7f;">)</span>
<span style="color: #94BFF3; font-weight: bold; font-style: italic;">ExceptionInfo</span> <span style="color: #ddaa77; font-weight: bold;">:db.error/db-not-found</span> <span style="color: #94BFF3; font-weight: bold; font-style: italic;">Could</span> not find testdb in catalog datomic.error/raise <span style="color: #7f7f7f;">(</span>error.clj:46<span style="color: #7f7f7f;">)</span>
</pre>
<p>
Whoops. We were a little too eager.. we actually need to create the
database and setup the schema first!
</p>
<pre class="src src-clojure">=> <span style="color: #7f7f7f;">(</span>setup-db<span style="color: #7f7f7f;">)</span>
#<promise$settable_future$reify__4424@5220c1b:
{<span style="color: #ddaa77; font-weight: bold;">:db-before</span> datomic.db.Db@4f97ab72,
<span style="color: #ddaa77; font-weight: bold;">:db-after</span> datomic.db.Db@df8e05ff,
<span style="color: #ddaa77; font-weight: bold;">:tx-data</span> [...], <span style="color: #ddaa77; font-weight: bold;">:tempids</span> {...}}
</pre>
<p>
Nice - notice how we have received an object back where we can get the
value of the db before and the value after our transaction. That's
universal for every transaction, including setting up the schema or
just writing any arbitrary data. We also get a temporary id
to actual id map back, which we could use to update our knowledge of
the entities we've just committed into the db.
</p>
<p>
For now, we ignore this result and try creating a new player again<sup><a class="footref" name="fnr.1" href="#fn.1">1</a></sup>:
</p>
<pre class="src src-clojure">=> <span style="color: #7f7f7f;">(</span>new-ent {<span style="color: #ddaa77; font-weight: bold;">:player/name</span> <span style="color: #CC9393;">"Deon"</span> <span style="color: #ddaa77; font-weight: bold;">:player/score</span> 0} <span style="color: #ddaa77; font-weight: bold;">:player</span><span style="color: #7f7f7f;">)</span>
{<span style="color: #ddaa77; font-weight: bold;">:entity/type</span> <span style="color: #ddaa77; font-weight: bold;">:player</span>,
<span style="color: #ddaa77; font-weight: bold;">:entity/uuid</span> #uuid <span style="color: #CC9393;">"527740ce-962f-49dd-9978-36e385980f4c"</span>,
<span style="color: #ddaa77; font-weight: bold;">:db/id</span> #db/id[<span style="color: #ddaa77; font-weight: bold;">:db.part/user</span> -1000000],
<span style="color: #ddaa77; font-weight: bold;">:player/score</span> 0,
<span style="color: #ddaa77; font-weight: bold;">:player/name</span> <span style="color: #CC9393;">"Deon"</span>}
=> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">def</span> <span style="color: #8cd0d3; font-style: italic;">p</span> *1<span style="color: #7f7f7f;">)</span>
#'space-shooter.db/p
=> <span style="color: #7f7f7f;">(</span>tx [p]<span style="color: #7f7f7f;">)</span>
#<promise$settable_future$reify__4424@3f901572:
{<span style="color: #ddaa77; font-weight: bold;">:db-before</span> datomic.db.Db@df8e05ff,
<span style="color: #ddaa77; font-weight: bold;">:db-after</span> datomic.db.Db@ee7763e0,
<span style="color: #ddaa77; font-weight: bold;">:tx-data</span> [...], <span style="color: #ddaa77; font-weight: bold;">:tempids</span> {...}}>
</pre>
<p>
Now we can query for the player to make sure it's there:
</p>
<pre class="src src-clojure">=> <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">d/q</span> '[<span style="color: #ddaa77; font-weight: bold;">:find</span> ?e <span style="color: #ddaa77; font-weight: bold;">:where</span> [?e <span style="color: #ddaa77; font-weight: bold;">:entity/type</span> <span style="color: #ddaa77; font-weight: bold;">:player</span>]] <span style="color: #7f7f7f;">(</span>db<span style="color: #7f7f7f;">))</span>
#{[17592186045418]}
=> <span style="color: #7f7f7f;">(</span><span style="color: #7CB8BB;">e</span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">first</span> *1<span style="color: #7f7f7f;">))</span>
{<span style="color: #ddaa77; font-weight: bold;">:entity/type</span> <span style="color: #ddaa77; font-weight: bold;">:player</span>,
<span style="color: #ddaa77; font-weight: bold;">:entity/uuid</span> #uuid <span style="color: #CC9393;">"527740ce-962f-49dd-9978-36e385980f4c"</span>,
<span style="color: #ddaa77; font-weight: bold;">:player/score</span> 0, <span style="color: #ddaa77; font-weight: bold;">:player/name</span> <span style="color: #CC9393;">"Deon"</span>,
<span style="color: #ddaa77; font-weight: bold;">:db/id</span> 17592186045418}
</pre>
<p>
And there it is. We should be able store any arbitrary entity we
should need in the game.
</p>
</div>
</div>
</div>
<div id="outline-container-7" class="outline-2">
<h2 id="sec-7"><span class="section-number-2">7</span> Game loop</h2>
<div class="outline-text-2" id="text-7">
</div>
<div id="outline-container-7-1" class="outline-3">
<h3 id="sec-7-1"><span class="section-number-3">7.1</span> Setup</h3>
<div class="outline-text-3" id="text-7-1">
<p>
Well now we get to the crunch - what does the game loop look like?
</p>
<p>
First we setup our namespace in <code>src/space_shooter/loop.clj</code>
</p>
<pre class="src src-clojure"><span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">ns</span> space-shooter.loop
<span style="color: #7f7f7f;">(</span><span style="color: #ddaa77; font-weight: bold;">:require</span>
[datomic.api <span style="color: #ddaa77; font-weight: bold;">:as</span> d]
[space-shooter.db <span style="color: #ddaa77; font-weight: bold;">:as</span> db]<span style="color: #7f7f7f;">))</span>
</pre>
<p>
We're going to write a few functions that will adhere to the signature
of <code>(defn actions [elapsedms db])</code> which will simply query the current db and
return a list of transactions that it should apply for this step. For
example, a function that would spawn a new bullet at every step (very fast.) :
</p>
<pre class="src src-clojure"><span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">defn</span> <span style="color: #8cd0d3; font-style: italic;">spawn-bullet</span> [elapsed db]
[<span style="color: #7f7f7f;">(</span>spawn <span style="color: #ddaa77; font-weight: bold;">:bullet</span> 10 nil [100.0 100.0] [5.0 5.0] [0.0 -5.0]<span style="color: #7f7f7f;">)</span>]<span style="color: #7f7f7f;">)</span>
</pre>
<p>
If we wanted to make it shoot only a bullet every second, we could
keep a timeout value as an entity in the db, but we're going to ignore
that in favour of a simple example.
</p>
</div>
</div>
<div id="outline-container-7-2" class="outline-3">
<h3 id="sec-7-2"><span class="section-number-3">7.2</span> Movement</h3>
<div class="outline-text-3" id="text-7-2">
<p>
Let's write a bit of code that will look for all the entities and move
them along according to their velocity and elapsed time.
</p>
<pre class="src src-clojure"><span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">defn</span> <span style="color: #8cd0d3; font-style: italic;">add-velocity</span> [elapsed thing]
[[<span style="color: #ddaa77; font-weight: bold;">:inc</span> <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">:db/id</span> thing<span style="color: #7f7f7f;">)</span> <span style="color: #ddaa77; font-weight: bold;">:thing/posx</span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">*</span> elapsed <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">:thing/velx</span> thing<span style="color: #7f7f7f;">))</span>]
[<span style="color: #ddaa77; font-weight: bold;">:inc</span> <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">:db/id</span> thing<span style="color: #7f7f7f;">)</span> <span style="color: #ddaa77; font-weight: bold;">:thing/posy</span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">*</span> elapsed <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">:thing/vely</span> thing<span style="color: #7f7f7f;">))</span>]]<span style="color: #7f7f7f;">)</span>
<span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">defn</span> <span style="color: #8cd0d3; font-style: italic;">movements</span> [elapsed db]
<span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">->></span>
<span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">d/q</span> '[<span style="color: #ddaa77; font-weight: bold;">:find</span> ?e <span style="color: #ddaa77; font-weight: bold;">:where</span> [?e <span style="color: #ddaa77; font-weight: bold;">:thing/posx</span>]] db<span style="color: #7f7f7f;">)</span>
<span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">mapcat</span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">comp</span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">partial</span> add-velocity elapsed<span style="color: #7f7f7f;">)</span>
<span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">partial</span> d/entity db<span style="color: #7f7f7f;">)</span> first<span style="color: #7f7f7f;">))))</span>
</pre>
<p>
So <code>movements</code> will look for all entities with an attribute
of :thing/posx that exists and put together tx functions for adding
all the movements to them. We're using the threading macro here to
take the results of <code>d/q</code> and pass it as the last argument of <code>mapcat</code>
</p>
<p>
Let's check that this actually works? First, we create a ship :
</p>
<pre class="src src-clojure">=> <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">db/tx</span> [<span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">db/spawn</span> <span style="color: #ddaa77; font-weight: bold;">:ship</span> 10 nil [100.0 100.0] [10.0 10.0] [1.0 2.0]<span style="color: #7f7f7f;">)</span>]<span style="color: #7f7f7f;">)</span>
#<promise$settable_future$reify__4424@3cde8a82:
{<span style="color: #ddaa77; font-weight: bold;">:db-before</span> datomic.db.Db@eebfe950,
<span style="color: #ddaa77; font-weight: bold;">:db-after</span> datomic.db.Db@c77c1b7f,
<span style="color: #ddaa77; font-weight: bold;">:tx-data</span> [...], <span style="color: #ddaa77; font-weight: bold;">:tempids</span> {...}}>
</pre>
<p>
Great, our ship has been spawned - now let's see how we'd move it:
</p>
<pre class="src src-clojure">=> <span style="color: #7f7f7f;">(</span>movements 100 <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">db/db</span><span style="color: #7f7f7f;">))</span>
<span style="color: #7f7f7f;">(</span>[<span style="color: #ddaa77; font-weight: bold;">:inc</span> 17592186045422 <span style="color: #ddaa77; font-weight: bold;">:thing/posx</span> 100.0]
[<span style="color: #ddaa77; font-weight: bold;">:inc</span> 17592186045422 <span style="color: #ddaa77; font-weight: bold;">:thing/posy</span> 200.0]<span style="color: #7f7f7f;">)</span>
=> <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">db/tx</span> *1<span style="color: #7f7f7f;">)</span>
#<promise$settable_future$reify__4424@4d036908:
{<span style="color: #ddaa77; font-weight: bold;">:db-before</span> datomic.db.Db@8f7ff0d8,
<span style="color: #ddaa77; font-weight: bold;">:db-after</span> datomic.db.Db@8ee561ed,
<span style="color: #ddaa77; font-weight: bold;">:tx-data</span> [...], <span style="color: #ddaa77; font-weight: bold;">:tempids</span> {}}
</pre>
<p>
That looks good, let's have a look at our ship and see if the position
has changed accordingly:
</p>
<pre class="src src-clojure">=> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">map</span> db/<span style="color: #7CB8BB;">e</span> <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">d/q</span> '[<span style="color: #ddaa77; font-weight: bold;">:find</span> ?e <span style="color: #ddaa77; font-weight: bold;">:where</span> [?e <span style="color: #ddaa77; font-weight: bold;">:entity/type</span> <span style="color: #ddaa77; font-weight: bold;">:ship</span>]] <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">db/db</span><span style="color: #7f7f7f;">)))</span>
<span style="color: #7f7f7f;">(</span>{<span style="color: #ddaa77; font-weight: bold;">:entity/type</span> <span style="color: #ddaa77; font-weight: bold;">:ship</span>,
<span style="color: #ddaa77; font-weight: bold;">:entity/uuid</span> #uuid <span style="color: #CC9393;">"5278077d-3497-4fdb-94fe-a032633d15f1"</span>,
<span style="color: #ddaa77; font-weight: bold;">:thing/posx</span> 200.0, <span style="color: #ddaa77; font-weight: bold;">:thing/posy</span> 300.0,
<span style="color: #ddaa77; font-weight: bold;">:thing/height</span> 10.0, <span style="color: #ddaa77; font-weight: bold;">:thing/vely</span> 2.0,
<span style="color: #ddaa77; font-weight: bold;">:thing/width</span> 10.0, <span style="color: #ddaa77; font-weight: bold;">:thing/velx</span> 1.0,
<span style="color: #ddaa77; font-weight: bold;">:thing/health</span> 10, <span style="color: #ddaa77; font-weight: bold;">:db/id</span> 17592186045422}<span style="color: #7f7f7f;">)</span>
</pre>
<p>
Beautiful. That worked nicely.
</p>
</div>
</div>
<div id="outline-container-7-3" class="outline-3">
<h3 id="sec-7-3"><span class="section-number-3">7.3</span> Collision handling</h3>
<div class="outline-text-3" id="text-7-3">
<p>
Let's keep this clean and assume that anything that gets hit by
something else loses as much health as the the thing that hit it.
</p>
<p>
We'll start with creating some things in our db that actually collide
and build the query that finds them:
</p>
<pre class="src src-clojure">=> <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">db/tx</span>
[<span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">db/spawn</span> <span style="color: #ddaa77; font-weight: bold;">:ship</span> 10 nil [200.0 200.0] [10.0 10.0] [1.0 2.0]<span style="color: #7f7f7f;">)</span>
<span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">db/spawn</span> <span style="color: #ddaa77; font-weight: bold;">:ship</span> 6 nil [205.0 205.0] [10.0 10.0] [1.0 2.0]<span style="color: #7f7f7f;">)</span>]<span style="color: #7f7f7f;">)</span>
#<promise$settable_future$reify__4424@45d017d4:....
</pre>
<p>
Two ships are going to collide! We expect one to come out with a
health of 4 and the other a health of -4. We'll take care of actually
removing an entity in a seperate cleanup function.
</p>
<p>
So, how do we find them? We can start with a collides function that
takes an entity's position and size and another position and size and
compare them for collision (single axis collision)
</p>
<pre class="src src-clojure"><span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">defn</span> <span style="color: #8cd0d3; font-style: italic;">collides</span> [ep es op os]
<span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">or</span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">and</span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">></span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">+</span> ep es<span style="color: #7f7f7f;">)</span> op<span style="color: #7f7f7f;">)</span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;"><</span> ep <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">+</span> op os<span style="color: #7f7f7f;">)))</span>
<span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">and</span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">></span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">+</span> op os<span style="color: #7f7f7f;">)</span> ep<span style="color: #7f7f7f;">)</span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;"><</span> op <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">+</span> ep es<span style="color: #7f7f7f;">)))))</span>
</pre>
<p>
Then we can run this query to find them:
</p>
<pre class="src src-clojure">=> <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">d/q</span>
'[<span style="color: #ddaa77; font-weight: bold;">:find</span> ?e ?o <span style="color: #ddaa77; font-weight: bold;">:where</span>
[?e <span style="color: #ddaa77; font-weight: bold;">:thing/posx</span> ?epx]
[?e <span style="color: #ddaa77; font-weight: bold;">:thing/width</span> ?ew]
[?o <span style="color: #ddaa77; font-weight: bold;">:thing/posx</span> ?opx]
[?o <span style="color: #ddaa77; font-weight: bold;">:thing/width</span> ?ow]
[<span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">space-shooter.loop/collides</span> ?epx ?ew ?opx ?ow<span style="color: #7f7f7f;">)</span>]
[?e <span style="color: #ddaa77; font-weight: bold;">:thing/posy</span> ?epy]
[?e <span style="color: #ddaa77; font-weight: bold;">:thing/height</span> ?eh]
[?o <span style="color: #ddaa77; font-weight: bold;">:thing/posy</span> ?opy]
[?o <span style="color: #ddaa77; font-weight: bold;">:thing/height</span> ?oh]
[<span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">space-shooter.loop/collides</span> ?epy ?eh ?opy ?oh<span style="color: #7f7f7f;">)</span>]
[<span style="color: #7f7f7f;">(</span>!= ?e ?o<span style="color: #7f7f7f;">)</span>]] <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">db/db</span><span style="color: #7f7f7f;">))</span>
#{[17592186045419 17592186045420] [17592186045420 17592186045419]}
</pre>
<p>
This shows us that ..419 collides with ..420 and ..420 collides with
..419. Excellent. Let's make sure another ship won't also collide if
outside the bounds :
</p>
<pre class="src src-clojure">=> <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">db/tx</span> [<span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">db/spawn</span> <span style="color: #ddaa77; font-weight: bold;">:ship</span> 100 nil [100.0 100.0] [10.0 10.0] [1.0 2.0]<span style="color: #7f7f7f;">)</span>]<span style="color: #7f7f7f;">)</span>
#<promise$settable_future$reify__4424@6596f6ef:...
</pre>
<p>
And if we run the query again, we see it hasn't affected our result.
Hurrah! But is there a slightly more succinct way of expressing this query?
</p>
<pre class="src src-clojure"><span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">defn</span> <span style="color: #8cd0d3; font-style: italic;">collides</span> [db e o pos size]
<span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">let</span> [ent <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">d/entity</span> db e<span style="color: #7f7f7f;">)</span>
oth <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">d/entity</span> db o<span style="color: #7f7f7f;">)</span>
ep <span style="color: #7f7f7f;">(</span>pos ent<span style="color: #7f7f7f;">)</span> es <span style="color: #7f7f7f;">(</span>size ent<span style="color: #7f7f7f;">)</span>
op <span style="color: #7f7f7f;">(</span>pos oth<span style="color: #7f7f7f;">)</span> os <span style="color: #7f7f7f;">(</span>size oth<span style="color: #7f7f7f;">)</span>]
<span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">or</span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">and</span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">></span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">+</span> ep es<span style="color: #7f7f7f;">)</span> op<span style="color: #7f7f7f;">)</span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;"><</span> ep <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">+</span> op os<span style="color: #7f7f7f;">)))</span>
<span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">and</span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">></span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">+</span> op os<span style="color: #7f7f7f;">)</span> ep<span style="color: #7f7f7f;">)</span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;"><</span> op <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">+</span> ep es<span style="color: #7f7f7f;">))))))</span>
</pre>
<p>
Then we can express our query like so:
</p>
<pre class="src src-clojure">=> <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">d/q</span>
'[<span style="color: #ddaa77; font-weight: bold;">:find</span> ?e ?o <span style="color: #ddaa77; font-weight: bold;">:where</span> [?e <span style="color: #ddaa77; font-weight: bold;">:thing/posx</span>] [?o <span style="color: #ddaa77; font-weight: bold;">:thing/posx</span>]
[<span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">space-shooter.loop/collides</span> $ ?e ?o <span style="color: #ddaa77; font-weight: bold;">:thing/posx</span> <span style="color: #ddaa77; font-weight: bold;">:thing/width</span><span style="color: #7f7f7f;">)</span>]
[<span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">space-shooter.loop/collides</span> $ ?e ?o <span style="color: #ddaa77; font-weight: bold;">:thing/posy</span> <span style="color: #ddaa77; font-weight: bold;">:thing/height</span><span style="color: #7f7f7f;">)</span>]
[<span style="color: #7f7f7f;">(</span>!= ?e ?o<span style="color: #7f7f7f;">)</span>]] <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">db/db</span><span style="color: #7f7f7f;">))</span>
#{[17592186045419 17592186045420] [17592186045420 17592186045419]}
</pre>
<p>
Same results and slightly more succinct query, but I'd venture to
guess that it would be a bit slower. Don't quote me on it though.
</p>
<p>
Anyhow - we'll go for the latter approach, since we can always
optimize later.
</p>
<pre class="src src-clojure"><span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">defn</span> <span style="color: #8cd0d3; font-style: italic;">collision-damage</span> [[thing other]]
[[<span style="color: #ddaa77; font-weight: bold;">:inc</span> <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">:db/id</span> thing<span style="color: #7f7f7f;">)</span> <span style="color: #ddaa77; font-weight: bold;">:thing/health</span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">-</span> <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">:thing/health</span> other<span style="color: #7f7f7f;">))</span>]]<span style="color: #7f7f7f;">)</span>
<span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">defn</span> <span style="color: #8cd0d3; font-style: italic;">collisions</span> [elapsed db]
<span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold;">->></span>
<span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">d/q</span>
'[<span style="color: #ddaa77; font-weight: bold;">:find</span> ?e ?o <span style="color: #ddaa77; font-weight: bold;">:where</span> [?e <span style="color: #ddaa77; font-weight: bold;">:thing/posx</span>] [?o <span style="color: #ddaa77; font-weight: bold;">:thing/posx</span>]
[<span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">space-shooter.loop/collides</span> $ ?e ?o <span style="color: #ddaa77; font-weight: bold;">:thing/posx</span> <span style="color: #ddaa77; font-weight: bold;">:thing/width</span><span style="color: #7f7f7f;">)</span>]
[<span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">space-shooter.loop/collides</span> $ ?e ?o <span style="color: #ddaa77; font-weight: bold;">:thing/posy</span> <span style="color: #ddaa77; font-weight: bold;">:thing/height</span><span style="color: #7f7f7f;">)</span>]
[<span style="color: #7f7f7f;">(</span>!= ?e ?o<span style="color: #7f7f7f;">)</span>]] db<span style="color: #7f7f7f;">)</span>
<span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">mapcat</span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">comp</span> collision-damage (fn [t] <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">map</span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">partial</span> d/entity db<span style="color: #7f7f7f;">)</span> t<span style="color: #7f7f7f;">))))))</span>
</pre>
<p>
And if we run this :
</p>
<pre class="src src-clojure">=> <span style="color: #7f7f7f;">(</span>collisions 0 <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">db/db</span><span style="color: #7f7f7f;">))</span>
<span style="color: #7f7f7f;">(</span>[<span style="color: #ddaa77; font-weight: bold;">:inc</span> 17592186045419 <span style="color: #ddaa77; font-weight: bold;">:thing/health</span> -6] [<span style="color: #ddaa77; font-weight: bold;">:inc</span> 17592186045420 <span style="color: #ddaa77; font-weight: bold;">:thing/health</span> -10]<span style="color: #7f7f7f;">)</span>
=> <span style="color: #7f7f7f;">(</span><span style="color: #8cd0d3; font-style: italic;">db/tx</span> *1<span style="color: #7f7f7f;">)</span>
#<promise$settable_future$reify__4424@23f23303:...
=> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">map</span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">comp</span> <span style="color: #7f7f7f;">(</span><span style="color: #F0DFAF; font-weight: bold; font-style: italic;">juxt</span> <span style="color: #ddaa77; font-weight: bold;">:db/id</span> <span style="color: #ddaa77; font-weight: bold;">:thing/posx</span> <span style="color: #ddaa77; font-weight: bold;">:thing/health</span><span style="color: #7f7f7f;">)</span> db/<span style="color: #7CB8BB;">e</span><span style="color: #7f7f7f;">)</span>