Uses accompanying Basket, Objects and Fruit libraries to create Fruit Basket game. If an object is caught, points are added; if an object in missed, a 'life' is lost.

Dependents:   Game_Controller_Project

Catch_Model.cpp

Committer:
Nathanj94
Date:
2017-04-18
Revision:
10:6605cd9d374b
Parent:
9:902b67101cdc
Child:
11:68e7c942f67b

File content as of revision 10:6605cd9d374b:

#include "Catch_Model.h"

Catch_Model::Catch_Model()
{
    
}

Catch_Model::~Catch_Model()
{
    
}

void Catch_Model::init(int basket_y, int basket_width, int objects_speed, int lives)
{
    _basket_y = basket_y;
    _basket_width = basket_width;
    _objects_speed = objects_speed;
    _lives = lives;
    _delay = 1;
    
    basket.init(_basket_y, _basket_width);
    objects.init(_objects_speed);
}

void Catch_Model::input(Gamepad &pad)
{
    _d = pad.get_direction();
    _mag = pad.get_mag();
}

void Catch_Model::update(N5110 &lcd, Gamepad &pad)
{
    check_basket_catch(lcd, pad);
    check_basket_miss(lcd, pad);
    
    basket.move_stick(_d, _mag);
    basket.move_LR(pad);
    objects.speed_select(pad);
    objects.move();
}

void Catch_Model::draw(N5110 &lcd)
{
    basket.draw(lcd);
    objects.draw(lcd);
    print_score(lcd);
    print_lives(lcd);
    print_delay(lcd);
}

void Catch_Model::check_basket_catch(N5110 &lcd, Gamepad &pad)
{
   int b_x_pos = basket.get_x();
   int b_y_pos = basket.get_y();
   int o_x_pos = objects.get_x();
   int o_y_pos = objects.get_y();
    
    if(
       (o_y_pos >= b_y_pos) &&
       (o_x_pos >= b_x_pos) &&
       (o_x_pos <= (b_x_pos + 6))
       ) {
           pad.tone(1000, 0.2);
           objects.undraw(lcd);
           add_score();
           objects.init(_objects_speed);
    }
}

void Catch_Model::check_basket_miss(N5110 &lcd, Gamepad &pad)
{
   int b_y_pos = basket.get_y();
   int o_y_pos = objects.get_y();
   
   int score_var;
   score_var = objects.get_score_var();
   
   if (o_y_pos > b_y_pos) {
       objects.undraw(lcd);
       if (score_var != 5) {
           _lives--;
          }
        pad.tone(100, 0.2);
        objects.init(_objects_speed);
      } 
}

void Catch_Model::check_a(N5110 &lcd, Gamepad &pad)
{
    if (
        (pad.check_event(Gamepad::A_PRESSED) == true) &&
        (_delay == 1)
        ) {
            objects.undraw(lcd);
            objects.init(_objects_speed);
        
            _delay = 0;
            timeout.attach(this, &Catch_Model::set_delay, 10.0);
        }
    
}

void Catch_Model::check_b(N5110 &lcd, Gamepad &pad)
{
    if (
        (pad.check_event(Gamepad::B_PRESSED) == true) &&
        (_delay == 1)
        ) {
            _lives++;
        
            _delay = 0;
            timeout.attach(this, &Catch_Model::set_delay, 10.0);
        }
    
}

void Catch_Model::check_x(N5110 &lcd, Gamepad &pad)
{
    if (
        (pad.check_event(Gamepad::X_PRESSED) == true) &&
        (_delay == 1)
        ) {  
            _delay = 0;
            timeout.attach(this, &Catch_Model::set_delay, 10.0);  
        }    
                
}

void Catch_Model::set_delay()
{
    _delay = 1;
}

void Catch_Model::add_score()
{
    int score_var;
    score_var = objects.get_score_var();
    
    if (score_var == 1) {
        basket.add_score_1();
    } else if (score_var == 2) {
        basket.add_score_2();
    } else if (score_var == 3) {
        basket.add_score_5();
    } else if (score_var == 4) {
        basket.add_score_10();
    } else {
        _lives--;
    }
}

int Catch_Model::get_lives() 
{
    return _lives;
}

void Catch_Model::print_lives(N5110 &lcd)
{
    char buffer[14];
    int lives = get_lives();
    
    int print_lives = sprintf(buffer, "LIVES:%d", lives);
    if (print_lives <= 14) {
        lcd.printString(buffer,0,0);
        lcd.refresh();
    }
}

void Catch_Model::print_score(N5110 &lcd)
{
    char buffer[14];
    int score = basket.get_score();
    
    int print_score;
    
    if ((score == 0)||(score <= 9)) {
        print_score = sprintf(buffer, "000%d", score);
    } else if (score <= 99) {
        print_score = sprintf(buffer, "00%2d", score);
    } else if (score <= 999 ) {
        print_score = sprintf(buffer, "0%3d", score);
    } else {
        print_score = sprintf(buffer, "%4d", score);
    }
    
    if (print_score <= 14) {
        lcd.printString(buffer,59,0);
        lcd.refresh();
    }
}
    
void Catch_Model::print_delay(N5110 &lcd)
{
    if (_delay == 1) {
        lcd.drawLine(46,4,50,8,1);
        lcd.drawLine(50,8,54,0,1);
        } else {
            lcd.drawLine(46,0,54,8,1);
            lcd.drawLine(46,8,54,0,1);
            }
}