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