New GameObject SpriteParticle
[supertux.git] / src / object / weak_block.cpp
1 //  $Id: weak_block.cpp 3435 2006-04-26 02:13:42Z sik0fewl $
2 //
3 //  SuperTux - Weak Block
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 #include <config.h>
22
23 #include "weak_block.hpp"
24 #include "lisp/lisp.hpp"
25 #include "object_factory.hpp"
26 #include "player.hpp"
27 #include "sector.hpp"
28 #include "resources.hpp"
29 #include "sprite/sprite_manager.hpp"
30 #include "sprite/sprite.hpp"
31 #include "random_generator.hpp"
32 #include "object/bullet.hpp"
33
34 WeakBlock::WeakBlock(const lisp::Lisp& lisp)
35   : state(STATE_NORMAL)
36 {
37   lisp.get("x", bbox.p1.x);
38   lisp.get("y", bbox.p1.y);
39   bbox.set_size(32, 32);
40   sprite = sprite_manager->create("images/objects/strawbox/strawbox.sprite");
41   sprite->set_action("normal");
42   flags |= FLAG_SOLID;
43   set_group(COLGROUP_STATIC);
44 }
45
46 WeakBlock::~WeakBlock()
47 {
48   delete sprite;
49 }
50
51 HitResponse
52 WeakBlock::collision(GameObject& other, const CollisionHit& )
53 {
54   switch (state) {
55
56     case STATE_NORMAL:
57       if (dynamic_cast<Bullet*>(&other)) {
58         state = STATE_BURNING;
59         sprite->set_action("burning", 1);
60         return FORCE_MOVE;
61       }
62       return FORCE_MOVE;
63       break;
64
65     case STATE_BURNING:
66       return FORCE_MOVE;
67       break;
68
69     case STATE_DISINTEGRATING:
70       return FORCE_MOVE;
71       break;
72
73   }
74
75   log_debug << "unhandled state" << std::endl;
76   return FORCE_MOVE;
77 }
78
79 void
80 WeakBlock::draw(DrawingContext& context)
81 {
82   Vector pos = get_pos();
83   sprite->draw(context, pos, LAYER_TILES);
84 }
85
86 void
87 WeakBlock::update(float )
88 {
89   switch (state) {
90
91     case STATE_NORMAL:
92       break;
93
94     case STATE_BURNING:
95       if (sprite->animation_done()) {
96         state = STATE_DISINTEGRATING;
97         sprite->set_action("disintegrating", 1);
98         flags &= ~FLAG_SOLID;
99         set_group(COLGROUP_DISABLED);
100       }
101       break;
102
103     case STATE_DISINTEGRATING:
104       if (sprite->animation_done()) {
105         remove_me();
106         return;
107       }
108       break;
109
110   }
111 }
112
113 IMPLEMENT_FACTORY(WeakBlock, "weak_block");