- don't add new objects until end of Sector::action()
[supertux.git] / src / sector.h
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 Matthias Braun <matze@braunis.de
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #ifndef __SECTOR_H__
20 #define __SECTOR_H__
21
22 #include <string>
23 #include <vector>
24 #include "vector.h"
25 #include "badguy.h"
26 #include "special.h"
27 #include "musicref.h"
28 #include "screen/drawing_context.h"
29
30 class GameObject;
31 class InteractiveObject;
32 class Background;
33 class Player;
34 class Camera;
35 class Trampoline;
36 class FlyingPlatform;
37 class TileMap;
38 class Upgrade;
39 class Bullet;
40 class BadGuy;
41 class Vector;
42 class LispReader;
43 class Tile;
44
45 struct SpawnPoint
46 {
47   std::string name;
48   Vector pos;
49 };
50
51 /** This class holds a sector (a part of a level) and all the game objects
52  * (badguys, player, background, tilemap, ...)
53  */
54 class Sector
55 {
56 public:
57   Sector();
58   ~Sector();
59
60   /// read sector from lisp file
61   void parse(LispReader& reader);
62   void parse_old_format(LispReader& reader);
63   /// write sector to lisp file
64   void write(LispWriter& writer);
65
66   /// activates this sector (change music, intialize player class, ...)
67   void activate(const std::string& spawnpoint = "main");
68
69   void action(float elapsed_time);
70   void draw(DrawingContext& context);
71
72   /// adds a gameobject
73   void add_object(GameObject* object);
74
75   const std::string& get_name() const
76   { return name; }
77
78   void play_music(int musictype);
79   int get_music_type();
80   
81   /** Checks for all possible collisions. And calls the
82       collision_handlers, which the collision_objects provide for this
83       case (or not). */
84   void collision_handler();
85                                                                                 
86   void add_score(const Vector& pos, int s);
87   void add_bouncy_distro(const Vector& pos);
88   void add_broken_brick(const Vector& pos, Tile* tile);
89   void add_broken_brick_piece(const Vector& pos,
90       const Vector& movement, Tile* tile);
91   void add_bouncy_brick(const Vector& pos);
92                                                                                 
93   BadGuy* add_bad_guy(float x, float y, BadGuyKind kind);
94                                                                                 
95   void add_upgrade(const Vector& pos, Direction dir, UpgradeKind kind);
96   bool add_bullet(const Vector& pos, float xm, Direction dir);
97                                                                                 
98   /** Try to grab the coin at the given coordinates */
99   void trygrabdistro(const Vector& pos, int bounciness);
100                                                                                 
101   /** Try to break the brick at the given coordinates */
102   bool trybreakbrick(const Vector& pos, bool small);
103                                                                                 
104   /** Try to get the content out of a bonus box, thus emptying it */
105   void tryemptybox(const Vector& pos, Direction col_side);
106                                                                                 
107   /** Try to bumb a badguy that might we walking above Tux, thus shaking
108       the tile which the badguy is walking on an killing him this way */
109   void trybumpbadguy(const Vector& pos);
110
111   /** @evil@ */
112   static Sector* current()
113   { return _current; }
114
115 private:
116   void load_music();
117   
118   static Sector* _current;
119   
120   std::string name;
121
122   MusicRef level_song;
123   MusicRef level_song_fast;
124
125 public:
126   std::string song_title;
127   float gravity;
128
129   // some special objects, where we need direct access
130   Player* player;
131   TileMap* solids;
132   Background* background;
133   Camera* camera;
134   
135 private:
136   typedef std::vector<BadGuy*> BadGuys;
137   BadGuys badguys;
138   typedef std::vector<Trampoline*> Trampolines;
139   Trampolines trampolines;
140   typedef std::vector<FlyingPlatform*> FlyingPlatforms;
141   FlyingPlatforms flying_platforms;
142
143   std::vector<Upgrade*> upgrades;
144   std::vector<Bullet*> bullets;
145
146 public: // ugly
147   typedef std::vector<InteractiveObject*> InteractiveObjects;
148   InteractiveObjects interactive_objects;
149   typedef std::vector<GameObject*> GameObjects;
150   GameObjects gameobjects;
151   GameObjects gameobjects_new; // For newly created objects
152
153 private:
154   typedef std::vector<SpawnPoint*> SpawnPoints;
155   SpawnPoints spawnpoints;
156
157   int distro_counter;
158   bool counting_distros;
159   int currentmusic;        
160 };
161
162 #endif
163