~facundo/enjuewemela/trunk

« back to all changes in this revision

Viewing changes to cocos/actions/tiledgrid_actions.py

  • Committer: facundo at com
  • Date: 2010-11-20 01:42:31 UTC
  • mfrom: (62.1.3 lint-issues)
  • Revision ID: facundo@taniquetil.com.ar-20101120014231-b2tkyc3mwr84ggcc
Project reorder and lint issues

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# ----------------------------------------------------------------------------
 
2
# cocos2d
 
3
# Copyright (c) 2008 Daniel Moisset, Ricardo Quesada, Rayentray Tappa, Lucio Torre
 
4
# All rights reserved.
 
5
#
 
6
# Redistribution and use in source and binary forms, with or without
 
7
# modification, are permitted provided that the following conditions are met:
 
8
#
 
9
#   * Redistributions of source code must retain the above copyright
 
10
#     notice, this list of conditions and the following disclaimer.
 
11
#   * Redistributions in binary form must reproduce the above copyright 
 
12
#     notice, this list of conditions and the following disclaimer in
 
13
#     the documentation and/or other materials provided with the
 
14
#     distribution.
 
15
#   * Neither the name of cocos2d nor the names of its
 
16
#     contributors may be used to endorse or promote products
 
17
#     derived from this software without specific prior written
 
18
#     permission.
 
19
#
 
20
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
21
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
22
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 
23
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 
24
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 
25
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 
26
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
27
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
28
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
29
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 
30
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
31
# POSSIBILITY OF SUCH DAMAGE.
 
32
# ----------------------------------------------------------------------------
 
33
'''Implementation of TiledGrid3DAction actions
 
34
'''
 
35
__docformat__ = 'restructuredtext'
 
36
 
 
37
import random
 
38
from cocos.euclid import *
 
39
from basegrid_actions import *
 
40
from cocos.director import director
 
41
 
 
42
rr = random.randrange
 
43
 
 
44
__all__ = [ 'FadeOutTRTiles',             # actions that don't modify the z coordinate
 
45
           'FadeOutBLTiles',
 
46
           'FadeOutUpTiles',
 
47
           'FadeOutDownTiles',
 
48
           'ShuffleTiles',
 
49
           'TurnOffTiles',
 
50
           'SplitRows',
 
51
           'SplitCols',
 
52
 
 
53
           'ShakyTiles3D',              # actions that modify the z coordinate
 
54
           'ShatteredTiles3D',
 
55
           'WavesTiles3D',
 
56
           'JumpTiles3D',
 
57
           ]
 
58
 
 
59
# Don't export this class
 
60
class Tile(object):
 
61
    def __init__(self, position=(0,0), start_position=(0,0), delta=(0,0) ):
 
62
        super(Tile,self).__init__()
 
63
        self.position = position
 
64
        self.start_position = start_position
 
65
        self.delta = delta
 
66
 
 
67
    def __repr__(self):
 
68
        return "(start_pos: %s  pos: %s   delta:%s)" % (self.start_position, self.position, self.delta)
 
69
 
 
70
class ShakyTiles3D( TiledGrid3DAction ):
 
71
    '''Simulates a shaky floor composed of tiles
 
72
 
 
73
    Example::
 
74
    
 
75
       scene.do( ShakyTiles3D( randrange=6, grid=(4,4), duration=10) )
 
76
    '''
 
77
 
 
78
    def init( self, randrange=6, *args, **kw ):
 
79
        '''
 
80
        :Parameters:
 
81
            `randrange` : int
 
82
                Number that will be used in random.randrange( -randrange, randrange) to do the effect
 
83
        '''
 
84
        super(ShakyTiles3D,self).init(*args,**kw)
 
85
        self.randrange = randrange
 
86
 
 
87
    def update( self, t ):
 
88
        for i in xrange(0, self.grid.x):
 
89
            for j in xrange(0, self.grid.y):
 
90
                    coords = self.get_original_tile(i,j)   
 
91
                    for k in xrange(0,len(coords),3):
 
92
                        x = rr(-self.randrange, self.randrange+1)
 
93
                        y = rr(-self.randrange, self.randrange+1)
 
94
                        z = rr(-self.randrange, self.randrange+1)
 
95
                        coords[k] += x
 
96
                        coords[k+1] += y
 
97
                        coords[k+2] += z
 
98
                    self.set_tile(i,j,coords)
 
