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