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

📄 worldobjects.cpp

📁 Symbian s60 游戏编程示例
💻 CPP
字号:
/*
* ============================================================================
*  Name     : CGfxTimer from CGfxTimer.h
*  Part of  : roids
*  Created  : 05/12/2001 by Twm Davies
*  Implementation notes:
*     Initial content was generated by Nokia Series 60 AppWizard.
*  Version  :
*  Copyright: Twm Davies
* ============================================================================
*/

// INCLUDE FILES
#include "WorldObjects.h"
#include "WorldPoint.h"
#include "MathUtils.h"

#include <e32std.h>
#include <e32base.h>

// Constants
const TInt KAngleStep = 9;
const TReal KAcceleration = 0.918;
const TReal KMaxAcceleration = 5.0;

//

TWorldPoint TWorldObject::WorldPoint() const
	{
	return iCenter;
	}

TPoint TWorldObject::WorldPointAsPoint() const
	{
	return TPoint(	MathUtils::Round( iCenter.X() ), 
					MathUtils::Round( iCenter.Y() ));
	}

// in degrees
void TWorldObject::SetFacingDirection(TInt aAngle)
	{
	iAngle = aAngle;
	}

void TWorldObject::AccelerateInDirection(TInt aAngle, TReal aForce)
	{
	TReal a = MathUtils::Sin(aAngle);
	TReal b = MathUtils::Cos(aAngle);

	iDeltaX+=(-(a*aForce));
	iDeltaY+=((b*aForce));
		
	if(iDeltaX>=KMaxAcceleration)
		iDeltaX=KMaxAcceleration;
	if(iDeltaY>=KMaxAcceleration)
		iDeltaY=KMaxAcceleration;
	}

// Copy speed and direction from another world object
void TWorldObject::CopySpeedAndDirection(const TWorldObject& aObject)
	{
	iDeltaX = aObject.iDeltaX;
	iDeltaY = aObject.iDeltaY;
	}
	
// Stop object moving completely
void TWorldObject::Stop()
	{
	iDeltaX = iDeltaY = 0;
	}
	
//
// angle in degrees
// Speed = world points per quantum
//
void TWorldObject::SetSpeedAndDirection(TInt aAngle, TReal aSpeed)
	{
	TReal a = MathUtils::Sin(aAngle);
	TReal b = MathUtils::Cos(aAngle);

	iDeltaX=(-(a*aSpeed));
	iDeltaY=((b*aSpeed));
	}
	
void TWorldObject::MoveForwards()
	{
	iCenter+=TWorldPoint(iDeltaX, iDeltaY);
	}
	
TBool TWorldObject::Intersects(TPoint aLineStart, TPoint aLineEnd)const
	{
	return DrawableShape().Intersects(aLineStart,aLineEnd);
	}	
	
TBool TWorldObject::Intersects(const TWorldObject& aOther) const
	{
	return DrawableShape().Intersects(aOther.DrawableShape());
	}	

void TWorldObject::SetColor(TRgb aColor)
	{
	iColor = aColor;
	}
TRgb TWorldObject::Color() const
	{
	return iColor;
	}
	
void TAsteroid::Construct(TInt aMinSides, TInt aMaxSides, TInt aMinRadius, TInt aMaxRadius)
	{		
	iAngle = 0;
	iAngleInc = MathUtils::Random(1,+5);
	iNumberOfHits=0;
	SetMaterial(ERockLimeStone);

	iInShock =EFalse;

	TInt maxPoints  = MathUtils::Random(aMinSides , aMaxSides);
	iMaxRadius=0;
	for (TInt j = 0; j < maxPoints; j++) 
		{			
		TReal theta = 2.0 * 3.14 / maxPoints * j;
		TReal radius = MathUtils::Random(aMinRadius, aMaxRadius);

		iMaxRadius = Max(iMaxRadius, MathUtils::Round(radius) );

	
		iShape[j].iX = -MathUtils::Round(radius * MathUtils::Sin(theta) );
		iShape[j].iY =  MathUtils::Round(radius * MathUtils::Cos(theta) );
		}

	iShape.SetPolyCount(maxPoints);
	}
		
void TShip::SetState(TState aState)
		{
		iState = aState;
		}
		
TShip::TState TShip::State() const
	{
	return iState;
	}

TShip::TShip()
	{
	iCenter = TWorldPoint(100,100);
	iAngle = 0;

		// Construct Triangle Shape
	iShape[0] = TPoint(-7,0);
	iShape[1] = TPoint(+7,-5);
	iShape[2] = TPoint(+7,+5);
	}

TPoint TShip::Nose()
	{
	iShape.RotateAndOffset(iAngle, WorldPointAsPoint(), iDrawableShape);
	return iDrawableShape[0];
	}

void TShip::Construct()
	{
	}

// Time based changes
void TShip::Quantum()
	{
	// Move forwards in current direction
	iCenter+=TWorldPoint(iDeltaX, iDeltaY);
	}

// User action changes
void TShip::IncAngle()
	{
	iAngle+=KAngleStep;
	if(iAngle>360)
		iAngle = iAngle-360;
	}

void TShip::DecAngle()
	{
	iAngle -=KAngleStep;
	if(iAngle<0) 
		iAngle = 360+iAngle;
	}

// Accelerate in the current direction
void TShip::Accelerate()
	{
	AccelerateInDirection(iAngle, 0.2);
	}

void TShip::CalculateDrawableShape()
	{
	iShape.RotateAndOffset(iAngle, WorldPointAsPoint(), iDrawableShape);
	}