99
                
 
100
 
 
101
class ShatteredTiles3D( TiledGrid3DAction ):
 
102
    '''ShatterTiles shatters the tiles according to a random value.
 
103
    It is similar to shakes (see `ShakyTiles3D`) the tiles just one frame, and then continue with
 
104
    that state for duration time.
 
105
    
 
106
    Example::
 
107
    
 
108
        scene.do( ShatteredTiles3D( randrange=12 ) )
 
109
    '''
 
110
 
 
111
    def init( self, randrange=6, *args, **kw ):
 
112
        '''
 
113
        :Parameters:
 
114
            `randrange` : int
 
115
                Number that will be used in random.randrange( -randrange, randrange) to do the effect
 
116
        '''
 
117
        super(ShatteredTiles3D,self).init(*args,**kw)
 
118
        self.randrange = randrange
 
119
        self._once = False
 
120
 
 
121
    def update( self, t ):
 
122
        if not self._once:
 
123
            for i in xrange(0, self.grid.x):
 
124
                for j in xrange(0, self.grid.y):
 
125
                    coords = self.get_original_tile(i,j)   
 
126
                    for k in xrange(0,len(coords),3):
 
127
                        x = rr(-self.randrange, self.randrange+1)
 
128
                        y = rr(-self.randrange, self.randrange+1)
 
129
                        z = rr(-self.randrange, self.randrange+1)
 
130
                        coords[k] += x
 
131
                        coords[k+1] += y
 
132
                        coords[k+2] += z
 
133
                    self.set_tile(i,j,coords)
 
134
            self._once = True
 
135
                
 
136
 
 
137
class ShuffleTiles( TiledGrid3DAction ):
 
138
    '''ShuffleTiles moves the tiles randomly across the screen.
 
139
    To put them back use: Reverse( ShuffleTiles() ) with the same seed parameter.
 
140
 
 
141
    Example::
 
142
 
 
143
       scene.do( ShuffleTiles( grid=(4,4), seed=1, duration=10) )
 
144
    '''
 
145
 
 
146
    def init(self, seed=-1, *args, **kw):
 
147
        '''
 
148
        :Parameters:
 
149
            `seed` : float
 
150
                Seed for the random in the shuffle.
 
151
        '''
 
152
        super(ShuffleTiles,self).init(*args, **kw)
 
153
        self.seed = seed
 
154
        
 
155
    def start(self):
 
156
        super(ShuffleTiles,self).start()
 
157
 
 
158
        self.tiles = {}
 
159
        self._once = False
 
160
 
 
161
        if self.seed != -1:
 
162
            random.seed( self.seed )
 
163
 
 
164
        # random positions
 
165
        self.nr_of_tiles = self.grid.x * self.grid.y
 
166
        self.tiles_order = range(self.nr_of_tiles )
 
167
        random.shuffle( self.tiles_order )
 
168
 
 
169
        for i in xrange(self.grid.x):
 
170
            for j in xrange(self.grid.y):
 
171
                self.tiles[(i,j)] = Tile( position = Point2(i,j), 
 
172
                                          start_position = Point2(i,j), 
 
173
                                          delta= self._get_delta(i,j) )
 
174
 
 
175
    def place_tile(self, i, j):
 
176
        t = self.tiles[(i,j)]
 
177
        coords = self.get_original_tile(i,j)
 
178
 
 
179
        for k in xrange(0,len(coords),3):                      
 
180
            coords[k] += int( t.position.x * self.target.grid.x_step )
 
181
            coords[k+1] += int( t.position.y * self.target.grid.y_step )
 
182
        self.set_tile(i,j,coords)
 
183
        
 
184
    def update(self, t ):
 
185
        for i in xrange(0, self.grid.x):
 
186
            for j in xrange(0, self.grid.y):
 
187
                self.tiles[(i,j)].position = self.tiles[(i,j)].delta * t
 
188
                self.place_tile(i,j)
 
189
                
 
190
    # private method
 
191
    def _get_delta(self, x, y):      
 
192
        idx = x * self.grid.y + y
 
193
        i,j = divmod( self.tiles_order[idx], self.grid.y )
 
194
        return Point2(i,j)-Point2(x,y)
 
195
 
 
196
 
 
197
class FadeOutTRTiles( TiledGrid3DAction ):
 
