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
# ----------------------------------------------------------------------------
38
There are 2 kinds of grids:
40
- `Grid3D` : A 3D grid with x,y and z coordinates
41
- `TiledGrid3D` : A 3D grid with x,y and z coordinates, composed
42
with independent tiles
45
Hence, there are 2 kinds of grid actions:
50
The `Grid3DAction` can modify any of vertex of the grid in any direction (x,y or z).
51
The `TiledGrid3DAction` can modify any tile of the grid without modifying the adjacent tiles.
53
To understand visually the difference between these 2 kinds of grids, try these examples:
55
- run ``test/test_shakytiles3d.py`` to see a `TiledGrid3DAction` example
56
- run ``test/test_shaky3d.py`` to see the `Grid3DAction` counterpart
59
__docformat__ = 'restructuredtext'
61
from pyglet.gl import *
63
from cocos.grid import Grid3D, TiledGrid3D
64
from cocos.director import director
65
from cocos.euclid import *
66
from base_actions import *
68
__all__ = [ 'GridException', # Grid Exceptions
69
'GridBaseAction', # Base classes
73
'AccelAmplitude', # Amplitude modifiers
75
'AccelDeccelAmplitude',
81
class GridException( Exception ):
84
class GridBaseAction( IntervalAction ):
85
'''GridBaseAction is the base class of all Grid Actions.'''
86
def init( self, grid=(4,4), duration=5):
87
"""Initialize the Grid Action
91
Number of horizontal and vertical quads in the grid
93
Number of seconds that the action will last
95
self.duration = duration
96
if not isinstance(grid,Point2):
101
new_grid = self.get_grid()
103
if self.target.grid and self.target.grid.reuse_grid > 0:
105
if self.target.grid.active \
106
and self.grid == self.target.grid.grid \
107
and type(new_grid) == type(self.target.grid):
108
# since we are reusing the grid,
109
# we must "cheat" the action that the original vertex coords are
110
# the ones that were inhereted.
111
self.target.grid.vertex_points = self.target.grid.vertex_list.vertices[:]
112
self.target.grid.reuse_grid -= 1
113
self.target.grid.reuse_grid = max(0, self.target.grid.reuse_grid)
115
# condition are not met
116
raise GridException("Cannot reuse grid. class grid or grid size did not match: %s vs %s and %s vs %s"
117
% ( str(self.grid), str(self.target.grid.grid), type(new_grid), type(self.target.grid) ) )
119
# Not reusing the grid
120
if self.target.grid and self.target.grid.active:
121
self.target.grid.active = False
122
self.target.grid = new_grid
123
self.target.grid.init( self.grid )
124
self.target.grid.active = True
126
x,y = director.get_window_size()
127
self.size_x = x // self.grid.x
128
self.size_y = y // self.grid.y
130
def __reversed__(self):
131
return _ReverseTime(self)
134
class Grid3DAction( GridBaseAction ):
135
'''Action that does transformations
136
to a 3D grid ( `Grid3D` )'''
141
def get_vertex( self, x, y):
142
'''Get the current vertex coordinate
150
:rtype: (float, float, float)
152
return self.target.grid.get_vertex(x,y)
154
def get_original_vertex( self, x, y):
155
'''Get the original vertex coordinate.
156
The original vertices are the ones weren't modified by the current action.
164
:rtype: (float, float, float)
166
return self.target.grid.get_original_vertex(x,y)
169
def set_vertex( self, x, y, v):
170
'''Set a vertex point is a certain value
177
`v` : (float, float, float)
178
tuple value for the vertex
180
return self.target.grid.set_vertex(x,y,v)
183
class TiledGrid3DAction( GridBaseAction ):
184
'''Action that does transformations
185
to a grid composed of tiles ( `TiledGrid3D` ).
186
You can transform each tile individually'''
191
def set_tile(self, x, y, coords):
192
'''Set the 4 tile coordinates
194
Coordinates positions::
203
x coodinate of the tile
205
y coordinate of the tile
206
`coords` : [ float, float, float, float, float, float, float, float, float, float, float, float ]
207
The 4 coordinates in the format (x0, y0, z0, x1, y1, z1,..., x3, y3, z3)
209
return self.target.grid.set_tile(x,y,coords)
211
def get_original_tile(self, x, y):
212
'''Get the 4-original tile coordinates.
214
Coordinates positions::
223
x coordinate of the tile
225
y coordinate of the tile
227
:rtype: [ float, float, float, float, float, float, float, float, float, float, float, float ]
228
:returns: The 4 coordinates with the following order: x0, y0, z0, x1, y1, z1,...,x3, y3, z3
230
return self.target.grid.get_original_tile(x,y)
232
def get_tile(self, x, y):
233
'''Get the current tile coordinates.
235
Coordinates positions::
244
x coordinate of the tile
246
y coordinate of the tile
248
:rtype: [ float, float, float, float, float, float, float, float, float, float, float, float ]
249
:returns: The 4 coordinates with the following order: x0, y0, z0, x1, y1, z1,...,x3, y3, z3
251
return self.target.grid.get_tile(x,y)
254
class AccelDeccelAmplitude( IntervalAction ):
256
Increases and Decreases the amplitude of Wave
260
# when t=0 and t=1 the amplitude will be 0
261
# when t=0.5 (half time), the amplitude will be 40
262
action = AccellDeccelAmplitude( Wave3D( waves=4, amplitude=40, duration=6) )
265
def init(self, other, rate=1.0 ):
269
`other` : `IntervalAction`
270
The action that will be affected
272
The acceleration rate. 1 is linear (default value)
274
if not hasattr(other,'amplitude'):
275
raise GridAction("Invalid Composition: IncAmplitude needs an action with amplitude")
279
self.duration = other.duration
282
self.other.target = self.target
291
self.other.amplitude_rate = f**self.rate
292
self.other.update( t )
294
def __reversed__(self):
295
return AccelDeccelAmplitude( Reverse(self.other) )
297
class AccelAmplitude( IntervalAction ):
299
Increases the waves amplitude from 0 to self.amplitude
303
# when t=0 the amplitude will be 0
304
# when t=1 the amplitude will be 40
305
action = AccelAmplitude( Wave3D( waves=4, amplitude=40, duration=6), rate=1.0 )
308
def init(self, other, rate=1 ):
312
`other` : `IntervalAction`
313
The action that will be affected
315
The acceleration rate. 1 is linear (default value)
317
if not hasattr(other,'amplitude'):
318
raise GridAction("Invalid Composition: IncAmplitude needs an action with amplitude")
321
self.duration = other.duration
325
self.other.target = self.target
329
self.other.amplitude_rate = (t ** self.rate)
330
self.other.update( t )
332
def __reversed__(self):
333
return DeccelAmplitude( Reverse(self.other), rate=self.rate )
336
class DeccelAmplitude( AccelAmplitude ):
338
Decreases the waves amplitude from self.amplitude to 0
342
# when t=1 the amplitude will be 0
343
# when t=0 the amplitude will be 40
344
action = DeccelAmplitude( Wave3D( waves=4, amplitude=40, duration=6), rate=1.0 )
349
self.other.amplitude_rate = ((1-t) ** self.rate)
350
self.other.update( t )
352
def __reversed__(self):
353
return AccelAmplitude( Reverse(self.other), rate=self.rate )
356
class StopGrid( InstantAction ):
357
"""StopGrid disables the current grid.
358
Every grid action, after finishing, leaves the screen with a certain grid figure.
359
This figure will be displayed until StopGrid or another Grid action is executed.
363
scene.do( Waves3D( duration=2) + StopGrid() )
366
if self.target.grid and self.target.grid.active:
367
self.target.grid.active = False
369
class ReuseGrid( InstantAction ):
370
"""Will reuse the current grid for the next grid action.
371
The next grid action must have these properties:
373
- Be of the same class as the current one ( `Grid3D` or `TiledGrid3D` )
376
If these condition are met, then the next grid action will receive as the ``original vertex``
377
or ``original tiles`` the current ones.
381
scene.do( Waves3D( duration=2) + ReuseGrid() + Lens3D(duration=2) )
383
def init(self, reuse_times=1):
387
Number of times that the current grid will be reused by Grid actions. Default: 1
389
self.reuse_times = reuse_times
391
if self.target.grid and self.target.grid.active:
392
self.target.grid.reuse_grid += self.reuse_times
394
raise GridException("ReuseGrid must be used when a grid is still active")