Dependencies:   mbed

Revision:
5:2d9f3c36bcb9
Parent:
4:9f41fc1c2ad1
Child:
6:5f76dd718dc3
--- a/main.cpp	Tue Feb 11 13:36:11 2020 +0000
+++ b/main.cpp	Tue Feb 11 17:28:56 2020 +0000
@@ -22,6 +22,7 @@
 N5110 LCD;
 Graphics Graphics;
 Controls Controls;
+Mechanics Mechanics;
 
 #ifndef STRUCTS
 #define STRUCTS
@@ -36,16 +37,32 @@
     Point_2D to;
 };
 
+struct Square_2D {
+    Point_2D TL;
+    Point_2D BR;
+};
+struct Triangle_2D {
+    Point_2D TL;
+    Point_2D BR;
+    int Type;
+};
+
 struct Map_Data {
     int number_of_track_lines;
     int number_of_dotted_lines;
     int number_of_flags;
+    int number_of_walls;
+    int number_of_off_track_squares;
+    int number_of_off_track_triangles;
+    int number_of_out_of_bounds_squares;
+    int number_of_out_of_bounds_triangles;
+    int number_of_gates;
 };
 
 #endif
 
 //MAP - 1
-Map_Data Map_1 = {36,1,2};
+Map_Data Map_1 = {36,1,2,16,14,19,8,5,12};
 
 //Lines
 const Line_2D Map_1_Track_Lines[36] = {
@@ -88,15 +105,119 @@
     {{-80,-120},{20,-20}},
 };
 
+//Dotted Lines
 const Line_2D Map_1_Track_Dotted_Lines[1] = {
     {{-20,0},{20,0}},
 };
 
+//Flags
 Point_2D Map_1_Flags[2] = {
     {-20,0},
     {24,0}
 };
 
+//Walls
+Line_2D Map_1_Walls[16] = {
+    //Inside
+    {{-40,80},{-40,0}},
+    {{-40,0},{-100,-60}},
+    {{-100,-60},{-240,-60}},
+    {{-240,-60},{-300,-120}},
+    {{-300,-120},{-340,-60}},
+    {{-340,-60},{-340,60}},
+    {{-340,60},{-280,60}},
+    {{-280,60},{-280,-20}},
+    {{-280,-20},{-100,-20}},
+    {{-100,-20},{-80,0}},
+    {{-80,0},{-80,80}},
+    {{-80,80},{-40,80}},
+    //Outside
+    {{40,160},{40,-220}},
+    {{40,-220},{-420,-220}},
+    {{-420,-220},{-420,160}},
+    {{-420,160},{40,160}},
+};
+
+//Off track? objects
+Square_2D Map_1_Off_Track_Square[14] = {
+    //Inside
+    {{-100,80},{-20,0}},
+    {{-80,100},{-40,80}},
+    {{-260,0},{-100,-80}},
+    {{-360,60},{-260,-100}},
+    {{-320,100},{-280,60}},
+    {{-280,80},{-260,60}},
+    {{-300,-100},{-260,-120}},
+    {{-200,60},{-60,40}},
+    {{-220,140},{-140,60}},
+    {{-200,-120},{20,-200}},
+    {{-400,200},{20,140}},
+    {{-500,200},{-400,-300}},
+    {{20,200},{100,-300}},
+    {{-400,-200},{20,-300}}
+};
+
+//Off track? objects
+Triangle_2D Map_1_Off_Track_Triangle[19] = {
+    {{-40,100},{-20,80},1},
+    {{-100,100},{-80,80},4},
+    {{-140,40},{-100,0},4},
+    {{-100,0},{-20,-80},2}, //
+    {{-260,40},{-220,0},1},
+    {{-260,-80},{-220,-120},2},
+    {{-200,-120},{-260,-160},2},
+    {{-280,100},{-260,80},1},
+    {{-360,100},{-320,60},4},
+    {{-360,-100},{-300,-160},3},
+    //Outside
+    {{-20,140},{20,100},3},
+    {{-80,-20},{20,-120},4},
+    {{-140,140},{-100,100},2},
+    {{-160,60},{-140,40},2}, //
+    {{-220,60},{-200,40},3},
+    {{-280,-120},{-200,-200},4},
+    {{-260,140},{-240,100},3},
+    {{-400,140},{-340,80},2}, //
+    {{-400,-120},{-320,-200},1}
+};
+
+Square_2D Map_1_Out_Of_Bounds_Square[8] = {
+    //Inside
+    {{-80,80},{-40,0}},
+    {{-340,60},{-280,-60}},
+    {{-280,-20},{-100,-60}},
+    {{-80,0},{-60,-20}},
+    //Outside
+    {{40,160},{60,-220}},
+    {{-420,-220},{40,-240}},
+    {{-440,160},{-420,-220}},
+    {{-420,180},{40,160}}
+};
+
+Triangle_2D Map_1_Out_Of_Bounds_Triangle[5] = {
+    //Inside
+    {{-60,0},{-40,-20},2},
+    {{-100,0},{-80,-20},4},
+    {{-100,-20},{-60,-60},2},
+    {{-300,-60},{-240,-120},2},
+    {{-340,-60},{-300,-120},3}
+};
+
+Square_2D Map_1_Gates[12] = {
+    {{-40,0},{40,-10}},
+    {{-40,80},{40,70}},
+    {{-80,160},{-70,80}},
+    {{-160,70},{-80,60}},
+    {{-200,60},{-190,-20}},
+    {{-310,160},{-300,60}},
+    {{-420,10},{-340,0}},
+    {{-420,-90},{-300, -100}},
+    {{-300,-100},{-290,-220}},
+    {{-200,-60},{-190,-220}},
+    {{-110,-60},{-100,-220}},
+    {{-100,-40},{40,-60}}
+};
+
 //GLOBAL VARIABLES
 
 //Transform
