-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdefold-script.crn
62 lines (55 loc) · 1.7 KB
/
defold-script.crn
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
;;;
; Main player script.
; Controls the player's behaviour.
;;;
(def gravity 9.81)
; Acquire input focus
(defn! init [self] :global
(declare msg)
(msg::post "." "acquire_input_focus")
(object/set self :speed_y 0)
(object/set self :jump_power 500)
(object/set self :move_speed 12.5)
(object/set self :touching_ground false))
; Move the game object in the given axis.
; Axis should be one of :x, :y or :z
(defn! move [dir amt]
(declare go)
(let [p (go::get_position)]
(object/set p dir (+ (object/get p dir) amt))
(go::set_position p)))
; Handle messages
(defn! on_message [self message-id message sender] :global
(declare hash)
(declare go)
(if (= (hash "contact_point_response") message-id)
(let [p (go::get_position)
p' (+ p (* message::normal message::distance))]
(object/set self :touching_ground true)
(go::set_position p'))))
; Update mechanics.
(defn! update [self dt] :global
(object/set self :speed_y (- self::speed_y gravity))
(move :y (* dt self::speed_y)))
; Jump action
(defn! jump! [self]
(if (object/get self :touching_ground)
(do
(object/set self :touching_ground false)
(object/set self :speed_y (object/get self :jump_power)))))
; Lateral impulse
(defn! go! [self dir]
(declare sprite)
(when dir
:left (sprite::set_hflip "#body" false)
:right (sprite::set_hflip "#body" true)
_ (throw (str "Unknown dir " dir)))
(move :x (* (object/get self :move_speed) (if (= dir :right) 1 -1))))
; Input handler for the player.
(defn! on_input [self action-id action] :global
(declare hash)
(declare go)
(when action-id
(hash "space") (jump! self)
(hash "right") (go! self :right)
(hash "left") (go! self :left)))