1
# ----------------------------------------------------------------------------
3
# Copyright (c) 2008 Daniel Moisset, Ricardo Quesada, Rayentray Tappa, Lucio Torre
6
# Redistribution and use in source and binary forms, with or without
7
# modification, are permitted provided that the following conditions are met:
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
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
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
35
__docformat__ = 'restructuredtext'
38
from cocos.euclid import *
39
from basegrid_actions import *
40
from cocos.director import director
44
__all__ = [ 'FadeOutTRTiles', # actions that don't modify the z coordinate
53
'ShakyTiles3D', # actions that modify the z coordinate
59
# Don't export this class
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
68
return "(start_pos: %s pos: %s delta:%s)" % (self.start_position, self.position, self.delta)
70
class ShakyTiles3D( TiledGrid3DAction ):
71
'''Simulates a shaky floor composed of tiles
75
scene.do( ShakyTiles3D( randrange=6, grid=(4,4), duration=10) )
78
def init( self, randrange=6, *args, **kw ):
82
Number that will be used in random.randrange( -randrange, randrange) to do the effect
84
super(ShakyTiles3D,self).init(*args,**kw)
85
self.randrange = randrange
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)
98
self.set_tile(i,j,coords)
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.
108
scene.do( ShatteredTiles3D( randrange=12 ) )
111
def init( self, randrange=6, *args, **kw ):
115
Number that will be used in random.randrange( -randrange, randrange) to do the effect
117
super(ShatteredTiles3D,self).init(*args,**kw)
118
self.randrange = randrange
121
def update( self, t ):
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)
133
self.set_tile(i,j,coords)
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.
143
scene.do( ShuffleTiles( grid=(4,4), seed=1, duration=10) )
146
def init(self, seed=-1, *args, **kw):
150
Seed for the random in the shuffle.
152
super(ShuffleTiles,self).init(*args, **kw)
156
super(ShuffleTiles,self).start()
162
random.seed( self.seed )
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 )
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) )
175
def place_tile(self, i, j):
176
t = self.tiles[(i,j)]
177
coords = self.get_original_tile(i,j)
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)
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
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)
197
class FadeOutTRTiles( TiledGrid3DAction ):
198
'''Fades out each tile following a diagonal Top-Right path until all the tiles are faded out.
202
scene.do( FadeOutTRTiles( grid=(16,12), duration=10) )
205
def update( self, t ):
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)
212
self.turn_off_tile(i,j)
214
self.transform_tile(i,j,distance)
216
self.turn_on_tile(i,j)
218
def turn_on_tile(self, x,y):
219
self.set_tile(x,y, self.get_original_tile(x,y) )
221
def transform_tile(self, x, y, t ):
222
coords = self.get_original_tile(x,y)
223
for c in xrange( len(coords) ):
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)
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)
237
self.set_tile(x,y,coords)
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] )
243
def test_func(self, i,j, t ):
247
return pow( (i+j) / float(x+y), 6 )
249
class FadeOutBLTiles( FadeOutTRTiles):
250
'''Fades out each tile following an Bottom-Left path until all the tiles are faded out.
254
scene.do( FadeOutBLTiles( grid=(16,12), duration=5) )
257
def test_func(self, i,j,t):
258
x,y = self.grid * (1-t)
261
return pow( (x+y) / float(i+j), 6)
263
class FadeOutUpTiles( FadeOutTRTiles):
264
'''Fades out each tile following an upwards path until all the tiles are faded out.
268
scene.do( FadeOutUpTiles( grid=(16,12), duration=5) )
271
def test_func(self, i,j, t):
275
return pow( (j) / float(y), 6 )
277
def transform_tile(self, x, y, t ):
278
coords = self.get_original_tile(x,y)
279
for c in xrange( len(coords) ):
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)
287
self.set_tile(x,y,coords)
289
class FadeOutDownTiles( FadeOutUpTiles):
290
'''Fades out each tile following an downwards path until all the tiles are faded out.
294
scene.do( FadeOutDownTiles( grid=(16,12), duration=5) )
297
def test_func(self, i,j, t):
298
x,y = self.grid * (1-t)
301
return pow( (y) / float(j), 6 )
304
class TurnOffTiles( TiledGrid3DAction ):
305
'''TurnOffTiles turns off each in random order
309
scene.do( TurnOffTiles( grid=(16,12), seed=1, duration=10) )
312
def init(self, seed=-1, *args, **kw):
313
super(TurnOffTiles,self).init( *args, **kw )
317
super(TurnOffTiles,self).start()
320
random.seed( self.seed )
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 )
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]
331
self.turn_off_tile(t)
335
def get_tile_pos(self, idx):
336
return divmod(idx, self.grid.y)
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) )
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] )
346
class WavesTiles3D( TiledGrid3DAction ):
347
'''Simulates waves using the math.sin() function in the z-axis of each tile
351
scene.do( WavesTiles3D( waves=5, amplitude=120, grid=(16,16), duration=10) )
354
def init( self, waves=4, amplitude=120, *args, **kw ):
358
Number of waves (2 * pi) that the action will perform. Default is 4
360
Wave amplitude (height). Default is 20
362
super(WavesTiles3D, self).init( *args, **kw )
364
#: Total number of waves to perform
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
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)
380
z = (math.sin(t*math.pi*self.waves*2 + (y+x) * .01) * self.amplitude * self.amplitude_rate )
382
for k in xrange( 0,len(coords),3 ):
385
self.set_tile( i,j, coords )
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
394
scene.do( JumpTiles3D( jumps=5, amplitude=40, grid=(16,16), duration=10) )
397
def init( self, jumps=4, amplitude=20, *args, **kw ):
401
Number of jumps(2 * pi) that the action will perform. Default is 4
403
Wave amplitude (height). Default is 20
405
super(JumpTiles3D, self).init( *args, **kw )
407
#: Total number of jumps to perform
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
415
def update( self, t ):
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 )
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)
424
for k in xrange( 0,len(coords),3 ):
430
self.set_tile( i,j, coords )
433
class SplitRows( TiledGrid3DAction ):
434
'''Split the screen in a number of rows, and move
435
these rows away from the screen.
437
The odds rows are moved to the left, while the even rows are moved to
442
scene.do( SplitRows( rows=3, duration=2) )
445
def init( self, rows=9, grid=(-1,-1), *args, **kw ):
449
Number of rows that will have the effect. Default: 9
452
raise Exception("This action doesn't receives the grid argument")
456
super(SplitRows, self).init( grid, *args, **kw )
459
def update( self, t ):
461
x,y = director.get_window_size()
463
for j in xrange(0, self.grid.y):
464
coords = self.get_original_tile(0,j)
466
for c in xrange(0, len(coords), 3):
470
coords[c] += direction * x * t
472
self.set_tile( 0,j, coords )
475
class SplitCols( TiledGrid3DAction ):
476
'''Split the screen in a number of columns, and move
477
these columns away from the screen.
479
The odds columns are moved to the upwards, while the even
480
columns are moved to the downwards.
484
scene.do( SplitCols( cols=3, duration=2) )
487
def init( self, cols=9, grid=(-1,-1), *args, **kw ):
491
Number of columns that will have the effect. Default: 9
494
raise Exception("This action doesn't receives the grid argument")
498
super(SplitCols, self).init( grid, *args, **kw )
501
def update( self, t ):
503
x,y = director.get_window_size()
505
for i in xrange(0, self.grid.x):
506
coords = self.get_original_tile(i,0)
508
for c in xrange(0, len(coords), 3):
512
coords[c+1] += direction * y * t
514
self.set_tile( i,0, coords )