Skip to content

Instantly share code, notes, and snippets.

@worrel
Last active August 28, 2016 16:00
Show Gist options
  • Save worrel/1986a7de190865f528d40e9bb9a7d75e to your computer and use it in GitHub Desktop.
Save worrel/1986a7de190865f528d40e9bb9a7d75e to your computer and use it in GitHub Desktop.
(require '[arcadia.core :as a])
(require '[arcadia.linear :as l])
(require '[simple :as as])
(import [UnityEngine Vector3 Transform Rigidbody GameObject Time])
(def cube (a/create-primitive :cube))
(def camera (a/object-named "Main Camera"))
(def cam-xform (a/cmpt camera Transform))
(a/cmpt+ cube Rigidbody)
(a/set-state! camera :anim-simple/cam-target orig-cube)
(a/set-state! camera :anim-simple/cam-offset (l/v3- (.. camera transform position) (.. orig-cube transform position)))
(a/hook+ orig-cube :update #'as/grow-shrink)
(a/hook+ orig-cube :start #'as/move-start)
(a/hook+ orig-cube :fixed-update #'as/push-fixed)
(a/hook+ orig-cube :fixed-update #'as/jump-fixed)
(a/hook+ camera :late-update #'as/cam-follow)
(ns simple
(:require [arcadia.core :as a]
[arcadia.linear :as l])
(:import [UnityEngine Debug GameObject Input Time Transform Resources Rigidbody Vector3]))
(defn ensure-state!
[go kw v]
(or (a/state go kw)
(a/set-state! go kw v)))
(defn spin
[go]
(let [xform (a/cmpt go Transform)
rate (ensure-state! go ::spin-rate 30)]
(.Rotate xform (l/v3* Vector3/back (* rate Time/deltaTime)))))
(defn grow-shrink
[go]
(let [xform (a/cmpt go Transform)
scale (.localScale xform)
x (.x scale)
set-dir #(a/update-state! go ::grow-shrink assoc :dir %)
state (ensure-state! go ::grow-shrink
{:dir 1 :min-x 1 :max-x 1.25 :rate 0.01})
_ (cond
(<= x (:min-x state)) (set-dir 1)
(> x (:max-x state)) (set-dir -1))
delta (l/v3 (* (:dir state) (:rate state)))]
(set! (.localScale xform) (l/v3+ scale delta))))
(defn move-towards
[go]
(when-let [target (a/state go ::move-target)]
(let [rb (a/cmpt go Rigidbody)
xform (a/cmpt target Transform)
speed (or (a/state go ::move-speed) 10)
step (* speed Time/deltaTime)]
(set! (.position rb)
(Vector3/MoveTowards
(.position rb)
(.position xform)
step)))))
(defn cam-offset
[cam target]
(l/v3- (.. cam transform position)
(.. target transform position)))
(defn cam-follow
[go]
(let [^GameObject target (ensure-state! go ::cam-target (a/object-named "Cube"))
^Vector3 offset (ensure-state! go ::cam-offset (cam-offset go target))]
(set! (.. go transform position)
(l/v3+ offset
(.. target transform position)))))
(defn move-start
[go]
(a/set-state! go ::move-rb (a/cmpt go Rigidbody)))
(defn ensure-rb
[go]
(ensure-state! go ::move-rb (a/cmpt go Rigidbody)))
(defn push-fixed
[go]
(let [rb (ensure-rb go)
speed (ensure-state! go ::push-speed 10)
^float h (Input/GetAxis "Horizontal")
^float v (Input/GetAxis "Vertical")
^Vector3 m (l/v3 h 0 v)]
(.AddForce rb (l/v3* m speed))))
(defn jump-enabled
[go col]
(when (= "Plane" (.. col gameObject name))
(a/set-state! go ::jump-enabled true)))
(defn jump-disabled
[go col]
(when (= "Plane" (.. col gameObject name))
(a/set-state! go ::jump-enabled false)))
(defn jump-fixed
[go]
(when (a/state go ::jump-enabled)
(let [rb (ensure-rb go)
^float height (ensure-state! go ::jump-height 10)
jumpkey (Input/GetKeyDown "space")]
(when jumpkey
(set! (.velocity rb) (l/v3 0 height 0))))))
@Defunctionalize
Copy link

repl.clj has local refactoring bugs. orig-cube got renamed cube, clean up references to orig-cube

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment