fix collision against spikes too wide, fix paths, try to fix jumping on badguys that...
[supertux.git] / src / badguy / willowisp.cpp
1 //  $Id: willowisp.cpp 3117 2006-03-25 17:19:54Z sommer $
2 // 
3 //  SuperTux - "Will-O-Wisp" Badguy
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
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include "willowisp.hpp"
23 #include "msg.hpp"
24 #include "game_session.hpp"
25
26 static const float FLYSPEED = 64; /**< speed in px per second */
27 static const float TRACK_RANGE = 384; /**< at what distance to start tracking the player */
28 static const float VANISH_RANGE = 512; /**< at what distance to stop tracking and vanish */
29
30 WillOWisp::WillOWisp(const lisp::Lisp& reader)
31   : mystate(STATE_IDLE), target_sector("main"), target_spawnpoint("main"), soundSource(0)
32 {
33   reader.get("x", start_position.x);
34   reader.get("y", start_position.y);
35   reader.get("sector", target_sector);
36   reader.get("spawnpoint", target_spawnpoint);
37
38   bbox.set_size(32, 32);  
39   sprite = sprite_manager->create("images/creatures/willowisp/willowisp.sprite");
40   countMe = false;
41 }
42
43 WillOWisp::~WillOWisp()
44 {
45   delete soundSource;
46 }
47
48 void
49 WillOWisp::write(lisp::Writer& writer)
50 {
51   writer.start_list("willowisp");
52
53   writer.write_float("x", start_position.x);
54   writer.write_float("y", start_position.y);
55   writer.write_string("sector", target_sector);
56   writer.write_string("spawnpoint", target_spawnpoint);
57
58   writer.end_list("willowisp");
59 }
60
61 void
62 WillOWisp::active_update(float elapsed_time)
63 {
64   Player* player = get_nearest_player();
65   if (!player) return;
66   Vector p1 = this->get_pos() + (this->get_bbox().p2 - this->get_bbox().p1) / 2;
67   Vector p2 = player->get_pos() + (player->get_bbox().p2 - player->get_bbox().p1) / 2;
68   Vector dist = (p2 - p1);
69
70   if (mystate == STATE_IDLE) {
71     if (dist.norm() <= TRACK_RANGE) {
72       mystate = STATE_TRACKING;
73     }
74   }
75   
76   if (mystate == STATE_TRACKING) {
77     if (dist.norm() <= VANISH_RANGE) {
78       Vector dir = dist.unit();
79       movement = dir*elapsed_time*FLYSPEED;
80     } else {
81       mystate = STATE_VANISHING;
82       sprite->set_action("vanishing", 1);
83     }
84     soundSource->set_position(get_pos());
85   }
86
87   if (mystate == STATE_WARPING) {
88     if(sprite->animation_done()) {
89       remove_me();
90     }
91   }
92
93   if (mystate == STATE_VANISHING) {
94     if(sprite->animation_done()) {
95       remove_me();
96     }
97   }
98   
99 }
100
101 void
102 WillOWisp::activate()
103 {
104   sprite->set_action("idle");
105
106   delete soundSource;
107   soundSource = sound_manager->create_sound_source("sounds/rain.wav");
108   if(!soundSource) {
109     msg_warning("Couldn't start WillOWisp sound");
110     return;
111   }
112   soundSource->set_position(get_pos());
113   soundSource->set_looping(true);
114   soundSource->set_gain(2.0);
115   soundSource->set_reference_distance(32);
116   soundSource->play();
117 }
118
119 void
120 WillOWisp::deactivate()
121 {
122   delete soundSource;
123   soundSource = 0;
124 }
125
126 void
127 WillOWisp::kill_fall()
128 {
129 }
130
131 HitResponse
132 WillOWisp::collision_player(Player& player, const CollisionHit& ) {
133   if(player.is_invincible()) return ABORT_MOVE;
134
135   if (mystate != STATE_TRACKING) return ABORT_MOVE;
136
137   mystate = STATE_WARPING;
138   sprite->set_action("warping", 1);
139
140   GameSession::current()->respawn(target_sector, target_spawnpoint);
141   sound_manager->play("sounds/warp.wav");
142
143   return CONTINUE;
144 }
145
146 IMPLEMENT_FACTORY(WillOWisp, "willowisp")
147