Browse Source

Convert everything over to OOPey style

Getty Ritter 1 year ago
parent
commit
595b9cd6b2
4 changed files with 64 additions and 81 deletions
  1. 6 7
      game.fnl
  2. 10 10
      mode/end.fnl
  3. 36 53
      mode/main.fnl
  4. 12 11
      mode/menu.fnl

+ 6 - 7
game.fnl

@@ -1,21 +1,20 @@
 (local fennel (require "lib.fennel"))
 (local repl (require "lib.stdio"))
 
-(var mode (require "mode.menu"))
+(var mode (: (require "mode.menu") :init))
 
 (fn set-mode [mn ...]
-  (set mode (require mn))
-  (when mode.init
-    (mode.init ...)))
+  (let [m (require mn)]
+    (set mode (: m :init ...))))
 
 (fn love.load []
   (repl.start))
 
 (fn love.draw []
-  (mode.draw))
+  (: mode :draw))
 
 (fn love.update [dt]
-  (mode.update dt set-mode))
+  (: mode :update dt set-mode))
 
 (fn love.keypressed [key]
   ;; LIVE RELOADING
@@ -36,4 +35,4 @@
   (when (= "escape" key)
     (love.event.quit))
 
-  (mode.keypressed key set-mode))
+  (: mode :keypressed key set-mode))

+ 10 - 10
mode/end.fnl

@@ -1,18 +1,18 @@
 (local (w h) (love.window.getMode))
 
-(var message "Unknown")
+(local End {})
+(set End.__index End)
 
-{
-  :name "end"
+(fn End.init [cls m]
+  (setmetatable {:message (.. "Player " m " wins")} cls))
 
-  :init (fn init [m] (set message (.. "Player " m " wins")))
+(fn End.update [self dt set-mode])
 
-  :update (fn update [dt set-mode])
-
-  :keypressed (fn keypressed [key set-mode]
+(fn End.keypressed [self key set-mode]
     (when (= "y" key) (set-mode "mode.main"))
     (when (= "n" key) (love.event.quit)))
 
-  :draw (fn draw [m]
-    (love.graphics.printf (.. message "\n\nPlay again? (y/n)") 0 (/ h 3) w "center"))
-}
+(fn End.draw [self m]
+  (love.graphics.printf (.. self.message "\n\nPlay again? (y/n)") 0 (/ h 3) w "center"))
+
+End

+ 36 - 53
mode/main.fnl

@@ -1,21 +1,11 @@
-; (local (speed ball-speed) (values 1 200))
 (local (w h) (love.window.getMode))
-; (local _state {:x 100 :y 100 :dx 2 :dy 1 :left (- (/ h 2) 50) :right (- (/ h 2) 50)})
-; (global state {:x 100 :y 100 :dx 2 :dy 1 :left (- (/ h 2) 50) :right (- (/ h 2) 50)})
-; (var pause false)
 
 (local keys {:a [:left -1] :z [:left 1] :up [:right -1] :down [:right 1]})
 
-;; (fn on-paddle? []
-;;   (or (and (< state.x 20)
-;;            (< state.left state.y) (< state.y (+ state.left 100)))
-;;       (and (< (- w 20) state.x)
-;;            (< state.right state.y) (< state.y (+ state.right 100)))))
-
 (global Pong {})
 (set Pong.__index Pong)
 
-(fn Pong.new [cls]
+(fn Pong.init [cls]
   (let [t {:x 100 :y 100
            :dx 2 :dy 1
            :left (- (/ h 2) 50) :right (- (/ h 2) 50)
@@ -29,45 +19,38 @@
       (and (< (- w 20) self.x)
            (< self.right self.y) (< self.y (+ self.right 100)))))
 
-(global pong nil)
-
-{
- :name "pong"
-
- :init (fn init []
-         (global pong (: Pong :new)))
-
- :update (fn update [dt set-mode]
-           (when (not pong.pause)
-             (set pong.x (+ pong.x (* pong.dx dt pong.ball-speed)))
-             (set pong.y (+ pong.y (* pong.dy dt pong.ball-speed)))
-             (each [key action (pairs keys)]
-               (let [[player dir] action]
-                 (when (love.keyboard.isDown key)
-                   (tset pong player (+ (. pong player) (* dir pong.speed))))))
-
-             (when (or
-                     (< pong.y 0)
-                     (> pong.y h))
-               (set pong.dy (- 0 pong.dy)))
-
-             (when (: pong :on-paddle?)
-               (set pong.dx (- 0 pong.dx)))
-
-             (when (< pong.x 0)
-               (set-mode "mode.end" "2"))
-             (when (> pong.x w)
-               (set-mode "mode.end" "1"))))
-
- :keypressed (fn keypressed [key set-mode]
-               (when (= "p" key)
-                 (set pong.pause (not pong.pause))))
-
- :draw (fn draw [m]
-         (when (not pong.pause)
-           (love.graphics.rectangle "fill" 10 pong.left 10 100)
-           (love.graphics.rectangle "fill" (- w 20) pong.right 10 100)
-           (love.graphics.circle "fill" pong.x pong.y 10))
-         (when pong.pause
-           (love.graphics.printf "Press 'p' to unpause" 0 (/ h 3) w "center")))
- }
+(fn Pong.update [self dt set-mode]
+  (when (not self.pause)
+    (set self.x (+ self.x (* self.dx dt self.ball-speed)))
+    (set self.y (+ self.y (* self.dy dt self.ball-speed)))
+    (each [key action (pairs keys)]
+      (let [[player dir] action]
+        (when (love.keyboard.isDown key)
+          (tset self player (+ (. self player) (* dir self.speed))))))
+
+    (when (or
+           (< self.y 0)
+           (> self.y h))
+      (set self.dy (- 0 self.dy)))
+
+    (when (: self :on-paddle?)
+      (set self.dx (- 0 self.dx)))
+
+    (when (< self.x 0)
+      (set-mode "mode.end" "2"))
+    (when (> self.x w)
+      (set-mode "mode.end" "1"))))
+
+(fn Pong.keypressed [self key set-mode]
+  (when (= "p" key)
+    (set self.pause (not self.pause))))
+
+(fn Pong.draw [self m]
+  (when (not self.pause)
+    (love.graphics.rectangle "fill" 10 self.left 10 100)
+    (love.graphics.rectangle "fill" (- w 20) self.right 10 100)
+    (love.graphics.circle "fill" self.x self.y 10))
+  (when self.pause
+    (love.graphics.printf "Press 'p' to unpause" 0 (/ h 3) w "center")))
+
+Pong

+ 12 - 11
mode/menu.fnl

@@ -1,13 +1,14 @@
 (local (w h) (love.window.getMode))
 
-{
- :name "menu"
-
- :update (fn update [dt set-mode])
-
- :keypressed (fn keypressed [key set-mode]
-   (when (= "return" key) (set-mode "mode.main")))
-
- :draw (fn draw [message]
-   (love.graphics.printf "Taking The L" 0 (/ h 3) w "center"))
-}
+(local Menu {})
+(set Menu.__index Menu)
+
+(fn Menu.init [cls]
+  (setmetatable {:name "menu"} Menu))
+(fn Menu.update [self dt set-mode])
+(fn Menu.keypressed [self key set-mode]
+  (when (= "return" key) (set-mode "mode.main")))
+(fn Menu.draw [self message]
+  (love.graphics.printf "Taking The L" 0 (/ h 3) w "center"))
+
+Menu