198
    '''Fades out each tile following a diagonal Top-Right path until all the tiles are faded out.
 
199
    
 
200
    Example::
 
201
    
 
202
       scene.do( FadeOutTRTiles( grid=(16,12), duration=10) )
 
203
    '''
 
204
 
 
205
    def update( self, t ):
 
206
                
 
207
        # direction right - up
 
208
        for i in xrange(self.grid.x):
 
209
            for j in xrange(self.grid.y):
 
210
                distance = self.test_func(i,j,t)
 
211
                if distance == 0:
 
212
                    self.turn_off_tile(i,j)
 
213
                elif distance < 1:
 
214
                    self.transform_tile(i,j,distance)
 
215
                else:
 
216
                    self.turn_on_tile(i,j)
 
217
 
 
218
    def turn_on_tile(self, x,y):
 
219
        self.set_tile(x,y, self.get_original_tile(x,y) )
 
220
 
 
221
    def transform_tile(self, x, y, t ):
 
222
        coords = self.get_original_tile(x,y)
 
223
        for c in xrange( len(coords) ):
 
224
 
 
225
            # x
 
226
            if c == 0*3 or c == 3*3:
 
227
                coords[c] = coords[c] + (self.target.grid.x_step / 2.0) * (1-t)
 
228
            elif c == 1*3 or c == 2*3:
 
229
                coords[c] = coords[c] - (self.target.grid.x_step / 2.0) * (1-t)
 
230
 
 
231
            # y
 
232
            if c == 0*3+1 or c == 1*3+1:
 
233
                coords[c] = coords[c] + (self.target.grid.y_step / 2.0) * (1-t)
 
234
            elif c == 2*3+1 or c == 3*3+1:
 
235
                coords[c] = coords[c] - (self.target.grid.y_step / 2.0) * (1-t)
 
236
 
 
237
        self.set_tile(x,y,coords)
 
238
 
 
239
 
 
240
    def turn_off_tile( self,x,y):
 
241
        self.set_tile(x,y,[0,0,0,0,0,0,0,0,0,0,0,0] )
 
242
 
 
243
    def test_func(self, i,j, t ):
 
244
        x,y = self.grid * t
 
245
        if x+y==0:
 
246
            return 1 
 
247
        return pow( (i+j) / float(x+y), 6 )
 
248
 
 
249
class FadeOutBLTiles( FadeOutTRTiles):
 
250
    '''Fades out each tile following an Bottom-Left path until all the tiles are faded out.
 
251
    
 
252
    Example::
 
253
    
 
254
       scene.do( FadeOutBLTiles( grid=(16,12), duration=5) )
 
255
    '''
 
256
 
 
257
    def test_func(self, i,j,t):
 
258
        x,y = self.grid * (1-t)
 
259
        if i+j==0:
 
260
            return 1 
 
261
        return pow( (x+y) / float(i+j), 6)
 
262
 
 
263
class FadeOutUpTiles( FadeOutTRTiles):
 
264
    '''Fades out each tile following an upwards path until all the tiles are faded out.
 
265
    
 
266
    Example::
 
267
    
 
268
       scene.do( FadeOutUpTiles( grid=(16,12), duration=5) )
 
269
    '''
 
270
 
 
271
    def test_func(self, i,j, t):
 
272
        x,y = self.grid * t
 
273
        if y==0:
 
274
            return 1 
 
275
        return pow( (j) / float(y), 6 )
 
276
 
 
277
    def transform_tile(self, x, y, t ):
 
278
        coords = self.get_original_tile(x,y)
 
279
        for c in xrange( len(coords) ):
 
280
 
 
281
            # y
 
282
            if c == 0*3+1 or c == 1*3+1:
 
283
                coords[c] = coords[c] + (self.target.grid.y_step / 2.0) * (1-t)
 
284
            elif c == 2*3+1 or c == 3*3+1:
 
285
                coords[c] = coords[c] - (self.target.grid.y_step / 2.0) * (1-t)
 
286
 
 
287
        self.set_tile(x,y,coords)
 
288
 
 
289
class FadeOutDownTiles( FadeOutUpTiles):
 
290
    '''Fades out each tile following an downwards path until all the tiles are faded out.
 
291
    
 
292
    Example::
 
293
    
 
294
       scene.do( FadeOutDownTiles( grid=(16,12), duration=5) )
 
295
    '''
 
296
 
 
297
    def test_func(self, i,j, t):
 
