2013-01-17 23:56:40 -05:00
|
|
|
/**
|
|
|
|
* 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,
|
2013-01-20 10:20:51 -05:00
|
|
|
'private _srcPos': 0,
|
|
|
|
'public srcPos': 0,
|
|
|
|
|
|
|
|
'private _bounds': null,
|
2013-01-17 23:56:40 -05:00
|
|
|
|
|
|
|
'private _moveCallback': null,
|
|
|
|
|
|
|
|
|
2013-01-21 23:04:33 -05:00
|
|
|
__construct: function( bounds, move_callback )
|
2013-01-17 23:56:40 -05:00
|
|
|
{
|
2013-01-20 10:20:51 -05:00
|
|
|
if ( !( Class.isA( ltjs.MapBounds, bounds ) ) )
|
|
|
|
{
|
|
|
|
throw TypeError( 'Invalid MapBounds provided' );
|
|
|
|
}
|
|
|
|
|
2013-01-17 23:56:40 -05:00
|
|
|
this._dir = this.__self.$( 'D_UP' );
|
|
|
|
|
2013-01-20 10:20:51 -05:00
|
|
|
this._bounds = bounds;
|
2013-01-17 23:56:40 -05:00
|
|
|
this._moveCallback = move_callback;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
'public move': function()
|
|
|
|
{
|
2013-01-20 10:20:51 -05:00
|
|
|
var method = [
|
|
|
|
'getLeftPos',
|
|
|
|
'getUpperPos',
|
|
|
|
'getRightPos',
|
|
|
|
'getLowerPos',
|
|
|
|
][ this._dir ];
|
|
|
|
|
|
|
|
this._moveCallback(
|
|
|
|
this._bounds[ method ].call( this._bounds, this.srcPos )
|
|
|
|
);
|
2013-01-17 23:56:40 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
} );
|