changed worldmap format a bit to be more consistent with level format
[supertux.git] / src / badguy / flyingsnowball.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 Matthias Braun <matze@braunis.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
21 #include <config.h>
22 #include <stdio.h>
23
24 #include "flyingsnowball.h"
25
26 static const float FLYTIME = 1.0;
27 static const float FLYSPEED = 100.0;
28
29 FlyingSnowBall::FlyingSnowBall(const lisp::Lisp& reader)
30 {
31   reader.get("x", start_position.x);
32   reader.get("y", start_position.y);
33   bbox.set_size(31.8, 31.8);
34   sprite = sprite_manager->create("flyingsnowball");
35   physic.enable_gravity(false);
36 }
37
38 FlyingSnowBall::FlyingSnowBall(float pos_x, float pos_y)
39 {
40   start_position.x = pos_x;
41   start_position.y = pos_y;
42   bbox.set_size(31.8, 31.8);
43   sprite = sprite_manager->create("flyingsnowball");
44   physic.enable_gravity(false);
45 }
46
47 void
48 FlyingSnowBall::write(lisp::Writer& writer)
49 {
50   writer.start_list("flyingsnowball");
51
52   writer.write_float("x", start_position.x);
53   writer.write_float("y", start_position.y);
54
55   writer.end_list("flyingsnowball");
56 }
57
58 void 
59 FlyingSnowBall::activate()
60 {
61   sprite->set_action(dir == LEFT ? "left" : "right");
62   mode = FLY_UP;
63   physic.set_velocity_y(FLYSPEED);
64   timer.start(FLYTIME/2);
65 }
66
67 bool
68 FlyingSnowBall::collision_squished(Player& player)
69 {
70   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
71   kill_squished(player);
72   return true;
73 }
74
75 HitResponse
76 FlyingSnowBall::collision_solid(GameObject& , const CollisionHit& hit)
77 {
78   if(fabsf(hit.normal.y) > .5) { // hit floor or roof?
79     physic.set_velocity_y(0);
80   }
81
82   return CONTINUE;
83 }
84
85 void
86 FlyingSnowBall::active_update(float elapsed_time) 
87 {
88   if(timer.check()) {
89     if(mode == FLY_UP) {
90       mode = FLY_DOWN;
91       physic.set_velocity_y(-FLYSPEED);
92     } else if(mode == FLY_DOWN) {
93       mode = FLY_UP;
94       physic.set_velocity_y(FLYSPEED);
95     }
96     timer.start(FLYTIME);
97   }
98   movement=physic.get_movement(elapsed_time);
99   dir= Sector::current()->player->get_pos().x>get_pos().x?RIGHT:LEFT;
100   sprite->set_action(dir == LEFT ? "left" : "right");
101 }
102
103 IMPLEMENT_FACTORY(FlyingSnowBall, "flyingsnowball")