eae7d0c4dbbdf27281ca80ac9c8f44b5bc459d69
[supertux.git] / src / object / weak_block.cpp
1 //  SuperTux - Weak Block
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "object/weak_block.hpp"
19
20 #include "object/bullet.hpp"
21 #include "supertux/object_factory.hpp"
22 #include "supertux/sector.hpp"
23
24 #include <math.h>
25
26 WeakBlock::WeakBlock(const Reader& lisp)
27   : MovingSprite(lisp, "images/objects/strawbox/strawbox.sprite", LAYER_TILES, COLGROUP_STATIC), state(STATE_NORMAL)
28 {
29   sprite->set_action("normal");
30 }
31
32 HitResponse
33 WeakBlock::collision(GameObject& other, const CollisionHit& )
34 {
35   switch (state) {
36
37     case STATE_NORMAL:
38       if (dynamic_cast<Bullet*>(&other)) {
39         startBurning();
40         return FORCE_MOVE;
41       }
42       return FORCE_MOVE;
43       break;
44
45     case STATE_BURNING:
46       return FORCE_MOVE;
47       break;
48
49     case STATE_DISINTEGRATING:
50       return FORCE_MOVE;
51       break;
52
53   }
54
55   log_debug << "unhandled state" << std::endl;
56   return FORCE_MOVE;
57 }
58
59 void
60 WeakBlock::update(float )
61 {
62   switch (state) {
63
64     case STATE_NORMAL:
65       break;
66
67     case STATE_BURNING:
68       if (sprite->animation_done()) {
69         state = STATE_DISINTEGRATING;
70         sprite->set_action("disintegrating", 1);
71         spreadHit();
72         set_group(COLGROUP_DISABLED);
73       }
74       break;
75
76     case STATE_DISINTEGRATING:
77       if (sprite->animation_done()) {
78         remove_me();
79         return;
80       }
81       break;
82
83   }
84 }
85
86 void
87 WeakBlock::startBurning()
88 {
89   if (state != STATE_NORMAL) return;
90   state = STATE_BURNING;
91   sprite->set_action("burning", 1);
92 }
93
94 void
95 WeakBlock::spreadHit()
96 {
97   Sector* sector = Sector::current();
98   if (!sector) {
99     log_debug << "no current sector" << std::endl;
100     return;
101   }
102   for(Sector::GameObjects::iterator i = sector->gameobjects.begin(); i != sector->gameobjects.end(); ++i) {
103     WeakBlock* wb = dynamic_cast<WeakBlock*>(*i);
104     if (!wb) continue;
105     if (wb == this) continue;
106     if (wb->state != STATE_NORMAL) continue;
107     float dx = fabsf(wb->get_pos().x - this->get_pos().x);
108     float dy = fabsf(wb->get_pos().y - this->get_pos().y);
109     if ((dx <= 32.5) && (dy <= 32.5)) wb->startBurning();
110   }
111 }
112
113
114 /* EOF */