Platform game written for the GHI/OutrageousCircuits RETRO game device. Navigate the caves collecting all the pickups and avoiding the creatures and haunted mine carts that patrol the caves. Oh and remember to watch out for the poisonous plants... This game demonstrates the ability to have multiple animated sprites where the sprites can overlap the background environment. See how the player moves past the fence and climbs the wall in the 3rd screen.

Dependencies:   mbed

Player.h

Committer:
taylorza
Date:
2014-12-27
Revision:
3:a93fe5f207f5
Parent:
2:97d01ba6cd91
Child:
4:45ff7fc8a431

File content as of revision 3:a93fe5f207f5:

CREATE_EFFECT(effect1)
    TONE(40, 1400, 16384, -20077, 128, 0),    
END_EFFECT

class Player : public GameObject
{
public:
    enum State { Stopped, Walking, Jumping, Falling, OnLadder };
    
    Player(Game &game) : 
        GameObject(game),
        _state(Stopped),
        _jumpY(0),
        _jumpX(0)
    {
        setCollisionRect(2, 0, 11, 16);
    }

    void alignX()
    {
        if (getX() % 8 != 0)
        {
            int8_t dx = getLastXDirection();
            if (dx < 0) moveLeft();
            else if (dx > 0) moveRight();
        }        
    }
    
    virtual void update()
    {      
        if (_state == OnLadder && !isLadderBelow() ) _state = Stopped;
        if (_state == OnLadder) setSpriteId(1);
        else setSpriteId(0);
        
        if (_state != Jumping && _state != Falling)
        {
            if (_state != OnLadder)
            {
                if (GameInput::isLeftPressed()) 
                {                         
                    _state = moveLeft() ? Walking : Stopped;                
                    animate();       
                }
                else if (GameInput::isRightPressed()) 
                { 
                    _state = moveRight() ? Walking : Stopped;                
                    animate(); 
                }
            }
            
            if (GameInput::isCirclePressed()) 
            { 
                if (GameInput::isLeftPressed()) _jumpX = -1;
                else if (GameInput::isRightPressed()) _jumpX = 1;
                else _jumpX = 0;
                
                if (_state != OnLadder || _jumpX != 0)
                {
                    _state = Jumping;
                    _jumpY = 0;   
                    PLAY_EFFECT(effect1);   
                }            
            }
            
            if (GameInput::isUpPressed())
            {
                if (isLadderAbove() || _state == OnLadder)
                {
                    if(moveUp())
                    {
                        _state = OnLadder;  
                        animate();              
                    }
                    else
                    {
                        _state = Stopped;
                    }
                }
            }
            else if (GameInput::isDownPressed())
            {
                if (isLadderBelow() || _state == OnLadder)
                {
                    if(moveDown())
                    {
                        _state = OnLadder;                
                        animate();
                    } 
                    else
                    {
                        _state = Stopped;
                    }
                }
            }        
        }
        
        if (_state != Jumping && _state != OnLadder)
        {     
            if (fall())
            {                
                _state = Falling; 
                alignX();                            
            }
            else
            {
                _state = Stopped;                                               
            }            
        }
                        
        if (_state == Jumping)
        {
            if (_jumpY < 10 && moveUp())
            {            
                ++_jumpY;
            }                                    
            else if (!moveDown())
            {                
                _state = Stopped;
                _jumpY = 0;
            }
            
            if (_state != Stopped)
            {
                if (_jumpX < 0) { moveLeft(); }
                else if (_jumpX > 0) { moveRight(); }    
                animate();
            }
        }              
    }
    
    virtual void draw()
    {
        GameObject::draw();
        
        if (detectCollision() != NULL || detectBlock()->getType() == Block::Deadly)
        {
            Game::Surface.drawRect(getX(), getY(), getX() + 15, getY() + 15, Color565::Green);
        }        
    }
    
private:
    State   _state;
    int8_t  _jumpY;     
    int8_t  _jumpX;
};