Created
August 10, 2012 08:16
-
-
Save rahcola/3312569 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns kineticc.behaviors | |
(:refer-clojure :exclude [delay loop]) | |
(:require [kineticc.epochal :as e]) | |
(:require [kineticc.operators :as o]) | |
(:require [clojure.data.json :as j])) | |
(defn remove-nil-vals | |
"Filter key-val pair in which val is nil." | |
[map] | |
(into {} (remove (comp nil? second) map))) | |
(defrecord Sum | |
[args] | |
e/Epochal | |
(duration [this] | |
(apply max (map e/duration args))) | |
(delay [this time] | |
(->Sum (map (fn [b] | |
(e/delay b time)) | |
args))) | |
j/Write-JSON | |
(write-json [this out escape-unicode?] | |
(j/write-json {:Sum {:args args}} | |
out | |
escape-unicode?))) | |
(derive Sum ::behavior) | |
(defmethod o/add [Sum Sum] | |
[this that] | |
(->Sum (concat (:args this) (:args that)))) | |
(defmethod o/add [Sum ::behavior] | |
[this that] | |
(->Sum (cons that (:args this)))) | |
(defmethod o/add [::behavior Sum] | |
[this that] | |
(o/add that this)) | |
(defmethod o/add [::behavior ::behavior] | |
[this that] | |
(->Sum [this that])) | |
(deftype Delay | |
[time behavior] | |
e/Epochal | |
(duration [this] (+ time (e/duration behavior))) | |
(delay [this time'] | |
(Delay. (+ time time') behavior)) | |
j/Write-JSON | |
(write-json [this out escape-unicode?] | |
(j/write-json {:Delay {:behavior behavior | |
:delay time}} | |
out | |
escape-unicode?))) | |
(derive Delay ::behavior) | |
(defrecord Behavior | |
[name duration] | |
e/Epochal | |
(duration [this] duration) | |
(delay [this time] | |
(->Delay time this)) | |
j/Write-JSON | |
(write-json [this out escape-unicode?] | |
(j/write-json {name (remove-nil-vals (dissoc this :name))} | |
out | |
escape-unicode?))) | |
(derive Behavior ::behavior) | |
(defmethod o/add [::behavior ::behavior] | |
[this that] | |
(->Sum [this that])) | |
(defn linear | |
[& {:keys [amount intersect duration]}] | |
(map->Behavior {:name :Linear | |
:duration duration | |
:intersect intersect | |
:amount amount})) | |
(defn exponential | |
[& {:keys [amount duration]}] | |
(map->Behavior {:name :Exponential | |
:amount amount | |
:duration duration})) | |
(defn oscillator | |
[& {:keys [amplitude frequency damping phase duration]}] | |
(map->Behavior {:name :Oscillator | |
:amplitude amplitude | |
:frequency frequency | |
:damping damping | |
:phase phase | |
:duration duration})) | |
(defn parabola | |
[& {:keys [extreme duration]}] | |
(map->Behavior {:name :Parabola | |
:extreme extreme | |
:duration duration})) | |
(defn quadtric-bezier | |
[& {:keys [p0 p1 p2 duration]}] | |
(map->Behavior {:name :QuadtricBezier | |
:p0 p0 | |
:p1 p1 | |
:p2 p2 | |
:duration duration})) | |
(defn cubic-bezier | |
[& {:keys [p0 p1 p2 p3 duration]}] | |
(map->Behavior {:name :CubicBezier | |
:p0 p0 | |
:p1 p1 | |
:p2 p2 | |
:p3 p3 | |
:duration duration})) | |
(defn symmetric-curve | |
[& {:keys [p0 p1 p3 duration]}] | |
(map->Behavior {:name :SymmetricCurve | |
:p0 p0 | |
:p1 p1 | |
:p3 p3 | |
:duration duration})) | |
(defn word-box | |
[& {:keys [width height duration]}] | |
(map->Behavior {:name :WordBox | |
:width width | |
:height height | |
:duration duration})) | |
(defn loop | |
[& {:keys [behavior loop-duration duration]}] | |
(map->Behavior {:name :Loop | |
:behavior behavior | |
:loopDuration loop-duration | |
:duration duration})) | |
(defn delay | |
[& {:keys [behavior delay]}] | |
(e/delay behavior delay)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(in-ns 'kineticc.behaviors) | |
(.equals (.getClass (Behavior. 1 1)) (.getClass (->Behavior 1 1))) | |
;=> false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment