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