blob: 8c1bb9ce8e4963dab310a4d535aeea049538eb8d (
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
|
(import utils [sub-points Direction])
;; Define systems here
(setv GRAVITY 5)
(defclass GoalHit [Exception])
(defn entities-by-type [entities types]
(gfor ent entities
:if (in ent.type types)
ent))
(defmacro defsystem [name pred #* body]
`(defn ~name [entity entities]
(when ~pred
~@body)))
(defsystem apply-gravity
(not entity.fixed)
(.move entity #(0 GRAVITY)))
(defsystem apply-collisions
(not entity.fixed)
(for [ent (gfor enti entities
:if (!= enti.id entity.id)
enti)]
(when (.colliderect entity.rect ent.rect)
(if (= ent.type "goal")
(raise (GoalHit))
(let [collision-rect (.clip entity.rect ent.rect)
move-x (get entity.total-move 0)
move-y (get entity.total-move 1)]
(when (!= move-x 0)
(.move entity #((* (if (> move-x 0) -1 1) collision-rect.width) 0)))
(when (!= move-y 0)
(.move entity #(0 (* (if (> move-y 0) -1 1) collision-rect.height))))))
(.ground entity))))
(defsystem run-enemies
(= entity.type "enemy")
;; If the player is on the tile next to the enemy, attack.
(let [player (next (entities-by-type entities ["player"]))
dist (sub-points entity.pos player.pos)
direction (.x-from-move Direction dist)]
;; If facing the player and within reach, attack)
(when (and (= direction entity.facing)
(< (get dist 1) 1) ;; Same level
(< (abs (get dist 0)) 2)
(not entity.attacking))
(setv entity.attacking True)
(print "attack")
)
))
|