From 7d731b516a5181ce4f39b0339c7bce1cdc7c0baa Mon Sep 17 00:00:00 2001 From: Christoph Sommer Date: Thu, 8 Jun 2006 01:22:13 +0000 Subject: [PATCH] New Wind object: gently pushes players in one direction / ATTN: changes were made to the handle_input section of player.cpp, need testing! SVN-Revision: 3640 --- data/images/engine/editor/wind.png | Bin 0 -> 336 bytes data/levels/test/wind.stl | 91 +++++++++++++++++++++++++++++++++++++ src/object/player.cpp | 11 ++++- src/object/player.hpp | 5 ++ src/object/wind.cpp | 80 ++++++++++++++++++++++++++++++++ src/object/wind.hpp | 50 ++++++++++++++++++++ 6 files changed, 236 insertions(+), 1 deletion(-) create mode 100644 data/images/engine/editor/wind.png create mode 100644 data/levels/test/wind.stl create mode 100644 src/object/wind.cpp create mode 100644 src/object/wind.hpp diff --git a/data/images/engine/editor/wind.png b/data/images/engine/editor/wind.png new file mode 100644 index 0000000000000000000000000000000000000000..defb4c438d403f6320ad5218d8bf43958e13d222 GIT binary patch literal 336 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1SJ1Ryj={WI14-?iy0WWg+Z8+Vb&Z8pdfpR zr>`sfH8u`L7Ovwq=RN?1WJ_ElN}Tg^b5rw57@Uhz6H8K46v{J8G895GQWe}ieFNU7 zsOA9`9`kf@4DmSrHf%TFAp@Q-ku!gA{Oh0EyVqFz|8aMNYdm4f=KSVUC$Sbemv$-t zK36z9X(4+r%jEl5D# zgvx#-e!C{-dXiIrT}1bBR)!VF|46=#PG>OL9lOSFVsRDc2FK4?iLToX&tARqrEs0z zw 0) physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x)); + if (end_speed.x < 0) physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x)); + if (end_speed.y > 0) physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y)); + if (end_speed.y < 0) physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y)); +} + +void Player::bounce(BadGuy& ) { if(controller->hold(Controller::JUMP)) diff --git a/src/object/player.hpp b/src/object/player.hpp index 5a0a8b944..c212822c5 100644 --- a/src/object/player.hpp +++ b/src/object/player.hpp @@ -167,6 +167,11 @@ public: */ void add_velocity(const Vector& velocity); + /** + * Adds velocity to the player until given end speed is reached + */ + void add_velocity(const Vector& velocity, const Vector& end_speed); + void bounce(BadGuy& badguy); bool is_dead() const diff --git a/src/object/wind.cpp b/src/object/wind.cpp new file mode 100644 index 000000000..a1265c534 --- /dev/null +++ b/src/object/wind.cpp @@ -0,0 +1,80 @@ +// $Id$ +// +// SuperTux - Wind +// Copyright (C) 2006 Christoph Sommer +// +// 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 distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// 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. + +#include + +#include "wind.hpp" +#include "video/drawing_context.hpp" +#include "object/player.hpp" +#include "object_factory.hpp" +#include "random_generator.hpp" +#include "sector.hpp" +#include "particles.hpp" + +Wind::Wind(const lisp::Lisp& reader) : acceleration(100), elapsed_time(0) +{ + reader.get("x", bbox.p1.x); + reader.get("y", bbox.p1.y); + float w = 32, h = 32; + reader.get("width", w); + reader.get("height", h); + bbox.set_size(w, h); + + float speed_x = 0, speed_y = 0; + reader.get("speed-x", speed_x); + reader.get("speed-y", speed_y); + speed = Vector(speed_x, speed_y); + + reader.get("acceleration", acceleration); + + set_group(COLGROUP_TOUCHABLE); +} + +void +Wind::update(float elapsed_time) +{ + this->elapsed_time = elapsed_time; + // TODO: nicer, configurable particles for wind? + if (systemRandom.rand(0, 100) < 20) { + // emit a particle + Vector ppos = Vector(systemRandom.randf(bbox.p1.x+8, bbox.p2.x-8), systemRandom.randf(bbox.p1.y+8, bbox.p2.y-8)); + Vector pspeed = Vector(speed.x, -speed.y); + Sector::current()->add_object(new Particles(ppos, 44, 46, pspeed, Vector(0,0), 1, Color(.4, .4, .4), 3, .1, LAYER_BACKGROUNDTILES+1)); + } +} + +void +Wind::draw(DrawingContext& ) +{ +} + +HitResponse +Wind::collision(GameObject& other, const CollisionHit& ) +{ + Player* player = dynamic_cast (&other); + if (player) { + if (!player->on_ground()) { + player->add_velocity(speed * acceleration * elapsed_time, speed); + } + } + + return ABORT_MOVE; +} + +IMPLEMENT_FACTORY(Wind, "wind"); diff --git a/src/object/wind.hpp b/src/object/wind.hpp new file mode 100644 index 000000000..39e5fd594 --- /dev/null +++ b/src/object/wind.hpp @@ -0,0 +1,50 @@ +// $Id$ +// +// SuperTux - Wind +// Copyright (C) 2006 Christoph Sommer +// +// 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 distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// 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. + +#ifndef SUPERTUX_WIND_H +#define SUPERTUX_WIND_H + +#include +#include "moving_object.hpp" +#include "math/rect.hpp" +#include "sprite/sprite.hpp" + +class Player; + +/** + * Defines an area that will gently push Players in one direction + */ +class Wind : public MovingObject +{ +public: + Wind(const lisp::Lisp& reader); + + void update(float elapsed_time); + void draw(DrawingContext& context); + HitResponse collision(GameObject& other, const CollisionHit& hit); + +private: + Vector speed; + float acceleration; + + float elapsed_time; /**< stores last elapsed_time gotten at update() */ +}; + +#endif + -- 2.11.0