ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el19zf

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
el19zf
Date:
Sun Apr 12 12:56:49 2020 +0000
Parent:
2:67b51ee7fc34
Child:
4:b12a49f0b730
Commit message:
build a control method for the basic element 'people'

Changed in this revision

People/People.cpp Show annotated file Show diff for this revision Revisions of this file
People/People.h Show annotated file Show diff for this revision Revisions of this file
PeopleEngine/PeopleEngine.cpp Show annotated file Show diff for this revision Revisions of this file
PeopleEngine/PeopleEngine.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/People/People.cpp	Sun Apr 12 04:35:32 2020 +0000
+++ b/People/People.cpp	Sun Apr 12 12:56:49 2020 +0000
@@ -8,7 +8,7 @@
         {1,1,1,1},
         {0,1,1,0},
         {0,1,1,0},
-};//try to simulate a people, but for the operability of game, seems not good
+};//try to draw a people, but for the operability of game, seems not good..
 
 People::People() {
     
@@ -27,13 +27,7 @@
     
     //Draw sprite to represent a people
     lcd.drawSprite(_x,_y,4,4,(int*)people_sprite);
-    printf("drawSprite");
-}
-
-void People::update()
-{
-    _x += _velocity.x;
-    _y += _velocity.y;
+    // printf("drawSprite");
 }
 
 void People::set_pos(Vector2D p)
@@ -42,23 +36,60 @@
     _y = p.y;
 }
 
-void People::set_velocity(Vector2D v)
+void People::set_velocity(Direction d,float mag)
 {
-    _velocity.x = v.x;
-    _velocity.y = v.y;
+    _d = d;
+    _mag = mag;
 }
 
 Vector2D People::get_pos()
 {
     Vector2D p = {_x,_y};
+    //printf("Coord = %f,%f\n",p.x,p.y);
     return p;
 }
 
-Vector2D People::get_velocity()
+void People::update()
 {
-    Vector2D v = {_velocity.x,_velocity.y};
-    return v;
+    if (_d == S) {
+        if(_mag < 0.25f)        {   _x += 0;    _y += 1;} else
+        if(0.25f < _mag < 0.5f) {   _x += 0;    _y += 2;} else
+        if(0.5f < _mag < 0.75f) {   _x += 0;    _y += 3;} else 
+                                {   _x += 0;    _y += 4;}}
+    if (_d == SE) {
+        if(_mag < 0.25f)        {   _x += 1;    _y += 1;} else
+        if(0.25f < _mag < 0.5f) {   _x += 2;    _y += 2;} else
+        if(0.5f < _mag < 0.75f) {   _x += 3;    _y += 3;} else 
+                                {   _x += 4;    _y += 4;}}
+    if (_d == E) {              
+        if(_mag < 0.25f)        {   _x += 1;    _y += 0;} else
+        if(0.25f < _mag < 0.5f) {   _x += 2;    _y += 0;} else
+        if(0.5f < _mag < 0.75f) {   _x += 3;    _y += 0;} else 
+                                {   _x += 4;    _y += 0;}}  
+    if (_d == NE) {
+        if(_mag < 0.25f)        {   _x += 1;    _y -= 1;} else
+        if(0.25f < _mag < 0.5f) {   _x += 2;    _y -= 2;} else
+        if(0.5f < _mag < 0.75f) {   _x += 3;    _y -= 3;} else 
+                                {   _x += 4;    _y -= 4;}}
+    if (_d == N) {
+        if(_mag < 0.25f)        {   _x += 0;    _y -= 1;} else
+        if(0.25f < _mag < 0.5f) {   _x += 0;    _y -= 2;} else
+        if(0.5f < _mag < 0.75f) {   _x += 0;    _y -= 3;} else 
+                                {   _x += 0;    _y -= 4;}}
+    if (_d == NW) {
+        if(_mag < 0.25f)        {   _x -= 1;    _y -= 1;} else
+        if(0.25f < _mag < 0.5f) {   _x -= 2;    _y -= 2;} else
+        if(0.5f < _mag < 0.75f) {   _x -= 3;    _y -= 3;} else 
+                                {   _x -= 4;    _y -= 4;}}
+    if (_d == W) {
+        if(_mag < 0.25f)        {   _x -= 1;    _y -= 0;} else
+        if(0.25f < _mag < 0.5f) {   _x -= 2;    _y -= 0;} else
+        if(0.5f < _mag < 0.75f) {   _x -= 3;    _y -= 0;} else 
+                                {   _x -= 4;    _y -= 0;}}
+    if (_d == NW) {
+        if(_mag < 0.25f)        {   _x -= 1;    _y += 1;} else
+        if(0.25f < _mag < 0.5f) {   _x -= 2;    _y += 2;} else
+        if(0.5f < _mag < 0.75f) {   _x -= 3;    _y += 3;} else 
+                                {   _x -= 4;    _y += 4;}}
 }
