Tentative checkin of tuxdev's "Object improvement patch, part 1"
[supertux.git] / src / object / weak_block.cpp
1 //  $Id$
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.hpp"
30 #include "random_generator.hpp"
31 #include "object/bullet.hpp"
32
33 WeakBlock::WeakBlock(const lisp::Lisp& lisp)
34   : MovingSprite(lisp, "images/objects/strawbox/strawbox.sprite", LAYER_TILES, COLGROUP_STATIC), state(STATE_NORMAL)
35 {
36   sprite->set_action("normal");
37   set_solid(true);
38 }
39
40 HitResponse
41 WeakBlock::collision(GameObject& other, const CollisionHit& )
42 {
43   switch (state) {
44
45     case STATE_NORMAL:
46       if (dynamic_cast<Bullet*>(&other)) {
47         startBurning();
48         return FORCE_MOVE;
49       }
50       return FORCE_MOVE;
51       break;
52
53     case STATE_BURNING:
54       return FORCE_MOVE;
55       break;
56
57     case STATE_DISINTEGRATING:
58       return FORCE_MOVE;
59       break;
60
61   }
62
63   log_debug << "unhandled state" << std::endl;
64   return FORCE_MOVE;
65 }
66
67 void
68 WeakBlock::update(float )
69 {
70   switch (state) {
71
72     case STATE_NORMAL:
73       break;
74
75     case STATE_BURNING:
76       if (sprite->animation_done()) {
77         state = STATE_DISINTEGRATING;
78         sprite->set_action("disintegrating", 1);
79         spreadHit();
80         set_solid(false);
81         set_group(COLGROUP_DISABLED);
82       }
83       break;
84
85     case STATE_DISINTEGRATING:
86       if (sprite->animation_done()) {
87         remove_me();
88         return;
89       }
90       break;
91
92   }
93 }
94
95 void
96 WeakBlock::startBurning()
97 {
98   if (state != STATE_NORMAL) return;
99   state = STATE_BURNING;
100   sprite->set_action("burning", 1);
101 }
102
103 void
104 WeakBlock::spreadHit()
105 {
106   Sector* sector = Sector::current();
107   if (!sector) {
108     log_debug << "no current sector" << std::endl;
109     return;
110   }
111   for(Sector::GameObjects::iterator i = sector->gameobjects.begin(); i != sector->gameobjects.end(); ++i) {
112     WeakBlock* wb = dynamic_cast<WeakBlock*>(*i);
113     if (!wb) continue;
114     if (wb == this) continue;
115     if (wb->state != STATE_NORMAL) continue;
116     float dx = fabsf(wb->get_pos().x - this->get_pos().x);
117     float dy = fabsf(wb->get_pos().y - this->get_pos().y);
118     if ((dx <= 32.5) && (dy <= 32.5)) wb->startBurning();
119   }
120 }
121
122
123 IMPLEMENT_FACTORY(WeakBlock, "weak_block");