Random stuff that I should have committed ages ago.
[supertux.git] / src / badguy / mrtree.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 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
20 #include <config.h>
21
22 #include "mrtree.hpp"
23
24 #include "stumpy.hpp"
25 #include "poisonivy.hpp"
26 #include "random_generator.hpp"
27 #include "object/sprite_particle.hpp"
28 #include "sector.hpp"
29 #include "lisp/writer.hpp"
30 #include "object_factory.hpp"
31 #include "audio/sound_manager.hpp"
32
33 #include <math.h>
34
35 static const float WALKSPEED = 100;
36
37 static const float POISONIVY_WIDTH = 32;
38 static const float POISONIVY_HEIGHT = 32;
39 static const float POISONIVY_Y_OFFSET = 24;
40
41
42 MrTree::MrTree(const lisp::Lisp& reader)
43   : WalkingBadguy(reader, "images/creatures/mr_tree/mr_tree.sprite","left","right")
44 {
45   walk_speed = WALKSPEED;
46   max_drop_height = 16;
47   sound_manager->preload("sounds/mr_tree.ogg");
48 }
49
50 void
51 MrTree::write(lisp::Writer& writer)
52 {
53   writer.start_list("mrtree");
54   WalkingBadguy::write(writer);
55   writer.end_list("mrtree");
56 }
57
58 bool
59 MrTree::collision_squished(GameObject& object)
60 {
61   // replace with Stumpy
62   Vector stumpy_pos = get_pos();
63   stumpy_pos.x += 20;
64   stumpy_pos.y += 25;
65   Stumpy* stumpy = new Stumpy(stumpy_pos, dir);
66   remove_me();
67   Sector::current()->add_object(stumpy);
68
69   // give Feedback
70   sound_manager->play("sounds/mr_tree.ogg", get_pos());
71   Player* player = dynamic_cast<Player*>(&object);
72   if (player) player->bounce(*this);
73
74   // spawn some particles
75   // TODO: provide convenience function in MovingSprite or MovingObject?
76   for (int px = (int)stumpy->get_bbox().p1.x; px < (int)stumpy->get_bbox().p2.x; px+=10) {
77     Vector ppos = Vector(px, stumpy->get_bbox().p1.y-5);
78     float angle = systemRandom.randf(-M_PI_2, M_PI_2);
79     float velocity = systemRandom.randf(45, 90);
80     float vx = sin(angle)*velocity;
81     float vy = -cos(angle)*velocity;
82     Vector pspeed = Vector(vx, vy);
83     Vector paccel = Vector(0, 100);
84     Sector::current()->add_object(new SpriteParticle("images/objects/particles/leaf.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
85   }
86
87   // spawn PoisonIvy
88   Vector leaf1_pos = Vector(stumpy_pos.x - POISONIVY_WIDTH - 1, stumpy_pos.y - POISONIVY_Y_OFFSET);
89   Rect leaf1_bbox = Rect(leaf1_pos.x, leaf1_pos.y, leaf1_pos.x + POISONIVY_WIDTH, leaf1_pos.y + POISONIVY_HEIGHT);
90   if (Sector::current()->is_free_of_movingstatics(leaf1_bbox, this)) {
91     PoisonIvy* leaf1 = new PoisonIvy(leaf1_bbox.p1, LEFT);
92     leaf1 = leaf1;
93     Sector::current()->add_object(leaf1);
94   }
95
96   // spawn PoisonIvy
97   Vector leaf2_pos = Vector(stumpy_pos.x + sprite->get_current_hitbox_width() + 1, stumpy_pos.y - POISONIVY_Y_OFFSET);
98   Rect leaf2_bbox = Rect(leaf2_pos.x, leaf2_pos.y, leaf2_pos.x + POISONIVY_WIDTH, leaf2_pos.y + POISONIVY_HEIGHT);
99   if (Sector::current()->is_free_of_movingstatics(leaf2_bbox, this)) {
100     PoisonIvy* leaf2 = new PoisonIvy(leaf2_bbox.p1, RIGHT);
101     leaf2 = leaf2;
102     Sector::current()->add_object(leaf2);
103   }
104
105   return true;
106 }
107
108 IMPLEMENT_FACTORY(MrTree, "mrtree")