3 // SuperTux - "Will-O-Wisp" Badguy
4 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
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.
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.
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
22 #include "willowisp.hpp"
24 #include "game_session.hpp"
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 */
30 WillOWisp::WillOWisp(const lisp::Lisp& reader)
31 : mystate(STATE_IDLE), target_sector("main"), target_spawnpoint("main"), soundSource(0)
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);
38 sprite = sprite_manager->create("images/creatures/willowisp/willowisp.sprite");
39 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
41 layer = LAYER_FLOATINGOBJECTS;
44 WillOWisp::~WillOWisp()
50 WillOWisp::write(lisp::Writer& writer)
52 writer.start_list("willowisp");
54 writer.write_float("x", start_position.x);
55 writer.write_float("y", start_position.y);
56 writer.write_string("sector", target_sector);
57 writer.write_string("spawnpoint", target_spawnpoint);
59 writer.end_list("willowisp");
63 WillOWisp::draw(DrawingContext& context)
65 sprite->draw(context, get_pos(), layer);
67 context.push_target();
68 context.set_target(DrawingContext::LIGHTMAP);
70 sprite->draw(context, get_pos(), layer);
76 WillOWisp::active_update(float elapsed_time)
78 Player* player = get_nearest_player();
80 Vector p1 = this->get_pos() + (this->get_bbox().p2 - this->get_bbox().p1) / 2;
81 Vector p2 = player->get_pos() + (player->get_bbox().p2 - player->get_bbox().p1) / 2;
82 Vector dist = (p2 - p1);
84 if (mystate == STATE_IDLE) {
85 if (dist.norm() <= TRACK_RANGE) {
86 mystate = STATE_TRACKING;
90 if (mystate == STATE_TRACKING) {
91 if (dist.norm() <= VANISH_RANGE) {
92 Vector dir = dist.unit();
93 movement = dir*elapsed_time*FLYSPEED;
95 mystate = STATE_VANISHING;
96 sprite->set_action("vanishing", 1);
98 soundSource->set_position(get_pos());
101 if (mystate == STATE_WARPING) {
102 if(sprite->animation_done()) {
107 if (mystate == STATE_VANISHING) {
108 if(sprite->animation_done()) {
116 WillOWisp::activate()
118 sprite->set_action("idle");
121 soundSource = sound_manager->create_sound_source("sounds/willowisp.wav");
123 log_warning << "Couldn't start WillOWisp sound" << std::endl;
126 soundSource->set_position(get_pos());
127 soundSource->set_looping(true);
128 soundSource->set_gain(2.0);
129 soundSource->set_reference_distance(32);
134 WillOWisp::deactivate()
141 WillOWisp::kill_fall()
146 WillOWisp::collision_player(Player& player, const CollisionHit& ) {
147 if(player.is_invincible()) return ABORT_MOVE;
149 if (mystate != STATE_TRACKING) return ABORT_MOVE;
151 mystate = STATE_WARPING;
152 sprite->set_action("warping", 1);
154 GameSession::current()->respawn(target_sector, target_spawnpoint);
155 sound_manager->play("sounds/warp.wav");
160 IMPLEMENT_FACTORY(WillOWisp, "willowisp")