ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el19zf

Dependencies:   mbed

Revision:
9:62d6559f0d50
Parent:
8:8287d2ef965d
Child:
12:009895f6b6e4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PeopleEngine/People.cpp	Sat May 09 08:27:07 2020 +0000
@@ -0,0 +1,100 @@
+#include "People.h"
+#define INIT_x 1
+#define INIT_y 23
+
+
+const int people_sprite[4][4] = {
+        {0,1,1,0},
+        {1,1,1,1},
+        {0,1,1,0},
+        {0,1,1,0},
+};//try to draw a people, but for the operability of game, seems not good..
+
+People::People() {
+    
+}
+
+People::~People() {
+    
+}
+    
+void People::init() {
+    
+    _x = INIT_x;
+    _y = INIT_y;//Set initial postion of people
+}
+void People::draw(N5110 &lcd) {
+    
+    //Draw sprite to represent a people
+    lcd.drawSprite(_x,_y,4,4,(int*)people_sprite);
+    // printf("drawSprite");
+}
+
+void People::set_pos(Vector2D p)
+{
+    _x = p.x;
+    _y = p.y;
+}
+
+void People::set_velocity(Direction d,float mag)
+{
+    _d = d;
+    _mag = mag;
+}
+
+Vector2D People::get_pos()
+{
+    Vector2D p = {_x,_y};
+    //printf("Coord = %f,%f\n",p.x,p.y);
+    return p;
+}
+
+void People::update()
+{
+    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;}}
+    //without going off screen
+    if (_x < 1) { _x = 1;} else
+    if (_x > 79) { _x = 79;} else
+    if (_y < 1) {_y = 1;} else
+    if (_y > 43) {_y = 43;} 
+}
+    
\ No newline at end of file