⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gameobject.hpp

📁 超级玛丽
💻 HPP
字号:
//-------------------------------------------------------------------------------------
//
// This is part of MarioDemo, a platformer demo for JGE++
// 
// Copyright (C) 2006 James Hui (a.k.a. Dr.Watson)
// 
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or (at your option) any
// later version.
// 
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 59 Temple
// Place, Suite 330, Boston, MA 02111-1307 USA
// 
// Bugs and comments can be forwarded to jhkhui@yahoo.com. 
// 
//-------------------------------------------------------------------------------------


#ifndef _GAMEOBJECT_HPP
#define _GAMEOBJECT_HPP



#define BLOCK_SPEED					0.15f

class GameObject
{
protected:
	bool mActive;

	bool mFlipped;
	bool mJumping;
	
	JTexture* mTexture;
	

	float mX;
	float mY;
	


	float mHScale;
	float mVScale;


	static GameStatePlay* mApp;
	static JGE* mEngine;
	static TileMap* mMap;

public:
	JSprite* mCurrAnimation;
	float mYVelocity;
	float mXVelocity;
	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; }

	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);
	mEngine = mApp->GetJGE();
	mMap = mApp->GetTileMap();
	
	mActive = false;

	mHScale = 1.0f;
	mVScale = 1.0f;
}


GameObject::~GameObject()
{
	
}



class MoveableObject: public GameObject
{
protected:
	
	bool mFlipped;


	float mYRenderOffset;
	
	int mLeftAdjustment;
	int mRightAdjustment;

	int mHitLeftAdjustment;
	int mHitRightAdjustment;

	int mFallLeftAdjustment;
	int mFallRightAdjustment;

	int mRealHeight;

public:
	bool mJumping;
	MoveableObject(GameStatePlay* app);
	virtual ~MoveableObject();

	virtual void Update(float dt);
	virtual void Render();

	virtual void HitTop(int left, int right, int row) {}
	virtual void HitBottom() {}
	virtual void HitLeft() {}
	virtual void HitRight() {}

	virtual void StandingOnNothing(int col, int row);
	bool right;

	bool left;
	
};


MoveableObject::MoveableObject(GameStatePlay* app): GameObject(app)
{


	right = true;
	left = true;
	mFlipped = true;
	mJumping = false;

	mXVelocity = 0.0f;
	mYVelocity = 0.0f;

}


MoveableObject::~MoveableObject()
{
	

}


void MoveableObject::Update(float dt)
{

	int col, row;
	if (mMap->CheckHoriz(((int)mX)+mLeftAdjustment, ((int)mX)+mRightAdjustment, (int)(mY), mFlipped, &col, &row) == 16)
	{
		mJumping = true;
		mYVelocity = -0.4f;

	}
	if (mMap->CheckHoriz(((int)mX)+mLeftAdjustment, ((int)mX)+mRightAdjustment, (int)(mY), mFlipped, &col, &row) == 17)
	{
		for(int i = -2; i<3; i++)
		{
			mMap->SetTileInfo(col+i, row, 9);
			mMap->SetTile(col+i, row, 0);
		}
			mJumping = true;
			mYVelocity = -0.05f;
							
	}
	if (mMap->CheckHoriz(((int)mX)+mLeftAdjustment, ((int)mX)+mRightAdjustment, (int)(mY), mFlipped, &col, &row) == 18)
	{
		//right = false;
	}
	if (mMap->CheckHoriz(((int)mX)+mLeftAdjustment, ((int)mX)+mRightAdjustment, (int)(mY), mFlipped, &col, &row) == 19)
	{
		//left = false;
	}
	
	
	if (mJumping)
	{
		float gravity = DEFAULT_GRAVITY;
		float distance = mYVelocity*dt + gravity*dt*dt/2;
		mYVelocity += gravity*dt;

		//mCurrAnimation = mJump;

		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) != 0)
				{
					//mJumping = false;
					mYVelocity = 0.0f;
					//mSprite = mIdle;	
					newY = mY;//(float)((row << TILE_SHIFT)-1);
					//done = true;
					//int lTilePos = (left>>TILE_SHIFT)<<TILE_SHIFT;
				//	int rTilePos = (right>>TILE_SHIFT)<<TILE_SHIFT;

					HitTop(left, right, row);

//					JumpingBlock* block = mApp->GetJumpingBlock();
//
//					if (left < lTilePos+24)
//						block->Animate(lTilePos>>TILE_SHIFT, row);
//					else
//						block->Animate(rTilePos>>TILE_SHIFT, row);

				}

		}
		else if (distance > 0.0f)
		{
			
			float y = mY;
			bool done = false;
			while (!done)
			{
				
				if (mMap->CheckHoriz(((int)mX)+mLeftAdjustment, ((int)mX)+mRightAdjustment, (int)(mY), mFlipped, &col, &row) != 0)
				{
					
					

					mJumping = false;
					//mCurrAnimation = mIdle;	
					newY = (float)((row << TILE_SHIFT)-1);
					done = true;
				}

				if (y == newY) 
					done = true;
				else
				{
					
					y += TILE_HEIGHT;
					if (y > newY)
						y = newY;
				}
			}
		}

		mY = newY;
		
	}
//	else
//	{
//#ifdef WIN32
//		if (mEngine->GetAnalogY() < 64 || mEngine->GetButtonClick(PSP_CTRL_CIRCLE))
//#else
//		if (mEngine->GetButtonClick(PSP_CTRL_CIRCLE))
//#endif
//		{
//			mCurrAnimation = mJump;
//			mJumping = true;
//			mYVelocity = BIGM_INITIAL_JUMP_VEL;
//		}
//		else if (mEngine->GetAnalogY() > 192)
//		{
//			mCurrAnimation = mDuck;
//			mXVelocity = 0.0f;
//		}
//	}
	

	bool moved = false;
	float xdelta = mXVelocity*dt;
	float xNew;

	if (xdelta < 0.0f)
	{
		//mFlipped = false;
		
		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)
		{
			xNew = mX;
			moved = false;

			HitLeft();
		}
		
		mX = xNew;
	}
	else if (xdelta > 0.0f)
	{
		//mFlipped = true;
		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) != 0)
		{
			xNew = mX;
			moved = false;

			HitRight();
		}
		
		mX = xNew;
	}

	//mCurrAnimation->SetFlip(mFlipped);

	if (!mJumping && moved)
	{
		int col, row;
		if (mMap->CheckHoriz(((int)mX)+mLeftAdjustment, ((int)mX)+mRightAdjustment, (int)(mY)+1, mFlipped, &col, &row) == 0)
		//if (mTileMap->CheckBelow((int)(mX)+30, (int)(mX)+30, ((int)mY)+1, &col, &row) == 0)
		{
			StandingOnNothing(col, row);
		}
	}

		
	mCurrAnimation->Update(dt);
	

//	mMap->SetTarget(mX - 210.0f, mY - 136.0f);
	//int currCol, currRow;
	//tileInfo = GetTileInfo(currCol, currRow);
	/*if (tileInfo == BLOCK_TUNNEL_02 && time == 20)
	{
		SetTileInfo(currCol, currRow, 0);
		SetTile(currCol, currRow, 0);
		mApp->SpawnTunnel_2(currCol, currRow);
	}*/


}


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()
{
	float x, y;

	mCurrAnimation->SetScale(mHScale, mVScale);
	//mCurrAnimation->SetFlip(mFlipped);
	mMap->GetPosition(&x, &y);
	mCurrAnimation->SetPosition(mX-x, mY-y+mYRenderOffset);
	mCurrAnimation->Render();

}



#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -