Skip to content

Instantly share code, notes, and snippets.

@nasser
Last active August 29, 2015 14:14
Show Gist options
  • Save nasser/885c75f57fde018135be to your computer and use it in GitHub Desktop.
Save nasser/885c75f57fde018135be to your computer and use it in GitHub Desktop.
(ns game.ghost
(:use arcadia.core
nasser.linear)
(:require [nasser.proto :as proto]
[nasser.component :refer [component]])
(:import [UnityEngine Transform Component Time Vector3 Debug Color]))
(defn fly-towards! [^Transform tf ^Vector3 p ^double speed ^double turn-rate]
(let [ds (* speed Time/deltaTime)
target-dir (Vector3/Normalize (v- p (.. tf position)))]
;; move
(set! (.. tf position)
(v+ (.. tf position)
(v* ds
(.. tf forward))))
;; follow
(set! (.. tf forward)
(Vector3/RotateTowards (.. tf forward)
target-dir
turn-rate
0.0))))
(defn new-target
([] (new-target 0.5))
([^double difficulty]
(if (< (rand) difficulty)
(.. (object-named "Player") transform position)
(v* 5
(UnityEngine.Random/insideUnitSphere)))))
(defn ghost-update [^Component this]
(let [trans (.transform this)
pos (.position trans)
targ (.target this)]
(if (< (Vector3/Distance pos targ) 2)
(set! (.target this) (new-target 0.1)))
(fly-towards! trans targ 1 0.01)))
(defn ghost-gizmos [^Component this]
(let [p (.. this gameObject transform position)
f (.. this gameObject transform forward)
target (.. this target)]
(proto/color Color/red)
(proto/arrow p f)
(proto/junction p 0.25)
(proto/color Color/yellow)
(proto/junction target 0.1)
(proto/color Color/white)
(proto/dotted-line p target)))
(component ghost [^Vector3 target])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment