ELEC2645 (2017/18) / Mbed 2 deprecated ll13jrm

Dependencies:   mbed

Snake/Snake.cpp

Committer:
JRM1986
Date:
2018-04-04
Revision:
10:62d8cb7742c3
Parent:
9:561e5681b7a6
Child:
11:e260c17a0489

File content as of revision 10:62d8cb7742c3:

#include "Snake.h"


Snake::Snake()
{

}

Snake::~Snake()
{

}

void Snake::init()
{
    
    _x = 41;
    _y = 23;
    
    _next = W;
    
}

void Snake::update(Direction in, Direction cur) 
{
    
    set_snake_direction(in, cur);
    Direction next = get_snake_direction();
    set_snake_position(next);
    Vector2D pos = get_snake_position();
    
}

void Snake::draw(N5110 &lcd)
{
    
    lcd.setPixel(_x, _y, true);
    
}

void Snake::set_snake_direction(Direction input, Direction current)
{
    

    
    switch(current) {
        
        case CENTRE:
        
            switch(input) {
                
                case CENTRE:
                
                _next = current;
                
                break;
                
                case N:
                
                _next = input;
                
                break;
                
                case E:
                
                _next = input;
                
                break;
                
                case S:
                
                _next = input;
                
                break;
                
                case W:
                
                _next = input;
                
                break;
                
                default:
                
                _next = current;
                error("Invalid state"); 
                
                break;
                
                }
                
        case N:
        
            switch(input) {
            
                case CENTRE:
                
                _next = current;
                
                break;
                
                case N:
                
                _next = input;
                
                break;
                
                case E:
                
                _next = input;
                
                break;
                
                case S:
                
                _next = current;
                
                break;
                
                case W:
                
                _next = input;
                
                break;
                
                default:
                
                _next = current;
                error("Invalid state"); 
                
                break;
                
                }
        case E:
        
            switch(input) {
            
                case CENTRE:
                
                _next = current;
                
                break;
                
                case N:
                
                _next = input;
                
                break;
                
                case E:
                
                _next = current;
                
                break;
                
                case S:
                
                _next = current;
                
                break;
                
                case W:
                
                _next = input;
                
                break;
                
                default:
                
                _next = current;
                error("Invalid state"); 
                
                break;
                
                }
        case S:
        
            switch(input) {
            
                case CENTRE:
                
                _next = current;
                
                break;
                
                case N:
                
                _next = current;
                
                break;
                
                case E:
                
                _next = input;
                
                break;
                
                case S:
                
                _next = current;
                
                break;
                
                case W:
                
                _next = input;
                
                break;
                
                default:
                
                _next = current;
                error("Invalid state"); 
                
                break;
                
                }
        case W:
        
            switch(input) {
            
                case CENTRE:
                
                _next = current;
                
                break;
                
                case N:
                
                _next = input;
                
                break;
                
                case E:
                
                _next = current;
                
                break;
                
                case S:
                
                _next = input;
                
                break;
                
                case W:
                
                _next = current;
                
                break;
                
                default:
                
                _next = current;
                error("Invalid state"); 
                
                break;
                
                }
            }
            
                                
}

Direction Snake::get_snake_direction()
{
    
    Direction d = _next;
    
    return d;
    
}

void Snake::set_snake_position(Direction next)
{
    next = get_snake_direction();
    Vector2D p;
    
    switch(next) {
        
            case(N):
        
            p.x = p.x;
            --p.y;
        
            break;
        
            case(E):
        
            ++p.x;
            p.y = p.y;
        
            break;
        
            case(S):
        
            p.x = p.x;
            ++p.y;
        
            break;
        
            case(W):
        
            --p.x;
            p.y = p.y;
        
            break;
            
            default:
            
            p.x = p.x;
            p.y = p.y;
            
            break;
            
            }
            
            _x = p.x;
            _y = p.y;
    
}

Vector2D Snake::get_snake_position()
{
    Vector2D pos = {_x, _y};
    
    return pos; 
}

 
 // FSM Attempt
 
 /* 
    int state = N;
     
    StateDirection fsm[4] = {
        
    {N,{N,E,N,W}},  
    {E,{N,E,S,E}},  
    {S,{S,E,S,W}}, 
    {W,{N,W,S,W}} 
     
};

    _direction = fsm[state].output;  // set ouput depending on current state
    state = fsm[state].nextState[input];  // read input and update curent state
      */