1 /* $Id: collidableobject.hpp,v 1.26.4.2 2006/01/20 11:33:52 chfreund Exp $ */
2 
3 #ifndef _COLLIDABLEOBJECT_HPP_
4 #define _COLLIDABLEOBJECT_HPP_
5 
6 #include <iostream>
7 #include "attachableobject.hpp"
8 #include "particles.hpp"
9 
10 /**********************************************************/
11 
12 class Player;
13 
14 /**********************************************************/
15 
16 /*!
17 Collidable objects are derived from the Object class. Their additional
18 feature is that they can collide with other collidable objects, earth and/or
19 particles depending on the implementation of CollidableObject::update.
20 */
21 
22 class CollidableObject : public AttachableObject {
23 	public:
24 		CollidableObject( void );
25 		virtual ~CollidableObject( void );
26 
27 	//////////
28 	// Health & Damage
29 
30 		/*! This method is called to determine the collision with another
31 		    collidable object or a particle at position \a x, \a c
32 		    \return \c true if the specified position is on or within the collision
33 	              rectangle of the object; \c false otherwise.
34 		*/
testHit(const int x,const int y)35 		bool testHit( const int x, const int y ) {
36 			if( ! m_collRectSet ) return false;
37 
38 			const bool hit =
39 			        x >= ROUND( m_pos.x ) + m_collRectX &&
40 			        x <= ROUND( m_pos.x ) + m_collRectX + m_collRectWidth - 1 &&
41 			        y >= ROUND( m_pos.y ) + m_collRectY &&
42 			        y <= ROUND( m_pos.y ) + m_collRectY + m_collRectHeight - 1;
43 
44 			DBG( 5 ) {
45 				if( ! hit ) {
46 					const real dx = m_pos.x - x,
47 					           dy = m_pos.y - y;
48 					INFO( "CollidableObject::testHit:no hit, dist = %6.1f\n"
49 					      "\tx : %3i, y : %3i\n"
50 					      "\tx1: %3i, y1: %3i\n"
51 					      "\tx2: %3i, y2: %3i\n",
52 					      SQRT_REAL( dx*dx + dy*dy ), x, y,
53 					      ROUND( m_pos.x ) + m_collRectX,
54 					      ROUND( m_pos.y ) + m_collRectY,
55 					      ROUND( m_pos.x ) + m_collRectX + m_collRectWidth  - 1,
56 					      ROUND( m_pos.y ) + m_collRectY + m_collRectHeight - 1 );
57 				}
58 			}
59 
60 			return hit;
61 		}
62 
63 		virtual bool testGrounded( void );
64 
65 		/*! This method is called when the object is damaged by the specified
66 		    particle \a p.
67 				Things that have to be done within this method:
68 				- add recoil to the object's force
69 				- calculate damage and subtract this value from \a m_health
70 				- play damage sample and set \a m_damageSampleTimeout to an appropriate value
71 				- destroy object if necessary
72 			\return the affected damage
73 
74 		    \todo Move the basic stuff into the base class CollidableObject.
75 		*/
76 
77 		virtual int applyDamage( const Particles::ParticleData& p );
78 		virtual int applyDamage( const int damage, Player* shooter );
79 
getHealth(void) const80 		Sint32 getHealth( void ) const { return m_health; }
getMass(void) const81 		real getMass( void ) const { return m_mass; }
getCollRectX(void) const82 		Sint32 getCollRectX( void ) const { return m_collRectX; }
getCollRectY(void) const83 		Sint32 getCollRectY( void ) const { return m_collRectY; }
getCollRectWidth(void) const84 		Sint32 getCollRectWidth( void ) const { return m_collRectWidth; }
getCollRectHeight(void) const85 		Sint32 getCollRectHeight( void ) const { return m_collRectHeight; }
86 
setHealth(const Sint32 health)87 		void setHealth( const Sint32 health ) { m_health = health; }
wasHurt(void) const88 		bool wasHurt( void ) const { return m_wasHurt; }
setWasHurt(const bool newWasHurt)89 		void setWasHurt( const bool newWasHurt ) { m_wasHurt = newWasHurt; }
90 		void setCollRect( void );
91 		void removeCollRect( void );
92 		bool testCollRect( void );
93 
94 		virtual void update( void );
95 		virtual bool preBallistics( const bool continueUpdate = true );
96 		virtual bool doBallistics( const bool continueUpdate = true );
97 		virtual bool postBallistics( const bool continueUpdate = true );
98 		virtual bool collidedWithObstacle( const Sint32 colX, const Sint32 colY );
99 
setFocusPriority(const int priority)100 		void setFocusPriority( const int priority ) { m_focusPriority = priority; }
getFocusPriority(void)101 		int getFocusPriority( void ) { return m_focusPriority; }
102 
103 		virtual void serialize( Uint8*& bufferPointer ) const;
104 		virtual void deserialize( Uint8*& bufferPointer );
105 		virtual Uint32 getSerializeBufferSize() const;
106 
107 		virtual void dump( std::ostream& out ) const;
108 
109 	protected:
110 		real m_mass;
111 		real m_bounceThresholdVel2,
112 		     m_bounceFactorHLVel,
113 		     m_bounceFactorLow,
114 		     m_bounceFactorHigh,
115 		     m_maxGroundedForce2,
116 		     m_maxGroundedForceVert;
117 		Sint32 m_collRectX, m_collRectY,
118 		       m_collRectWidth, m_collRectHeight;
119 		Sint32 m_health;
120 		Sint32 m_damageSampleTimeout;
121 		Sint32 m_focusPriority; /* local value which is _not_ serialized */
122 		bool m_collRectSet;
123 		bool m_firstUpdate;
124 		bool m_wasHurt;
125 };
126 
127 /**********************************************************/
128 
129 #endif // _COLLIDABLEOBJECT_HPP_
130