@@ -109,6 +230,13 @@
 float max_speed = 4;
 int handling = 2;
 
+//Mechanics
+bool off_track = false;
+bool out_of_bounds = false;
+int lap_gate = 0;
+int laps = 1;
+bool direction = true;
+
 //Game parameters
 int game_fps = 50;
 
@@ -117,11 +245,35 @@
     LCD.init();
     Device.init();
     while(1) {
-        speed = Controls.Get_Speed(speed, max_speed, Device);
+
+        //MECHANICS
+        off_track = Mechanics.Is_Offtrack(translation, Map_1_Off_Track_Square, Map_1_Off_Track_Triangle, Map_1);
+        out_of_bounds = Mechanics.Is_Out_Of_Bounds(translation, Map_1_Out_Of_Bounds_Square, Map_1_Out_Of_Bounds_Triangle, Map_1);
+        laps = Mechanics.Get_Laps(laps, Map_1_Gates, Map_1.number_of_gates, translation, lap_gate);
+        lap_gate = Mechanics.Get_Gate(Map_1_Gates, Map_1.number_of_gates, translation, lap_gate);
+
+        //CONTROLS
         angle = Controls.Get_Angle(angle, handling, Device);
-        translation = Controls.Get_Translation(translation, angle, speed, Device);
+        translation = Controls.Get_Translation(translation, angle, speed, out_of_bounds, Device);
+        speed = Controls.Get_Speed(speed, max_speed, off_track, out_of_bounds, Device);
+
+        //GRAPHICS
+        LCD.clear();
         Graphics.Change_Contrast(LCD,Device);
-        Graphics.Draw_Map(game_fps, translation, angle, squish, (Line_2D *) Map_1_Track_Lines, (Line_2D *)Map_1_Track_Dotted_Lines, (Point_2D *)Map_1_Flags, Map_1, LCD);
+        Graphics.Draw_Map(
+            translation,
+            angle,
+            squish,
+            (Line_2D *) Map_1_Track_Lines,
+            (Line_2D *)Map_1_Track_Dotted_Lines,
+            (Line_2D *)Map_1_Walls,
+            (Point_2D *)Map_1_Flags,
+            Map_1,
+            LCD
+        );
+        Graphics.Draw_Laps(laps, LCD);
+        LCD.refresh();
+        wait((1 / float(game_fps)));
     }