ELEC2645 (2018/19) / Mbed 2 deprecated el17aio

Dependencies:   mbed

Revision:
9:241a1a7d8527
Parent:
6:8473dacbeb65
Child:
11:73cd744ffa80
--- a/Ship/Ship.cpp	Tue Mar 12 10:53:11 2019 +0000
+++ b/Ship/Ship.cpp	Tue Apr 09 05:14:07 2019 +0000
@@ -24,11 +24,11 @@
 */
 
 // Set basic ship to be default
-void Ship::init(int ship_speed,int ship_width,int ship_height,int ship_xpos,int ship_ypos)
+void Ship::init(int ship_width,int ship_height,int ship_speed,int ship_xpos,int ship_ypos)
 {
-    _ship_speed = ship_speed;
     _ship_width = ship_width;
     _ship_height = ship_height;
+    _ship_speed = ship_speed;
     _ship_xpos = ship_xpos;
     _ship_xpos = ship_ypos;
 //    _ship_shape[_ship_height][_ship_width] = ship_shape;
@@ -39,34 +39,31 @@
     lcd.drawSprite(_ship_xpos,_ship_ypos,6,9,(int *)basic);
 }
 
-void Ship::update_ship(int x_joystick,int y_joystick)
-{
-    // Do not move sprite until joystick is moved reasonably
+void Ship::update_ship(float x_joystick,float y_joystick)
+{   // Only change position if joystick is reasonably moved
     if(-0.25 > x_joystick || x_joystick > 0.25 || -0.25 > y_joystick || y_joystick > 0.25) {
+
+        // Update positions using joystick and the intended ships speed
+        _ship_xpos = _ship_xpos + (x_joystick*_ship_speed);
+        _ship_ypos = _ship_ypos - (y_joystick*_ship_speed);
+
         // Dont let sprite move out of screen
-        if(_ship_xpos > (84 - (1.5 * _ship_width))) {
-            if(x_joystick < -0.25) {
-                _ship_xpos = _ship_xpos + (x_joystick*_ship_speed);
-            }
-        } else if(_ship_xpos < (0.5 *_ship_width)) {
-            if(x_joystick > 0.25) {
-                _ship_xpos = _ship_xpos + (x_joystick*_ship_speed);
-            }
-        } else {
-            _ship_xpos = _ship_xpos + (x_joystick*_ship_speed);
+        if(_ship_xpos < 0) {
+            _ship_xpos = 0;
+        }
+        if(_ship_xpos > (84 - (_ship_width))) {
+            _ship_xpos = 84 - _ship_width;
+        }
+        if(_ship_ypos < 0) {
+            _ship_ypos = 0;
+        }
+        if(_ship_ypos > (48 - (_ship_height))) {
+            _ship_ypos = 48 - _ship_height;
         }
 
-        if(_ship_ypos > 1) {
-            if(y_joystick > 0.25) {
-                _ship_ypos = _ship_ypos - (y_joystick*_ship_speed);
-            }
-        }
-        if(_ship_ypos < (48 - _ship_height)) {
-            if(y_joystick < -0.25) {
-                _ship_ypos = _ship_ypos - (y_joystick*_ship_speed);
-            }
-        }
     }
+    // printf("y_joysticzk = %f\n",y_joystick);
+    // printf("x_joystick = %f\n",x_joystick);
 }
 // returns the current ship position on screen as a vector
 Vector2D Ship::get_pos()