summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorequa <equaa@protonmail.com>2021-04-18 14:45:55 -0500
committerequa <equaa@protonmail.com>2021-04-18 14:48:02 -0500
commit78530480d35be5dbb57f1a264147bec48d6cf800 (patch)
tree197350b11e46593db326b563e0917e9536329aa1 /lib
parent0c8a8cf8d861bc4ef3162e45e8b58d2f0173d2f7 (diff)
visual changes (zooming) and optimization
also profiling
Diffstat (limited to 'lib')
-rw-r--r--lib/cells.fnl13
-rw-r--r--lib/game.fnl85
-rw-r--r--lib/main.fnl25
-rw-r--r--lib/state.fnl1
4 files changed, 95 insertions, 29 deletions
diff --git a/lib/cells.fnl b/lib/cells.fnl
index 442c7c7..dde8372 100644
--- a/lib/cells.fnl
+++ b/lib/cells.fnl
@@ -12,8 +12,8 @@
 (fn neighbors> [f threshold]
   (var x 0)
   ;; nnn this could be faster maybe
-  (each [k v (ipairs neighbors)]
-    (when (> (cell.aliveness (f v)) threshold)
+  (for [k 1 8]
+    (when (> (cell.aliveness (f (. neighbors k))) threshold)
       (set x (+ x 1))))
   x)
 
@@ -28,10 +28,11 @@
          nil))
    cell.update
    (fn [self get]
-     (if (or (= (neighbors> get 0) 3)
-             (= (neighbors> get 0) 2))
+     (let [n (neighbors> get 0)]
+       (if (or (= n 3)
+               (= n 2))
          self
-         nil))
+         nil)))
    cell.aliveness
    #1
    cell.color
@@ -58,8 +59,6 @@
    cell.color
    #(if (= $.stage 0)
         [0.7 0.4 0.3]
-        (= $.stage 5)
-        [0.5 0.4 0.3]
         (= $.stage 1)
         [0.2 0.2 0.3])
    })
diff --git a/lib/game.fnl b/lib/game.fnl
index da96d9f..01f882d 100644
--- a/lib/game.fnl
+++ b/lib/game.fnl
@@ -6,6 +6,10 @@
 (fn lerp* [a b c d x]
   (+ c (* (/ (- x a) (- b a)) (- d c))))
 
+(fn vec-sub [a b]
+  {:x (- a.x b.x)
+   :y (- a.y b.y)})
+
 (fn vec-lerp* [a b c d x]
   {:x (lerp* a.x b.x c.x d.x x.x)
     :y (lerp* a.y b.y c.y d.y x.y)})
@@ -21,6 +25,7 @@
 (fn update [self]
   (set self.ship.x (+ self.ship.x 0.02))
   (set self.ship.y (+ self.ship.y 0.005))
+  (set self.radius (lerp* 0 1 self.radius self.target-radius 0.3))
   (when (= self.tick 0)
     (for [x 0 (- self.width 1)]
       (for [y 0 (- self.height 1)]
@@ -50,29 +55,46 @@
 
 (fn draw [self]
   (local (width height) (love.graphics.getDimensions))
-  (love.graphics.scale width height)
-  (local camera-size (math.max width height))
-  (let [camera-size (math.max width height)
-        camera-box {:x width :y height}
-        camera-box {:x 1 :y 1}
+  ;; (love.graphics.scale width height)
+  (let [camera-size (math.min width height)
         radius-x (* self.radius (/ width camera-size))
         radius-y (* self.radius (/ height camera-size))
-        camera-a {:x (- self.ship.x radius-x)
-                  :y (- self.ship.y radius-y)}
-        camera-b {:x (+ self.ship.x radius-x)
-                  :y (+ self.ship.y radius-y)}
+        clipped-x (math.min radius-x self.max-radius)
+        clipped-y (math.min radius-y self.max-radius)
+        display-a {:x (* width (- 1 (/ clipped-x radius-x)) 0.5)
+                   :y (* height (- 1 (/ clipped-y radius-y)) 0.5)}
+        display-b {:x (- width display-a.x) :y (- height display-a.y)}
+        display-size (vec-sub display-b display-a)
+        camera-a {:x (- self.ship.x clipped-x)
+                  :y (- self.ship.y clipped-y)}
+        camera-b {:x (+ self.ship.x clipped-x)
+                  :y (+ self.ship.y clipped-y)}
         cell-box (vec-lerp* {:x 0 :y 0}
-                            {:x (* 2 radius-x)
-                             :y (* 2 radius-y)}
+                            {:x (* 2 clipped-x)
+                             :y (* 2 clipped-y)}
+                            ;; TODO: this is wrong
                             {:x 0 :y 0}
-                            camera-box
+                            display-size
                             {:x 1 :y 1})]
+    (love.graphics.setScissor (- display-a.x 1) (- display-a.y 1)
+                              (- display-b.x display-a.x -2)
+                              (- display-b.y display-a.y -2))
+    (love.graphics.setColor 0.2 0.2 0.2)
+    (love.graphics.setLineWidth 4)
+    (love.graphics.rectangle :line
+                             display-a.x display-a.y
+                             (- display-b.x display-a.x)
+                             (- display-b.y display-a.y))
+    (love.graphics.setScissor display-a.x display-a.y
+                              (- display-b.x display-a.x)
+                              (- display-b.y display-a.y))
+    (love.graphics.clear)
     (for [x (math.floor camera-a.x) (math.floor camera-b.x)]
       (for [y (math.floor camera-a.y) (math.floor camera-b.y)]
         (let [vec {:x (% x self.width) :y (% y self.height)}
-              render-a (vec-lerp* camera-a camera-b {:x 0 :y 0} camera-box
+              render-a (vec-lerp* camera-a camera-b display-a display-b
                                   {: x : y})
-              render-b (vec-lerp* camera-a camera-b {:x 0 :y 0} camera-box
+              render-b (vec-lerp* camera-a camera-b display-a display-b
                                   {:x (+ x 1) :y (+ y 1)})
               the (. self.grid vec.x vec.y)
               color (and the (cell.color the))]
@@ -82,28 +104,51 @@
                                      (id render-a.x)
                                      (id render-a.y)
                                      (id cell-box.x)
-                                     (id cell-box.y))))))))
+                                     (id cell-box.y))))))
+    ;; draw other stuff
+    ))
     ;; (love.graphics.setLineWidth 0.1)
     ;; (love.graphics.line 0 0 0.3 0.3)
     ;; (love.graphics.polygon :line 0.3 0.3 0.6 0.3 0.4 0.6)
     ;; (love.graphics.line 0.4 0.8 0.4 0.8)
     ;; (love.graphics.print :Gaming))
 
