summary refs log tree commit diff stats
path: root/libs/cocos2d/CCMotionStreak.m
diff options
context:
space:
mode:
Diffstat (limited to 'libs/cocos2d/CCMotionStreak.m')
-rwxr-xr-xlibs/cocos2d/CCMotionStreak.m104
1 files changed, 104 insertions, 0 deletions
diff --git a/libs/cocos2d/CCMotionStreak.m b/libs/cocos2d/CCMotionStreak.m new file mode 100755 index 0000000..42737b9 --- /dev/null +++ b/libs/cocos2d/CCMotionStreak.m
@@ -0,0 +1,104 @@
1/*
2 * cocos2d for iPhone: http://www.cocos2d-iphone.org
3 *
4 * Copyright (c) 2008, 2009 Jason Booth
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 *
25 *********************************************************
26 *
27 * Motion Streak manages a Ribbon based on it's motion in absolute space.
28 * You construct it with a fadeTime, minimum segment size, texture path, texture
29 * length and color. The fadeTime controls how long it takes each vertex in
30 * the streak to fade out, the minimum segment size it how many pixels the
31 * streak will move before adding a new ribbon segement, and the texture
32 * length is the how many pixels the texture is stretched across. The texture
33 * is vertically aligned along the streak segemnts.
34 */
35
36#import "CCMotionStreak.h"
37#import "Support/CGPointExtension.h"
38
39@implementation CCMotionStreak
40
41@synthesize ribbon = ribbon_;
42
43+(id)streakWithFade:(float)fade minSeg:(float)seg image:(NSString*)path width:(float)width length:(float)length color:(ccColor4B)color
44{
45 return [[[self alloc] initWithFade:(float)fade minSeg:seg image:path width:width length:length color:color] autorelease];
46}
47
48-(id)initWithFade:(float)fade minSeg:(float)seg image:(NSString*)path width:(float)width length:(float)length color:(ccColor4B)color
49{
50 if( (self=[super init])) {
51 segThreshold_ = seg;
52 width_ = width;
53 lastLocation_ = CGPointZero;
54 ribbon_ = [CCRibbon ribbonWithWidth:width_ image:path length:length color:color fade:fade];
55 [self addChild:ribbon_];
56
57 // update ribbon position. Use schedule:interval and not scheduleUpdated. issue #1075
58 [self schedule:@selector(update:) interval:0];
59 }
60 return self;
61}
62
63-(void)update:(ccTime)delta
64{
65 CGPoint location = [self convertToWorldSpace:CGPointZero];
66 [ribbon_ setPosition:ccp(-1*location.x, -1*location.y)];
67 float len = ccpLength(ccpSub(lastLocation_, location));
68 if (len > segThreshold_)
69 {
70 [ribbon_ addPointAt:location width:width_];
71 lastLocation_ = location;
72 }
73 [ribbon_ update:delta];
74}
75
76
77-(void)dealloc
78{
79 [super dealloc];
80}
81
82#pragma mark MotionStreak - CocosNodeTexture protocol
83
84-(void) setTexture:(CCTexture2D*) texture
85{
86 [ribbon_ setTexture: texture];
87}
88
89-(CCTexture2D*) texture
90{
91 return [ribbon_ texture];
92}
93
94-(ccBlendFunc) blendFunc
95{
96 return [ribbon_ blendFunc];
97}
98
99-(void) setBlendFunc:(ccBlendFunc)blendFunc
100{
101 [ribbon_ setBlendFunc:blendFunc];
102}
103
104@end