src/badguy/fish.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "badguy/fish.hpp"
00018 
00019 #include "sprite/sprite.hpp"
00020 #include "supertux/object_factory.hpp"
00021 #include "supertux/tile.hpp"
00022 
00023 static const float FISH_JUMP_POWER = -600;
00024 static const float FISH_WAIT_TIME = 1;
00025 
00026 Fish::Fish(const Reader& reader) :
00027   BadGuy(reader, "images/creatures/fish/fish.sprite", LAYER_TILES-1), 
00028   waiting(),
00029   stop_y(0)
00030 {
00031   physic.enable_gravity(true);
00032 }
00033 
00034 Fish::Fish(const Vector& pos) :
00035   BadGuy(pos, "images/creatures/fish/fish.sprite", LAYER_TILES-1), 
00036   waiting(),
00037   stop_y(0)
00038 {
00039   physic.enable_gravity(true);
00040 }
00041 
00042 void
00043 Fish::collision_solid(const CollisionHit& chit)
00044 {
00045   hit(chit);
00046 }
00047 
00048 HitResponse
00049 Fish::collision_badguy(BadGuy& , const CollisionHit& chit)
00050 {
00051   return hit(chit);
00052 }
00053 
00054 void
00055 Fish::draw(DrawingContext& context)
00056 {
00057   if(waiting.started())
00058     return;
00059 
00060   if (get_state() == STATE_FALLING) {
00061     sprite->set_action("down");
00062     sprite->draw(context, get_pos(), layer);
00063   }
00064   else if (get_state() == STATE_ACTIVE) {
00065     sprite->draw(context, get_pos(), layer);
00066   }
00067 }
00068 
00069 HitResponse
00070 Fish::hit(const CollisionHit& hit)
00071 {
00072   if(hit.top) {
00073     physic.set_velocity_y(0);
00074   }
00075 
00076   return CONTINUE;
00077 }
00078 
00079 void
00080 Fish::collision_tile(uint32_t tile_attributes)
00081 {
00082   if ((tile_attributes & Tile::WATER) && (physic.get_velocity_y() >= 0)) {
00083 
00084     // initialize stop position if uninitialized
00085     if (stop_y == 0) stop_y = get_pos().y + get_bbox().get_height();
00086 
00087     // stop when we have reached the stop position
00088     if (get_pos().y >= stop_y) {
00089       if(!frozen)
00090         start_waiting();
00091       movement = Vector(0, 0);
00092     }
00093 
00094   }
00095 }
00096 
00097 void
00098 Fish::active_update(float elapsed_time)
00099 {
00100   BadGuy::active_update(elapsed_time);
00101 
00102   // waited long enough?
00103   if(waiting.check()) {
00104     jump();
00105   }
00106 
00107   // set sprite
00108   if(!frozen)
00109     sprite->set_action(physic.get_velocity_y() < 0 ? "normal" : "down");
00110 
00111   // we can't afford flying out of the tilemap, 'cause the engine would remove us.
00112   if ((get_pos().y - 31.8) < 0) // too high, let us fall
00113   {
00114     physic.set_velocity_y(0);
00115     physic.enable_gravity(true);
00116   }
00117 }
00118 
00119 void
00120 Fish::start_waiting()
00121 {
00122   waiting.start(FISH_WAIT_TIME);
00123   set_colgroup_active(COLGROUP_DISABLED);
00124   physic.enable_gravity(false);
00125   physic.set_velocity_y(0);
00126 }
00127 
00128 void
00129 Fish::jump()
00130 {
00131   physic.set_velocity_y(FISH_JUMP_POWER);
00132   physic.enable_gravity(true);
00133   set_colgroup_active(COLGROUP_MOVING);
00134 }
00135 
00136 void
00137 Fish::freeze()
00138 {
00139   BadGuy::freeze();
00140   sprite->set_action(physic.get_velocity_y() < 0 ? "iced" : "iced-down");
00141   waiting.stop();
00142 }
00143 
00144 void
00145 Fish::unfreeze()
00146 { // does this happen at all? (or do fishes die when they fall frozen?)
00147   BadGuy::unfreeze();
00148   start_waiting();
00149 }
00150 
00151 bool
00152 Fish::is_freezable() const
00153 {
00154   return true;
00155 }
00156 
00157 /* EOF */

Generated on Mon Apr 14 03:38:18 2014 for SuperTux by  doxygen 1.5.1