<commit_before>(ns defender-cljs.canvas.scene
  "Includes abstractions over the three.js scene to store categories
  of certain displayed data"
  (:use [defender-cljs.utils :only [log]])
  (:require [defender-cljs.constants]
            [defender-cljs.canvas.camera :as camera]
            [defender-cljs.canvas.system :as system]
            [defender-cljs.actor :as a]
            [defender-cljs.constants :as c]))

(defn create-scene [& {:keys []}]
  {:scene-instance (THREE.Scene.)
   :actor-list (atom [])})

(defn add-actor!
  "Adds an actor to the scene"
  [scene actor]
  (.add (-> scene :scene-instance) (-> actor :entity))
  (swap! (-> scene :actor-list) conj actor))

(defn remove-actor!
  "Removes an actor from the scene"
  [scene actor]
  (.remove (-> scene :scene-instance) (-> actor :entity))
  (reset! (-> scene :actor-list)
          (filter #(not= actor %) @(-> scene :actor-list))))

(defn add-object! [scene obj]
  (.add (-> scene :scene-instance) obj))

(defn remove-object! [scene obj]
  (.remove (-> scene :scene-instance obj)))

;;
;; Scenes.
;;

;; Main scene which moves across the screen
(def main (create-scene))

;; Static scene for displaying HUD elements
(def hud (create-scene))


;;
;; Systems.
;;

;;system for re-positioning the actors on the other side of the map
;;based on the current position of the main camera
;;this gives the impression that the actors are on a finite map bound on the x axis

(defn modulus-wrap 
  "the map world is [-map-width/2, map-width/2]. This function wraps
  the x coordinate into that range"
  [x]
  (let [[camx _ _] (a/get-position camera/main-camera)
        extent (/ c/map-width 2)
        xmax (+ camx extent)
        xmin (- camx extent)]
    (cond
     (> x xmax)
     (+ xmin (rem x xmax))
     (< x xmin)
     (- xmax (rem x xmin))
     :else x)))

(defn generate-fixed-width-map []
  (let [[x _ _] (a/get-position camera/main-camera)
        actors
        (filter
         (fn [actor]
           (cond
            (= (:type actor) "camera") false
            :else true))
         @(:actor-list main))]
    (doseq [actor actors]
      (let [[xpos ypos zpos] (a/get-position actor)
            xpos (modulus-wrap xpos)]
        (a/set-position! actor [xpos ypos zpos])))))

(system/add-system!
 :fixed-width-map
 (reify
   system/System
   (run [_ props]
     (generate-fixed-width-map))))<commit_msg>added new and better modulus wrap<commit_after>(ns defender-cljs.canvas.scene
  "Includes abstractions over the three.js scene to store categories
  of certain displayed data"
  (:use [defender-cljs.utils :only [log]])
  (:require [defender-cljs.constants]
            [defender-cljs.canvas.camera :as camera]
            [defender-cljs.canvas.system :as system]
            [defender-cljs.actor :as a]
            [defender-cljs.constants :as c]))

(defn create-scene [& {:keys []}]
  {:scene-instance (THREE.Scene.)
   :actor-list (atom [])})

(defn add-actor!
  "Adds an actor to the scene"
  [scene actor]
  (.add (-> scene :scene-instance) (-> actor :entity))
  (swap! (-> scene :actor-list) conj actor))

(defn remove-actor!
  "Removes an actor from the scene"
  [scene actor]
  (.remove (-> scene :scene-instance) (-> actor :entity))
  (reset! (-> scene :actor-list)
          (filter #(not= actor %) @(-> scene :actor-list))))

(defn add-object! [scene obj]
  (.add (-> scene :scene-instance) obj))

(defn remove-object! [scene obj]
  (.remove (-> scene :scene-instance obj)))

;;
;; Scenes.
;;

;; Main scene which moves across the screen
(def main (create-scene))

;; Static scene for displaying HUD elements
(def hud (create-scene))


;;
;; Systems.
;;

;;system for re-positioning the actors on the other side of the map
;;based on the current position of the main camera
;;this gives the impression that the actors are on a finite map bound on the x axis

(defn modulus-wrap [entity-x camera-x]
  (let [xmin 0
        xmax map-width
        local-x (- entity-x camera-x)]
    (if (or (> local-x xmax) (< local-x xmin))
      (+ camera-x (mod local-x xmax))
      entity-x)))

(defn generate-fixed-width-map []
  (let [[cam-x _ _] (a/get-position camera/main-camera)
        actors
        (filter
         (fn [actor]
           (cond
            (= (:type actor) "camera") false
            :else true))
         @(:actor-list main))]
    (doseq [actor actors]
      (let [[entity-x ypos zpos] (a/get-position actor)
            entity-x (modulus-wrap entity-x cam-x)]
        (a/set-position! actor [xpos ypos zpos])))))

(system/add-system!
 :fixed-width-map
 (reify
   system/System
   (run [_ props]
     (generate-fixed-width-map))))
{'fraction_non_alphanumeric': 0.1517375350744658, 'fraction_numerical': 0.0008633714655730628, 'mean_word_length': 2.692430278884462, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1201308', 'n_tokens_mistral': 1578, 'n_tokens_neox': 1541, 'n_words': 476}