~ubuntu-branches/ubuntu/precise/gnome-games/precise-updates

« back to all changes in this revision

Viewing changes to swell-foop/src/Board.js

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2012-03-19 20:46:10 UTC
  • mfrom: (1.1.105)
  • Revision ID: package-import@ubuntu.com-20120319204610-2nd2xqq39j8y7t5q
Tags: 1:3.3.92-0ubuntu1
* New upstream release.
  - Swell Foop ported to Vala, no longer in staging
    (LP: #939200, LP: #939210)
* debian/patches/git_fix-iagno-ai.patch: Dropped, upstream
* debian/control.in:
  - Drop no longer needed swell-foop dependencies
* debian/rules:
  - Don't install staging games
* debian/swell-foop.install:
  - Don't install gir files any more; they're not needed

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Clutter = imports.gi.Clutter;
2
 
GLib = imports.gi.GLib;
3
 
Gdk = imports.gi.Gdk;
4
 
Light = imports.Light;
5
 
Score = imports.Score;
6
 
main = imports.main;
7
 
Settings = imports.Settings;
8
 
gettext = imports.gettext;
9
 
_ = gettext.gettext;
10
 
 
11
 
Board = new GType({
12
 
        parent: Clutter.Group.type,
13
 
        name: "Board",
14
 
        init: function(self)
15
 
        {
16
 
                // Private
17
 
                var lights = [], all_lights = [];
18
 
                var last_light;
19
 
                var animating = false;
20
 
                var anim_timeline;
21
 
                
22
 
                // TODO: when a click is rejected, queue it up, like in the C version
23
 
                
24
 
                function done_animating()
25
 
                {
26
 
                        animating = false;
27
 
                        
28
 
                        return false;
29
 
                }
30
 
                
31
 
                function _connected_lights(li)
32
 
                {
33
 
                        if(!li || li.visited || li.get_closed())
34
 
                                return [ ];
35
 
                        
36
 
                        var x = li.get_light_x();
37
 
                        var y = li.get_light_y();
38
 
                        
39
 
                        li.visited = true;
40
 
                        
41
 
                        var con = [li];
42
 
                        
43
 
                        // while(GLib.main_context_pending())
44
 
                        //      GLib.main_context_iteration();
45
 
                        
46
 
                        var a = [], b = [], c = [], d = [];
47
 
                        
48
 
                        if(lights[x][y+1] && (li.get_state() == lights[x][y+1].get_state()))
49
 
                                a = _connected_lights(lights[x][y+1]);
50
 
                        
51
 
                        if(lights[x][y-1] && (li.get_state() == lights[x][y-1].get_state()))
52
 
                                b = _connected_lights(lights[x][y-1]);
53
 
                        
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]);
57
 
                        
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]);
61
 
                        
62
 
                        return con.concat(a,b,c,d);
63
 
                }
64
 
                
65
 
                function connected_lights(li)
66
 
                {
67
 
                        for(var i in all_lights)
68
 
                                all_lights[i].visited = false;
69
 
                        
70
 
                        if(!li.get_light_x) // We're picking something other than a light!
71
 
                                return [ li ];
72
 
                        
73
 
                        return _connected_lights(li);
74
 
                }
75
 
                
76
 
                function light_lights_from(li)
77
 
                {
78
 
                        var i;
79
 
                        
80
 
                        var cl = connected_lights(li);
81
 
                        
82
 
                        if(cl.length < 2)
83
 
                                return false;
84
 
                        
85
 
                        for(i in cl)
86
 
                                cl[i].opacity = 255;
87
 
                        
88
 
                        oldcl = cl;
89
 
                        
90
 
                        return cl;
91
 
                }
92
 
                
93
 
                function light_entered(actor, event)
94
 
                {
95
 
                        if(actor === last_light)
96
 
                                return false;
97
 
                        
98
 
                        last_light = actor;
99
 
                        
100
 
                        var lights_lit = light_lights_from(actor).length;
101
 
                        var new_score = Score.calculate_score(lights_lit);
102
 
                        var score_string = _("No points");
103
 
                        
104
 
                        if(new_score > 0)
105
 
                                score_string = Seed.sprintf(gettext.ngettext("%d point", "%d points", new_score), new_score);
106
 
                        
107
 
                        main.message_label.label = score_string;
108
 
                        
109
 
                        return false;
110
 
                }
111
 
                
112
 
                function light_left(actor, event)
113
 
                {
114
 
                        var connected = connected_lights(actor);
115
 
                        
116
 
                        for(var i in connected)
117
 
                                if(!connected[i].get_closed())
118
 
                                        connected[i].opacity = 180;
119
 
                        
120
 
                        return false;
121
 
                }
122
 
                
123
 
                function board_left()
124
 
                {
125
 
                        for(var i in all_lights)
126
 
                                if(!all_lights[i].get_closed())
127
 
                                        all_lights[i].opacity = 180;
128
 
                        
129
 
                        main.message_label.label = "";
130
 
                        
131
 
                        return false;
132
 
                }
133
 
                
134
 
                function colors_changed()
135
 
                {
136
 
                        self.new_game();
137
 
                }
138
 
                
139
 
                // Public
140
 
                this.has_completed = function ()
