/*
* $Id: game.js 178 2010-08-10 19:33:41Z victor $
*/

tdWorld.game = {

    started: false,
    characterCreated: false,
    world : null,
    welcome: null,
    paused: false,
    gameStarted: false,
    resourcesLoaded: false,

    init: function()
    {
        this.loadResources();
        
        if( !this.started )
        {
            this.started = true;
            
            this.world = new tooDeeWorld(
                                {
                                    canvasId: 'CANVAS',
                                    pixelPerMeter: 42
                                });
        
            tdWorld.browser.addKeyHandler( function(e) { this.Trigger('keystroke',[e]); }, this.world );
            
            this.world.Bind('keystroke',this.onKey,this);
            
            var me = this, timer = null;
            function checkForResources()
            {
                if( me.resourcesLoaded )
                {
                    me.doWelcomeScreen();
                }
                else
                {
                    setTimeout(checkForResources,500);
                }
            }
            
            checkForResources();
        }
        
        return this.world;
    },
    
    onKey: function(e)
    {
        if( this.gameStarted && e.keyCode == 32 )
        {
            if( this.paused )
            {
                this.world.Running(true);
                this.world.Run();
                this.paused = false;
            }
            else
            {
                this.world.Stepping(true);
                this.paused = true;
            }
        }
    },
    
    createCharacter: function()
    {
        if( !this.characterCreated )
        {
            var world = this.world;
            
            world.AddSprite( new tooDeeMark( { world: world,
                                               name: 'mark.start',
                                               center: { x: 3.5, y: 3 },
                                               len: 2 }))
            
            world.AddSprite( new tooDeeCouple( { world: world,
                                                 zOrder: 5,
                                                 center: new tooDeeVec2( 0, 3 ) }));
            
            this.characterCreated = true;
        }
    },

    doWelcomeScreen: function()
    {
        var world = this.world;
        
        this.welcome = world.AddSprite( new tooDeeWelcome( { world: world } ) );
        this.welcome.Bind( 'titleDone', this.createCharacter, this, 1 );
        
        tdWorld.browser.addKeyHandler(this.startFade,this);
    },

    startFade: function()
    {
        this.welcome.FadeOut(this.startGame,this);
        tdWorld.browser.removeKeyHandler(this.startFade);
    },

    startGame: function()
    {
        if( this.welcome )
        {
            this.world.RemoveSprite( this.welcome );
            this.welcome.Destroy();
            this.welcome = null;
        }
        
        this.createCharacter();
                    
        this.world.waitingSpouse = this.world.AddSprite( new tooDeeWaitingSpouse( { world: this.world } ) );
    
        var tileMan = new tooDeeTileManager();
        tileMan._populate();
        var tiles = this.world.tiles = new tooDeeTiles( {world: this.world, tileManager: tileMan });        
        this.world.AddSprite( tiles );
        
        this.gameStarted = true;        
    },
    
    loadResources: function()
    {
        if( localStorage['figure.remix'] )
        {
            this.resourcesLoaded = true;
        }
        else
        {
            function loadFigures()
            {
                for( var name in BFP.figure )
                {
                    localStorage.setItem('figure.' + name,BFP.figure[name]);
                }
                
                this.resourcesLoaded = true;
            }
            
            tdWorld.browser.fetchScript('/scripts/animations.defs.js',loadFigures,this);
        }
    }

};
