kugelblitz update - the behaviour intended for the most basic version is almost done...
[supertux.git] / src / badguy / kugelblitz.cpp
1 //  $Id: Kugelblitz.cpp 2654 2005-06-29 14:16:22Z wansti $
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 Marek Moeckel <wansti@gmx.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include "kugelblitz.hpp"
23 #include "sector.hpp"
24 #include "object/tilemap.hpp"
25 #include "tile.hpp"
26
27 #define  LIFETIME 5
28 #define  MOVETIME 0.75
29 #define  BASE_SPEED 200
30 #define  RAND_SPEED 150
31
32 Kugelblitz::Kugelblitz(const lisp::Lisp& reader)
33     : groundhit_pos_set(false)
34 {
35   reader.get("x", start_position.x);
36   start_position.y = 0; //place above visible area
37   bbox.set_size(63.8, 63.8);
38   sprite = sprite_manager->create("kugelblitz");
39   sprite->set_action("falling");
40   physic.enable_gravity(false);
41 }
42
43 void
44 Kugelblitz::write(lisp::Writer& writer)
45 {
46   writer.start_list("kugelblitz");
47
48   writer.write_float("x", start_position.x);
49   writer.write_float("y", start_position.y);
50
51   writer.end_list("kugelblitz");
52 }
53
54 void
55 Kugelblitz::activate()
56 {
57   physic.set_velocity_y(-300);
58   physic.set_velocity_x(-20); //fall a little to the left
59   direction = 1;
60   dying = false;
61 }
62
63 HitResponse
64 Kugelblitz::collision_solid(GameObject& other, const CollisionHit& chit)
65 {
66   //TODO: Explode when Tux is hit
67   return hit(chit);
68 }
69
70 HitResponse
71 Kugelblitz::collision_badguy(BadGuy& other , const CollisionHit& chit)
72 {
73   //Let the Kugelblitz explode, too?
74   other.kill_fall();
75   return hit(chit);
76 }
77
78 HitResponse
79 Kugelblitz::hit(const CollisionHit& chit)
80 {
81   // hit floor?
82   if(chit.normal.y < -.5) {
83     if (!groundhit_pos_set)
84     {
85       pos_groundhit = get_pos();
86       groundhit_pos_set = true;
87     }
88     sprite->set_action("flying");
89     physic.set_velocity_y(0);
90     //Set random initial speed and direction
91     if ((rand() % 2) == 1) direction = 1; else direction = -1;
92     int speed = (BASE_SPEED + (rand() % RAND_SPEED)) * direction;
93     physic.set_velocity_x(speed);
94     movement_timer.start(MOVETIME);
95     lifetime.start(LIFETIME);
96
97   } else if(chit.normal.y < .5) { // bumped on roof
98     physic.set_velocity_y(0);
99   }
100
101   return CONTINUE;
102 }
103
104 void
105 Kugelblitz::active_update(float elapsed_time)
106 {
107   if (lifetime.check()) {
108     if (!dying) {
109       sprite->set_action("pop");
110       lifetime.start(0.2);
111       dying = true;
112     }
113     else remove_me();
114   }
115   else {
116     if (groundhit_pos_set) {
117       if (movement_timer.check()) {
118         if (direction == 1) direction = -1; else direction = 1;
119         int speed = (BASE_SPEED + (rand() % RAND_SPEED)) * direction;
120         physic.set_velocity_x(speed);
121         movement_timer.start(MOVETIME);
122       }
123     }
124     if (Sector::current()->solids->get_tile_at(get_pos())->getAttributes() == 16) {
125       //HIT WATER
126     }
127   }
128   BadGuy::active_update(elapsed_time);  
129 }
130
131 void
132 Kugelblitz::kill_fall()
133 {
134 }
135
136 IMPLEMENT_FACTORY(Kugelblitz, "kugelblitz")