X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fbadguy%2Fmrtree.cpp;h=500bd30ecf84ac3ba87af862440a9f6d7d22104d;hb=dfaf28adf2756d68091c7e54ab16fcb174dfcc5c;hp=c4d33f113bda354bb1ad6f6a5d7a720b29cfab93;hpb=93a74f493825b4a4c76774f190b2fc39b2488d8f;p=supertux.git diff --git a/src/badguy/mrtree.cpp b/src/badguy/mrtree.cpp index c4d33f113..500bd30ec 100644 --- a/src/badguy/mrtree.cpp +++ b/src/badguy/mrtree.cpp @@ -1,12 +1,10 @@ -// $Id$ -// // SuperTux // Copyright (C) 2006 Matthias Braun // -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -14,161 +12,93 @@ // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// along with this program. If not, see . + +#include "badguy/mrtree.hpp" -#include +#include "audio/sound_manager.hpp" +#include "badguy/poisonivy.hpp" +#include "badguy/stumpy.hpp" +#include "math/random_generator.hpp" +#include "object/player.hpp" +#include "object/sprite_particle.hpp" +#include "supertux/object_factory.hpp" +#include "supertux/sector.hpp" -#include "mrtree.hpp" -#include "poisonivy.hpp" +#include -static const float WALKSPEED = 100; -static const float WALKSPEED_SMALL = 120; -static const float INVINCIBLE_TIME = 1; +static const float TREE_SPEED = 100; static const float POISONIVY_WIDTH = 32; static const float POISONIVY_HEIGHT = 32; static const float POISONIVY_Y_OFFSET = 24; - -MrTree::MrTree(const lisp::Lisp& reader) - : mystate(STATE_BIG) -{ - reader.get("x", start_position.x); - reader.get("y", start_position.y); - sprite = sprite_manager->create("images/creatures/mr_tree/mr_tree.sprite"); - sprite->set_action(dir == LEFT ? "large-left" : "large-right"); - bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height()); -} - -void -MrTree::write(lisp::Writer& writer) -{ - writer.start_list("mrtree"); - - writer.write_float("x", start_position.x); - writer.write_float("y", start_position.y); - - writer.end_list("mrtree"); -} - -void -MrTree::activate() +MrTree::MrTree(const Reader& reader) + : WalkingBadguy(reader, "images/creatures/mr_tree/mr_tree.sprite","left","right") { - if (mystate == STATE_BIG) { - physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED); - sprite->set_action(dir == LEFT ? "large-left" : "large-right"); - return; - } - if (mystate == STATE_INVINCIBLE) { - physic.set_velocity_x(0); - sprite->set_action(dir == LEFT ? "small-left" : "small-right"); - return; - } - if (mystate == STATE_NORMAL) { - physic.set_velocity_x(dir == LEFT ? -WALKSPEED_SMALL : WALKSPEED_SMALL); - sprite->set_action(dir == LEFT ? "small-left" : "small-right"); - return; - } + walk_speed = TREE_SPEED; + max_drop_height = 16; + SoundManager::current()->preload("sounds/mr_tree.ogg"); } -void -MrTree::active_update(float elapsed_time) +bool +MrTree::is_freezable() const { - if ((mystate == STATE_INVINCIBLE) && (invincible_timer.check())) { - mystate = STATE_NORMAL; - activate(); - } - - if (might_fall()) - { - dir = (dir == LEFT ? RIGHT : LEFT); - activate(); - } - - BadGuy::active_update(elapsed_time); + return true; } bool -MrTree::collision_squished(Player& player) +MrTree::collision_squished(GameObject& object) { - // if we're big, we shrink - if(mystate == STATE_BIG) { - mystate = STATE_INVINCIBLE; - invincible_timer.start(INVINCIBLE_TIME); - - float old_x_offset = sprite->get_current_hitbox_x_offset(); - float old_y_offset = sprite->get_current_hitbox_y_offset(); - activate(); - - // shrink bounding box and adjust sprite position to where the stump once was - bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height()); - Vector pos = get_pos(); - pos.x += sprite->get_current_hitbox_x_offset() - old_x_offset; - pos.y += sprite->get_current_hitbox_y_offset() - old_y_offset; - set_pos(pos); - - sound_manager->play("sounds/mr_tree.ogg", get_pos()); - player.bounce(*this); - - Vector leaf1_pos = Vector(pos.x - POISONIVY_WIDTH - 1, pos.y - POISONIVY_Y_OFFSET); - Rect leaf1_bbox = Rect(leaf1_pos.x, leaf1_pos.y, leaf1_pos.x + POISONIVY_WIDTH, leaf1_pos.y + POISONIVY_HEIGHT); - if (Sector::current()->is_free_space(leaf1_bbox)) { - PoisonIvy* leaf1 = new PoisonIvy(leaf1_bbox.p1.x, leaf1_bbox.p1.y, LEFT); - Sector::current()->add_object(leaf1); - } - Vector leaf2_pos = Vector(pos.x + sprite->get_current_hitbox_width() + 1, pos.y - POISONIVY_Y_OFFSET); - Rect leaf2_bbox = Rect(leaf2_pos.x, leaf2_pos.y, leaf2_pos.x + POISONIVY_WIDTH, leaf2_pos.y + POISONIVY_HEIGHT); - if (Sector::current()->is_free_space(leaf2_bbox)) { - PoisonIvy* leaf2 = new PoisonIvy(leaf2_bbox.p1.x, leaf2_bbox.p1.y, RIGHT); - Sector::current()->add_object(leaf2); - } - - return true; + // replace with Stumpy + Vector stumpy_pos = get_pos(); + stumpy_pos.x += 20; + stumpy_pos.y += 25; + auto stumpy = std::make_shared(stumpy_pos, dir); + remove_me(); + Sector::current()->add_object(stumpy); + + // give Feedback + SoundManager::current()->play("sounds/mr_tree.ogg", get_pos()); + Player* player = dynamic_cast(&object); + if (player) player->bounce(*this); + + // spawn some particles + // TODO: provide convenience function in MovingSprite or MovingObject? + for (int px = (int)stumpy->get_bbox().p1.x; px < (int)stumpy->get_bbox().p2.x; px+=10) { + Vector ppos = Vector(px, stumpy->get_bbox().p1.y-5); + float angle = graphicsRandom.randf(-M_PI_2, M_PI_2); + float velocity = graphicsRandom.randf(45, 90); + float vx = sin(angle)*velocity; + float vy = -cos(angle)*velocity; + Vector pspeed = Vector(vx, vy); + Vector paccel = Vector(0, 100); + Sector::current()->add_object(std::make_shared("images/objects/particles/leaf.sprite", + "default", + ppos, ANCHOR_MIDDLE, + pspeed, paccel, + LAYER_OBJECTS-1)); } - // if we're still invincible, we ignore the hit - if (mystate == STATE_INVINCIBLE) { - sound_manager->play("sounds/mr_treehit.ogg", get_pos()); - player.bounce(*this); - return true; + // spawn PoisonIvy + Vector leaf1_pos(stumpy_pos.x - POISONIVY_WIDTH - 1, stumpy_pos.y - POISONIVY_Y_OFFSET); + Rectf leaf1_bbox(leaf1_pos.x, leaf1_pos.y, leaf1_pos.x + POISONIVY_WIDTH, leaf1_pos.y + POISONIVY_HEIGHT); + if (Sector::current()->is_free_of_movingstatics(leaf1_bbox, this)) { + auto leaf1 = std::make_shared(leaf1_bbox.p1, LEFT); + leaf1->countMe = false; + Sector::current()->add_object(leaf1); } - // if we're small, we die - if (mystate == STATE_NORMAL) { - sprite->set_action(dir == LEFT ? "squished-left" : "squished-right"); - bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height()); - kill_squished(player); - return true; + // spawn PoisonIvy + Vector leaf2_pos(stumpy_pos.x + sprite->get_current_hitbox_width() + 1, stumpy_pos.y - POISONIVY_Y_OFFSET); + Rectf leaf2_bbox(leaf2_pos.x, leaf2_pos.y, leaf2_pos.x + POISONIVY_WIDTH, leaf2_pos.y + POISONIVY_HEIGHT); + if (Sector::current()->is_free_of_movingstatics(leaf2_bbox, this)) { + auto leaf2 = std::make_shared(leaf2_bbox.p1, RIGHT); + leaf2->countMe = false; + Sector::current()->add_object(leaf2); } - //TODO: exception? return true; } -HitResponse -MrTree::collision_solid(GameObject& , const CollisionHit& hit) -{ - if(fabsf(hit.normal.y) > .5) { - physic.set_velocity_y(0); - } else { - dir = dir == LEFT ? RIGHT : LEFT; - activate(); - } - - return CONTINUE; -} - -HitResponse -MrTree::collision_badguy(BadGuy& , const CollisionHit& hit) -{ - if(fabsf(hit.normal.x) > .8) { // left or right hit - dir = dir == LEFT ? RIGHT : LEFT; - activate(); - } - - return CONTINUE; -} - -IMPLEMENT_FACTORY(MrTree, "mrtree") - +/* EOF */