298
        x,y = self.grid * (1-t)
 
299
        if j==0:
 
300
            return 1 
 
301
        return pow( (y) / float(j), 6 )
 
302
 
 
303
 
 
304
class TurnOffTiles( TiledGrid3DAction ):
 
305
    '''TurnOffTiles turns off each in random order
 
306
    
 
307
    Example::
 
308
    
 
309
       scene.do( TurnOffTiles( grid=(16,12), seed=1, duration=10) )
 
310
    '''
 
311
 
 
312
    def init(self, seed=-1, *args, **kw):
 
313
        super(TurnOffTiles,self).init( *args, **kw )    
 
314
        self.seed = seed
 
315
        
 
316
    def start(self):
 
317
        super(TurnOffTiles,self).start()
 
318
 
 
319
        if self.seed != -1:
 
320
            random.seed( self.seed )
 
321
 
 
322
        self.nr_of_tiles = self.grid.x * self.grid.y
 
323
        self.tiles_order = range(self.nr_of_tiles )
 
324
        random.shuffle( self.tiles_order )
 
325
        
 
326
    def update( self, t ):
 
327
        l = int( t * self.nr_of_tiles )
 
328
        for i in xrange( self.nr_of_tiles):
 
329
            t = self.tiles_order[i]
 
330
            if i < l:
 
331
                self.turn_off_tile(t)
 
332
            else:
 
333
                self.turn_on_tile(t)
 
334
 
 
335
    def get_tile_pos(self, idx):
 
336
        return divmod(idx, self.grid.y)
 
337
    
 
338
    def turn_on_tile(self, t):
 
339
        x,y = self.get_tile_pos(t)
 
340
        self.set_tile(x,y, self.get_original_tile(x,y) )
 
341
 
 
342
    def turn_off_tile(self,t):
 
343
        x,y = self.get_tile_pos(t)
 
344
        self.set_tile(x,y,[0,0,0,0,0,0,0,0,0,0,0,0] )
 
345
 
 
346
class WavesTiles3D( TiledGrid3DAction ):
 
347
    '''Simulates waves using the math.sin() function in the z-axis of each tile
 
348
 
 
349
    Example::
 
350
    
 
351
       scene.do( WavesTiles3D( waves=5, amplitude=120, grid=(16,16), duration=10) )
 
352
    '''
 
353
 
 
354
    def init( self, waves=4, amplitude=120, *args, **kw ):
 
355
        '''
 
356
        :Parameters:
 
357
            `waves` : int
 
358
                Number of waves (2 * pi) that the action will perform. Default is 4
 
359
            `amplitude` : int
 
360
                Wave amplitude (height). Default is 20
 
361
        '''
 
362
        super(WavesTiles3D, self).init( *args, **kw )
 
363
 
 
364
        #: Total number of waves to perform
 
365
        self.waves=waves
 
366
  
 
367
        #: amplitude rate. Default: 1.0
 
368
        #: This value is modified by other actions like `AccelAmplitude`.
 
369
        self.amplitude_rate = 1.0
 
370
        self.amplitude=amplitude
 
371
 
 
372
    def update( self, t ):
 
373
        for i in xrange(0, self.grid.x):
 
374
            for j in xrange(0, self.grid.y):
 
375
                coords = self.get_original_tile(i,j)
 
376
 
 
377
                x = coords[0]
 
378
                y = coords[1]
 
379
 
 
380
                z = (math.sin(t*math.pi*self.waves*2 + (y+x) * .01) * self.amplitude * self.amplitude_rate )
 
381
 
 
382
                for k in xrange( 0,len(coords),3 ):
 
383
                    coords[k+2] += z
 
384
 
 
385
                self.set_tile( i,j, coords )
 
386
 
 
387
 
 
388
class JumpTiles3D( TiledGrid3DAction ):
 
389
    '''Odd tiles will perform a jump in the z-axis using the sine function,
 
390
    while the even tiles will perform a jump using sine+pi function
 
391
 
 
392
    Example::
 
393
    
 
394
       scene.do( JumpTiles3D( jumps=5, amplitude=40, grid=(16,16), duration=10) )
 
395
    '''
 
396
 
 
397
    def init( self, jumps=4, amplitude=20, *args, **kw ):
 
398
        '''
 
399
        :Parameters:
 
400
            `jumps` : int
 
401
                Number of jumps(2 * pi) that the action will perform. Default is 4
 
402
            `amplitude` : int
 
403
                Wave amplitude (height). Default is 20
 
404
        '''
 
