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