]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
lua: ui object, queued dialog ui
authorClyne Sullivan <clyne@bitgloo.com>
Sun, 20 Nov 2022 02:36:09 +0000 (21:36 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Sun, 20 Nov 2022 02:36:09 +0000 (21:36 -0500)
Scripts/init.lua
Scripts/ui.lua [new file with mode: 0644]

index 1ae2efcc55c94a50526fb5ff269db9b30f51745d..c3b6d3b2368fcb6cc71890ceb7e605996394407e 100644 (file)
@@ -1,6 +1,9 @@
 game.loadFont("default", "Assets/FreePixel.ttf", 16)
 game.loadFont("dialog", "Assets/FreePixel.ttf", 16)
 
+-- Include UI functionality
+dofile("Scripts/ui.lua")
+
 player = {
     Player = 0,
     EventListeners = {
@@ -26,14 +29,14 @@ player = {
         JumpKeyReleased = function(self)
         end,
         MousePressed = function(self, mx, my)
+            ui.dialog:update(mx, my)
+
             mp = game.uiToWorldCoord(mx, my)
-            if math.abs(mp.x - self.Position.x) < 1 and math.abs(mp.y - self.Position.y) < 1 then
-                game.dialog(30, 50, 400, 100)
-                game.puts("dialog", 36, 52, "What do you think you're doing?")
-            else
-                if mx > 30 and mx < 430 and my > 50 and my < 150 then
-                    game.dialogClear()
-                end
+            if math.abs(mp.x - self.Position.x) < 1 and
+               math.abs(mp.y - self.Position.y) < 1 then
+                ui.dialog:queue("Hi there! My name is Bob.")
+                ui.dialog:queue("Maybe.")
+                ui.dialog:queue("I'm really not sure, actually...")
             end
         end
     },
diff --git a/Scripts/ui.lua b/Scripts/ui.lua
new file mode 100644 (file)
index 0000000..f6c65f2
--- /dev/null
@@ -0,0 +1,33 @@
+ui = {
+    dialog = {
+        buf = {},
+        idx = 0,
+        rdx = 0,
+
+        queue = function(self, text)
+            self.idx = self.idx + 1
+            self.buf[self.idx] = text
+
+            if self.idx == 1 then
+                game.dialog(30, 50, 400, 100)
+                game.puts("dialog", 36, 52, text)
+            end
+        end,
+
+        update = function(self, mx, my)
+            if self.idx > 0 then
+                if mx > 30 and mx < 430 and my > 50 and my < 150 then
+                    self.rdx = self.rdx + 1
+                    if self.rdx == self.idx then
+                        self.idx = 0
+                        self.rdx = 0
+                        game.dialogClear()
+                    else
+                        game.puts("dialog", 36, 52, self.buf[self.rdx + 1])
+                    end
+                end
+            end
+        end
+    }
+}
+