@@ -12,6 +12,7 @@ import {ContainerKeys} from "../../../inversify.config";
1212export class ContextMenuController extends BaseController {
1313
1414 private _selectedUnit : BaseUnit ;
15+ private _menuElement : HTMLElement ;
1516
1617 constructor (
1718 private _game : Game ,
@@ -20,23 +21,69 @@ export class ContextMenuController extends BaseController {
2021 @inject ( ContainerKeys . CONFIG ) private _config : GameConfig
2122 ) {
2223 super ( ) ;
24+ this . _menuElement = document . getElementById ( 'context-menu' ) ;
2325 }
2426
2527 preload ( ) {
26- this . _gameSubject . subscribe ( GameEvent . UnitSelected , unit => this . _selectedUnit = unit ) ;
27- this . _gameSubject . subscribe ( GameEvent . CancelAction , unit => this . _selectedUnit = null ) ;
28+ this . _gameSubject . subscribe ( GameEvent . UnitSelected , this . _onUnitSelected ) ;
29+ this . _gameSubject . subscribe ( GameEvent . CancelAction , this . _onCancelAction ) ;
30+ this . _gameSubject . subscribe ( GameEvent . UnitAttackActionSelected , this . _hideMenu ) ;
31+ this . _gameSubject . subscribe ( GameEvent . UnitWaitActionSelected , this . _hideMenu ) ;
2832 this . _inputSubject . subscribe ( InputEvent . KeyAttack , this . _onKeyAttack ) ;
33+ this . _inputSubject . subscribe ( InputEvent . KeyWait , this . _onKeyWait ) ;
34+
35+ //handle events from context menu
36+ window . addEventListener ( 'actionSelected' , e => {
37+ let action = e [ 'detail' ] [ 'action' ] ;
38+ switch ( action ) {
39+ case 'attack' :
40+ this . _gameSubject . dispatch ( GameEvent . UnitAttackActionSelected , this . _selectedUnit ) ;
41+ break ;
42+ case 'wait' :
43+ this . _gameSubject . dispatch ( GameEvent . UnitWaitActionSelected , this . _selectedUnit ) ;
44+ break ;
45+ case 'cancel' :
46+ this . _gameSubject . dispatch ( GameEvent . CancelAction ) ;
47+ break ;
48+ default :
49+ console . error ( 'Invalid action dispatched on actionSelected' ) ;
50+ }
51+ } ) ;
2952 }
3053
54+ private _hideMenu = ( ) : void => {
55+ this . _menuElement . style . display = 'none' ;
56+ } ;
57+
3158 // ------------------------------------
3259 // ---------- EVENT HANDLERS ----------
3360 // ------------------------------------
3461
62+ private _onUnitSelected = ( unit : BaseUnit ) : void => {
63+ if ( unit . hasActedThisTurn )
64+ return ;
65+
66+ this . _selectedUnit = unit ;
67+
68+ this . _menuElement . style . display = 'block' ;
69+ this . _menuElement . style . left = `${ unit . spr . x + 50 } px` ;
70+ this . _menuElement . style . top = `${ unit . spr . y - 80 } px` ;
71+ } ;
72+
73+ private _onCancelAction = ( ) : void => {
74+ this . _selectedUnit = null ;
75+ this . _hideMenu ( ) ;
76+ } ;
77+
3578 private _onKeyAttack = ( ) : void => {
36- //TODO: eventually we only want to dispatch this if it is a VALID time to initiate attack. I put it in here because attack will be the menu
37- // TODO: maybe this should be moved to unit controller? or perhaps action selected events belong here
3879 if ( ! ! this . _selectedUnit ) {
3980 this . _gameSubject . dispatch ( GameEvent . UnitAttackActionSelected , this . _selectedUnit ) ;
4081 }
4182 } ;
83+
84+ private _onKeyWait = ( ) : void => {
85+ if ( ! ! this . _selectedUnit ) {
86+ this . _gameSubject . dispatch ( GameEvent . UnitWaitActionSelected , this . _selectedUnit ) ;
87+ }
88+ } ;
4289}
0 commit comments