]> gitweb.factorcode.org Git - factor.git/commitdiff
raylib.demo: allow diagonal movement
authorDoug Coleman <doug.coleman@gmail.com>
Sat, 29 Jul 2023 21:04:46 +0000 (16:04 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Sat, 29 Jul 2023 21:04:46 +0000 (16:04 -0500)
extra/raylib/demo/demo.factor

index a045f5226e42e7b04f284599e15b117ca57ca1c9..6814b70a96a8abafbc87177db24f409f70689b7e 100644 (file)
@@ -1,6 +1,7 @@
 ! Copyright (C) 2019 Jack Lucas
 ! See https://factorcode.org/license.txt for BSD license.
-USING: accessors combinators kernel math namespaces raylib ;
+USING: accessors arrays combinators kernel math namespaces
+raylib ;
 IN: raylib.demo
 
 : say-hello ( -- )
@@ -25,15 +26,18 @@ SYMBOL: player
     get-screen-height 2 /
     Vector2 boa player set ;
 
-! Make this cleaner
+: check-axis-movement ( key-negative key-positive -- unit/f )
+    [ is-key-down ] bi@ 2array {
+        { { t f } [ -1.0 ] }
+        { { f t } [  1.0 ] }
+        [ drop f ]
+    } case ;
+
 : change-player-position ( -- )
-    player get {
-        { [ KEY_RIGHT is-key-down ] [ [  2.0 + ] change-x ] }
-        { [ KEY_LEFT  is-key-down ] [ [ -2.0 + ] change-x ] }
-        { [ KEY_DOWN  is-key-down ] [ [  2.0 + ] change-y ] }
-        { [ KEY_UP    is-key-down ] [ [ -2.0 + ] change-y ] }
-        [ ]
-    } cond drop ;
+    player get
+    KEY_LEFT KEY_RIGHT check-axis-movement [ '[ _ 2.0 * + ] change-x ] when*
+    KEY_UP KEY_DOWN check-axis-movement [ '[ _ 2.0 * + ] change-y ] when*
+    drop ;
 
 : render-loop ( -- )
     begin-drawing