Steven Mahasin / Mbed 2 deprecated DreamDungeon

Dependencies:   mbed MotionSensor

Files at this revision

API Documentation at this revision

Comitter:
el17sm
Date:
Mon Apr 15 03:05:40 2019 +0000
Parent:
4:d1aeb131e533
Child:
6:104c2506237e
Commit message:
Cleaned up code a bit

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Apr 15 02:28:55 2019 +0000
+++ b/main.cpp	Mon Apr 15 03:05:40 2019 +0000
@@ -9,60 +9,16 @@
 */
 
 #include "main.h"
-N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
-Gamepad gamepad;
-
-bool matrix_collision_test(int pos_x, int pos_y, int map_no, int width, int length){
-    for (int j = pos_y; j < pos_y+length; j++){
-        for(int i = pos_x; i < pos_x+width; i++){
-            if ((j>=48) || (i>=84) || (j<0) || (i<0)) {
-            }
-            else if ((level_map[0][j][i] == 1)) {
-                return false;
-            }
-        }
-    }
-    return true;
-}
 
 int main()
 {
     lcd.init();
     lcd.setContrast(0.45);
     gamepad.init();
-    
-    float player_pos[2] = {39,27};
-    float future_player_pos[2] = {39,27};
-    int face = 2;
-    int counter = 0;
 
     while(1){
         
-        Vector2D mapped_coord = gamepad.get_mapped_coord();
-        future_player_pos[0] = player_pos[0] + mapped_coord.x/2;
-        future_player_pos[1] = player_pos[1] - mapped_coord.y/2;
-        if(matrix_collision_test(future_player_pos[0], player_pos[1], 0, 6, 5)){
-            player_pos[0] = future_player_pos[0];
-        }
-        if(matrix_collision_test(player_pos[0], future_player_pos[1], 0, 6, 5)){
-            player_pos[1] = future_player_pos[1];
-        }
-        moving = false;
-        if (abs(mapped_coord.x) + abs(mapped_coord.y) > 0.1f){
-            moving = true;
-            if (mapped_coord.y < 0 && abs(mapped_coord.y) > abs(mapped_coord.x)){
-                face = 2;
-            }
-            else if (mapped_coord.y > 0 && abs(mapped_coord.y) > abs(mapped_coord.x)){
-                face = 0;
-            }
-            else if (mapped_coord.x > 0 && abs(mapped_coord.x) > abs(mapped_coord.y)){
-                face = 1;
-            }
-            else if (mapped_coord.x < 0 && abs(mapped_coord.x) > abs(mapped_coord.y)){
-                face = 3;
-            }
-        }
+        move_player();
         
         lcd.clear();
         lcd.drawSprite(0,0,48,84,(int *)level_map[1]);
--- a/main.h	Mon Apr 15 02:28:55 2019 +0000
+++ b/main.h	Mon Apr 15 03:05:40 2019 +0000
@@ -6,3 +6,52 @@
 #include "math.h"
 #include "sprites.h"
 
+N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
+Gamepad gamepad;
+
+float player_pos[2] = {39,27};
+float future_player_pos[2] = {39,27};
+int face = 2;
+int counter = 0;
+
+bool matrix_collision_test(int pos_x, int pos_y, int map_no, int width, int length){
+    for (int j = pos_y; j < pos_y+length; j++){
+        for(int i = pos_x; i < pos_x+width; i++){
+            if ((j>=48) || (i>=84) || (j<0) || (i<0)) {
+            }
+            else if ((level_map[0][j][i] == 1)) {
+                return false;
+            }
+        }
+    }
+    return true;
+}
+
+void move_player() {
+    
+    Vector2D mapped_coord = gamepad.get_mapped_coord();
+    future_player_pos[0] = player_pos[0] + mapped_coord.x/2;
+    future_player_pos[1] = player_pos[1] - mapped_coord.y/2;
+    if(matrix_collision_test(future_player_pos[0], player_pos[1], 0, 6, 5)){
+        player_pos[0] = future_player_pos[0];
+    }
+    if(matrix_collision_test(player_pos[0], future_player_pos[1], 0, 6, 5)){
+        player_pos[1] = future_player_pos[1];
+    }
+    moving = false;
+    if (abs(mapped_coord.x) + abs(mapped_coord.y) > 0.1f){
+        moving = true;
+        if (mapped_coord.y < 0 && abs(mapped_coord.y) > abs(mapped_coord.x)){
+            face = 2;
+        }
+        else if (mapped_coord.y > 0 && abs(mapped_coord.y) > abs(mapped_coord.x)){
+            face = 0;
+        }
+        else if (mapped_coord.x > 0 && abs(mapped_coord.x) > abs(mapped_coord.y)){
+            face = 1;
+        }
+        else if (mapped_coord.x < 0 && abs(mapped_coord.x) > abs(mapped_coord.y)){
+            face = 3;
+        }
+    }    
+}
\ No newline at end of file