📄 worldobjects.h
字号:
#ifndef WORLDOBJECTS_H_
#define WORLDOBJECTS_H_
#include "Polygon.h"
#include "WorldPoint.h"
#include "MathUtils.h"
#include <GDI.H>
class TWorldObject
{
public:
virtual void Quantum(){}; // Time slice, subclasses should implement motion over time, default behaviour is to be very still
virtual void AccelerateInDirection(TInt aAngle, TReal aForce);
virtual void CalculateDrawableShape()=0;
virtual const MPolygon& DrawableShape() const =0 ;
void CopySpeedAndDirection(const TWorldObject& aObject);
void Stop();
void SetSpeedAndDirection(TInt aAngle, TReal aSpeed);
void MoveForwards();
TWorldPoint WorldPoint() const;
TPoint WorldPointAsPoint() const;
void SetWorldPoint(TWorldPoint aCenter);
void SetWorldPoint(TPoint aCenter);
// Colour
void SetColor(TRgb aColor);
TRgb Color() const;
TBool Intersects(TPoint aLineStart, TPoint aLineEnd) const;
TBool Intersects(const TWorldObject& aOther) const;
// is Object active in the world
TBool IsActive() const;
// setative - converience flag used for fixed arrays of objects (can be treated as deleted if not active)
inline void SetActive(TBool aActive);
// in degrees
TInt FacingDirection();
// in degrees
inline void SetFacingDirection(TInt aAngle);
protected:
TWorldPoint iCenter; // Origin of the object in the real world
TInt iAngle; // Current facing direction (
TReal iDeltaX; // Current Motion per quantum in x direction
TReal iDeltaY; // Current Motion per quantum in y direction
TBool iIsActive;
TRgb iColor;
};
class TShip : public TWorldObject
{
public:
enum TState{EAlive, EInvinsible, EExploding, EDead};
void SetState(TState aState);
TState State() const;
public:
TShip();
TPoint Nose();
void Construct();
// Time based changes
void Quantum();
// User action changes
void IncAngle();
void DecAngle();
// Accelerate in the current direction
void Accelerate();
void CalculateDrawableShape();
const MPolygon& DrawableShape() const;
private:
TState iState;
TFixedPolygon<3> iShape;
TFixedPolygon<3> iDrawableShape;
};
class TAsteroid : public TWorldObject
{
public:
enum TRockMaterial{ERockLimeStone, ERockHard, ERockRubber, ERockTitanium};
public:
enum {KMaxRockSides=40};
TAsteroid();
// Returns true if asteroid is destroyed
TBool Hit();
TBool InShock() const;
void SetNumberOfHitsBeforeSplit(TInt aNum);
void Construct(TInt aMinSides=5, TInt aMaxSides=10, TInt aMinRadius=10, TInt aMaxRadius=35);
virtual void Quantum();
virtual void CalculateDrawableShape();
const MPolygon& DrawableShape() const;
TInt MaxRadius() const;
void SetMaterial(TRockMaterial aMaterial);
TRockMaterial Material() const;
private:
TFixedPolygon<KMaxRockSides> iShape;
TFixedPolygon<KMaxRockSides> iDrawableShape;
TInt iMaxRadius;
TInt iNumberOfHitsBeforeSplit;
TInt iNumberOfHits;
TBool iInShock;
TInt iAngleInc;
TRockMaterial iMaterial;
};
class TExplosionFragment : public TWorldObject
{
public:
TExplosionFragment();
void SetPoints(const TPoint& a1, const TPoint& a2);
void ResetAge();
void IncAge(TInt aStep);
TInt Age() const;
void Construct();
virtual void Quantum();
virtual void CalculateDrawableShape();
const MPolygon& DrawableShape() const;
private:
TFixedPolygon<2> iShape;
TFixedPolygon<2> iDrawableShape;
TInt iAge;
};
class TProjectile : public TWorldObject
{
public:
TProjectile();
void Construct();
TWorldPoint LastPoint() const;
TPoint LastPointAsPoint() const;
void SetLastPoint(TWorldPoint aPoint);
virtual void Quantum();
virtual void CalculateDrawableShape();
virtual const MPolygon& DrawableShape() const;
private:
TFixedPolygon<4> iShape;
TFixedPolygon<4> iDrawableShape;
TWorldPoint iLastPoint;
};
class TCapsule : public TWorldObject
{
public:
TCapsule();
void Reset();
virtual void Quantum();
virtual void CalculateDrawableShape();
virtual const MPolygon& DrawableShape() const;
private:
TFixedPolygon<4> iShape;
TFixedPolygon<4> iDrawableShape;
TReal iS;
TReal iDs;
};
// in degrees
inline TInt TWorldObject::FacingDirection()
{
return iAngle;
}
// setative - converience flag used for fixed arrays of objects (can be treated as deleted if not active)
inline void TWorldObject::SetActive(TBool aActive)
{
iIsActive = aActive;
}
inline void TWorldObject::SetWorldPoint(TWorldPoint aCenter)
{
iCenter = aCenter;
}
inline void TWorldObject::SetWorldPoint(TPoint aCenter)
{
SetWorldPoint(TWorldPoint(aCenter.iX, aCenter.iY));
}
// is Object active in the world
inline TBool TWorldObject::IsActive() const
{
return iIsActive;
}
#endif /*WORLDOBJECTS_H_*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -