2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __ACTIONS_CCACTION_H__
|
|
|
|
#define __ACTIONS_CCACTION_H__
|
|
|
|
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCObject.h"
|
|
|
|
#include "CCGeometry.h"
|
|
|
|
#include "CCPlatformMacros.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2012-06-20 18:07:27 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup actions
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2010-09-28 18:27:33 +08:00
|
|
|
/**
|
2013-06-20 14:13:12 +08:00
|
|
|
@brief Base class for Action objects.
|
2010-07-20 14:42:56 +08:00
|
|
|
*/
|
2013-06-23 17:22:23 +08:00
|
|
|
class CC_DLL Action : public Object, public Clonable
|
2010-07-20 14:42:56 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-08-29 03:42:25 +08:00
|
|
|
/// Default tag used for all the actions
|
|
|
|
static const int INVALID_TAG = -1;
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-12-13 06:38:12 +08:00
|
|
|
virtual std::string description() const;
|
2010-07-20 14:42:56 +08:00
|
|
|
|
2013-06-14 08:25:14 +08:00
|
|
|
/** returns a clone of action */
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual Action* clone() const = 0;
|
2010-07-20 14:42:56 +08:00
|
|
|
|
2013-07-16 03:43:22 +08:00
|
|
|
/** returns a new action that performs the exactly the reverse action */
|
|
|
|
virtual Action* reverse() const = 0;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//! return true if the action has finished
|
2013-11-16 21:08:00 +08:00
|
|
|
virtual bool isDone() const;
|
2010-07-20 14:42:56 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//! called before the action start. It will also set the target.
|
2013-07-18 07:56:19 +08:00
|
|
|
virtual void startWithTarget(Node *target);
|
2010-07-20 14:42:56 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
|
|
|
called after the action has finished. It will set the 'target' to nil.
|
2010-09-29 17:39:45 +08:00
|
|
|
IMPORTANT: You should never call "[action stop]" manually. Instead, use: "target->stopAction(action);"
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-11-16 21:08:00 +08:00
|
|
|
virtual void stop();
|
2010-07-20 14:42:56 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//! called every frame with it's delta time. DON'T override unless you know what you are doing.
|
2012-06-08 13:55:28 +08:00
|
|
|
virtual void step(float dt);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
called once per frame. time a value between 0 and 1
|
|
|
|
|
|
|
|
For example:
|
|
|
|
- 0 means that the action just started
|
|
|
|
- 0.5 means that the action is in the middle
|
|
|
|
- 1 means that the action is over
|
|
|
|
*/
|
2012-06-08 13:55:28 +08:00
|
|
|
virtual void update(float time);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
inline Node* getTarget() const { return _target; }
|
2012-04-19 14:35:52 +08:00
|
|
|
/** The action will modify the target properties. */
|
2013-07-18 07:56:19 +08:00
|
|
|
inline void setTarget(Node *target) { _target = target; }
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
inline Node* getOriginalTarget() const { return _originalTarget; }
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Set the original target, since target can be nil.
|
2013-06-20 14:13:12 +08:00
|
|
|
Is the target that were used to run the action. Unless you are doing something complex, like ActionManager, you should NOT call this method.
|
2012-04-19 14:35:52 +08:00
|
|
|
The target is 'assigned', it is not 'retained'.
|
|
|
|
@since v0.8.2
|
|
|
|
*/
|
2013-11-16 21:08:00 +08:00
|
|
|
inline void setOriginalTarget(Node *originalTarget) { _originalTarget = originalTarget; }
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
inline int getTag() const { return _tag; }
|
|
|
|
inline void setTag(int tag) { _tag = tag; }
|
2010-07-20 14:42:56 +08:00
|
|
|
|
|
|
|
protected:
|
2013-11-22 09:06:06 +08:00
|
|
|
Action();
|
|
|
|
virtual ~Action();
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Node *_originalTarget;
|
2012-04-19 14:35:52 +08:00
|
|
|
/** The "target".
|
|
|
|
The target will be set with the 'startWithTarget' method.
|
|
|
|
When the 'stop' method is called, target will be set to nil.
|
|
|
|
The target is 'assigned', it is not 'retained'.
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
Node *_target;
|
2012-04-19 14:35:52 +08:00
|
|
|
/** The action tag. An identifier of the action */
|
2013-06-15 14:03:30 +08:00
|
|
|
int _tag;
|
2013-11-22 09:06:06 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
CC_DISALLOW_COPY_AND_ASSIGN(Action);
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
2010-09-28 18:27:33 +08:00
|
|
|
/**
|
|
|
|
@brief
|
|
|
|
Base class actions that do have a finite time duration.
|
2010-07-20 14:42:56 +08:00
|
|
|
Possible actions:
|
|
|
|
- An action with a duration of 0 seconds
|
|
|
|
- An action with a duration of 35.5 seconds
|
2010-09-28 18:27:33 +08:00
|
|
|
|
|
|
|
Infinite time actions are valid
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
class CC_DLL FiniteTimeAction : public Action
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
//! get duration in seconds of the action
|
2013-11-16 21:08:00 +08:00
|
|
|
inline float getDuration() const { return _duration; }
|
2012-04-19 14:35:52 +08:00
|
|
|
//! set duration in seconds of the action
|
2013-06-15 14:03:30 +08:00
|
|
|
inline void setDuration(float duration) { _duration = duration; }
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-16 03:43:22 +08:00
|
|
|
//
|
|
|
|
// Overrides
|
|
|
|
//
|
|
|
|
virtual FiniteTimeAction* reverse() const override = 0;
|
|
|
|
virtual FiniteTimeAction* clone() const override = 0;
|
2013-06-14 08:25:14 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
protected:
|
2013-11-22 09:06:06 +08:00
|
|
|
FiniteTimeAction()
|
|
|
|
: _duration(0)
|
|
|
|
{}
|
|
|
|
virtual ~FiniteTimeAction(){}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//! duration in seconds
|
2013-06-15 14:03:30 +08:00
|
|
|
float _duration;
|
2013-11-22 09:06:06 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
CC_DISALLOW_COPY_AND_ASSIGN(FiniteTimeAction);
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
class ActionInterval;
|
|
|
|
class RepeatForever;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2010-09-28 18:27:33 +08:00
|
|
|
/**
|
|
|
|
@brief Changes the speed of an action, making it take longer (speed>1)
|
2010-07-20 14:42:56 +08:00
|
|
|
or less (speed<1) time.
|
|
|
|
Useful to simulate 'slow motion' or 'fast forward' effect.
|
2013-06-20 14:13:12 +08:00
|
|
|
@warning This action can't be Sequenceable because it is not an IntervalAction
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
class CC_DLL Speed : public Action
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-07-16 03:43:22 +08:00
|
|
|
/** create the action */
|
2013-11-16 21:08:00 +08:00
|
|
|
static Speed* create(ActionInterval* action, float speed);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
inline float getSpeed(void) const { return _speed; }
|
2012-04-19 14:35:52 +08:00
|
|
|
/** alter the speed of the inner function in runtime */
|
2013-11-16 21:08:00 +08:00
|
|
|
inline void setSpeed(float speed) { _speed = speed; }
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
2013-11-16 21:08:00 +08:00
|
|
|
void setInnerAction(ActionInterval *action);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-23 20:36:41 +08:00
|
|
|
inline ActionInterval* getInnerAction() const { return _innerAction; }
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-16 03:43:22 +08:00
|
|
|
//
|
|
|
|
// Override
|
|
|
|
//
|
|
|
|
virtual Speed* clone() const override;
|
|
|
|
virtual Speed* reverse() const override;
|
2013-07-18 07:56:19 +08:00
|
|
|
virtual void startWithTarget(Node* target) override;
|
2013-07-16 03:43:22 +08:00
|
|
|
virtual void stop() override;
|
|
|
|
virtual void step(float dt) override;
|
2013-11-16 21:08:00 +08:00
|
|
|
virtual bool isDone() const override;
|
2013-07-16 03:43:22 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
protected:
|
2013-11-22 09:06:06 +08:00
|
|
|
Speed();
|
|
|
|
virtual ~Speed(void);
|
|
|
|
/** initializes the action */
|
|
|
|
bool initWithAction(ActionInterval *action, float speed);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
float _speed;
|
2013-06-20 14:13:12 +08:00
|
|
|
ActionInterval *_innerAction;
|
2013-11-22 09:06:06 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
CC_DISALLOW_COPY_AND_ASSIGN(Speed);
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
2010-09-28 18:27:33 +08:00
|
|
|
/**
|
2013-06-20 14:13:12 +08:00
|
|
|
@brief Follow is an action that "follows" a node.
|
2010-09-28 18:27:33 +08:00
|
|
|
|
|
|
|
Eg:
|
2013-08-01 20:55:43 +08:00
|
|
|
@code
|
2013-06-20 14:13:12 +08:00
|
|
|
layer->runAction(Follow::actionWithTarget(hero));
|
2013-08-01 20:55:43 +08:00
|
|
|
@endcode
|
2010-09-28 18:27:33 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Instead of using Camera as a "follower", use this action instead.
|
2010-09-28 18:27:33 +08:00
|
|
|
@since v0.99.2
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
class CC_DLL Follow : public Action
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-07-26 12:04:03 +08:00
|
|
|
/**
|
|
|
|
* Creates the action with a set boundary or with no boundary.
|
|
|
|
*
|
2013-08-01 20:55:43 +08:00
|
|
|
* @param followedNode The node to be followed.
|
2013-07-26 12:04:03 +08:00
|
|
|
* @param rect The boundary. If \p rect is equal to Rect::ZERO, it'll work
|
|
|
|
* with no boundary.
|
2013-07-16 03:43:22 +08:00
|
|
|
*/
|
2013-08-01 20:55:43 +08:00
|
|
|
static Follow* create(Node *followedNode, const Rect& rect = Rect::ZERO);
|
2013-11-22 09:06:06 +08:00
|
|
|
|
|
|
|
inline bool isBoundarySet() const { return _boundarySet; }
|
|
|
|
/** alter behavior - turn on/off boundary */
|
|
|
|
inline void setBoudarySet(bool value) { _boundarySet = value; }
|
|
|
|
|
|
|
|
//
|
|
|
|
// Override
|
|
|
|
//
|
|
|
|
virtual Follow* clone() const override;
|
|
|
|
virtual Follow* reverse() const override;
|
|
|
|
virtual void step(float dt) override;
|
|
|
|
virtual bool isDone() const override;
|
|
|
|
virtual void stop() override;
|
|
|
|
|
|
|
|
protected:
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
|
|
|
* @js ctor
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
Follow()
|
2013-11-22 09:06:06 +08:00
|
|
|
: _followedNode(nullptr)
|
|
|
|
, _boundarySet(false)
|
|
|
|
, _boundaryFullyCovered(false)
|
|
|
|
, _leftBoundary(0.0)
|
|
|
|
, _rightBoundary(0.0)
|
|
|
|
, _topBoundary(0.0)
|
|
|
|
, _bottomBoundary(0.0)
|
|
|
|
, _worldRect(Rect::ZERO)
|
2012-04-19 14:35:52 +08:00
|
|
|
{}
|
2013-09-13 13:52:42 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-11-16 21:08:00 +08:00
|
|
|
virtual ~Follow();
|
2013-07-26 12:04:03 +08:00
|
|
|
/**
|
|
|
|
* Initializes the action with a set boundary or with no boundary.
|
|
|
|
*
|
2013-08-01 20:55:43 +08:00
|
|
|
* @param followedNode The node to be followed.
|
2013-07-26 12:04:03 +08:00
|
|
|
* @param rect The boundary. If \p rect is equal to Rect::ZERO, it'll work
|
|
|
|
* with no boundary.
|
|
|
|
*/
|
2013-08-01 20:55:43 +08:00
|
|
|
bool initWithTarget(Node *followedNode, const Rect& rect = Rect::ZERO);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// node to follow
|
2013-06-20 14:13:12 +08:00
|
|
|
Node *_followedNode;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// whether camera should be limited to certain area
|
2013-06-15 14:03:30 +08:00
|
|
|
bool _boundarySet;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// if screen size is bigger than the boundary - update not needed
|
2013-06-15 14:03:30 +08:00
|
|
|
bool _boundaryFullyCovered;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// fast access to the screen dimensions
|
2013-06-20 14:13:12 +08:00
|
|
|
Point _halfScreenSize;
|
|
|
|
Point _fullScreenSize;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// world boundaries
|
2013-06-15 14:03:30 +08:00
|
|
|
float _leftBoundary;
|
|
|
|
float _rightBoundary;
|
|
|
|
float _topBoundary;
|
|
|
|
float _bottomBoundary;
|
2013-06-20 14:13:12 +08:00
|
|
|
Rect _worldRect;
|
2013-06-14 08:25:14 +08:00
|
|
|
|
2013-11-22 09:06:06 +08:00
|
|
|
private:
|
|
|
|
CC_DISALLOW_COPY_AND_ASSIGN(Follow);
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
2012-06-20 18:07:27 +08:00
|
|
|
// end of actions group
|
|
|
|
/// @}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // __ACTIONS_CCACTION_H__
|