forked from joshclick/CanaClone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunner.m
executable file
·189 lines (150 loc) · 5.33 KB
/
Runner.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// Runner.m
// CanaClone
//
// Created by Josh Click on 12/31/12.
// Copyright 2012 __MyCompanyName__. All rights reserved.
//
#import "Runner.h"
#import "BuildingsLayer.h"
@implementation Runner
@synthesize isTouched;
-(void)changeState:(CharacterStates)newState
{
[self stopAllActions];
id action = nil;
//id movementAction = nil;
//CGPoint newPosition;
characterState = newState;
switch (newState) {
case kStateRunning:
action = [CCAnimate actionWithAnimation:runningAnim];
break;
case kStateJumping:
action = [CCAnimate actionWithAnimation:jumpingAnim];
break;
case kStateFalling:
action = [CCAnimate actionWithAnimation:fallingAnim];
break;
case kStateRolling:
action = [CCAnimate actionWithAnimation:rollingAnim];
break;
}
if (action) {
if (newState == kStateRunning)
//[self runAction:[CCRepeatForever actionWithAction:action]];
[self runAction:action];
else
[self runAction:action];
}
}
#pragma mark -
-(void)updateStateWithDeltaTime:(ccTime)deltaTime currentPlatHeight:(int)platHeight
{
minPos = platHeight;
if ((characterState == kStateRunning) ||
(characterState == kStateRolling)) {
if (isTouched) {
[self changeState:kStateJumping];
if (velocity.y == 0) velocity.y = 4.5f; //initial jump speed
}
}
if (velocity.y > 3.2 && isTouched) { //if in process of jumping but below max speed
velocity.y += 0.40f;
}
if (self.position.y > minPos) { //if above platform level
velocity.y += gravity; //apply gravity
if ([self numberOfRunningActions] == 0)
[self changeState:kStateFalling];
}
[self addYPosition:velocity.y * deltaTime * 50];
if (self.position.y < minPos) { //if below platform level
[self stopAllActions];
[self setPosition:ccp(self.position.x, minPos)];
if (velocity.y < -11) //if downward speed fast enough, roll animation
[self changeState:kStateRolling];
velocity.y = 0;
}
if ([self numberOfRunningActions] == 0) {
if (self.position.y == minPos)
[self changeState:kStateRunning];
}
}
- (void)addYPosition:(CGFloat)yDelta
{
[self setPosition:ccp(self.position.x, self.position.y + yDelta)];
}
- (void)initAnimations {
runningAnim = [self loadPlistForAnimationWithName:@"runningAnim"
andClassName:NSStringFromClass([self class])];
jumpingAnim = [self loadPlistForAnimationWithName:@"jumpingAnim"
andClassName:NSStringFromClass([self class])];
fallingAnim = [self loadPlistForAnimationWithName:@"fallingAnim"
andClassName:NSStringFromClass([self class])];
rollingAnim = [self loadPlistForAnimationWithName:@"rollingAnim"
andClassName:NSStringFromClass([self class])];
}
-(CCAnimation*)loadPlistForAnimationWithName:(NSString*)animationName
andClassName:(NSString*)className
{
CCAnimation *animationToReturn = nil;
NSString *fullFileName = [NSString stringWithFormat:@"%@.plist",className];
NSString *plistPath;
// 1: Get the Path to the plist file
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES)
objectAtIndex:0];
plistPath = [rootPath stringByAppendingPathComponent:fullFileName];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
plistPath = [[NSBundle mainBundle] pathForResource:className
ofType:@"plist"];
}
// 2: Read in the plist file
NSDictionary *plistDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
// 3: If the plistDictionary was null, the file was not found.
if (plistDictionary == nil) {
CCLOG(@"Error reading plist: %@.plist", className);
return nil; // No Plist Dictionary or file found
}
// 4: Get just the mini-dictionary for this animation
NSDictionary *animationSettings = [plistDictionary objectForKey:animationName];
if (animationSettings == nil) {
CCLOG(@"Could not locate AnimationWithName:%@",animationName);
return nil;
}
// 5: Get the delay value for the animation
float animationDelay = [[animationSettings objectForKey:@"delay"] floatValue];
animationToReturn = [CCAnimation animation];
[animationToReturn setDelayPerUnit:animationDelay];
// 6: Add the frames to the animation
NSString *animationFramePrefix = [animationSettings objectForKey:@"filenamePrefix"];
NSString *animationFrames = [animationSettings objectForKey:@"animationFrames"];
NSArray *animationFrameNumbers = [animationFrames componentsSeparatedByString:@","];
//[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Runner_Atlas.plist"];
//CCSpriteBatchNode *animBatch = [CCSpriteBatchNode batchNodeWithFile:@"Runner_Atlas.png"];
for (NSString *frameNumber in animationFrameNumbers) {
NSString *frameName = [NSString stringWithFormat:@"%@%@.png",
animationFramePrefix,
frameNumber];
[animationToReturn addSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache]
spriteFrameByName:frameName]];
}
return animationToReturn;
}
- (id)initWithSpriteFrame:(CCSpriteFrame*)spriteFrame
{
if (self = [super initWithSpriteFrame:(CCSpriteFrame *)spriteFrame])
{
gravity = -0.50f;
maxVelocity.x = 1000;
maxVelocity.y = 360;
velocity = ccp(0,0);
minPos = 100;
isTouched = NO;
[self initAnimations];
[self runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:runningAnim]]];
}
return self;
}
@end