merged new collision detection branch back into mainline
[supertux.git] / src / badguy / skullyhop.cpp
1 //  $Id$
2 //
3 //  SkullyHop - A Hopping Skull
4 //  Copyright (C) 2006 Christoph Sommer <supertux@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
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include "skullyhop.hpp"
23 #include "random_generator.hpp"
24
25 namespace {
26   const float VERTICAL_SPEED = -450;   /**< y-speed when jumping */
27   const float HORIZONTAL_SPEED = 220; /**< x-speed when jumping */
28   const float MIN_RECOVER_TIME = 0.1; /**< minimum time to stand still before starting a (new) jump */
29   const float MAX_RECOVER_TIME = 1.0; /**< maximum time to stand still before starting a (new) jump */
30 }
31
32 SkullyHop::SkullyHop(const lisp::Lisp& reader)
33         : BadGuy(reader, "images/creatures/skullyhop/skullyhop.sprite")
34 {
35 }
36
37 SkullyHop::SkullyHop(const Vector& pos, Direction d)
38         : BadGuy(pos, d, "images/creatures/skullyhop/skullyhop.sprite")
39 {
40 }
41
42 void
43 SkullyHop::write(lisp::Writer& writer)
44 {
45   writer.start_list("skullyhop");
46   writer.write_float("x", start_position.x);
47   writer.write_float("y", start_position.y);
48   writer.end_list("skullyhop");
49 }
50
51 void
52 SkullyHop::activate()
53 {
54   // initial state is JUMPING, because we might start airborne
55   state = JUMPING;
56   sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
57 }
58
59 void
60 SkullyHop::set_state(SkullyHopState newState)
61 {
62   if (newState == STANDING) {
63     physic.set_velocity_x(0);
64     physic.set_velocity_y(0);
65     sprite->set_action(dir == LEFT ? "standing-left" : "standing-right");
66
67     float recover_time = systemRandom.randf(MIN_RECOVER_TIME,MAX_RECOVER_TIME);
68     recover_timer.start(recover_time);
69   } else
70   if (newState == CHARGING) {
71     sprite->set_action(dir == LEFT ? "charging-left" : "charging-right", 1);
72   } else
73   if (newState == JUMPING) {
74     sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
75     physic.set_velocity_x(dir == LEFT ? -HORIZONTAL_SPEED : HORIZONTAL_SPEED);
76     physic.set_velocity_y(VERTICAL_SPEED);
77   }
78
79   state = newState;
80 }
81
82 bool
83 SkullyHop::collision_squished(Player& player)
84 {
85   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
86   kill_squished(player);
87   return true;
88 }
89
90 void
91 SkullyHop::collision_solid(const CollisionHit& hit)
92 {
93   // ignore collisions while standing still
94   if(state != JUMPING)
95     return;
96
97   // check if we hit the floor while falling
98   if(hit.bottom) {
99     set_state(STANDING);
100   }
101   // check if we hit the roof while climbing
102   if(hit.top) { 
103     physic.set_velocity_y(0);
104   }
105
106   // check if we hit left or right while moving in either direction
107   if(hit.left || hit.right) {
108     dir = dir == LEFT ? RIGHT : LEFT;
109     sprite->set_action(dir == LEFT ? "jumping-left" : "jumping-right");
110     physic.set_velocity_x(-0.25*physic.get_velocity_x());
111   }
112 }
113
114 HitResponse
115 SkullyHop::collision_badguy(BadGuy& , const CollisionHit& hit)
116 {
117   // behaviour for badguy collisions is the same as for collisions with solids
118   collision_solid(hit);
119
120   return CONTINUE;
121 }
122
123 void
124 SkullyHop::active_update(float elapsed_time)
125 {
126   BadGuy::active_update(elapsed_time);
127
128   // charge when fully recovered
129   if ((state == STANDING) && (recover_timer.check())) {
130     set_state(CHARGING);
131     return;
132   }
133
134   // jump as soon as charging animation completed
135   if ((state == CHARGING) && (sprite->animation_done())) {
136     set_state(JUMPING);
137     return;
138   } 
139 }
140
141 IMPLEMENT_FACTORY(SkullyHop, "skullyhop")