GameObject.hpp
上传用户:zhj2929
上传日期:2022-07-23
资源大小:28772k
文件大小:8k
- #ifndef _GAMEOBJECT_HPP
- #define _GAMEOBJECT_HPP
- #define BLOCK_SPEED 0.15f
- class GameObject
- {
- protected:
- bool mActive;
- bool mFlipped;
- bool mJumping;
- bool mDucking;
- bool mBullet;
-
- JTexture* mTexture;
- JTexture* mQuestion;
- JSprite* mCurrAnimation;
- float mX;
- float mY;
-
- float mYVelocity;
- float mXVelocity;
- float mHScale;
- float mVScale;
- static GameStatePlay* mApp;
- static JGE* mEngine;
- static TileMap* mMap;
- public:
- GameObject(GameStatePlay *app);
- virtual ~GameObject();
-
- virtual void Update(float dt) = 0;
- virtual void Render() = 0;
- void SetPosition(float x, float y) { mX = x; mY = y; }
- void SetActive(bool flag) { mActive = flag; }
- bool IsActive() { return mActive; }
- float GetX() { return mX; }
- float GetY() { return mY; }
- void SetX(float x){mX=x;}
- void SetY(float y){mY=y;}
- JSprite* GetSprite() { return mCurrAnimation; }
-
- };
- JGE* GameObject::mEngine = NULL;
- GameStatePlay* GameObject::mApp = NULL;
- TileMap* GameObject::mMap = NULL;
- GameObject::GameObject(GameStatePlay *app)
- {
- mApp = app;
- mTexture = mApp->GetTexture(0);
- mQuestion = mApp->GetTexture(1);
- mEngine = mApp->GetJGE();
- mMap = mApp->GetTileMap();
-
- mActive = false;
- mHScale = 1.0f;
- mVScale = 1.0f;
- }
- GameObject::~GameObject()
- {
-
- }
- class MoveableObject: public GameObject
- {
- protected:
-
- bool mFlipped;
- bool mJumping;
- bool mBullet;
- float mYRenderOffset;
-
- int mLeftAdjustment;
- int mRightAdjustment;
- int mHitLeftAdjustment;
- int mHitRightAdjustment;
- int mFallLeftAdjustment;
- int mFallRightAdjustment;
- int mRealHeight;
- float bulletdirection;
- public:
- MoveableObject(GameStatePlay* app);
- virtual ~MoveableObject();
- virtual void Update(float dt);
- virtual void Render();
- virtual void HitTop(int left, int right, int row) {}
- virtual void EatFruit(int left, int right, int row) {}
- virtual void HitBottom(int left, int right, int row) {}
- virtual void HitLeft() {}
- virtual void HitRight(int left, int right, int row) {}
- virtual void Bullet(float x, float y,float direction) {}
- virtual void StandingOnNothing(int col, int row);
-
- };
- MoveableObject::MoveableObject(GameStatePlay* app): GameObject(app)
- {
- mFlipped = true;
- mJumping = false;
- mDucking = false;
- mBullet=false;
- mXVelocity = 0.0f;
- mYVelocity = 0.0f;
- bulletdirection=1;
- }
- MoveableObject::~MoveableObject()
- {
-
- }
- void MoveableObject::Update(float dt)
- {
- int col, row;
- bool moved = false;
- float xdelta = mXVelocity*dt;
- float xNew;
-
- if (mJumping)
- {
- float gravity = DEFAULT_GRAVITY;
- float distance = mYVelocity*dt + gravity*dt*dt/2;
- mYVelocity += gravity*dt;
- float newY = mY + distance;
-
-
- if (distance < 0.0f)
- {
- int left = ((int)mX)+23;
- int right = ((int)mX)+47;
- if(mMap->CheckHoriz(left, right, (int)(mY)-mRealHeight, mFlipped, &col, &row) ==4||mMap->CheckHoriz(left, right, (int)(mY)-mRealHeight, mFlipped, &col, &row) ==3)
- {
- newY = mY + distance;
- EatFruit(left, right, row);
- }
- else if (mMap->CheckHoriz(left, right, (int)(mY)-mRealHeight, mFlipped, &col, &row) != 0&&mMap->CheckHoriz(left, right, (int)(mY)-mRealHeight, mFlipped, &col, &row) !=4&&mMap->CheckHoriz(left, right, (int)(mY)-mRealHeight, mFlipped, &col, &row) !=3)
- {
- mYVelocity = 0.0f;
- newY = mY;
-
- HitTop(left, right, row);
- }
- }
- else if (distance > 0.0f)
- {
- int left = ((int)mX)+23;
- int right = ((int)mX)+47;
- float y = mY;
- bool done = false;
-
- while (!done)
- {
-
- if(mMap->CheckHoriz(((int)mX)+mLeftAdjustment, ((int)mX)+mRightAdjustment, (int)(mY), mFlipped, &col, &row) == 3||mMap->CheckHoriz(((int)mX)+mLeftAdjustment, ((int)mX)+mRightAdjustment, (int)(mY), mFlipped, &col, &row) == 4)
- {
- newY = mY + distance;
- EatFruit(left, right, row);
- }
-
- else if (mMap->CheckHoriz(((int)mX)+mLeftAdjustment, ((int)mX)+mRightAdjustment, (int)(mY), mFlipped, &col, &row) != 0)
- {
- mJumping = false;
- newY = (float)((row << TILE_SHIFT)-1);
- done = true;
- }
- if (y == newY)
- done = true;
- else
- {
-
- y += TILE_HEIGHT;
- if (y > newY)
- y = newY;
- }
- }
- }
- mY = newY;
-
- }
-
- if (xdelta < 0.0f)
- {
-
- xNew = mX + xdelta;
- if (xNew < 0.0f)
- xNew = 0.0f;
- moved = true;
- if (mMap->CheckVert(((int)mX)+mHitLeftAdjustment, (int)(mY)-mRealHeight, (int)(mY), &col, &row) != 0&&mMap->CheckVert(((int)mX)+mHitLeftAdjustment, (int)(mY)-mRealHeight, (int)(mY), &col, &row) != 3&&mMap->CheckVert(((int)mX)+mHitLeftAdjustment, (int)(mY)-mRealHeight, (int)(mY), &col, &row) != 4)
- {
- xNew = mX;
- moved = false;
- HitLeft();
- }
- bulletdirection=-1;
- mX = xNew;
- }
- else if (xdelta > 0.0f)
- {
- int left = ((int)mX)+23;
- int right = ((int)mX)+47;
- moved = true;
- xNew = mX + xdelta;
- if (xNew > VIRTUAL_WIDTH-32.0f)
- xNew = VIRTUAL_WIDTH-32.0f;
- if(mMap->CheckVert(((int)mX)+mHitRightAdjustment, (int)(mY)-mRealHeight, (int)(mY), &col, &row) == 10)
- {
- moved = true;
- HitRight(left, right, row);
- }
-
- else if (mMap->CheckVert(((int)mX)+mHitRightAdjustment, (int)(mY)-mRealHeight, (int)(mY), &col, &row) != 0&&mMap->CheckVert(((int)mX)+mHitRightAdjustment, (int)(mY)-mRealHeight, (int)(mY), &col, &row) != 3&&mMap->CheckVert(((int)mX)+mHitRightAdjustment, (int)(mY)-mRealHeight, (int)(mY), &col, &row) != 4)
- {
- xNew = mX;
- moved = false;
- HitRight(left, right, row);
-
- }
-
- bulletdirection=1;
- mX = xNew;
- }
- if(mDucking)
- {
- int col, row;
- int left = ((int)mX)+23;
- int right = ((int)mX)+47;
-
- if (mMap->CheckHoriz(((int)mX)+mLeftAdjustment, ((int)mX)+mRightAdjustment, (int)(mY)+1, mFlipped, &col, &row) == 6||mMap->CheckHoriz(((int)mX)+mLeftAdjustment, ((int)mX)+mRightAdjustment, (int)(mY)+1, mFlipped, &col, &row) == 10)
- {
- HitBottom( left, right,row);
-
-
-
- }
- mDucking = false ;
- }
- if (!mJumping &&!mDucking&& moved)
- {
- int col, row;
- int left = ((int)mX)+23;
- int right = ((int)mX)+47;
- float gravity = DEFAULT_GRAVITY;
- float distance = mYVelocity*dt + gravity*dt*dt/2;
- mYVelocity += gravity*dt;
- float newY = mY + distance;
- if (mMap->CheckHoriz(((int)mX)+mLeftAdjustment, ((int)mX)+mRightAdjustment, (int)(mY)+1, mFlipped, &col, &row) == 0)
- {
- StandingOnNothing(col, row);
- }
-
-
- else if (mMap->CheckHoriz(((int)mX)+mLeftAdjustment, ((int)mX)+mRightAdjustment, (int)(mY), mFlipped, &col, &row) == 3||mMap->CheckHoriz(((int)mX)+mLeftAdjustment, ((int)mX)+mRightAdjustment, (int)(mY), mFlipped, &col, &row) == 4)
- {
- EatFruit(left, right, row);
-
-
- }
-
-
- }
- if (!mJumping &&!mDucking&& !moved)
- {
- int col, row;
- int left = ((int)mX)+23;
- int right = ((int)mX)+47;
- float gravity = DEFAULT_GRAVITY;
- float distance = mYVelocity*dt + gravity*dt*dt/2;
- mYVelocity += gravity*dt;
- float newY = mY + distance;
- if (mMap->CheckHoriz(((int)mX)+mLeftAdjustment, ((int)mX)+mRightAdjustment, (int)(mY)+1, mFlipped, &col, &row) == 3||mMap->CheckHoriz(((int)mX)+mLeftAdjustment, ((int)mX)+mRightAdjustment, (int)(mY)+1, mFlipped, &col, &row) == 4)
- {
-
- mYVelocity += gravity*dt;
- newY = mY + distance;
- mJumping = true;
- EatFruit(left, right, row);
- }
-
- }
- if(mBullet)
- {
- Bullet(mX,mY,bulletdirection);
- }
-
- mCurrAnimation->Update(dt);
- }
- void MoveableObject::StandingOnNothing(int col, int row)
- {
- mYVelocity = 0.0f;
- mJumping = true;
- if (mFlipped)
- mX = (float)((col<<TILE_SHIFT)+mFallRightAdjustment);
- else
- mX = (float)((col<<TILE_SHIFT)+mFallLeftAdjustment);
- }
- void MoveableObject::Render()
- {
- if(mY<=470)
- {
- float x, y;
- mCurrAnimation->SetScale(mHScale, mVScale);
- mMap->GetPosition(&x, &y);
- mCurrAnimation->SetPosition(mX-x, mY-y+mYRenderOffset);
- mCurrAnimation->Render();
- }
- }
- #endif