-
-
     
\ No newline at end of file
--- a/People/People.h	Sun Apr 12 04:35:32 2020 +0000
+++ b/People/People.h	Sun Apr 12 12:56:49 2020 +0000
@@ -26,15 +26,14 @@
     //accessors   
     void set_pos(Vector2D p);
         
-    void set_velocity(Vector2D v);
+    void set_velocity(Direction d,float mag);
         
     //mutators
     Vector2D get_pos();
-        
-    Vector2D get_velocity();
-        
+       
 private:
-    Vector2D _velocity;
+    Direction _d;
+    float _mag;
     int _x;
     int _y;
 };
--- a/PeopleEngine/PeopleEngine.cpp	Sun Apr 12 04:35:32 2020 +0000
+++ b/PeopleEngine/PeopleEngine.cpp	Sun Apr 12 12:56:49 2020 +0000
@@ -0,0 +1,35 @@
+#include "PeopleEngine.h"
+
+PeopleEngine::PeopleEngine()
+{
+
+}
+
+PeopleEngine::~PeopleEngine()
+{
+    
+}
+
+void PeopleEngine::init()
+{
+    _people.init();
+}
+void PeopleEngine::read_input(Gamepad &pad)
+{
+    //directions held in an enum and magnitude in polar coordinates
+    _d = pad.get_direction();
+    _mag = pad.get_mag();
+    //printf("velocity = %f,%f\n",_v.x,_v.y);
+}
+void PeopleEngine::update()
+{
+   _people.set_velocity(_d,_mag);
+   _people.update();
+   _p=_people.get_pos();
+   printf("position = %f,%f\n",_p.x,_p.y);
+}
+   
+void PeopleEngine::draw(N5110 &lcd)
+{
+    _people.draw(lcd);
+} 
\ No newline at end of file
--- a/PeopleEngine/PeopleEngine.h	Sun Apr 12 04:35:32 2020 +0000
+++ b/PeopleEngine/PeopleEngine.h	Sun Apr 12 12:56:49 2020 +0000
@@ -6,4 +6,33 @@
 #include "N5110.h"
 #include "People.h"
 
+/** People class
+    @set a Engine of 'People' and do some basic check
+    @author Zeyu Feng
+    @date April 2020
+   */
 
+class PeopleEngine
+{
+    
+public:   
+    PeopleEngine();//constructor
+    ~PeopleEngine();//destructor
+    
+    void init();
+    void read_input(Gamepad &pad);
+    void update();
+    void draw(N5110 &lcd);
+
+private:
+
+    People _people;
+    Direction _d;
+    Vector2D _p;
+    float _mag;
+
+};
+#endif
+    
+
+    
\ No newline at end of file
--- a/main.cpp	Sun Apr 12 04:35:32 2020 +0000
+++ b/main.cpp	Sun Apr 12 12:56:49 2020 +0000
@@ -15,21 +15,31 @@
 #include "Gamepad.h"
 #include "N5110.h"
 #include "People.h"
-
+#include "PeopleEngine.h"
 
 // objects
 Gamepad pad;
 N5110 lcd;
-People people;
+PeopleEngine engine;
 
 int main()
 {
+    //initial
     lcd.init();
     lcd.setContrast(0.5);
+    engine.init();
     pad.init();
-    people.init();
-    people.draw(lcd);
     lcd.refresh();
-
+    
+    //a infinite loop to control position of the people
+    while(1) {
+        lcd.clear();
+        engine.read_input(pad);
+        engine.update();
+        engine.draw(lcd);
+        lcd.refresh();
+        wait_ms(100);
+    }
+        
 }