-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.js
177 lines (162 loc) · 5.37 KB
/
application.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* Startup script for TicTacToe.
*
* Copyright UNC Open Web Team 2010. All Rights Reserved.
*/
dojo.provide('ttt.Main');
dojo.require('dijit.layout.BorderContainer');
dojo.require('dijit.layout.ContentPane');
dojo.require('dojo.parser');
dojo.require('dojo.hash');
dojo.require('ttt.GameBoardAudio');
dojo.require('ttt.GameBoardView');
dojo.require('ttt.GameTurnView');
dojo.require('ttt.GameBoardModel');
dojo.require('ttt.GameBoardMouse');
dojo.require('ttt.GameBoardKeys');
dojo.require('ttt.GameTopics');
dojo.declare('ttt.Main', null, {
constructor: function() {
// audio api
this._audio = null;
// game widgets
this._gameWidgets = [];
// last known preferences
this._prefs = {};
// @todo: first player, alternates between games
this._player = 0;
// listen for pause notices from container
dojo.subscribe('/org/hark/pause', this, '_onPause');
// listen for preference changes from container
dojo.subscribe('/org/hark/prefs/response', this, '_onPrefChange');
// listen for game reset
dojo.subscribe(ttt.CTRL_RESET_GAME, this, 'resetGame');
// build an audio instance with caching enabled
var def = uow.getAudio({defaultCaching: true});
// when audio is ready, kick off a game reset
def.addCallback(dojo.hitch(this, '_onAudioReady'));
},
_onAudioReady: function(audio) {
this._audio = audio;
// ask for initial prefs
dojo.publish('/org/hark/prefs/request');
// kick off the game
this.resetGame();
},
_onPrefChange: function(prefs, name) {
this._prefs = prefs;
if(name) {
var value = prefs[name];
var slug = name[0].toUpperCase() + name.slice(1)
this['_on'+slug](value);
}
},
_onPause: function(paused) {
dojo.forEach(this._gameWidgets, function(widget) {
if(widget.pause) {
widget.pause(paused);
}
});
},
_onMouseEnabled: function(value) {
if(this._mctrl) {
this._mctrl.destroyRecursive();
this._mctrl = null;
} else {
var mctrl = new ttt.GameBoardMouse({model : 'game', view : 'board'});
this._mctrl = mctrl;
mctrl.placeAt(dojo.body(), 'last');
this._gameWidgets.push(mctrl);
}
},
_onSpeechEnabled: function(value) {
if(!value) {
// just mute it
this._audio.setProperty({
name : 'volume',
value: 0.0,
channel: 'tttSpeech'
});
} else {
// restore volume
this._onSpeechVolume(this._prefs.speechVolume);
}
},
_onSpeechRate: function(value) {
this._audio.setProperty({
name : 'rate',
value : value,
channel : 'tttSpeech'
});
},
_onVolume: function(value) {
// master volume multiplier against individual volumes
this._audio.setProperty({
name : 'volume',
value: value*this._prefs.speechVolume,
channel: 'tttSpeech'
});
this._audio.setProperty({
name : 'volume',
value: value*this._prefs.soundVolume,
channel: 'tttSound'
});
},
_onSpeechVolume: function(value) {
this._audio.setProperty({
name : 'volume',
value: value*this._prefs.volume,
channel: 'tttSpeech'
});
},
_onSoundVolume: function(value) {
this._audio.setProperty({
name : 'volume',
value: value*this._prefs.volume,
channel: 'tttSound'
});
},
resetGame: function() {
// destroy existing game widgets
dojo.forEach(this._gameWidgets, function(widget) {
widget.destroyRecursive();
});
this._gameWidgets = [];
// fetch layout widgets in markup
var layout = dijit.byId('layout');
var h = dojo.hash();
var size = 3;
if(h) {
size = Number(h.split('x')[0]);
}
// build all new game components
var model = new ttt.GameBoardModel({id : 'game', size : size});
model.placeAt(dojo.body(), 'first');
this._gameWidgets.push(model);
var bview = new ttt.GameBoardView({
model : 'game',
region: 'center',
id : 'board'
});
bview.placeAt(layout, 1);
this._gameWidgets.push(bview);
var tview = new ttt.GameTurnView({model : 'game', region: 'bottom'});
tview.placeAt(layout, 'last');
this._gameWidgets.push(tview);
var aview = new ttt.GameBoardAudio({model : 'game', audio: this._audio});
aview.placeAt(dojo.body(), 'last');
this._gameWidgets.push(aview);
if(this._prefs.mouseEnabled) {
var mctrl = new ttt.GameBoardMouse({model : 'game', view : 'board'});
this._mctrl = mctrl;
mctrl.placeAt(dojo.body(), 'last');
this._gameWidgets.push(mctrl);
}
var kctrl = new ttt.GameBoardKeys({model : 'game', view : 'board'});
kctrl.placeAt(dojo.body(), 'last');
this._gameWidgets.push(kctrl);
}
});
dojo.ready(function() {
var app = new ttt.Main();
});