emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/pacmacs 05e30ca856 076/472: Implement support for frame du


From: ELPA Syncer
Subject: [nongnu] elpa/pacmacs 05e30ca856 076/472: Implement support for frame duration. Close #23
Date: Thu, 6 Jan 2022 21:59:14 -0500 (EST)

branch: elpa/pacmacs
commit 05e30ca856949f77d8e521549428e644d29a4dc5
Author: rexim <reximkut@gmail.com>
Commit: rexim <reximkut@gmail.com>

    Implement support for frame duration. Close #23
---
 pacman-anim.el           | 38 ++++++++++++++++++++++++++------------
 pacman.el                | 37 ++++++++++++++++++++-----------------
 test/pacman-anim-test.el | 35 +++++++++++++++++++++++------------
 3 files changed, 69 insertions(+), 41 deletions(-)

diff --git a/pacman-anim.el b/pacman-anim.el
index de2f09a726..8cb26396e0 100644
--- a/pacman-anim.el
+++ b/pacman-anim.el
@@ -40,8 +40,13 @@
 (defun pacman-make-anim (frames sprite-sheet)
   (list :frames frames
         :current-frame 0
+        :duration-counter 0
         :sprite-sheet sprite-sheet))
 
+(defun pacman-make-frame (frame duration)
+  (list :frame frame
+        :duration duration))
+
 (defun pacman-load-anim (animation-name)
   (let* ((aseprite-json-file (format "sprites/%s.json" animation-name))
          (sprite-sheet-file (format "sprites/%s.xpm" animation-name))
@@ -65,10 +70,12 @@
     (< order1 order2)))
 
 (defun pacman-convert-aseprite-frame (aseprite-frame)
-  (let* ((frame (cdr (assoc 'frame (cdr aseprite-frame)))))
-    (mapcar (lambda (n)
-              (cdr (assoc n frame)))
-            '(x y w h))))
+  (let* ((frame (cdr (assoc 'frame (cdr aseprite-frame))))
+         (duration (cdr (assoc 'duration (cdr aseprite-frame)))))
+    (pacman-make-frame (mapcar (lambda (n)
+                                 (cdr (assoc n frame)))
+                               '(x y w h))
+                       duration)))
 
 (defun pacman-anim-get-frame (anim)
   (plist-bind ((frames :frames)
@@ -76,16 +83,23 @@
       anim
     (nth current-frame frames)))
 
-(defun pacman-anim-next-frame (anim)
+(defun pacman-anim-next-frame (anim time)
   (plist-bind ((frames :frames)
-               (current-frame :current-frame))
+               (current-frame :current-frame)
+               (duration-counter :duration-counter))
       anim
-    (let ((new-current-frame (mod (+ current-frame 1)
-                                  (length frames))))
-      (plist-put anim :current-frame new-current-frame))))
-
-(defun pacman-anim-object-next-frame (anim-object)
-  (plist-map anim-object :animation #'pacman-anim-next-frame))
+    (let ((duration (plist-get (pacman-anim-get-frame anim) :duration)))
+      (if (<= duration (+ duration-counter time))
+          (let ((new-current-frame (mod (+ current-frame 1)
+                                        (length frames))))
+            (plist-put anim :duration-counter 0)
+            (plist-put anim :current-frame new-current-frame))
+        (plist-put anim :duration-counter (+ duration-counter time))))))
+
+(defun pacman-anim-object-next-frame (anim-object time)
+  (plist-map anim-object :animation
+             #'(lambda (anim)
+                 (pacman-anim-next-frame anim time))))
 
 (provide 'pacman-anim)
 
diff --git a/pacman.el b/pacman.el
index 23507e240b..701b7f833e 100644
--- a/pacman.el
+++ b/pacman.el
@@ -40,6 +40,7 @@
 (require 'pacman-utils)
 
 (defconst pacman-buffer-name "*Pacman*")
+(defconst pacman-tick-duration-ms 100)
 
 (defvar pacman-timer nil)
 (defvar pacman-counter 0)
@@ -52,19 +53,21 @@
       (list :row 0
             :column 0
             :direction 'right
-            :animation (pacman-load-anim "Pacman-Chomping-Right")))
-
-(defvar pacman-direction-table
-  (list 'left  (cons -1 0)
-        'right (cons 1 0)
-        'up    (cons 0 -1)
-        'down  (cons 0 1)))
-
-(defvar pacman-direction-animation-table
-  (list 'left  (pacman-load-anim "Pacman-Chomping-Left")
-        'right (pacman-load-anim "Pacman-Chomping-Right")
-        'up    (pacman-load-anim "Pacman-Chomping-Up")
-        'down  (pacman-load-anim "Pacman-Chomping-Down")))
+            :animation (pacman-load-anim "Red-Ghost-Right")))
+
+(defvar pacman-direction-table nil)
+(setq pacman-direction-table
+      (list 'left  (cons -1 0)
+            'right (cons 1 0)
+            'up    (cons 0 -1)
+            'down  (cons 0 1)))
+
+(defvar pacman-direction-animation-table nil)
+(setq pacman-direction-animation-table
+      (list 'left  (pacman-load-anim "Red-Ghost-Left")
+            'right (pacman-load-anim "Red-Ghost-Right")
+            'up    (pacman-load-anim "Red-Ghost-Up")
+            'down  (pacman-load-anim "Red-Ghost-Down")))
 
 (defvar pacman-empty-cell nil)
 (setq pacman-empty-cell
@@ -122,7 +125,7 @@
   (switch-to-buffer-other-window pacman-buffer-name)
   (pacman-mode)
   (unless pacman-timer
-    (setq pacman-timer (run-at-time nil 0.1 'pacman-tick))))
+    (setq pacman-timer (run-at-time nil (* pacman-tick-duration-ms 0.001) 
'pacman-tick))))
 
 (defun pacman-destroy ()
   (when pacman-timer
@@ -175,9 +178,9 @@
   (interactive)
   (with-current-buffer pacman-buffer-name
     (let ((inhibit-read-only t))
-      (pacman-anim-object-next-frame pacman-player-state)
+      (pacman-anim-object-next-frame pacman-player-state 
pacman-tick-duration-ms)
       (dolist (pill pacman-pills)
-        (pacman-anim-object-next-frame pill))
+        (pacman-anim-object-next-frame pill pacman-tick-duration-ms))
       
       (pacman-step-object pacman-player-state)
       (let* ((direction (plist-get pacman-player-state :direction))
@@ -205,7 +208,7 @@
 (defun pacman-render-object (anim-object)
   (let* ((anim (plist-get anim-object :animation))
          (sprite-sheet (plist-get anim :sprite-sheet))
-         (current-frame (pacman-anim-get-frame anim)))
+         (current-frame (plist-get (pacman-anim-get-frame anim) :frame)))
     (pacman-insert-image sprite-sheet current-frame)))
 
 (defun pacman-clear-board ()
diff --git a/test/pacman-anim-test.el b/test/pacman-anim-test.el
index 09ea407a75..cf157506a5 100644
--- a/test/pacman-anim-test.el
+++ b/test/pacman-anim-test.el
@@ -1,6 +1,7 @@
 (ert-deftest pacman-make-anim-test ()
   (should (equal (list :frames (list 1 2 3 4 5)
                        :current-frame 0
+                       :duration-counter 0
                        :sprite-sheet 42)
                  (pacman-make-anim (number-sequence 1 5) 42))))
 
@@ -10,19 +11,22 @@
     (should (equal 3 (pacman-anim-get-frame anim)))))
 
 (ert-deftest pacman-anim-next-frame-test ()
-  (let ((anim (list :frames (number-sequence 1 4)
-                    :current-frame 2)))
-    (pacman-anim-next-frame anim)
+  (let ((anim (list :frames (mapcar (lambda (x)
+                                      (pacman-make-frame (+ 41 x) 100))
+                                    (number-sequence 1 4)) 
+                    :current-frame 2
+                    :duration-counter 0)))
+    (pacman-anim-next-frame anim 100)
     (should (equal 3 (plist-get anim :current-frame)))
 
-    (pacman-anim-next-frame anim)
+    (pacman-anim-next-frame anim 100)
     (should (equal 0 (plist-get anim :current-frame)))))
 
 (ert-deftest pacman-anim-object-next-frame-test ()
   (with-mock
    (stub pacman-anim-next-frame => 42)
    (let ((anim-object '(:animation 41)))
-     (pacman-anim-object-next-frame anim-object)
+     (pacman-anim-object-next-frame anim-object 100)
      (should (equal '(:animation 42)
                     anim-object)))))
 
@@ -33,8 +37,10 @@
                            (x . 1)
                            (y . 2)
                            (h . 3)
-                           (w . 4))))
-        (expected-frame (list 1 2 4 3)))
+                           (w . 4))
+                          (duration . 100)))
+        (expected-frame (list :frame (list 1 2 4 3)
+                              :duration 100)))
     (should (equal expected-frame
                    (pacman-convert-aseprite-frame aseprite-frame)))))
 
@@ -68,14 +74,19 @@
 
 (ert-deftest pacman-load-anim-test ()
   (let* ((input-aseprite-format '((frames
-                                   (frame-3\.ase (frame (h . 3) (w . 3) (y . 
3) (x . 3)))
-                                   (frame-2\.ase (frame (h . 2) (w . 2) (y . 
2) (x . 2)))
-                                   (frame-1\.ase (frame (h . 1) (w . 1) (y . 
1) (x . 1)))
-                                   (frame-0\.ase (frame (h . 0) (w . 0) (y . 
0) (x . 0))))))
+                                   (frame-3\.ase (frame (h . 3) (w . 3) (y . 
3) (x . 3))
+                                                 (duration . 400))
+                                   (frame-2\.ase (frame (h . 2) (w . 2) (y . 
2) (x . 2))
+                                                 (duration . 300))
+                                   (frame-1\.ase (frame (h . 1) (w . 1) (y . 
1) (x . 1))
+                                                 (duration . 200))
+                                   (frame-0\.ase (frame (h . 0) (w . 0) (y . 
0) (x . 0))
+                                                 (duration . 100)))))
          (input-sprite-sheet 42)
          (expected-output (pacman-make-anim
                            (mapcar #'(lambda (x)
-                                       (make-list 4 x))
+                                       (pacman-make-frame (make-list 4 x)
+                                                          (* (1+ x) 100)))
                                    (number-sequence 0 3))
                            input-sprite-sheet)))
     (with-mock



reply via email to

[Prev in Thread] Current Thread [Next in Thread]