1
0
Fork 0
lasertank-js/lib/MapAction.js

80 lines
1.9 KiB
JavaScript

/**
* Represents an action being performed on a game object
*
* Copyright (C) 2012 Mike Gerwitz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
ltjs.MapAction = Class( 'MapAction',
{
// arranged by keycode
'const D__MIN': 0,
'const D_LEFT': 0,
'const D_UP': 1,
'const D_RIGHT': 2,
'const D_DOWN': 3,
'const D__MAX': 3,
'private _dir': 0,
'private _stateCallback': null,
'private _moveCallback': null,
__construct: function( state_callback, move_callback )
{
this._dir = this.__self.$( 'D_UP' );
this._stateCallback = state_callback;
this._moveCallback = move_callback;
},
'public changeState': function( newstate )
{
this._stateCallback( newstate );
},
'public move': function()
{
this._moveCallback( this._dir );
},
set direction( todir )
{
todir = +todir;
// ensure valid direction was provided (use >= and <= check because
// todir could be NaN, etc)
if ( !( ( todir >= this.__self.$( 'D__MIN' ) )
&& ( todir <= this.__self.$( 'D__MAX' ) )
) )
{
throw Error( 'Invalid tank direction' );
}
this._dir = todir;
},
get direction()
{
return this._dir;
},
} );