summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorequa <equaa@protonmail.com>2021-04-20 18:20:05 -0500
committerequa <equaa@protonmail.com>2021-04-20 18:20:05 -0500
commitc9b60c569f2ad015f043667c44ff9f43d50ec2e5 (patch)
treef4a3bb3f363a5a3c9200bc8ee5a9eab408085a1c /lib
parent00af64d4e503dd82abf4657ec6fd44b0a418adb2 (diff)
death screen
Diffstat (limited to 'lib')
-rw-r--r--lib/death.fnl35
-rw-r--r--lib/font.fnl2
-rw-r--r--lib/game.fnl60
-rw-r--r--lib/main.fnl10
-rw-r--r--lib/player.fnl6
5 files changed, 82 insertions, 31 deletions
diff --git a/lib/death.fnl b/lib/death.fnl
new file mode 100644
index 0000000..8456827
--- /dev/null
+++ b/lib/death.fnl
@@ -0,0 +1,35 @@
+(local state (require :lib.state))
+(local font (require :lib.font))
+
+(fn draw [self]
+  (local (width height) (love.graphics.getDimensions))
+  (state.draw self.present-state)
+  ;; TODO: translation
+  (love.graphics.reset)
+  (love.graphics.setColor 0.8 0.8 0.8 (/ (math.max 0 (- self.age 30)) 10))
+  (love.graphics.setFont font.big)
+  (love.graphics.printf "moli a" (- (/ width 2) 500) (- (/ height 2) 64) 1000 :center)
+  (love.graphics.setFont font.small)
+  (love.graphics.printf "nena Z li open e musi sin" (- (/ width 2) 500) (+ (/ height 2)) 1000 :center))
+
+(fn update [self]
+  ;; TODO: actually update in case it returns something else
+  (state.update self.present-state)
+  (set self.age (math.min 40 (+ self.age 1)))
+  (if self.transition
+      self.future-state
+      nil))
+
+(fn keypressed [self key scancode repeat]
+  (when (and (not repeat) (= key :z))
+    (set self.transition true))
+  )
+
+(fn init [self present-state future-state]
+  (setmetatable
+    {: present-state
+     : future-state
+     :age 0}
+    self))
+
+{state.draw draw state.init init state.update update state.keypressed keypressed}
diff --git a/lib/font.fnl b/lib/font.fnl
new file mode 100644
index 0000000..73a18b8
--- /dev/null
+++ b/lib/font.fnl
@@ -0,0 +1,2 @@
+{:small (love.graphics.newFont "font/Montserrat-SemiBold.ttf" 12)
+ :big (love.graphics.newFont "font/Montserrat-SemiBold.ttf" 48)}
diff --git a/lib/game.fnl b/lib/game.fnl
index 3f99550..229f433 100644
--- a/lib/game.fnl
+++ b/lib/game.fnl
@@ -4,6 +4,7 @@
 (local vec (require :lib.vec))
 (local entity (require :lib.entity))
 (local player (require :lib.player))
+(local death (require :lib.death))
 (local fv (require :fennel.view))
 
 (fn lerp* [a b c d x]
@@ -68,7 +69,12 @@
       (tset e entity.duration (- (entity.duration e) 1))
       (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)
+      ;; TODO: new game
+      (state.init death self ((. (getmetatable self) :inner-init)
+                              (getmetatable self) 1 self))
+      nil))
 
 (fn id [x] x)
 
@@ -165,29 +171,33 @@
              self.max-radius
              (+ self.target-radius 4))))))
 
+(fn inner-init [self level past]
+  (let [past (or past {})
+        width 64
+        height 64]
+    (setmetatable
+      {:width width
+       :height height
+       :radius (or past.radius 32)
+       :target-radius (or past.radius 32)
+       :max-radius (/ (math.min height width) 2)
+       :min-radius 16
+       :level level
+       :tick 0
+       :rate 6
+       :entities {: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)
+                                             (< $1 10)
+                                             (cell.init cells.brain)
+                                             nil)
+                                         nil))
+       :grid-alt (new-grid width height #nil)
+       }
+      self)))
+
 (fn init [self]
-  (local width 64)
-  (local height 64)
-  (setmetatable
-    {: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
-     :entities {:player (entity.init player {:x 32 :y 32})}
-     :grid (new-grid width height #(if (= (math.random 6) 1)
-                                (if (> $1 44)
-                                    (cell.init cells.life)
-                                    (< $1 10)
-                                    (cell.init cells.brain)
-                                    nil)
-                                nil))
-     :grid-alt (new-grid width height #nil)
-     }
-    self))
+  (self.inner-init self 1))
 
-{state.draw draw state.init init state.update update state.keypressed keypressed}
+{: inner-init state.draw draw state.init init state.update update state.keypressed keypressed}
diff --git a/lib/main.fnl b/lib/main.fnl
index 71319b8..9af1bb8 100644
--- a/lib/main.fnl
+++ b/lib/main.fnl
@@ -2,6 +2,7 @@
 (local proto (require :lib.proto))
 (local state (require :lib.state))
 (local game (require :lib.game))
+(local font (require :lib.font))
 (local profi (require :vendor.ProFi))
 (local profi? false)
 
@@ -14,6 +15,7 @@
    :lib.bullet
    :lib.cells
    :lib.game
+   :lib.death
    :lib.main])
 
 ;; the
@@ -25,8 +27,7 @@
     (set love.frame 0))
   (love.keyboard.setKeyRepeat true)
   (global the-state (state.init game))
-  (global messages (or messages {}))
-  (print "a"))
+  (global messages (or messages {})))
 
 (fn love.draw []
   (match (pcall #(state.draw the-state))
@@ -36,7 +37,9 @@
                 (print (.. "draw \n" x))
                 (love.graphics.print (.. "draw: \n" x))))
   (love.graphics.reset)
+  ;; (love.graphics.setFont font.small)
   (love.graphics.print (love.timer.getFPS))
+  ;; (love.graphics.print :soko font.big 100 100)
   (when true ;; debug stuff
     (love.graphics.print (table.concat
                            (lume.map messages #$.msg)
@@ -58,7 +61,8 @@
       (os.exit)))
   ;; TODO: make state changes actually possible
   (match (pcall #(state.update the-state))
-    (true x) nil
+    (true next) (global the-state next)
+    (true nil) nil
     (false x) (do
                 (print (.. "update: \n" x))
                 (table.insert messages
diff --git a/lib/player.fnl b/lib/player.fnl
index 8e31a18..5fca05a 100644
--- a/lib/player.fnl
+++ b/lib/player.fnl
@@ -8,7 +8,7 @@
   (setmetatable {entity.position pos
                  entity.velocity {:x 0 :y 0}
                  :target-spin 0
-                 :shot-age 0
+                 :shot-age 20
                  :direction 0}
                 self))
 
@@ -37,8 +37,8 @@
       (entity.init
         bullet
         (vec.clone (entity.position self))
-        {:x (+ (* (math.cos self.direction) 0.6))
-         :y (+ (* (math.sin self.direction) 0.6))}))))
+        {:x (+ v.x (* (math.cos self.direction) 0.6))
+         :y (+ v.y (* (math.sin self.direction) 0.6))}))))
 
 (fn draw [self game id]
   (love.graphics.setColor 1 1 1)