577c2a1bbe3d51596408d8b85eb113d2af680ed3
[supertux.git] / src / object / wind.cpp
1 //  $Id$
2 //
3 //  SuperTux - Wind
4 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.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
20 #include <config.h>
21
22 #include "wind.hpp"
23 #include "video/drawing_context.hpp"
24 #include "object/player.hpp"
25 #include "object_factory.hpp"
26 #include "random_generator.hpp"
27 #include "sector.hpp"
28 #include "particles.hpp"
29 #include "scripting/wind.hpp"
30 #include "scripting/squirrel_util.hpp"
31 #include "lisp/lisp.hpp"
32
33 Wind::Wind(const lisp::Lisp& reader)
34   : blowing(true), acceleration(100), elapsed_time(0)
35 {
36   reader.get("name", name);
37   reader.get("x", bbox.p1.x);
38   reader.get("y", bbox.p1.y);
39   float w = 32, h = 32;
40   reader.get("width", w);
41   reader.get("height", h);
42   bbox.set_size(w, h);
43
44   reader.get("blowing", blowing);
45
46   float speed_x = 0, speed_y = 0;
47   reader.get("speed-x", speed_x);
48   reader.get("speed-y", speed_y);
49   speed = Vector(speed_x, speed_y);
50
51   reader.get("acceleration", acceleration);
52
53   set_group(COLGROUP_TOUCHABLE);
54 }
55
56 void
57 Wind::update(float elapsed_time)
58 {
59   this->elapsed_time = elapsed_time;
60
61   if (!blowing) return;
62
63   // TODO: nicer, configurable particles for wind?
64   if (systemRandom.rand(0, 100) < 20) {
65     // emit a particle
66     Vector ppos = Vector(systemRandom.randf(bbox.p1.x+8, bbox.p2.x-8), systemRandom.randf(bbox.p1.y+8, bbox.p2.y-8));
67     Vector pspeed = Vector(speed.x, speed.y);
68     Sector::current()->add_object(new Particles(ppos, 44, 46, pspeed, Vector(0,0), 1, Color(.4f, .4f, .4f), 3, .1f, LAYER_BACKGROUNDTILES+1));
69   }
70 }
71
72 void
73 Wind::draw(DrawingContext& )
74 {
75 }
76
77 HitResponse
78 Wind::collision(GameObject& other, const CollisionHit& )
79 {
80   if (!blowing) return ABORT_MOVE;
81
82   Player* player = dynamic_cast<Player*> (&other);
83   if (player) {
84     if (!player->on_ground()) {
85       player->add_velocity(speed * acceleration * elapsed_time, speed);
86     }
87   }
88
89   return ABORT_MOVE;
90 }
91
92 void
93 Wind::expose(HSQUIRRELVM vm, SQInteger table_idx)
94 {
95   if (name == "")
96     return;
97
98   Scripting::Wind* interface = new Scripting::Wind(this);
99   expose_object(vm, table_idx, interface, name, true);
100 }
101
102 void
103 Wind::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
104 {
105   if (name == "")
106     return;
107
108   Scripting::unexpose_object(vm, table_idx, name);
109 }
110
111 void
112 Wind::start()
113 {
114   blowing = true;
115 }
116
117 void
118 Wind::stop()
119 {
120   blowing = false;
121 }
122
123 IMPLEMENT_FACTORY(Wind, "wind");