Badguys now inherit from MovingSprite
[supertux.git] / src / badguy / willowisp.cpp
1 //  $Id$
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 "log.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   : BadGuy(reader, "images/creatures/willowisp/willowisp.sprite", LAYER_FLOATINGOBJECTS), mystate(STATE_IDLE), target_sector("main"), target_spawnpoint("main"), soundSource(0)
32 {
33   reader.get("sector", target_sector);
34   reader.get("spawnpoint", target_spawnpoint);
35
36   countMe = false;
37 }
38
39 WillOWisp::WillOWisp(const WillOWisp& other) 
40         : BadGuy(other), mystate(other.mystate), target_sector(other.target_sector), target_spawnpoint(other.target_spawnpoint)
41 {
42   soundSource = sound_manager->create_sound_source("sounds/willowisp.wav");
43 }
44
45 WillOWisp::~WillOWisp()
46 {
47   delete soundSource;
48 }
49
50 void
51 WillOWisp::write(lisp::Writer& writer)
52 {
53   writer.start_list("willowisp");
54
55   writer.write_float("x", start_position.x);
56   writer.write_float("y", start_position.y);
57   writer.write_string("sector", target_sector);
58   writer.write_string("spawnpoint", target_spawnpoint);
59
60   writer.end_list("willowisp");
61 }
62
63 void
64 WillOWisp::draw(DrawingContext& context)
65 {
66   sprite->draw(context, get_pos(), layer);
67   
68   context.push_target();
69   context.set_target(DrawingContext::LIGHTMAP);
70
71   sprite->draw(context, get_pos(), layer);
72   
73   context.pop_target();
74 }
75
76 void
77 WillOWisp::active_update(float elapsed_time)
78 {
79   Player* player = get_nearest_player();
80   if (!player) return;
81   Vector p1 = this->get_pos() + (this->get_bbox().p2 - this->get_bbox().p1) / 2;
82   Vector p2 = player->get_pos() + (player->get_bbox().p2 - player->get_bbox().p1) / 2;
83   Vector dist = (p2 - p1);
84
85   if (mystate == STATE_IDLE) {
86     if (dist.norm() <= TRACK_RANGE) {
87       mystate = STATE_TRACKING;
88     }
89   }
90   
91   if (mystate == STATE_TRACKING) {
92     if (dist.norm() <= VANISH_RANGE) {
93       Vector dir = dist.unit();
94       movement = dir*elapsed_time*FLYSPEED;
95     } else {
96       mystate = STATE_VANISHING;
97       sprite->set_action("vanishing", 1);
98     }
99     soundSource->set_position(get_pos());
100   }
101
102   if (mystate == STATE_WARPING) {
103     if(sprite->animation_done()) {
104       remove_me();
105     }
106   }
107
108   if (mystate == STATE_VANISHING) {
109     if(sprite->animation_done()) {
110       remove_me();
111     }
112   }
113   
114 }
115
116 void
117 WillOWisp::activate()
118 {
119   sprite->set_action("idle");
120
121   delete soundSource;
122   soundSource = sound_manager->create_sound_source("sounds/willowisp.wav");
123   if(!soundSource) {
124     log_warning << "Couldn't start WillOWisp sound" << std::endl;
125     return;
126   }
127   soundSource->set_position(get_pos());
128   soundSource->set_looping(true);
129   soundSource->set_gain(2.0);
130   soundSource->set_reference_distance(32);
131   soundSource->play();
132 }
133
134 void
135 WillOWisp::deactivate()
136 {
137   delete soundSource;
138   soundSource = 0;
139 }
140
141 void
142 WillOWisp::kill_fall()
143 {
144 }
145
146 HitResponse
147 WillOWisp::collision_player(Player& player, const CollisionHit& ) {
148   if(player.is_invincible()) return ABORT_MOVE;
149
150   if (mystate != STATE_TRACKING) return ABORT_MOVE;
151
152   mystate = STATE_WARPING;
153   sprite->set_action("warping", 1);
154
155   GameSession::current()->respawn(target_sector, target_spawnpoint);
156   sound_manager->play("sounds/warp.wav");
157
158   return CONTINUE;
159 }
160
161 IMPLEMENT_FACTORY(WillOWisp, "willowisp")
162