const MPolygon& TShip::DrawableShape() const
	{			
	return iDrawableShape;
	}	

TAsteroid::TAsteroid()
	{
	}
	
// Returns true if asteroid is destroyed
TBool TAsteroid::Hit()
	{
	iNumberOfHits++;
	iInShock =ETrue;
	return iNumberOfHits == iNumberOfHitsBeforeSplit;
	}

TBool TAsteroid::InShock() const
	{
	return iInShock;
	}

void TAsteroid::SetNumberOfHitsBeforeSplit(TInt aNum)
	{
	iNumberOfHitsBeforeSplit = aNum;
	}

void TAsteroid::Quantum()
	{
	// Rotate asteroid and move forwards
	iAngle = (iAngle+iAngleInc)%360;
	MoveForwards();
	};

void TAsteroid::CalculateDrawableShape()
	{
	if(iInShock)
		{
		iShape.RotateOffsetAndScale(iAngle, WorldPointAsPoint(), 1.2, 1.2, iDrawableShape);
		iInShock = EFalse;
		}
	else
		iShape.RotateAndOffset(iAngle, WorldPointAsPoint(), iDrawableShape);
	iDrawableShape.SetPolyCount(iShape.PolyCount());		

	}

const MPolygon& TAsteroid::DrawableShape() const
	{ 
	return iDrawableShape;
	}

TInt TAsteroid::MaxRadius() const
	{
	return iMaxRadius;
	}

void TAsteroid::SetMaterial(TRockMaterial aMaterial)
	{
	iMaterial = aMaterial;
	TRgb color = KRgbWhite;
	switch(iMaterial)
		{
	case TAsteroid::ERockLimeStone:
		color = KRgbWhite;
		break;
	case TAsteroid::ERockHard:
		color = TRgb(245, 245, 245);
		break;
	case TAsteroid::ERockRubber:
		color = TRgb(215, 215, 215);
		break;
	case TAsteroid::ERockTitanium:
		color = TRgb(128, 231, 246);
		break;
		}
	SetColor(color);
				
	SetNumberOfHitsBeforeSplit( static_cast<TInt>(aMaterial) +1 );
	}

TAsteroid::TRockMaterial TAsteroid::Material() const
	{
	return iMaterial;
	}

TExplosionFragment::TExplosionFragment()
	{
	iAge=0;
	SetPoints(TPoint(0,0), TPoint(0,0));
	};

void TExplosionFragment::SetPoints(const TPoint& a1, const TPoint& a2)
	{
	iShape[0] = a1;
	iShape[1] = a2;
	}

void TExplosionFragment::ResetAge()
	{
	iAge =0;
	}

void TExplosionFragment::IncAge(TInt aStep)
	{
	iAge+=aStep;
	}

TInt TExplosionFragment::Age() const
	{
	return iAge;
	}

void TExplosionFragment::Construct()
	{		
	}

 void TExplosionFragment::Quantum()
	{		
	MoveForwards();
	};

 void TExplosionFragment::CalculateDrawableShape()
	{		
	iShape.Offset(WorldPointAsPoint(), iDrawableShape);
	iDrawableShape.SetPolyCount(iShape.PolyCount());		
	}

const MPolygon& TExplosionFragment::DrawableShape() const
	{ 
	return iDrawableShape;
	}
	
TProjectile::TProjectile()
	{
	iShape[0]= TPoint(-3,-3);
	iShape[1]= TPoint(+3,+3);
	iShape[2]= TPoint(+3,-3);
	iShape[3]= TPoint(-3,+3);
	}

void TProjectile::Construct()
	{		
	}
	
TWorldPoint TProjectile::LastPoint() const
	{
	return iLastPoint;
	}

TPoint TProjectile::LastPointAsPoint() const
	{
	return TPoint(MathUtils::Round(iLastPoint.X()), MathUtils::Round(iLastPoint.Y())) ;
	}

void TProjectile::SetLastPoint(TWorldPoint aPoint)
	{
	iLastPoint = aPoint;
	}

 void TProjectile::Quantum()
	{
	iLastPoint = WorldPoint();
	MoveForwards();
	}

void TProjectile::CalculateDrawableShape()
	{
	TPoint point(WorldPointAsPoint());
	for(TInt i=0; i<4; i++)
		iDrawableShape[i]= iShape[i]+point;
	}

const MPolygon& TProjectile::DrawableShape() const
	{
	return iDrawableShape;
	}	
		
TCapsule::TCapsule()
	{
	Reset();
	}
	
void TCapsule::Reset()
	{
	const TInt Kr = 4; //size of pill
	iShape[0] = TPoint(-Kr,0);
	iShape[1] = TPoint(0,+Kr);
	iShape[2] = TPoint(+Kr,0);	
	iShape[3] = TPoint(0,-Kr);	
	iAngle =0 ;
	iS = 1;
	iDs = 0.02;
	}
	
void TCapsule::Quantum()
	{
	iAngle = (iAngle+5)%360;
	iS+=iDs;
	
	if( (iS>1.5)|| (iS<0.9))
		iDs= -iDs;	
	}
	
void TCapsule::CalculateDrawableShape()
	{	
	iShape.RotateOffsetAndScale(iAngle,WorldPointAsPoint(),iS, iS,  iDrawableShape);
	}
	
const MPolygon& TCapsule::DrawableShape() const
	{
	return iDrawableShape;
	}

⌨️ 快捷键说明

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