Trampoline is now a Rock. WalkingBadguys can bounce on Trampoline.
[supertux.git] / src / badguy / walking_badguy.cpp
1 //  $Id$
2 //
3 //  SuperTux - WalkingBadguy
4 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.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  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "walking_badguy.hpp"
23 #include "log.hpp"
24
25
26 WalkingBadguy::WalkingBadguy(const Vector& pos, const std::string& sprite_name, const std::string& walk_left_action, const std::string& walk_right_action, int layer)
27         : BadGuy(pos, sprite_name, layer), walk_left_action(walk_left_action), walk_right_action(walk_right_action), walk_speed(80), max_drop_height(-1)
28 {
29 }
30
31 WalkingBadguy::WalkingBadguy(const Vector& pos, Direction direction, const std::string& sprite_name, const std::string& walk_left_action, const std::string& walk_right_action, int layer)
32         : BadGuy(pos, direction, sprite_name, layer), walk_left_action(walk_left_action), walk_right_action(walk_right_action), walk_speed(80), max_drop_height(-1)
33 {
34 }
35
36 WalkingBadguy::WalkingBadguy(const lisp::Lisp& reader, const std::string& sprite_name, const std::string& walk_left_action, const std::string& walk_right_action, int layer)
37         : BadGuy(reader, sprite_name, layer), walk_left_action(walk_left_action), walk_right_action(walk_right_action), walk_speed(80), max_drop_height(-1)
38 {
39 }
40
41 void
42 WalkingBadguy::write(lisp::Writer& writer)
43 {
44   writer.write_float("x", start_position.x);
45   writer.write_float("y", start_position.y);
46 }
47
48 void
49 WalkingBadguy::activate()
50 {
51   if(frozen)
52     return;
53   sprite->set_action(dir == LEFT ? walk_left_action : walk_right_action);
54   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
55   physic.set_velocity_x(dir == LEFT ? -walk_speed : walk_speed);
56 }
57
58 void
59 WalkingBadguy::active_update(float elapsed_time)
60 {
61   BadGuy::active_update(elapsed_time);
62
63   if (max_drop_height > -1) {
64     if (on_ground() && might_fall(max_drop_height+1))
65     {
66       turn_around();
67     }
68   }
69
70 }
71
72 void
73 WalkingBadguy::collision_solid(const CollisionHit& hit)
74 {
75
76   update_on_ground_flag(hit);
77
78   if (hit.top || hit.bottom) {
79     physic.set_velocity_y(0);
80   }
81
82   if ((hit.left && dir == LEFT) || (hit.right && dir == RIGHT)) {
83     turn_around();
84   }
85
86 }
87
88 HitResponse
89 WalkingBadguy::collision_badguy(BadGuy& , const CollisionHit& hit)
90 {
91
92   if ((hit.left && (dir == LEFT)) || (hit.right && (dir == RIGHT))) {
93     turn_around();
94   }
95
96   return CONTINUE;
97 }
98
99 void
100 WalkingBadguy::turn_around()
101 {
102   if(frozen)
103     return;
104   dir = dir == LEFT ? RIGHT : LEFT;
105   sprite->set_action(dir == LEFT ? walk_left_action : walk_right_action);
106   physic.set_velocity_x(-physic.get_velocity_x());
107 }
108
109 void
110 WalkingBadguy::freeze()
111 {
112   BadGuy::freeze();
113   physic.set_velocity_x(0);
114 }
115
116 void
117 WalkingBadguy::unfreeze()
118 {
119   BadGuy::unfreeze();
120   WalkingBadguy::activate();
121 }
122
123  
124 float 
125 WalkingBadguy::get_velocity_y() const
126 {
127   return physic.get_velocity_y();
128 }
129
130 void 
131 WalkingBadguy::set_velocity_y(float vy)
132 {
133   physic.set_velocity_y(vy);
134 }
135
136