From 5d0adfa1ab28d5ea65e9e9c084ab56f2ccfbf193 Mon Sep 17 00:00:00 2001
From: Joshua Saxby <joshua.a.saxby@gmail.com>
Date: Sun, 9 Feb 2020 18:45:13 +0000
Subject: [PATCH] Added move commands

---
 source/main.cpp | 53 ++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 39 insertions(+), 14 deletions(-)

diff --git a/source/main.cpp b/source/main.cpp
index a3f6735..8ba3929 100755
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -215,17 +215,6 @@ MicroBitImage Block::image() const {
 }
 
 
-// int x = 1;
-
-// void a_button_pressed(MicroBitEvent) {
-//     x--;
-// }
-
-// void b_button_pressed(MicroBitEvent) {
-//     x++;
-// }
-
-
 void debug_test() {
     // XXX: simple debug test to check my FLASH images were stored correctly
     // for accurately-timed animations irrespective of framerate, track time
@@ -312,14 +301,35 @@ bool can_shape_move(
 MicroBitImage stacked_shapes(5, 5);
 // game state variables
 bool game_over;
+// player left/right shift request tracker
+int move_command = 0; // 0 = do nothing, -1 = shift left, +1 = shift right
 // TODO: add tracking of score
 
 
+/*
+ * Very simple event-handlers for player pressing A/B to shift current Block
+ * left or right respectively.
+ */
+void a_button_pressed(MicroBitEvent) {
+    move_command = -1;
+}
+
+void b_button_pressed(MicroBitEvent) {
+    move_command = +1;
+}
+
+
 void start_new_game() {
     game_over = false; // we're starting a new game
     // clear the stacked shapes image
     stacked_shapes.clear();
-    // TODO: add event-handlers for button-presses
+    // set up event handlers
+    UBIT.messageBus.listen(
+        MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, a_button_pressed
+    );
+    UBIT.messageBus.listen(
+        MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, b_button_pressed
+    );
     unsigned long stopwatch = UBIT.systemTime();
     unsigned long move_speed = 350; // move down one block every 350ms
     // this is the main game loop
@@ -333,7 +343,6 @@ void start_new_game() {
         // if we can't send this Block down at creation, it's game over
         game_over = not can_shape_move(block, origin, stacked_shapes, down);
         if (not game_over) {
-            bool can_move = true;
             do {
                 // draw the scene
                 UBIT.display.print(stacked_shapes);
@@ -349,6 +358,16 @@ void start_new_game() {
                         stopwatch = stopwatch_now;
                     }
                 }
+                // take note of any move requests from the player
+                if (move_command != 0) {
+                    // create unit vector from move command
+                    Vector move(move_command, 0);
+                    if (can_shape_move(block, origin, stacked_shapes, move)) {
+                        origin = origin + move;
+                    }
+                    // reset move command to prevent it sticking
+                    move_command = 0;
+                }
                 // delay
                 UBIT.sleep(50); // 50ms sleep = 20fps
             } while (can_shape_move(block, origin, stacked_shapes, down));
@@ -357,7 +376,13 @@ void start_new_game() {
         }
     } while (not game_over);
     // TODO: clean up game state 
-    // TODO: remove event-handlers
+    // remove event-handlers
+    UBIT.messageBus.ignore(
+        MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, a_button_pressed
+    );
+    UBIT.messageBus.ignore(
+        MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, b_button_pressed
+    );
     UBIT.display.scroll("GAME OVER!");
     // TODO: add display of score
 }
-- 
GitLab