405
        super(JumpTiles3D, self).init( *args, **kw )
 
406
 
 
407
        #: Total number of jumps to perform
 
408
        self.jumps=jumps
 
409
  
 
410
        #: amplitude rate. Default: 1.0
 
411
        #: This value is modified by other actions like `AccelAmplitude`.
 
412
        self.amplitude_rate = 1.0
 
413
        self.amplitude=amplitude
 
414
 
 
415
    def update( self, t ):
 
416
 
 
417
        sinz = (math.sin(t*math.pi*self.jumps*2 + (0) * .01) * self.amplitude * self.amplitude_rate )
 
418
        sinz2= (math.sin(math.pi+t*math.pi*self.jumps*2 + (0) * .01) * self.amplitude * self.amplitude_rate )
 
419
 
 
420
        for i in xrange(0, self.grid.x):
 
421
            for j in xrange(0, self.grid.y):
 
422
                coords = self.get_original_tile(i,j)
 
423
 
 
424
                for k in xrange( 0,len(coords),3 ):
 
425
                    if (i+j) % 2 == 0:
 
426
                        coords[k+2] += sinz
 
427
                    else:
 
428
                        coords[k+2] += sinz2
 
429
 
 
430
                self.set_tile( i,j, coords )
 
431
 
 
432
 
 
433
class SplitRows( TiledGrid3DAction ):
 
434
    '''Split the screen in a number of rows, and move
 
435
    these rows away from the screen.
 
436
 
 
437
    The odds rows are moved to the left, while the even rows are moved to
 
438
    the right.
 
439
 
 
440
    Example::
 
441
    
 
442
       scene.do( SplitRows( rows=3, duration=2) )
 
443
    '''
 
444
 
 
445
    def init( self, rows=9, grid=(-1,-1), *args, **kw ):
 
446
        '''
 
447
        :Parameters:
 
448
            `rows` : int
 
449
                Number of rows that will have the effect. Default: 9
 
450
        '''
 
451
        if grid != (-1,-1):
 
452
            raise Exception("This action doesn't receives the grid argument")
 
453
 
 
454
        grid = (1,rows)
 
455
        self.rows = rows
 
456
        super(SplitRows, self).init( grid, *args, **kw )
 
457
 
 
458
 
 
459
    def update( self, t ):
 
460
 
 
461
        x,y = director.get_window_size()
 
462
 
 
463
        for j in xrange(0, self.grid.y):
 
464
            coords = self.get_original_tile(0,j)
 
465
 
 
466
            for c in xrange(0, len(coords), 3):
 
467
                direction = 1
 
468
                if j % 2 == 0:
 
469
                    direction = -1
 
470
                coords[c] += direction * x * t
 
471
 
 
472
            self.set_tile( 0,j, coords )
 
473
 
 
474
 
 
475
class SplitCols( TiledGrid3DAction ):
 
476
    '''Split the screen in a number of columns, and move
 
477
    these columns away from the screen.
 
478
 
 
479
    The odds columns are moved to the upwards, while the even
 
480
    columns are moved to the downwards.
 
481
 
 
482
    Example::
 
483
    
 
484
       scene.do( SplitCols( cols=3, duration=2) )
 
485
    '''
 
486
 
 
487
    def init( self, cols=9, grid=(-1,-1), *args, **kw ):
 
488
        '''
 
489
        :Parameters:
 
490
            `cols` : int
 
491
                Number of columns that will have the effect. Default: 9
 
492
        '''
 
493
        if grid != (-1,-1):
 
494
            raise Exception("This action doesn't receives the grid argument")
 
495
 
 
496
        grid = (cols,1)
 
497
        self.cols = cols
 
498
        super(SplitCols, self).init( grid, *args, **kw )
 
499
 
 
500
 
 
501
    def update( self, t ):
 
502
 
 
503
        x,y = director.get_window_size()
 
504
 
 
505
        for i in xrange(0, self.grid.x):
 
506
            coords = self.get_original_tile(i,0)
 
507
 
 
508
            for c in xrange(0, len(coords), 3):
 
509
                direction = 1
 
510
                if i % 2 == 0:
 
511
                    direction = -1
 
512
                coords[c+1] += direction * y * t
 
513
 
 
514
            self.set_tile( i,0, coords )