Forward declarations stink / Path is now serializable / Forward declarations stink
[supertux.git] / src / object / path.hpp
1 //  $Id$
2 // 
3 //  SuperTux Path
4 //  Copyright (C) 2005 Philipp <balinor@pnxs.de>
5 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 // 
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 //  02111-1307, USA.
21 #ifndef __PATH_HPP__
22 #define __PATH_HPP__
23
24 #include <string>
25 #include <list>
26 #include <map>
27
28 #include "math/vector.hpp"
29 #include "game_object.hpp"
30 #include "lisp/lisp.hpp"
31 #include "serializable.hpp"
32
33
34 /**
35  * Helper class that stores an individual node of a Path
36  */
37 class PathNode
38 {
39 public:
40   Vector position; /**< position (in pixels) of this node */
41   float time; /**< time (in seconds) to get to this node */
42 };
43
44
45 /**
46  * Path an object can travel along. Made up of multiple nodes of type PathNode.
47  */
48 class Path : public GameObject, public Serializable
49 {
50 public:
51   Path(const lisp::Lisp& reader);
52   ~Path();
53
54   virtual void update(float elapsed_time);
55   virtual void draw(DrawingContext& context);
56
57   virtual void write(lisp::Writer& writer);
58
59   const Vector& GetPosition();
60   const Vector& GetLastMovement();
61
62   const std::string GetName();
63
64   // WARNING: returns NULL if not found !
65   static Path* GetByName(const std::string& name);
66
67 private:
68   std::string name; /**< name this path can be referenced with, stored in PathRegistry */
69   bool circular; /**< true: start with the first node once the last one has been reached. false: path will stop at last node */
70   bool forward; /**< true: travel to nodes in the order they were defined. false: inverse order */
71   std::vector<PathNode> pathNodes; /**< list of nodes that make up this path */
72
73   Vector position; /**< current position */
74   Vector velocity; /**< current velocity */
75   Vector last_movement; /**< amount of pixels we moved in the last call to update */
76
77   int destinationNode; /**< current destination Node */
78   float timeToGo; /**< seconds until we arrive at the destination */
79
80   static std::map<std::string,Path*> registry;
81 };
82
83 #endif