141
 
                {
142
 
                        for(var i in all_lights)
143
 
                        {
144
 
                                li = all_lights[i];
145
 
                                
146
 
                                if(!li.get_closed() && (connected_lights(li).length > 1))
147
 
                                        return false;
148
 
                        }
149
 
                        
150
 
                        return true;
151
 
                };
152
 
                
153
 
                this.has_won = function ()
154
 
                {
155
 
                        for(var i in all_lights)
156
 
                        {
157
 
                                li = all_lights[i];
158
 
                                
159
 
                                if(!li.get_closed())
160
 
                                        return false;
161
 
                        }
162
 
                        
163
 
                        return true;
164
 
                };
165
 
                
166
 
                this.get_lights = function ()
167
 
                {
168
 
                        return lights;
169
 
                };
170
 
                
171
 
                this.remove_region = function (actor, event)
172
 
                {
173
 
                        if(animating)
174
 
                                return false;
175
 
                                                
176
 
                        var cl = connected_lights(actor);
177
 
                        
178
 
                        if(cl.length < 2)
179
 
                                return false;
180
 
                                
181
 
                        main.message_label.label = "";
182
 
                        
183
 
                        var close_timeline = new Clutter.Timeline({duration: 500});
184
 
                        
185
 
                        for(var i in cl)
186
 
                                cl[i].close_tile(close_timeline);
187
 
                        
188
 
                        close_timeline.start();
189
 
                        
190
 
                        var real_x = 0, timeline = 0;
191
 
                        
192
 
                        animating = true;
193
 
                        
194
 
                        anim_timeline = new Clutter.Timeline({duration: 500});
195
 
                        
196
 
                        for(var x in lights)
197
 
                        {
198
 
                                var y, li;
199
 
                                var good_lights = [];
200
 
                                var bad_lights = [];
201
 
                                
202
 
                                for(y in lights[x])
203
 
                                {
204
 
                                        li = lights[x][y];
205
 
                                        
206
 
                                        if(!li.get_closed())
207
 
                                                good_lights.push(li);
208
 
                                        else
209
 
                                                bad_lights.push(li);
210
 
                                }
211
 
                                
212
 
                                lights[real_x] = good_lights.concat(bad_lights);
213
 
                                
214
 
                                var empty_col = true;
215
 
                                
216
 
                                for(y in lights[real_x])
217
 
                                {
218
 
                                        li = lights[real_x][y];
219
 
                                        
220
 
                                        li.set_light_x(real_x);
221
 
                                        li.set_light_y(parseInt(y,10));
222
 
                                        
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;
225
 
                                        
226
 
                                        if(!li.get_closed() && ((new_x != li.x) ||
227
 
                                                                (new_y != li.y)))
228
 
                                        {
229
 
                                                li.animate_to(new_x, new_y, anim_timeline);
230
 
                                        }
231
 
                                        
232
 
                                        if(!li.get_closed())
233
 
                                                empty_col = false;
234
 
                                        
235
 
                                        // GLib.main_context_iteration();
236
 
                                }
237
 
                                
238
 
                                // GLib.main_context_iteration();
239
 
                                
240
 
                                if(!empty_col)
241
 
                                        real_x++;
242
 
                        }
243
 
                
244
 
                        anim_timeline.signal.completed.connect(done_animating);
245
 
                        anim_timeline.start();
246
 
                        
247
 
                        for(; real_x < main.size_o.columns; real_x++)
248
 
                                lights[real_x] = null;
249
 
                        
250
 
                        Score.increment_score(cl.length);
251
 
                        
252
 
                        if(self.has_completed())
253
 
                                Score.game_completed(self.has_won())
254
 
                        
255
 
                        cl = last_light = null;
256
 
                        
257
 
                        return false;
258
 
                };
259
 
                
260
 
                this.new_game = function ()
261
 
                {
262
 
                        var children = self.get_children();
263
 
                        
264
 
                        for(var i in children)
265
 
                                self.remove_actor(children[i]);
266
 
                        
267
 
                        if(Score.final_score)
268
 
                                Score.final_score.hide_score();
269
 
                        
270
 
                        Score.set_score(0);
271
 
                        
272
 
                        all_lights = [];
273
 
                        
274
 
                        for(var x = 0; x < main.size_o.columns; x++)
275
 
                        {
276
 
                                lights[x] = [];
277
 
                                for(var y = 0; y < main.size_o.rows; y++)
278
 
                                {
279
 
                                        var li = new Light.Light();
280
 
                                
281
 
                                        li.set_light_x(x);
282
 
                                        li.set_light_y(y);
283
 
                                
284
 
                                        li.set_position(x * main.tile_size + main.offset,
285
 
                                                                        (main.size_o.rows - y - 1) * main.tile_size + main.offset);
286
 
                                        self.add_actor(li);
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);
290
 
                                
291
 
                                        lights[x][y] = li;
292
 
                                        all_lights.push(lights[x][y]);
293
 
                                }
294
 
                        }
295
 
                };
296
 
                
297
 
                // Implementation
298
 
                this.reactive = true;
299
 
                
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);
304
 
                
305
 
                Settings.Watcher.signal.colors_changed.connect(colors_changed);
306
 
        }
307
 
});
308