+(fn keypressed [self key scancode repeat]
+  (when (= key "=")
+    (set self.target-radius
+         (math.max
+           self.min-radius
+           (math.min
+             self.max-radius
+             (- self.target-radius 4)))))
+  (when (= key "-")
+    (set self.target-radius
+         (math.max
+           self.min-radius
+           (math.min
+             self.max-radius
+             (+ self.target-radius 4))))))
+
 (fn init [self]
+  (local width 64)
+  (local height 64)
   (setmetatable
-    {:width 64
-     :height 64
+    {:width width
+     :height height
      :ship {:x 31 :y 31}
      :radius 32
+     :target-radius 32
+     :max-radius (/ (math.min height width) 2)
+     :min-radius 16
      :tick 0
      :rate 6
-     :grid (new-grid 64 64 #(if (= (math.random 6) 1)
+     :grid (new-grid width height #(if (= (math.random 6) 1)
                                 (if (< $1 52)
                                     (cell.init cells.life)
                                     (cell.init cells.brain))
                                 nil))
-     :grid-alt (new-grid 64 64 #nil)
+     :grid-alt (new-grid width height #nil)
      }
     self))
 
-{state.draw draw state.init init state.update update}
+{state.draw draw state.init init state.update update state.keypressed keypressed}
diff --git a/lib/main.fnl b/lib/main.fnl
index 4a9d26e..03383ad 100644
--- a/lib/main.fnl
+++ b/lib/main.fnl
@@ -2,6 +2,8 @@
 (local proto (require :lib.proto))
 (local state (require :lib.state))
 (local game (require :lib.game))
+(local profi (require :vendor.ProFi))
+(local profi? false)
 
 ;; i am thinking we could actually do a really hacky thing (modules add themselves
 ;; to this list) with this later but
@@ -16,6 +18,10 @@
 ;; oh thats why it doesnt work lmao
 
 (fn love.load []
+  (when profi?
+    (profi:start))
+  (set love.frame 0)
+  (love.keyboard.setKeyRepeat true)
   (global the-state (state.init game))
   (global messages {})
   (print "a"))
@@ -42,6 +48,13 @@
 
 ;; TODO: we need a better way to display errors at runtime for updates too
 (fn love.update []
+  (when profi?
+    (profi:startHooks)
+    (set love.frame (+ love.frame 1))
+    (when (= (% love.frame 100) 0)
+      (profi:stop)
+      (profi:writeReport)
+      (os.exit)))
   ;; TODO: make state changes actually possible
   (match (pcall #(state.update the-state))
     (true x) nil
@@ -49,10 +62,18 @@
                 (print (.. "update: \n" x))
                 (table.insert messages
                               {:ticks 1
-                               :msg (.. "update: \n" x)}))))
+                               :msg (.. "update: \n" x)})))
+  (when profi?
+    (profi:stopHooks)))
 
 (fn love.keypressed [key scancode repeat]
-  ;; (print key scancode repeat)
+  (match (pcall #(state.keypressed the-state key scancode repeat))
+    (true x) nil
+    (false x) (do
+                (print (.. "keypressed: \n" x))
+                (table.insert messages
+                              {:ticks 5
+                               :msg (.. "keypressed: \n" x)})))
   (when (= key "r")
     (each [k v (lume.ripairs messages)]
       (when (= v.type :reload-error)
diff --git a/lib/state.fnl b/lib/state.fnl
index d98b94e..41dc312 100644
--- a/lib/state.fnl
+++ b/lib/state.fnl
@@ -16,4 +16,5 @@
  ;; all of the next functions are just. regular love functions, exactly the same
  ;; i hope
  :draw (proto.meta-method-opt :state.draw)
+ :keypressed (proto.meta-method-opt :state.keypressed)
  }