1
Clutter = imports.gi.Clutter;
2
GLib = imports.gi.GLib;
7
Settings = imports.Settings;
8
gettext = imports.gettext;
12
parent: Clutter.Group.type,
17
var lights = [], all_lights = [];
19
var animating = false;
22
// TODO: when a click is rejected, queue it up, like in the C version
24
function done_animating()
31
function _connected_lights(li)
33
if(!li || li.visited || li.get_closed())
36
var x = li.get_light_x();
37
var y = li.get_light_y();
43
// while(GLib.main_context_pending())
44
// GLib.main_context_iteration();
46
var a = [], b = [], c = [], d = [];
48
if(lights[x][y+1] && (li.get_state() == lights[x][y+1].get_state()))
49
a = _connected_lights(lights[x][y+1]);
51
if(lights[x][y-1] && (li.get_state() == lights[x][y-1].get_state()))
52
b = _connected_lights(lights[x][y-1]);
54
if(lights[x+1] && lights[x+1][y] &&
55
(li.get_state() == lights[x+1][y].get_state()))
56
c = _connected_lights(lights[x+1][y]);
58
if(lights[x-1] && lights[x-1][y] &&
59
(li.get_state() == lights[x-1][y].get_state()))
60
d = _connected_lights(lights[x-1][y]);
62
return con.concat(a,b,c,d);
65
function connected_lights(li)
67
for(var i in all_lights)
68
all_lights[i].visited = false;
70
if(!li.get_light_x) // We're picking something other than a light!
73
return _connected_lights(li);
76
function light_lights_from(li)
80
var cl = connected_lights(li);
93
function light_entered(actor, event)
95
if(actor === last_light)
100
var lights_lit = light_lights_from(actor).length;
101
var new_score = Score.calculate_score(lights_lit);
102
var score_string = _("No points");
105
score_string = Seed.sprintf(gettext.ngettext("%d point", "%d points", new_score), new_score);
107
main.message_label.label = score_string;
112
function light_left(actor, event)
114
var connected = connected_lights(actor);
116
for(var i in connected)
117
if(!connected[i].get_closed())
118
connected[i].opacity = 180;
123
function board_left()
125
for(var i in all_lights)
126
if(!all_lights[i].get_closed())
127
all_lights[i].opacity = 180;
129
main.message_label.label = "";
134
function colors_changed()
140
this.has_completed = function ()
142
for(var i in all_lights)
146
if(!li.get_closed() && (connected_lights(li).length > 1))
153
this.has_won = function ()
155
for(var i in all_lights)
166
this.get_lights = function ()
171
this.remove_region = function (actor, event)
176
var cl = connected_lights(actor);
181
main.message_label.label = "";
183
var close_timeline = new Clutter.Timeline({duration: 500});
186
cl[i].close_tile(close_timeline);
188
close_timeline.start();
190
var real_x = 0, timeline = 0;
194
anim_timeline = new Clutter.Timeline({duration: 500});
199
var good_lights = [];
207
good_lights.push(li);
212
lights[real_x] = good_lights.concat(bad_lights);
214
var empty_col = true;
216
for(y in lights[real_x])
218
li = lights[real_x][y];
220
li.set_light_x(real_x);
221
li.set_light_y(parseInt(y,10));
223
var new_x = real_x * main.tile_size + main.offset;
224
var new_y = (main.size_o.rows - y - 1) * main.tile_size + main.offset;
226
if(!li.get_closed() && ((new_x != li.x) ||
229
li.animate_to(new_x, new_y, anim_timeline);
235
// GLib.main_context_iteration();
238
// GLib.main_context_iteration();
244
anim_timeline.signal.completed.connect(done_animating);
245
anim_timeline.start();
247
for(; real_x < main.size_o.columns; real_x++)
248
lights[real_x] = null;
250
Score.increment_score(cl.length);
252
if(self.has_completed())
253
Score.game_completed(self.has_won())
255
cl = last_light = null;
260
this.new_game = function ()
262
var children = self.get_children();
264
for(var i in children)
265
self.remove_actor(children[i]);
267
if(Score.final_score)
268
Score.final_score.hide_score();
274
for(var x = 0; x < main.size_o.columns; x++)
277
for(var y = 0; y < main.size_o.rows; y++)
279
var li = new Light.Light();
284
li.set_position(x * main.tile_size + main.offset,
285
(main.size_o.rows - y - 1) * main.tile_size + main.offset);
287
li.signal.button_release_event.connect(self.remove_region);
288
li.signal.enter_event.connect(light_entered);
289
li.signal.leave_event.connect(light_left);
292
all_lights.push(lights[x][y]);
298
this.reactive = true;
300
// Enable & connect to widget leave signal
301
var gdkwindow = main.clutter_embed.get_window();
302
gdkwindow.set_events(gdkwindow.get_events() | Gdk.EventMask.LEAVE_NOTIFY_MASK);
303
main.clutter_embed.signal.leave_notify_event.connect(board_left);
305
Settings.Watcher.signal.colors_changed.connect(colors_changed);