A game for Lab 4 of ECE 4180

Dependencies:   4DGL-uLCD-SE LSM9DS1_Library SDFileSystem mbed-rtos mbed wave_player

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Player.cpp Source File

Player.cpp

00001 #include "Player.h"
00002 
00003 #define WIDTH 10
00004 #define HEIGHT 10
00005 
00006 Player::Player(uLCD_4DGL* screenInit, InputHandler* input, int startX, int startY) 
00007 {
00008     screen = screenInit;
00009     x = startX;
00010     y = startY;
00011     lastX = x-1;
00012     lastY = y;
00013     
00014     inputManager = input;
00015 }
00016 
00017 void Player::update(int windowWidth, int windowHeight) 
00018 {
00019     float xAccel = inputManager->getXAccel() * 4;
00020     float yAccel = inputManager->getYAccel() * 4;
00021     
00022     int speedX = (int)(xAccel + (xAccel > 0 ? 0.5 : -0.5)) ; // Maximum Speed of 4
00023     int speedY = (int)(yAccel + (yAccel > 0 ? 0.5 : -0.5)) ; // Maximum Speed of 4
00024     
00025     x += speedX;
00026     y += speedY;
00027     
00028     // Adjust to be in the window 
00029     if (x <= WIDTH/2) x = WIDTH/2;
00030     if (y <= WIDTH/2) y = HEIGHT/2;
00031     if (x + WIDTH/2 >= windowWidth) x = windowWidth - WIDTH/2;
00032     if (y + HEIGHT/2 >= windowHeight) y = windowHeight - HEIGHT/2;
00033 }
00034 
00035 void Player::draw() 
00036 {
00037     screen_mutex.lock();
00038     if (lastX != x || lastY != y) {
00039         screen->filled_circle(lastX, lastY, HEIGHT, BLACK);
00040         screen->filled_circle(x, y, HEIGHT/2, GREEN);
00041     }
00042     lastX = x;
00043     lastY = y;
00044     screen_mutex.unlock();
00045 }
00046 
00047 // Bounding Box x
00048 int Player::getX() 
00049 {
00050     return x - WIDTH/2;
00051 }
00052 
00053 int Player::getY() 
00054 {
00055     return y - HEIGHT/2;
00056 }
00057 
00058 int Player::getWidth() 
00059 {
00060     return WIDTH;
00061 }
00062 
00063 int Player::getHeight() 
00064 {
00065     return HEIGHT;
00066 }
00067