Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / object / wind.cpp
1 //  SuperTux - Wind
2 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "object/wind.hpp"
18
19 #include "math/random_generator.hpp"
20 #include "object/particles.hpp"
21 #include "object/player.hpp"
22 #include "scripting/squirrel_util.hpp"
23 #include "scripting/wind.hpp"
24 #include "supertux/object_factory.hpp"
25 #include "supertux/sector.hpp"
26 #include "util/reader.hpp"
27 #include "video/drawing_context.hpp"
28
29 Wind::Wind(const Reader& reader)
30   : blowing(true), acceleration(100), elapsed_time(0)
31 {
32   reader.get("name", name);
33   reader.get("x", bbox.p1.x);
34   reader.get("y", bbox.p1.y);
35   float w = 32, h = 32;
36   reader.get("width", w);
37   reader.get("height", h);
38   bbox.set_size(w, h);
39
40   reader.get("blowing", blowing);
41
42   float speed_x = 0, speed_y = 0;
43   reader.get("speed-x", speed_x);
44   reader.get("speed-y", speed_y);
45   speed = Vector(speed_x, speed_y);
46
47   reader.get("acceleration", acceleration);
48
49   set_group(COLGROUP_TOUCHABLE);
50 }
51
52 void
53 Wind::update(float elapsed_time)
54 {
55   this->elapsed_time = elapsed_time;
56
57   if (!blowing) return;
58
59   // TODO: nicer, configurable particles for wind?
60   if (systemRandom.rand(0, 100) < 20) {
61     // emit a particle
62     Vector ppos = Vector(systemRandom.randf(bbox.p1.x+8, bbox.p2.x-8), systemRandom.randf(bbox.p1.y+8, bbox.p2.y-8));
63     Vector pspeed = Vector(speed.x, speed.y);
64     Sector::current()->add_object(new Particles(ppos, 44, 46, pspeed, Vector(0,0), 1, Color(.4f, .4f, .4f), 3, .1f,
65                                                 LAYER_BACKGROUNDTILES+1));
66   }
67 }
68
69 void
70 Wind::draw(DrawingContext& )
71 {
72 }
73
74 HitResponse
75 Wind::collision(GameObject& other, const CollisionHit& )
76 {
77   if (!blowing) return ABORT_MOVE;
78
79   Player* player = dynamic_cast<Player*> (&other);
80   if (player) {
81     if (!player->on_ground()) {
82       player->add_velocity(speed * acceleration * elapsed_time, speed);
83     }
84   }
85
86   return ABORT_MOVE;
87 }
88
89 void
90 Wind::expose(HSQUIRRELVM vm, SQInteger table_idx)
91 {
92   if (name == "")
93     return;
94
95   Scripting::Wind* interface = new Scripting::Wind(this);
96   expose_object(vm, table_idx, interface, name, true);
97 }
98
99 void
100 Wind::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
101 {
102   if (name == "")
103     return;
104
105   Scripting::unexpose_object(vm, table_idx, name);
106 }
107
108 void
109 Wind::start()
110 {
111   blowing = true;
112 }
113
114 void
115 Wind::stop()
116 {
117   blowing = false;
118 }
119
120 IMPLEMENT_FACTORY(Wind, "wind");
121
122 /* EOF */