axmol/extensions/physics-nodes/PhysicsSprite.h

155 lines
5.4 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +08:00
/* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
* Copyright (c) 2012 cocos2d-x.org
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
* Copyright (c) 2021 @aismann; Peter Eismann, Germany; dreifrankensoft
2019-11-23 20:27:39 +08:00
*
* 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 __PHYSICSNODES_CCPHYSICSSPRITE_H__
#define __PHYSICSNODES_CCPHYSICSSPRITE_H__
#include "2d/Sprite.h"
2019-11-23 20:27:39 +08:00
#include "extensions/ExtensionMacros.h"
#include "extensions/ExtensionExport.h"
#include "base/EventListenerCustom.h"
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
#if (AX_ENABLE_CHIPMUNK_INTEGRATION || AX_ENABLE_BOX2D_INTEGRATION)
2019-11-23 20:27:39 +08:00
struct cpBody;
class b2Body;
NS_AX_EXT_BEGIN
2019-11-23 20:27:39 +08:00
/** A Sprite subclass that is bound to a physics body.
It works with:
2022-07-16 10:43:05 +08:00
- Chipmunk: Preprocessor macro AX_ENABLE_CHIPMUNK_INTEGRATION should be defined
- Objective-Chipmunk: Preprocessor macro AX_ENABLE_CHIPMUNK_INTEGRATION should be defined
- Box2d: Preprocessor macro AX_ENABLE_BOX2D_INTEGRATION should be defined
2019-11-23 20:27:39 +08:00
Features and Limitations:
- Scale and Skew properties are ignored.
- Position and rotation are going to updated from the physics body
- If you update the rotation or position manually, the physics body will be updated
- You can't enble both Chipmunk support and Box2d support at the same time. Only one can be enabled at compile time
* @lua NA
*/
2022-07-16 10:43:05 +08:00
class AX_EX_DLL PhysicsSprite : public Sprite
2019-11-23 20:27:39 +08:00
{
public:
static PhysicsSprite* create();
/** Creates an sprite with a texture.
The rect used will be the size of the texture.
The offset will be (0,0).
*/
2021-12-25 10:04:45 +08:00
static PhysicsSprite* createWithTexture(Texture2D* pTexture);
2019-11-23 20:27:39 +08:00
/** Creates an sprite with a texture and a rect.
The offset will be (0,0).
*/
2021-12-25 10:04:45 +08:00
static PhysicsSprite* createWithTexture(Texture2D* pTexture, const Rect& rect);
2019-11-23 20:27:39 +08:00
/** Creates an sprite with an sprite frame. */
2021-12-25 10:04:45 +08:00
static PhysicsSprite* createWithSpriteFrame(SpriteFrame* pSpriteFrame);
2019-11-23 20:27:39 +08:00
/** Creates an sprite with an sprite frame name.
An SpriteFrame will be fetched from the SpriteFrameCache by name.
If the SpriteFrame doesn't exist it will raise an exception.
@since v0.9
*/
2021-12-25 10:04:45 +08:00
static PhysicsSprite* createWithSpriteFrameName(const char* pszSpriteFrameName);
2019-11-23 20:27:39 +08:00
/** Creates an sprite with an image filename.
The rect used will be the size of the image.
The offset will be (0,0).
*/
2021-12-25 10:04:45 +08:00
static PhysicsSprite* create(const char* pszFileName);
2019-11-23 20:27:39 +08:00
/** Creates an sprite with an image filename and a rect.
The offset will be (0,0).
*/
2021-12-25 10:04:45 +08:00
static PhysicsSprite* create(const char* pszFileName, const Rect& rect);
2019-11-23 20:27:39 +08:00
PhysicsSprite();
virtual bool isDirty() const override;
/** Keep the sprite's rotation separate from the body. */
bool isIgnoreBodyRotation() const;
void setIgnoreBodyRotation(bool bIgnoreBodyRotation);
//
// Chipmunk specific
//
/** Body accessor when using regular Chipmunk */
cpBody* getCPBody() const;
2021-12-25 10:04:45 +08:00
void setCPBody(cpBody* pBody);
2019-11-23 20:27:39 +08:00
//
// Box2d specific
//
/** Body accessor when using box2d */
b2Body* getB2Body() const;
2021-12-25 10:04:45 +08:00
void setB2Body(b2Body* pBody);
2019-11-23 20:27:39 +08:00
float getPTMRatio() const;
void setPTMRatio(float fPTMRatio);
virtual void syncPhysicsTransform() const;
// overrides
virtual const Vec2& getPosition() const override;
virtual void getPosition(float* x, float* y) const override;
virtual float getPositionX() const override;
virtual float getPositionY() const override;
virtual Vec3 getPosition3D() const override;
2021-12-25 10:04:45 +08:00
virtual void setPosition(const Vec2& position) override;
2019-11-23 20:27:39 +08:00
virtual void setPosition(float x, float y) override;
virtual void setPositionX(float x) override;
virtual void setPositionY(float y) override;
virtual void setPosition3D(const Vec3& position) override;
virtual float getRotation() const override;
virtual void setRotation(float fRotation) override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
virtual void onEnter() override;
virtual void onExit() override;
protected:
const Vec2& getPosFromPhysics() const;
2021-12-25 10:04:45 +08:00
void afterUpdate(EventCustom* event);
2019-11-23 20:27:39 +08:00
protected:
2021-12-25 10:04:45 +08:00
bool _ignoreBodyRotation;
2019-11-23 20:27:39 +08:00
// chipmunk specific
2021-12-25 10:04:45 +08:00
cpBody* _CPBody;
2019-11-23 20:27:39 +08:00
// box2d specific
2021-12-25 10:04:45 +08:00
b2Body* _pB2Body;
float _PTMRatio;
2019-11-23 20:27:39 +08:00
// Event for update synchronise physic transform
2022-08-08 18:02:17 +08:00
ax::EventListenerCustom* _syncTransform;
2019-11-23 20:27:39 +08:00
};
NS_AX_EXT_END
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
#endif // AX_ENABLE_CHIPMUNK_INTEGRATION || AX_ENABLE_BOX2D_INTEGRATION
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
#endif // __PHYSICSNODES_CCPHYSICSSPRITE_H__