Skip to content

Instantly share code, notes, and snippets.

@LearnCocos2D
Last active December 11, 2015 07:58
Show Gist options
  • Save LearnCocos2D/4569474 to your computer and use it in GitHub Desktop.
Save LearnCocos2D/4569474 to your computer and use it in GitHub Desktop.
KTAction interface with KTMoveAction example from KoboldTouch. Simple, readable code. Actions are reusable. Checks for illegal uses like running the same action on multiple objects and re-running the same action on the same object will automatically restart the action. A specialized variant of actions called "abilities" will be added soon: they …
/** Actions act on (influence, change properties, etc.) a specific model. The defining property of actions is that whatever
they're supposed to do eventually ends, which will remove the action from the model automatically. For example moving the
model to a specific location - upon arrival at the destination the action removes itself from the model and its memory is released.
Note: KoboldTouch actions are quite similar to CCAction* classes. The most notable difference is that they act on KTEntityModel
objects, never on views directly, to conform to the MVC programming model.
Caution: It is illegal to re-run the same action on the same or a different model object while the action is running. You can
however re-run the action after it has been stopped.
*/
@interface KTAction : NSObject
{
@protected
__weak KTEntityModel* _model;
float _elapsedTime;
float _duration;
@private
}
/** The KTEntityModel object this action is associated with. Will be nil if the action isn't running. */
@property (nonatomic, weak, readonly) KTEntityModel* model;
/** Check if the action is still running. If it is not running, it is safe to re-run the action on the same or a different model. */
@property (nonatomic, readonly) BOOL isRunning;
/** How long the action will run before it stops automatically (in seconds). */
@property (nonatomic, readonly) float duration;
/** How much time has elapsed since the action started running (in seconds). */
@property (nonatomic, readonly) float elapsedTime;
/// Internal use
-(id) initWithDuration:(float)duration;
-(void) startWithModel:(KTEntityModel*)model;
-(void) update:(KTStepInfo*)stepInfo;
-(void) stop;
-(void) setup;
-(void) updateWithDeltaTime:(float)deltaTime;
@end
/** Changes the position property of the KTEntityModel until it has arrived at the target location. */
@interface KTMoveAction : KTAction
{
@private
CGPoint _startPosition;
CGPoint _destination;
CGPoint _deltaPosition;
float _speed;
BOOL _destinationIsOffset;
}
/** Create a move action with duration (in seconds) and a destination position (in points). Similar to CCMoveTo. */
+(id) actionWithDuration:(float)duration destination:(CGPoint)destination;
/** Create a move action with duration (in seconds) and a position offset (in points). Similar to CCMoveBy. */
+(id) actionWithDuration:(float)duration offset:(CGPoint)offset;
/** Create a move action with a speed (in points per frame) and a destination position (in points). Similar to CCMoveTo. */
+(id) actionWithSpeed:(float)speed destination:(CGPoint)destination;
/** Create a move action with a speed (in points per frame) and a position offset (in points). Similar to CCMoveBy. */
+(id) actionWithSpeed:(float)speed offset:(CGPoint)offset;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment