Custom BonusBlocks now always run scripts if defined. The use of "script" as the...
[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 "object/explosion.hpp"
22 #include "supertux/object_factory.hpp"
23 #include "supertux/sector.hpp"
24 #include "sprite/sprite.hpp"
25 #include "sprite/sprite_manager.hpp"
26 #include "util/reader.hpp"
27
28 #include <math.h>
29
30 WeakBlock::WeakBlock(const Reader& lisp)
31 : MovingSprite(lisp, "images/objects/weak_block/strawbox.sprite", LAYER_TILES, COLGROUP_STATIC), state(STATE_NORMAL),
32 linked(true)
33 {
34   sprite->set_action("normal");
35   //Check if this weakblock destroys adjacent weakblocks
36   if(lisp.get("linked", linked)){
37     if(! linked){
38       sprite_name = "images/objects/weak_block/meltbox.sprite";
39       sprite = sprite_manager->create(sprite_name);
40       sprite->set_action("normal");
41     }
42   }
43 }
44
45 HitResponse
46 WeakBlock::collision_bullet(Bullet& bullet, const CollisionHit& hit)
47 {
48   switch (state) {
49                         
50     case STATE_NORMAL:
51       //Ensure only fire destroys weakblock
52       if(bullet.get_type() == FIRE_BONUS) {
53         startBurning();
54         bullet.remove_me();
55       }
56       //Other bullets ricochet
57       else {
58         bullet.ricochet(*this, hit);
59       }
60     break;
61                         
62     case STATE_BURNING:
63     case STATE_DISINTEGRATING:
64       break;
65                         
66     default:
67       log_debug << "unhandled state" << std::endl;
68       break;
69         }
70         
71         return FORCE_MOVE;
72 }
73
74 HitResponse
75 WeakBlock::collision(GameObject& other, const CollisionHit& hit)
76 {
77   switch (state) {
78                                 
79       case STATE_NORMAL:
80         if (Bullet* bullet = dynamic_cast<Bullet*> (&other)) {
81           return collision_bullet(*bullet, hit);
82         }
83         //Explosions destroy weakblocks as well
84         if (dynamic_cast<Explosion*> (&other)) {
85           startBurning();
86         }
87         break;
88                                 
89       case STATE_BURNING:
90       case STATE_DISINTEGRATING:
91         break;
92                                 
93       default:
94         log_debug << "unhandled state" << std::endl;
95         break;
96   }
97         
98   return FORCE_MOVE;
99 }
100
101 void
102 WeakBlock::update(float )
103 {
104   switch (state) {
105                                 
106       case STATE_NORMAL:
107         break;
108                                 
109       case STATE_BURNING:
110         if (sprite->animation_done()) {
111           state = STATE_DISINTEGRATING;
112           sprite->set_action("disintegrating", 1);
113           spreadHit();
114           set_group(COLGROUP_DISABLED);
115         }
116         break;
117                                 
118       case STATE_DISINTEGRATING:
119         if (sprite->animation_done()) {
120           remove_me();
121           return;
122         }
123         break;
124                                 
125   }
126 }
127
128 void
129 WeakBlock::startBurning()
130 {
131   if (state != STATE_NORMAL) return;
132   state = STATE_BURNING;
133   sprite->set_action("burning", 1);
134 }
135
136 void
137 WeakBlock::spreadHit()
138 {
139   //Destroy adjacent weakblocks if applicable
140   if(linked) {
141     Sector* sector = Sector::current();
142     if (!sector) {
143       log_debug << "no current sector" << std::endl;
144       return;
145     }
146     for(Sector::GameObjects::iterator i = sector->gameobjects.begin(); i != sector->gameobjects.end(); ++i) {
147       WeakBlock* wb = dynamic_cast<WeakBlock*>(*i);
148       if (!wb) continue;
149       if (wb == this) continue;
150       if (wb->state != STATE_NORMAL) continue;
151       float dx = fabsf(wb->get_pos().x - this->get_pos().x);
152       float dy = fabsf(wb->get_pos().y - this->get_pos().y);
153       if ((dx <= 32.5) && (dy <= 32.5)) wb->startBurning();
154     }
155   }
156 }
157
158
159 /* EOF */