summary refs log tree commit diff
path: root/lib/game.fnl
diff options
context:
space:
mode:
authorequa <equaa@protonmail.com>2021-04-20 22:13:13 -0500
committerequa <equaa@protonmail.com>2021-04-20 22:13:13 -0500
commit5c4b1aba561c206744b3354478069677b3947e67 (patch)
tree70441a4c663e37ba9f67071cc3e4085075d69884 /lib/game.fnl
parentc9b60c569f2ad015f043667c44ff9f43d50ec2e5 (diff)
death -> other paused/non-game screens
Diffstat (limited to 'lib/game.fnl')
-rw-r--r--lib/game.fnl47
1 files changed, 40 insertions, 7 deletions
diff --git a/lib/game.fnl b/lib/game.fnl
index 229f433..a0218b3 100644
--- a/lib/game.fnl
+++ b/lib/game.fnl
@@ -4,7 +4,7 @@
 (local vec (require :lib.vec))
 (local entity (require :lib.entity))
 (local player (require :lib.player))
-(local death (require :lib.death))
+(local transition (require :lib.transition))
 (local fv (require :fennel.view))
 
 (fn lerp* [a b c d x]
@@ -21,8 +21,11 @@
 (fn update [self]
   ;; (set ship-pos.x (+ self.ship.x 0.02))
   ;; (set ship-pos.y (+ self.ship.y 0.005))
+  (var grid-alive? true)
   (set self.radius (lerp* 0 1 self.radius self.target-radius 0.3))
+
   (when (= self.tick 0)
+    (set grid-alive? false)
     (for [x 0 (- self.width 1)]
       (for [y 0 (- self.height 1)]
         (fn get [v]
@@ -31,8 +34,10 @@
              (% (+ v.y y) self.height)))
         (if (. self.grid x y)
             ;; check if alive
-            (tset self.grid-alt x y (cell.update (. self.grid x y)
+            (do
+              (tset self.grid-alt x y (cell.update (. self.grid x y)
                                                  get))
+              (set grid-alive? true))
             ;; check for neighbors and then use one at random
             (do
               (var neighbors [])
@@ -70,10 +75,29 @@
       (if (= (entity.duration e) 0)
           (tset self.entities id nil))))
   ;; TODO: we should do this for only the first death frame
-  (if (not self.entities.player)
+  (if self.pause
+      (do
+        (set self.pause nil)
+        (state.init transition
+                    :pause
+                    self
+                    self))
+      (not self.entities.player)
       ;; TODO: new game
-      (state.init death self ((. (getmetatable self) :inner-init)
-                              (getmetatable self) 1 self))
+      (state.init transition
+                  :death
+                  self
+                  ((. (getmetatable self) :inner-init)
+                   (getmetatable self) 1 self)
+                  self.level)
+      (not grid-alive?)
+      (state.init transition
+                  :win
+                  self
+                  ((. (getmetatable self) :inner-init)
+                   (getmetatable self) (+ self.level 1) self)
+                  self.level)
+      true
       nil))
 
 (fn id [x] x)
@@ -156,6 +180,8 @@
     ;; (love.graphics.print :Gaming))
 
 (fn keypressed [self key scancode repeat]
+  (when (= key "escape")
+    (set self.pause true))
   (when (= key "=")
     (set self.target-radius
          (math.max
@@ -185,7 +211,10 @@
        :level level
        :tick 0
        :rate 6
-       :entities {:player (entity.init player {:x 31.5 :y 31.5})}
+       :ship-pos {:x 31.5 :y 31.5}
+       :entities (if (= level -1)
+                     {}
+                     {:player (entity.init player {:x 31.5 :y 31.5})})
        :grid (new-grid width height #(if (= (math.random 6) 1)
                                          (if (> $1 44)
                                              (cell.init cells.life)
@@ -197,7 +226,11 @@
        }
       self)))
 
+;; what the hell
 (fn init [self]
-  (self.inner-init self 1))
+  (state.init transition
+              :menu
+              (self.inner-init self -1)
+              (self.inner-init self 1)))
 
 {: inner-init state.draw draw state.init init state.update update state.keypressed keypressed}