src/object/physical_obj.h

Go to the documentation of this file.
00001 /******************************************************************************
00002  *  Wormux is a convivial mass murder game.
00003  *  Copyright (C) 2001-2004 Lawrence Azzoug.
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00018  ******************************************************************************
00019  * Abstract class used for physical object (object with a size, mass,
00020  * etc.). This object can have differents state : ready, is moving, or ghost
00021  * (is outside of the world).
00022  *
00023  * You can : make the object move (with collision test), change state, etc.
00024  * If the object go outside of the world, it become a ghost.
00025  *****************************************************************************/
00026 
00027 #ifndef PHYSICAL_OBJECT_H
00028 #define PHYSICAL_OBJECT_H
00029 
00030 #include "physics.h"
00031 #include "../tool/point.h"
00032 #include "../tool/rectangle.h"
00033 
00034 // Alive state
00035 typedef enum
00036 {
00037   ALIVE,
00038   DEAD,
00039   GHOST,
00040   DROWNED
00041 } alive_t;
00042 
00043 extern const double PIXEL_PER_METER;
00044 
00045 double MeterDistance (const Point2i &p1, const Point2i &p2);
00046 
00047 class PhysicalObj : public Physics
00048 {
00049 private:
00050   // collision management
00051   bool m_goes_through_wall;
00052   bool m_collides_with_characters;
00053   bool m_collides_with_objects;
00054 
00055   Point2i m_rebound_position;  
00056 protected:
00057   PhysicalObj* m_overlapping_object;
00058 
00059   virtual void CheckOverlapping();
00060 
00061 protected:
00062   std::string m_name;
00063 
00064   // Rectangle used for collision tests
00065   uint m_test_left, m_test_right, m_test_top, m_test_bottom;
00066 
00067   // Object size and position.
00068   uint m_width, m_height;
00069 
00070   std::string m_rebound_sound;
00071 
00072   alive_t m_alive;
00073   int life_points; // Only used by petrol barrel and bonus box (character use their own damage system for now..)
00074 
00075   bool m_allow_negative_y;
00076 
00077 public:
00078   PhysicalObj (const std::string &name, const std::string &xml_config="");
00079   virtual ~PhysicalObj ();
00080 
00081   //-------- Set position and size -------
00082 
00083   // Set/Get position
00084   void SetX (int x);
00085   void SetY (int y);
00086   void SetXY(const Point2i &position);
00087   int GetX() const;
00088   int GetY() const;
00089   const Point2i GetPosition() const;
00090 
00091   // Set/Get size
00092   void SetSize(const Point2i &newSize);
00093   int GetWidth() const;
00094   int GetHeight() const;
00095   Point2i GetSize() const;
00096 
00097   // Set/Get test rectangles
00098   void SetTestRect (uint left, uint right, uint top, uint bottom);
00099   const Rectanglei GetTestRect() const;   
00100   int GetTestWidth() const;
00101   int GetTestHeight() const;
00102 
00103   //----------- Access to datas (read only) ----------
00104   virtual const std::string &GetName() const { return m_name; }
00105   int GetCenterX() const;
00106   int GetCenterY() const;
00107   const Point2i GetCenter() const;
00108   const Rectanglei GetRect() const;
00109   bool GoesThroughWall() const { return m_goes_through_wall; }
00110 
00111   //----------- Physics related function ----------
00112 
00113   // Update position (and state) with current time
00114   void UpdatePosition();
00115 
00116   // Move the character until he gets out of the ground
00117   bool PutOutOfGround();
00118   bool PutOutOfGround(double direction); //Where direction is the angle of the direction
00119                                          // where the object is moved
00120 
00121   // Collision management
00122   void SetCollisionModel(bool goes_through_wall,
00123                          bool collides_with_characters,
00124                          bool collides_with_objects);
00125   void SetOverlappingObject(PhysicalObj* obj);
00126   virtual bool IsOverlapping(const PhysicalObj* obj) const;
00127 
00128   bool IsInVacuumXY(const Point2i &position, bool check_objects = true) const;
00129   bool IsInVacuum(const Point2i &offset, bool check_objects = true) const; // Relative to current position
00130   PhysicalObj* CollidedObjectXY(const Point2i & position) const;
00131   PhysicalObj* CollidedObject(const Point2i & offset) const; // Relative to current position
00132   bool FootsInVacuumXY(const Point2i & position) const;
00133   bool FootsInVacuum() const;
00134 
00135   bool FootsOnFloor(int y) const;
00136 
00137   bool IsInWater() const;
00138 
00139   // The object is outside of the world
00140   bool IsOutsideWorldXY(Point2i position) const;
00141   bool IsOutsideWorld(const Point2i &offset) const;
00142 
00143   // Refresh datas
00144   virtual void Refresh() = 0;
00145 
00146   // Draw the object
00147   virtual void Draw() = 0;
00148 
00149   // Damage handling
00150   void AddDamage(uint damage_points);
00151 
00152   //-------- state ----
00153   void Init();
00154   void Ghost();
00155   void Drown();
00156   void GoOutOfWater(); // usefull for supertux.
00157 
00158   virtual bool IsImmobile() const;
00159   bool IsDead() const;
00160   bool IsGhost() const;
00161   bool IsDrowned() const;
00162 
00163   // Est-ce que deux objets se touchent ? (utilise les rectangles de test)
00164   bool ObjTouche(const PhysicalObj &b) const;
00165 
00166   // Est-ce que le point p touche l'objet ?
00167   bool ObjTouche(const Point2i &p) const;
00168 
00169   bool PutRandomly(bool on_top_of_world, double min_dst_with_characters);
00170 
00171 protected:
00172   virtual void SignalRebound();
00173   virtual void SignalObjectCollision(PhysicalObj * obj);
00174   virtual void SignalGroundCollision();
00175   virtual void SignalCollision();
00176   virtual void SignalOutOfMap();
00177 
00178 private:
00179   //Renvoie la position du point de contact entre
00180   //l'obj et le terrain
00181   bool ContactPoint (int &x, int &y);
00182 
00183   void NotifyMove(Point2d oldPos, Point2d newPos);
00184 
00185   // The object fall directly to the ground (or become a ghost)
00186   void DirectFall();
00187 
00188   // Directly after a rebound, if we are stuck in a wall, we stop moving
00189   void CheckRebound();
00190 };
00191 
00192 #endif

Generated on Mon Jan 1 13:10:59 2007 for Wormux by  doxygen 1.4.7