Final Commit

Dependencies:   mbed

SnakeEngine/SnakeEngine.cpp

Committer:
JRM1986
Date:
2018-05-01
Revision:
23:3081be418c89
Parent:
21:63c5590cb2c2
Child:
24:4b180348826e

File content as of revision 23:3081be418c89:

#include "SnakeEngine.h"


SnakeEngine::SnakeEngine()
{

}

SnakeEngine::~SnakeEngine()
{

}
///////////////// global methods /////////////////


///////////////// public methods /////////////////


void SnakeEngine::set_even_array(Gamepad &pad)
{
    
    int i;
    int length = g_tl;
    
    extern int g_odd_array_x[100];
    extern int g_odd_array_y[100];
    
    Vector2D pos = _snake.get_snake_position();
       
    g_odd_array_x[0] = pos.x;
    g_odd_array_y[0] = pos.y;
    
    for(i = length; i >= 0; --i) {
        
        if(i > 0) {
            
        g_even_array_x[i] = g_odd_array_x[i-1];
        g_even_array_y[i] = g_odd_array_y[i-1];
        
        }

        else if(i == 0) {
            
            g_even_array_x[0] = pos.x;
            g_even_array_y[0] = pos.y;
            
            }
     

    }
    
}

void SnakeEngine::set_odd_array(Gamepad &pad)
{
    
    int i;
    int length = g_tl;
    
    extern int g_even_array_x[100];
    extern int g_even_array_y[100];
    
    Vector2D pos = _snake.get_snake_position();
    
    g_even_array_x[0] = pos.x;
    g_even_array_y[0] = pos.y;
    
    for(i = length; i >= 0; --i) {
        
        if(i > 0) {
            
        g_odd_array_x[i] = g_even_array_x[i-1];
        g_odd_array_y[i] = g_even_array_y[i-1];
        
        }

        else if(i == 0) {
            
            g_odd_array_x[0] = pos.x;
            g_odd_array_y[0] = pos.y;
            
            }
        
    }
    
}


void SnakeEngine::set_tail_array(Gamepad &pad)
{
    
    int c = g_engine_fc % 2;
    
    if(c == 1) {
        
        set_odd_array(pad);
        
        }
        
        else if (c == 0) {
            
            set_even_array(pad);
            
            }
            
}

void SnakeEngine::draw_tail(N5110 &lcd)
{
    int length = g_tl;
    int c;
    c = g_engine_fc % 2;
    int i;
    int x;
    int y;
    
    //printf("Odd/Even %i \n", c);
    
    for(i=0; i<=length; ++i) {
            
    if(c == 1) {
        
        x = g_odd_array_x[i];
        y = g_odd_array_y[i];
        
        lcd.setPixel(x,y,true);
                
        }
        
        else if (c == 0) {
        
            x = g_even_array_x[i];
            y = g_even_array_y[i];
            
            lcd.setPixel(x,y,true);
                        
            }
            
        }
}

int g_tail_length()
{
    
    extern int g_tl;
    
    return g_tl;
    
}


void SnakeEngine::init(Direction in, Direction cur, int snake_pos_x, int snake_pos_y)
{
    _snake_pos_x = snake_pos_x; 
    _snake_pos_y = snake_pos_y;
    _in = in;
    _cur = cur;
    
    _snake.init(_in, _cur, _snake_pos_x, _snake_pos_y);
    
    _food.init(true);
    
}

void SnakeEngine::get_input(Gamepad &pad)
{
    
    _in = pad.get_direction();
    
}

void SnakeEngine::update(Gamepad &pad)
{
    //int length;
    bool food_col;
    ++g_engine_fc;
    
    
    _snake.update(_in, _cur);
    food_col = detect_food_collision(pad);
    _collision = food_col;
    set_tail_length(food_col);
    //length = get_tail_length();
    //printf("Tail Length = %i \n", length);
    set_tail_array(pad);

}


void SnakeEngine::draw(N5110 &lcd)
{
    
    lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
    lcd.setContrast(0.5);
    _food.update(_collision);
    _food.draw(lcd);
    //_snake.draw(lcd);
    //draw_tail(lcd);
    //_snake.update(_in, _cur);
    _snake.draw(lcd);
    draw_tail(lcd);
    
}

bool SnakeEngine::detect_food_collision(Gamepad &pad)
{
    Vector2D snake_pos = _snake.get_snake_position();
    Vector2D food_pos = _food.get_food_position();
    
    bool success_flag = false;
    
    if((snake_pos.x == food_pos.x) && (snake_pos.y == food_pos.y)) {
        
        success_flag = true;
        
        }
        
        else {
            
            success_flag = false;
            
            }
        
    return success_flag;
    
}

bool SnakeEngine::detect_wall_collision(Gamepad &pad)
{
    
    Vector2D snake_pos = _snake.get_snake_position();

    bool success_flag = false;
    
    if((snake_pos.x == (0 || 84)) || (snake_pos.y == (0 || 48))) {
        
        success_flag = true;
        
        }
        
        return success_flag;
    
}


void SnakeEngine::set_tail_length(bool collision_detected)
{
    
    if(collision_detected) {
        
        ++g_tl;
        
        }
        
        else {
            
            g_tl = g_tl;
            
            }
            
}

int SnakeEngine::get_tail_length()
{
    
    int length = g_tl;
    
    return length;
    
}