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