From 8a627e73d824b5a14249cfe066dc2fdc643ce28d Mon Sep 17 00:00:00 2001 From: Christoph Sommer Date: Sun, 23 Jul 2006 22:33:17 +0000 Subject: [PATCH] Tentative checkin of tuxdev's "Object improvement patch, part 1" SVN-Revision: 4076 --- src/badguy/mriceblock.cpp | 5 +-- src/game_object.cpp | 11 ++++- src/game_object.hpp | 22 +++++----- src/moving_object.cpp | 21 +++++++++- src/moving_object.hpp | 62 ++++++++++++++++++++++++++-- src/object/ambient_sound.cpp | 89 ++++++++++------------------------------- src/object/ambient_sound.hpp | 38 ++++++++++++------ src/object/block.cpp | 2 +- src/object/camera.cpp | 12 +++--- src/object/camera.hpp | 2 +- src/object/candle.cpp | 7 ++-- src/object/candle.hpp | 1 - src/object/display_effect.cpp | 19 ++++----- src/object/display_effect.hpp | 2 +- src/object/invisible_block.cpp | 2 +- src/object/moving_sprite.cpp | 6 +-- src/object/platform.cpp | 12 +++--- src/object/platform.hpp | 1 - src/object/player.cpp | 25 ++++++------ src/object/player.hpp | 2 +- src/object/portable.hpp | 19 +++++++++ src/object/rock.cpp | 3 +- src/object/scripted_object.cpp | 11 ++--- src/object/skull_tile.cpp | 4 +- src/object/text_object.cpp | 11 ++--- src/object/text_object.hpp | 2 +- src/object/thunderstorm.cpp | 2 +- src/object/thunderstorm.hpp | 1 - src/object/tilemap.cpp | 15 +------ src/object/tilemap.hpp | 1 - src/object/trampoline.cpp | 3 +- src/object/unstable_tile.cpp | 8 ++-- src/object/weak_block.cpp | 18 ++++----- src/object/wind.cpp | 5 ++- src/object/wind.hpp | 2 - src/scripting/ambient_sound.hpp | 4 +- src/sector.cpp | 12 +++--- src/worldmap/level.cpp | 3 +- src/worldmap/level.hpp | 1 - src/worldmap/worldmap.cpp | 8 ++-- 40 files changed, 254 insertions(+), 220 deletions(-) diff --git a/src/badguy/mriceblock.cpp b/src/badguy/mriceblock.cpp index 0e46ba619..c4c0b1674 100644 --- a/src/badguy/mriceblock.cpp +++ b/src/badguy/mriceblock.cpp @@ -210,10 +210,7 @@ MrIceBlock::set_state(IceState state) if(ice_state == state) return; - if(state == ICESTATE_FLAT) - flags |= FLAG_PORTABLE; - else - flags &= ~FLAG_PORTABLE; + set_portable(state == ICESTATE_FLAT); switch(state) { case ICESTATE_NORMAL: diff --git a/src/game_object.cpp b/src/game_object.cpp index 1f75d62bc..08c0eb148 100644 --- a/src/game_object.cpp +++ b/src/game_object.cpp @@ -22,9 +22,16 @@ #include "game_object.hpp" #include "object_remove_listener.hpp" -GameObject::GameObject() - : wants_to_die(false), remove_listeners(0), flags(0) + +GameObject::GameObject(std::string name) + : wants_to_die(false), remove_listeners(0), name(name) +{ +} + +GameObject::GameObject(const lisp::Lisp& lisp) + : wants_to_die(false), remove_listeners(0), name("") { + lisp.get("name" , name); } GameObject::~GameObject() diff --git a/src/game_object.hpp b/src/game_object.hpp index d9541ed1d..77c398058 100644 --- a/src/game_object.hpp +++ b/src/game_object.hpp @@ -21,6 +21,7 @@ #include #include "refcounter.hpp" +#include "lisp/lisp.hpp" class DrawingContext; class ObjectRemoveListener; @@ -39,7 +40,8 @@ class ObjectRemoveListener; class GameObject : public RefCounter { public: - GameObject(); + GameObject(std::string name = ""); + GameObject(const lisp::Lisp& lisp); virtual ~GameObject(); /** This function is called once per frame and allows the object to update @@ -78,18 +80,14 @@ public: remove_listeners = entry; } - // flags - enum { - /// the tile so you can stand on it - FLAG_SOLID = (1 << 0), - /// the object can be carried around (inherits from Portable) - FLAG_PORTABLE = (1 << 1) - }; - - int get_flags() const + std::string get_name() const { - return flags; + return name; } + // --- BEGIN METHODS TO EXPOSE TO SQUIRREL --- // + //void set_visible(bool visible); + //bool is_visible(); + // --- END METHODS TO EXPOSE TO SQUIRREL --- // private: /** this flag indicates if the object should be removed at the end of the @@ -105,7 +103,7 @@ private: RemoveListenerListEntry* remove_listeners; protected: - int flags; + std::string name; /**< user-defined name for use in scripts or empty string if not scriptable */ }; #endif /*SUPERTUX_GAMEOBJECT_H*/ diff --git a/src/moving_object.cpp b/src/moving_object.cpp index f71e909d0..07be14103 100644 --- a/src/moving_object.cpp +++ b/src/moving_object.cpp @@ -20,9 +20,26 @@ #include "moving_object.hpp" -MovingObject::MovingObject() +MovingObject::MovingObject(std::string name) : + GameObject(name), bbox(0, 0, 0, 0), group(COLGROUP_MOVING), solid(false) +{ +} + +MovingObject::MovingObject(const lisp::Lisp& lisp) : + GameObject(lisp), bbox(0, 0, 0, 0), group(COLGROUP_MOVING) +{ + lisp.get("x", bbox.p1.x); + lisp.get("y", bbox.p1.y); + lisp.get("w", bbox.p2.x); + lisp.get("h", bbox.p2.y); + lisp.get("solid", solid); + bbox.p2.x+=bbox.p1.x; + bbox.p2.y+=bbox.p1.y; +} + +MovingObject::MovingObject(Rect bbox, CollisionGroup group, bool solid) : + bbox(bbox), group(group), solid(solid) { - group = COLGROUP_MOVING; } MovingObject::~MovingObject() diff --git a/src/moving_object.hpp b/src/moving_object.hpp index 22a8c3e9c..ae0e1d753 100644 --- a/src/moving_object.hpp +++ b/src/moving_object.hpp @@ -77,7 +77,8 @@ enum CollisionGroup { class MovingObject : public GameObject { public: - MovingObject(); + MovingObject(std::string name = ""); + MovingObject(const lisp::Lisp& lisp); virtual ~MovingObject(); /** this function is called when the object collided with something solid */ @@ -135,11 +136,11 @@ public: * using this function. There are no collision detection checks performed * here so bad things could happen. */ - virtual void set_size(float w, float h) + /*virtual void set_size(float w, float h) { dest.set_size(w, h); bbox.set_size(w, h); - } + }*/ CollisionGroup get_group() const { @@ -151,7 +152,60 @@ public: this->group = group; } + // --- BEGIN METHODS TO EXPOSE TO SQUIRREL --- // + void set_solid(bool solid) + { + this->solid = solid; + } + bool is_solid() const + { + return solid; + } + void move(float x, float y) + { + bbox.move(Vector(x, y)); + } + void set_pos(float x, float y) + { + set_pos(Vector(x, y)); + } + float get_pos_x() const + { + return bbox.get_left(); + } + float get_pos_y() const + { + return bbox.get_top(); + } + void set_size(float w, float h) + { + dest.set_size(w, h); + bbox.set_size(w, h); + } + float get_width() const + { + return bbox.get_width(); + } + float get_height() const + { + return bbox.get_height(); + } + void set_velocity(float x, float y) + { + movement = Vector(x, y); + } + float get_velocity_x() const + { + return movement.x; + } + float get_velocity_y() const + { + return movement.y; + } + // --- END METHODS TO EXPOSE TO SQUIRREL --- // + protected: + MovingObject(Rect bbox, CollisionGroup group, bool solid); friend class Sector; friend class CollisionGrid; friend class Platform; @@ -175,6 +229,8 @@ private: * during collision detection */ Rect dest; + + bool solid; /**< true if this object should be considered when doing collision detection */ }; #endif diff --git a/src/object/ambient_sound.cpp b/src/object/ambient_sound.cpp index b4231863f..a4a26e7a5 100644 --- a/src/object/ambient_sound.cpp +++ b/src/object/ambient_sound.cpp @@ -32,29 +32,10 @@ #include "log.hpp" #include "scripting/squirrel_util.hpp" -AmbientSound::AmbientSound(const lisp::Lisp& lisp) +AmbientSound::AmbientSound(const lisp::Lisp& lisp) : + MovingObject(lisp), sample(""), sound_source(0), latency(0), + distance_factor(0), distance_bias(0), maximumvolume(1), currentvolume(0) { - name=""; - position.x = 0; - position.y = 0; - - dimension.x = 0; - dimension.y = 0; - - distance_factor = 0; - distance_bias = 0; - maximumvolume = 1; - sample = ""; - currentvolume = 0; - - if (!(lisp.get("x", position.x)&&lisp.get("y", position.y))) { - log_warning << "No Position in ambient_sound" << std::endl; - } - - lisp.get("name" , name); - lisp.get("width" , dimension.x); - lisp.get("height", dimension.y); - lisp.get("distance_factor",distance_factor); lisp.get("distance_bias" ,distance_bias ); lisp.get("sample" ,sample ); @@ -62,9 +43,8 @@ AmbientSound::AmbientSound(const lisp::Lisp& lisp) // set dimension to zero if smaller than 64, which is default size in flexlay - if ((dimension.x <= 64) || (dimension.y <= 64)) { - dimension.x = 0; - dimension.y = 0; + if ((get_width() <= 64) || (get_height() <= 64)) { + set_size(0, 0); } // square all distances (saves us a sqrt later) @@ -80,23 +60,14 @@ AmbientSound::AmbientSound(const lisp::Lisp& lisp) silence_distance = 1/distance_factor; lisp.get("silence_distance",silence_distance); - - sound_source = 0; // not playing at the beginning - latency=0; } -AmbientSound::AmbientSound(Vector pos, float factor, float bias, float vol, std::string file) +AmbientSound::AmbientSound(Vector pos, float factor, float bias, float vol, std::string file) : + sample(file), sound_source(0), latency(0), distance_factor(factor*factor), + distance_bias(bias*bias), maximumvolume(vol), currentvolume(0) { - position.x=pos.x; - position.y=pos.y; - - dimension.x=0; - dimension.y=0; - - distance_factor=factor*factor; - distance_bias=bias*bias; - maximumvolume=vol; - sample=file; + bbox.p1 = pos; + bbox.p2 = pos; // set default silence_distance @@ -104,9 +75,6 @@ AmbientSound::AmbientSound(Vector pos, float factor, float bias, float vol, std: silence_distance = 10e99; else silence_distance = 1/distance_factor; - - sound_source = 0; // not playing at the beginning - latency=0; } AmbientSound::~AmbientSound() { @@ -156,10 +124,10 @@ AmbientSound::update(float deltat) py=Sector::current()->player->get_pos().y; // Relate to which point in the area - rx=pxset_gain(currentvolume*maximumvolume); if (sqrdistance>=silence_distance && currentvolume<1e-3) - stop_playing(); + stop_playing(); latency=0; } else { if (sqrdistance (this); - expose_object(vm, table_idx, interface, name, false); + if(name.empty()) return; + expose_object(vm, table_idx, dynamic_cast(this), name, false); } void AmbientSound::unexpose(HSQUIRRELVM vm, SQInteger table_idx) { + if(name.empty()) return; Scripting::unexpose_object(vm, table_idx, name); } -void -AmbientSound::set_pos(float x, float y){ - position.x = x; - position.y = y; -} - -float -AmbientSound::get_pos_x(){; - return position.x; -} - -float -AmbientSound::get_pos_y(){ - return position.y; -} - IMPLEMENT_FACTORY(AmbientSound, "ambient_sound"); diff --git a/src/object/ambient_sound.hpp b/src/object/ambient_sound.hpp index fc349236f..9adf724e3 100644 --- a/src/object/ambient_sound.hpp +++ b/src/object/ambient_sound.hpp @@ -43,7 +43,7 @@ #ifndef __AMBIENT_SOUND_H__ #define __AMBIENT_SOUND_H__ -#include "game_object.hpp" +#include "moving_object.hpp" #include "resources.hpp" #include "player.hpp" #include "script_interface.hpp" @@ -51,27 +51,43 @@ class SoundSource; -class AmbientSound : public GameObject, public ScriptInterface, public Scripting::AmbientSound +class AmbientSound : public MovingObject, public ScriptInterface, public Scripting::AmbientSound { public: AmbientSound(const lisp::Lisp& lisp); AmbientSound(Vector pos, float factor, float bias, float vol, std::string file); ~AmbientSound(); - void set_pos(Vector newpos) + /*void set_pos(Vector newpos) { position=newpos; } - const Vector get_pos() const + const Vector &get_pos() const { - return position; + return get_pos(); + }*/ + + // --- BEGIN METHODS TO EXPOSE TO SQUIRREL --- // + void set_pos(float x, float y) + { + MovingObject::set_pos(x, y); + } + + float get_pos_x() const + { + return MovingObject::get_pos_x(); } - // --- Scripting Interface --- + float get_pos_y() const + { + return MovingObject::get_pos_y(); + } + // --- END METHODS TO EXPOSE TO SQUIRREL --- // - void set_pos(float x, float y); - float get_pos_x(); - float get_pos_y(); + HitResponse collision(GameObject&, const CollisionHit&) + { + return ABORT_MOVE; + } protected: virtual void hit(Player& player); @@ -82,10 +98,6 @@ protected: virtual void expose(HSQUIRRELVM vm, SQInteger table_idx); virtual void unexpose(HSQUIRRELVM vm, SQInteger table_idx); private: - std::string name; /**< user-defined name for use in scripts or empty string if not scriptable */ - Vector position; - Vector dimension; - std::string sample; SoundSource* sound_source; int latency; diff --git a/src/object/block.cpp b/src/object/block.cpp index 4bd14094b..c5b0b7a6c 100644 --- a/src/object/block.cpp +++ b/src/object/block.cpp @@ -53,7 +53,7 @@ Block::Block(Sprite* newsprite) { bbox.set_size(32, 32.1); set_group(COLGROUP_STATIC); - flags |= FLAG_SOLID; + set_solid(true); sound_manager->preload("sounds/upgrade.wav"); sound_manager->preload("sounds/brick.wav"); } diff --git a/src/object/camera.cpp b/src/object/camera.cpp index ef22b8f11..ae6fecb2d 100644 --- a/src/object/camera.cpp +++ b/src/object/camera.cpp @@ -39,10 +39,10 @@ #include "path.hpp" #include "path_walker.hpp" -Camera::Camera(Sector* newsector) - : sector(newsector), do_backscrolling(true), scrollchange(NONE) +Camera::Camera(Sector* newsector, std::string name) : + GameObject(name), mode(NORMAL), sector(newsector), do_backscrolling(true), + scrollchange(NONE) { - mode = NORMAL; } Camera::~Camera() @@ -52,14 +52,16 @@ Camera::~Camera() void Camera::expose(HSQUIRRELVM vm, SQInteger table_idx) { + if(name.empty()) return; Scripting::Camera* interface = new Scripting::Camera(this); - expose_object(vm, table_idx, interface, "Camera", true); + expose_object(vm, table_idx, interface, name, true); } void Camera::unexpose(HSQUIRRELVM vm, SQInteger table_idx) { - Scripting::unexpose_object(vm, table_idx, "Camera"); + if(name.empty()) return; + Scripting::unexpose_object(vm, table_idx, name); } const Vector& diff --git a/src/object/camera.hpp b/src/object/camera.hpp index de6c43dae..73e6d3c9e 100644 --- a/src/object/camera.hpp +++ b/src/object/camera.hpp @@ -42,7 +42,7 @@ class PathWalker; class Camera : public GameObject, public Serializable, public ScriptInterface { public: - Camera(Sector* sector); + Camera(Sector* sector, std::string name = ""); virtual ~Camera(); /// parse camera mode from lisp file diff --git a/src/object/candle.cpp b/src/object/candle.cpp index 0279167ab..924bc38a3 100644 --- a/src/object/candle.cpp +++ b/src/object/candle.cpp @@ -27,9 +27,8 @@ #include "object_factory.hpp" Candle::Candle(const lisp::Lisp& lisp) - : MovingSprite(lisp, "images/objects/candle/candle.sprite", LAYER_BACKGROUNDTILES+1, COLGROUP_DISABLED), burning(true), name("") + : MovingSprite(lisp, "images/objects/candle/candle.sprite", LAYER_BACKGROUNDTILES+1, COLGROUP_DISABLED), burning(true) { - lisp.get("name", name); lisp.get("burning", burning); if (burning) { @@ -48,7 +47,7 @@ Candle::collision(GameObject&, const CollisionHit& ) void Candle::expose(HSQUIRRELVM vm, SQInteger table_idx) { - if (name == "") return; + if (name.empty()) return; Scripting::Candle* interface = new Scripting::Candle(this); expose_object(vm, table_idx, interface, name, true); } @@ -56,7 +55,7 @@ Candle::expose(HSQUIRRELVM vm, SQInteger table_idx) void Candle::unexpose(HSQUIRRELVM vm, SQInteger table_idx) { - if (name == "") return; + if (name.empty()) return; Scripting::unexpose_object(vm, table_idx, name); } diff --git a/src/object/candle.hpp b/src/object/candle.hpp index 9c3547a6f..6b40b3e03 100644 --- a/src/object/candle.hpp +++ b/src/object/candle.hpp @@ -47,7 +47,6 @@ public: private: bool burning; /**< true if candle is currently lighted */ - std::string name; /**< user-defined name for use in scripts or empty string if not scriptable */ }; diff --git a/src/object/display_effect.cpp b/src/object/display_effect.cpp index ca3125d6e..4e6a35265 100644 --- a/src/object/display_effect.cpp +++ b/src/object/display_effect.cpp @@ -27,10 +27,10 @@ static const float BORDER_SIZE = 75; -DisplayEffect::DisplayEffect() - : screen_fade(NO_FADE), screen_fadetime(0), screen_fading(0), - border_fade(NO_FADE), border_fadetime(0), border_size(0), - black(false), borders(false) +DisplayEffect::DisplayEffect(std::string name) : + GameObject(name), screen_fade(NO_FADE), screen_fadetime(0), screen_fading(0), + border_fade(NO_FADE), border_fadetime(0), border_size(0), black(false), + borders(false) { } @@ -41,18 +41,15 @@ DisplayEffect::~DisplayEffect() void DisplayEffect::expose(HSQUIRRELVM vm, SQInteger table_idx) { - Scripting::DisplayEffect* interface = static_cast (this); - expose_object(vm, table_idx, interface, "Effect", false); + if (name.empty()) return; + expose_object(vm, table_idx, dynamic_cast(this), name, false); } void DisplayEffect::unexpose(HSQUIRRELVM vm, SQInteger table_idx) { - try { - Scripting::unexpose_object(vm, table_idx, "Effect"); - } catch(...) { - // for now... - } + if (name.empty()) return; + Scripting::unexpose_object(vm, table_idx, name); } void diff --git a/src/object/display_effect.hpp b/src/object/display_effect.hpp index 40510cc00..582dcbe9d 100644 --- a/src/object/display_effect.hpp +++ b/src/object/display_effect.hpp @@ -28,7 +28,7 @@ class DisplayEffect : public GameObject, public Scripting::DisplayEffect, public ScriptInterface { public: - DisplayEffect(); + DisplayEffect(std::string name = ""); virtual ~DisplayEffect(); void expose(HSQUIRRELVM vm, SQInteger table_idx); diff --git a/src/object/invisible_block.cpp b/src/object/invisible_block.cpp index 73ccf5612..621dfb4de 100644 --- a/src/object/invisible_block.cpp +++ b/src/object/invisible_block.cpp @@ -69,7 +69,7 @@ InvisibleBlock::hit(Player& ) sprite->set_action("empty"); start_bounce(); - flags |= FLAG_SOLID; + set_solid(true); set_group(COLGROUP_STATIC); visible = true; } diff --git a/src/object/moving_sprite.cpp b/src/object/moving_sprite.cpp index 953d55691..8dc5376ea 100644 --- a/src/object/moving_sprite.cpp +++ b/src/object/moving_sprite.cpp @@ -45,7 +45,7 @@ MovingSprite::MovingSprite(const Vector& pos, const std::string& sprite_name, in } MovingSprite::MovingSprite(const lisp::Lisp& reader, const Vector& pos, int layer, CollisionGroup collision_group) - : layer(layer) + : MovingObject(reader), layer(layer) { bbox.set_pos(pos); if (!reader.get("sprite", sprite_name)) throw std::runtime_error("no sprite name set"); @@ -55,7 +55,7 @@ MovingSprite::MovingSprite(const lisp::Lisp& reader, const Vector& pos, int laye } MovingSprite::MovingSprite(const lisp::Lisp& reader, const std::string& sprite_name, int layer, CollisionGroup collision_group) - : sprite_name(sprite_name), layer(layer) + : MovingObject(reader), sprite_name(sprite_name), layer(layer) { if (!reader.get("x", bbox.p1.x)) throw std::runtime_error("no x position set"); if (!reader.get("y", bbox.p1.y)) throw std::runtime_error("no y position set"); @@ -65,7 +65,7 @@ MovingSprite::MovingSprite(const lisp::Lisp& reader, const std::string& sprite_n } MovingSprite::MovingSprite(const lisp::Lisp& reader, int layer, CollisionGroup collision_group) - : layer(layer) + : MovingObject(reader), layer(layer) { if (!reader.get("x", bbox.p1.x)) throw std::runtime_error("no x position set"); if (!reader.get("y", bbox.p1.y)) throw std::runtime_error("no y position set"); diff --git a/src/object/platform.cpp b/src/object/platform.cpp index 6d04777be..e890b92b5 100644 --- a/src/object/platform.cpp +++ b/src/object/platform.cpp @@ -35,10 +35,9 @@ #include "scripting/squirrel_util.hpp" Platform::Platform(const lisp::Lisp& reader) - : MovingSprite(reader, Vector(0,0), LAYER_OBJECTS, COLGROUP_STATIC), name(""), speed(Vector(0,0)) + : MovingSprite(reader, Vector(0,0), LAYER_OBJECTS, COLGROUP_STATIC), speed(Vector(0,0)) { bool running = true; - reader.get("name", name); reader.get("running", running); const lisp::Lisp* pathLisp = reader.get_lisp("path"); if(pathLisp == NULL) @@ -48,12 +47,13 @@ Platform::Platform(const lisp::Lisp& reader) walker.reset(new PathWalker(path.get(), running)); bbox.set_pos(path->get_base()); - flags |= FLAG_SOLID; + set_solid(true); } Platform::Platform(const Platform& other) - : MovingSprite(other), ScriptInterface(other), name(other.name), speed(other.speed) + : MovingSprite(other), ScriptInterface(other), speed(other.speed) { + name = other.name; path.reset(new Path(*other.path)); walker.reset(new PathWalker(*other.walker)); walker->path = &*path; @@ -93,7 +93,7 @@ Platform::stop_moving() void Platform::expose(HSQUIRRELVM vm, SQInteger table_idx) { - if (name == "") return; + if (name.empty()) return; Scripting::Platform* interface = new Scripting::Platform(this); expose_object(vm, table_idx, interface, name, true); } @@ -101,7 +101,7 @@ Platform::expose(HSQUIRRELVM vm, SQInteger table_idx) void Platform::unexpose(HSQUIRRELVM vm, SQInteger table_idx) { - if (name == "") return; + if (name.empty()) return; Scripting::unexpose_object(vm, table_idx, name); } diff --git a/src/object/platform.hpp b/src/object/platform.hpp index ad1d999d7..74cd70487 100644 --- a/src/object/platform.hpp +++ b/src/object/platform.hpp @@ -61,7 +61,6 @@ public: } private: - std::string name; /**< user-defined name for use in scripts or empty string if not scriptable */ std::auto_ptr path; std::auto_ptr walker; Vector speed; diff --git a/src/object/player.cpp b/src/object/player.cpp index 48f90358b..7664a3b2d 100644 --- a/src/object/player.cpp +++ b/src/object/player.cpp @@ -108,8 +108,9 @@ TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer) feet->draw(context, pos, layer-2); } -Player::Player(PlayerStatus* _player_status) - : player_status(_player_status), grabbed_object(NULL), ghost_mode(false) +Player::Player(PlayerStatus* _player_status, std::string name) : + MovingObject(name), player_status(_player_status), grabbed_object(NULL), + ghost_mode(false) { controller = main_controller; smalltux_gameover = sprite_manager->create("images/creatures/tux_small/smalltux-gameover.sprite"); @@ -170,14 +171,15 @@ Player::init() void Player::expose(HSQUIRRELVM vm, SQInteger table_idx) { - Scripting::Player* interface = static_cast (this); - Scripting::expose_object(vm, table_idx, interface, "Tux", false); + if (name.empty()) return; + Scripting::expose_object(vm, table_idx, dynamic_cast(this), name, false); } void Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx) { - Scripting::unexpose_object(vm, table_idx, "Tux"); + if (name.empty()) return; + Scripting::unexpose_object(vm, table_idx, name); } void @@ -926,14 +928,11 @@ Player::collision(GameObject& other, const CollisionHit& hit) } // if we hit something from the side that is portable, the ACTION button is pressed and we are not already holding anything: grab it - if ((hit.left || hit.right) && (other.get_flags() & FLAG_PORTABLE) && controller->hold(Controller::ACTION) && (!grabbed_object)) { - Portable* portable = dynamic_cast (&other); - assert(portable != NULL); - if(portable) { - grabbed_object = portable; - grabbed_object->grab(*this, get_pos(), dir); - return CONTINUE; - } + Portable* portable = dynamic_cast (&other); + if ((hit.left || hit.right) && (portable && portable->is_portable()) && controller->hold(Controller::ACTION) && (!grabbed_object)) { + grabbed_object = portable; + grabbed_object->grab(*this, get_pos(), dir); + return CONTINUE; } #ifdef DEBUG diff --git a/src/object/player.hpp b/src/object/player.hpp index 866a0bf74..bfe1ba10a 100644 --- a/src/object/player.hpp +++ b/src/object/player.hpp @@ -122,7 +122,7 @@ public: Physic physic; public: - Player(PlayerStatus* player_status); + Player(PlayerStatus* player_status, std::string name = ""); virtual ~Player(); virtual void expose(HSQUIRRELVM vm, SQInteger table_idx); diff --git a/src/object/portable.hpp b/src/object/portable.hpp index 52802ba78..d3a2aef97 100644 --- a/src/object/portable.hpp +++ b/src/object/portable.hpp @@ -33,6 +33,10 @@ class Portable { public: + Portable(bool portable = false) : + portable(portable) + { + } virtual ~Portable() { } @@ -43,6 +47,21 @@ public: virtual void ungrab(MovingObject& , Direction ) {} + + // --- BEGIN METHODS TO EXPOSE TO SQUIRREL --- // + void set_portable(bool portable) + { + this->portable = portable; + } + + bool is_portable() const + { + return portable; + } + // --- END METHODS TO EXPOSE TO SQUIRREL --- // + +private: + bool portable; /**< true if this object can currently be carried */ }; #endif diff --git a/src/object/rock.cpp b/src/object/rock.cpp index f0b6c2c43..aa883a1ae 100644 --- a/src/object/rock.cpp +++ b/src/object/rock.cpp @@ -34,7 +34,8 @@ Rock::Rock(const lisp::Lisp& reader) sound_manager->preload( ROCK_SOUND ); on_ground = false; grabbed = false; - flags |= FLAG_SOLID | FLAG_PORTABLE; + set_solid(true); + set_portable(true); } void diff --git a/src/object/scripted_object.cpp b/src/object/scripted_object.cpp index 67963ec26..e95e5b520 100644 --- a/src/object/scripted_object.cpp +++ b/src/object/scripted_object.cpp @@ -47,20 +47,19 @@ ScriptedObject::ScriptedObject(const lisp::Lisp& lisp) lisp.get("physic-enabled", physic_enabled); lisp.get("visible", visible); lisp.get("z-pos", layer); - if(solid) - flags |= FLAG_SOLID; } void ScriptedObject::expose(HSQUIRRELVM vm, SQInteger table_idx) { - Scripting::ScriptedObject* interface = static_cast (this); - expose_object(vm, table_idx, interface, name, false); + if (name.empty()) return; + expose_object(vm, table_idx, dynamic_cast(this), name, false); } void ScriptedObject::unexpose(HSQUIRRELVM vm, SQInteger table_idx) { + if (name.empty()) return; Scripting::unexpose_object(vm, table_idx, name); } @@ -125,10 +124,6 @@ void ScriptedObject::set_solid(bool solid) { this->solid = solid; - if(solid) - flags |= FLAG_SOLID; - else - flags ^= FLAG_SOLID; } bool diff --git a/src/object/skull_tile.cpp b/src/object/skull_tile.cpp index 04f140c59..814309393 100644 --- a/src/object/skull_tile.cpp +++ b/src/object/skull_tile.cpp @@ -34,7 +34,7 @@ static const float FALLTIME = 0.8; SkullTile::SkullTile(const lisp::Lisp& lisp) : MovingSprite(lisp, "images/objects/skull_tile/skull_tile.sprite", LAYER_TILES, COLGROUP_STATIC), hit(false), falling(false) { - flags |= FLAG_SOLID; + set_solid(true); } HitResponse @@ -72,7 +72,7 @@ SkullTile::update(float elapsed_time) if(timer.check()) { falling = true; physic.enable_gravity(true); - flags &= ~FLAG_SOLID; + set_solid(false); timer.stop(); } else if(!timer.started()) { timer.start(FALLTIME); diff --git a/src/object/text_object.cpp b/src/object/text_object.cpp index af3564368..19c94164e 100644 --- a/src/object/text_object.cpp +++ b/src/object/text_object.cpp @@ -27,8 +27,8 @@ #include "scripting/squirrel_util.hpp" #include "log.hpp" -TextObject::TextObject() - : fading(0), fadetime(0), visible(false) +TextObject::TextObject(std::string name) : + GameObject(name), fading(0), fadetime(0), visible(false) { font = blue_text; centered = false; @@ -41,14 +41,15 @@ TextObject::~TextObject() void TextObject::expose(HSQUIRRELVM vm, SQInteger table_idx) { - Scripting::Text* interface = static_cast (this); - Scripting::expose_object(vm, table_idx, interface, "Text", false); + if (name.empty()) return; + Scripting::expose_object(vm, table_idx, dynamic_cast(this), name, false); } void TextObject::unexpose(HSQUIRRELVM vm, SQInteger table_idx) { - Scripting::unexpose_object(vm, table_idx, "Text"); + if (name.empty()) return; + Scripting::unexpose_object(vm, table_idx, name); } void diff --git a/src/object/text_object.hpp b/src/object/text_object.hpp index e649ef10e..20581add6 100644 --- a/src/object/text_object.hpp +++ b/src/object/text_object.hpp @@ -31,7 +31,7 @@ class TextObject : public GameObject, public Scripting::Text, public ScriptInterface { public: - TextObject(); + TextObject(std::string name = ""); virtual ~TextObject(); void expose(HSQUIRRELVM vm, SQInteger table_idx); diff --git a/src/object/thunderstorm.cpp b/src/object/thunderstorm.cpp index f86279c73..1253b352f 100644 --- a/src/object/thunderstorm.cpp +++ b/src/object/thunderstorm.cpp @@ -42,7 +42,7 @@ namespace { } Thunderstorm::Thunderstorm(const lisp::Lisp& reader) - : name(""), running(true), interval(10.0f) + : GameObject(reader), running(true), interval(10.0f) { reader.get("name", name); reader.get("running", running); diff --git a/src/object/thunderstorm.hpp b/src/object/thunderstorm.hpp index ee1430346..afbc5acb0 100644 --- a/src/object/thunderstorm.hpp +++ b/src/object/thunderstorm.hpp @@ -71,7 +71,6 @@ public: void electrify(); private: - std::string name; /**< user-defined name for use in scripts or empty string if not scriptable */ bool running; /**< whether we currently automatically trigger lightnings */ float interval; /**< time between two lightnings */ diff --git a/src/object/tilemap.cpp b/src/object/tilemap.cpp index 96a51536d..84c7ff628 100644 --- a/src/object/tilemap.cpp +++ b/src/object/tilemap.cpp @@ -42,20 +42,16 @@ TileMap::TileMap() drawing_effect(NO_EFFECT) { tilemanager = tile_manager; - - if(solid) - flags |= FLAG_SOLID; } TileMap::TileMap(const lisp::Lisp& reader, TileManager* new_tile_manager) - : solid(false), speed(1), width(-1), height(-1), z_pos(0), x_offset(0), y_offset(0), + : GameObject(reader), solid(false), speed(1), width(-1), height(-1), z_pos(0), x_offset(0), y_offset(0), drawing_effect(NO_EFFECT) { tilemanager = new_tile_manager; if(tilemanager == 0) tilemanager = tile_manager; - reader.get("name", name); reader.get("z-pos", z_pos); reader.get("solid", solid); reader.get("speed", speed); @@ -64,8 +60,6 @@ TileMap::TileMap(const lisp::Lisp& reader, TileManager* new_tile_manager) log_warning << "Speed of solid tilemap is not 1. fixing" << std::endl; speed = 1; } - if(solid) - flags |= FLAG_SOLID; reader.get("width", width); reader.get("height", height); @@ -85,15 +79,12 @@ TileMap::TileMap(const lisp::Lisp& reader, TileManager* new_tile_manager) } TileMap::TileMap(std::string name, int z_pos, bool solid, size_t width, size_t height) - : name(name), solid(solid), speed(1), width(0), height(0), z_pos(z_pos), + : GameObject(name), solid(solid), speed(1), width(0), height(0), z_pos(z_pos), x_offset(0), y_offset(0), drawing_effect(NO_EFFECT) { tilemanager = tile_manager; resize(width, height); - - if(solid) - flags |= FLAG_SOLID; } TileMap::~TileMap() @@ -175,8 +166,6 @@ TileMap::set(int newwidth, int newheight, const std::vector&newt, z_pos = new_z_pos; solid = newsolid; - if(solid) - flags |= FLAG_SOLID; // make sure all tiles are loaded for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i) diff --git a/src/object/tilemap.hpp b/src/object/tilemap.hpp index cb87573b7..8cc75230d 100644 --- a/src/object/tilemap.hpp +++ b/src/object/tilemap.hpp @@ -118,7 +118,6 @@ private: private: TileManager* tilemanager; - std::string name; bool solid; float speed; int width, height; diff --git a/src/object/trampoline.cpp b/src/object/trampoline.cpp index ed83793ec..73768757c 100644 --- a/src/object/trampoline.cpp +++ b/src/object/trampoline.cpp @@ -37,7 +37,6 @@ Trampoline::Trampoline(const lisp::Lisp& lisp) : MovingSprite(lisp, "images/objects/trampoline/trampoline.sprite" ) { sound_manager->preload( TRAMPOLINE_SOUND ); - flags |= FLAG_PORTABLE; physic.set_velocity(0, 0); physic.enable_gravity(true); on_ground = false; @@ -46,13 +45,13 @@ Trampoline::Trampoline(const lisp::Lisp& lisp) //Check if this trampoline is not portable if( lisp.get( "portable", portable ) ){ if( !portable ){ - flags ^= FLAG_PORTABLE; //we need another sprite sprite_name = "images/objects/trampoline/trampoline_fix.sprite"; sprite = sprite_manager->create( sprite_name ); sprite->set_action("normal"); } } + set_portable(portable); } void diff --git a/src/object/unstable_tile.cpp b/src/object/unstable_tile.cpp index 9780c7395..67578c50d 100644 --- a/src/object/unstable_tile.cpp +++ b/src/object/unstable_tile.cpp @@ -60,11 +60,11 @@ UnstableTile::update(float elapsed_time) case STATE_CRUMBLING: if (sprite->animation_done()) { - state = STATE_DISINTEGRATING; - sprite->set_action("disintegrating", 1); - flags &= ~FLAG_SOLID; + state = STATE_DISINTEGRATING; + sprite->set_action("disintegrating", 1); + set_solid(false); set_group(COLGROUP_DISABLED); - physic.enable_gravity(true); + physic.enable_gravity(true); } break; diff --git a/src/object/weak_block.cpp b/src/object/weak_block.cpp index 26752712a..90f3006cd 100644 --- a/src/object/weak_block.cpp +++ b/src/object/weak_block.cpp @@ -34,7 +34,7 @@ WeakBlock::WeakBlock(const lisp::Lisp& lisp) : MovingSprite(lisp, "images/objects/strawbox/strawbox.sprite", LAYER_TILES, COLGROUP_STATIC), state(STATE_NORMAL) { sprite->set_action("normal"); - flags |= FLAG_SOLID; + set_solid(true); } HitResponse @@ -44,8 +44,8 @@ WeakBlock::collision(GameObject& other, const CollisionHit& ) case STATE_NORMAL: if (dynamic_cast(&other)) { - startBurning(); - return FORCE_MOVE; + startBurning(); + return FORCE_MOVE; } return FORCE_MOVE; break; @@ -74,18 +74,18 @@ WeakBlock::update(float ) case STATE_BURNING: if (sprite->animation_done()) { - state = STATE_DISINTEGRATING; - sprite->set_action("disintegrating", 1); - spreadHit(); - flags &= ~FLAG_SOLID; + state = STATE_DISINTEGRATING; + sprite->set_action("disintegrating", 1); + spreadHit(); + set_solid(false); set_group(COLGROUP_DISABLED); } break; case STATE_DISINTEGRATING: if (sprite->animation_done()) { - remove_me(); - return; + remove_me(); + return; } break; diff --git a/src/object/wind.cpp b/src/object/wind.cpp index fff0b793e..c4c9b8c79 100644 --- a/src/object/wind.cpp +++ b/src/object/wind.cpp @@ -29,7 +29,9 @@ #include "scripting/wind.hpp" #include "scripting/squirrel_util.hpp" -Wind::Wind(const lisp::Lisp& reader) : name(""), blowing(true), acceleration(100), elapsed_time(0) +Wind::Wind(const lisp::Lisp& reader) : + MovingObject(reader), blowing(true), acceleration(100), + elapsed_time(0) { reader.get("x", bbox.p1.x); reader.get("y", bbox.p1.y); @@ -38,7 +40,6 @@ Wind::Wind(const lisp::Lisp& reader) : name(""), blowing(true), acceleration(100 reader.get("height", h); bbox.set_size(w, h); - reader.get("name", name); reader.get("blowing", blowing); float speed_x = 0, speed_y = 0; diff --git a/src/object/wind.hpp b/src/object/wind.hpp index 8411ca296..53304d6ff 100644 --- a/src/object/wind.hpp +++ b/src/object/wind.hpp @@ -54,8 +54,6 @@ public: virtual void unexpose(HSQUIRRELVM vm, SQInteger table_idx); private: - std::string name; /**< user-defined name for use in scripts or empty string if not scriptable */ - bool blowing; /**< true if wind is currently switched on */ Vector speed; float acceleration; diff --git a/src/scripting/ambient_sound.hpp b/src/scripting/ambient_sound.hpp index b547b802b..7fefdb363 100644 --- a/src/scripting/ambient_sound.hpp +++ b/src/scripting/ambient_sound.hpp @@ -13,8 +13,8 @@ public: #endif virtual void set_pos(float x, float y) = 0; - virtual float get_pos_x() = 0; - virtual float get_pos_y() = 0; + virtual float get_pos_x() const = 0; + virtual float get_pos_y() const = 0; }; } diff --git a/src/sector.cpp b/src/sector.cpp index f9aaf7cd1..20ed7fc0b 100644 --- a/src/sector.cpp +++ b/src/sector.cpp @@ -72,9 +72,9 @@ Sector::Sector(Level* parent) : level(parent), currentmusic(LEVEL_MUSIC), gravity(10), player(0), camera(0) { - add_object(new Player(player_status)); - add_object(new DisplayEffect()); - add_object(new TextObject()); + add_object(new Player(player_status, "Tux")); + add_object(new DisplayEffect("Effect")); + add_object(new TextObject("Text")); // create a new squirrel table for the sector using namespace Scripting; @@ -132,7 +132,7 @@ GameObject* Sector::parse_object(const std::string& name, const lisp::Lisp& reader) { if(name == "camera") { - Camera* camera = new Camera(this); + Camera* camera = new Camera(this, "Camera"); camera->parse(reader); return camera; } else if(name == "particles-snow") { @@ -201,7 +201,7 @@ Sector::parse(const lisp::Lisp& sector) if(!camera) { log_warning << "sector '" << name << "' does not contain a camera." << std::endl; update_game_objects(); - add_object(new Camera(this)); + add_object(new Camera(this, "Camera")); } update_game_objects(); @@ -327,7 +327,7 @@ Sector::parse_old_format(const lisp::Lisp& reader) } // add a camera - Camera* camera = new Camera(this); + Camera* camera = new Camera(this, "Camera"); add_object(camera); update_game_objects(); diff --git a/src/worldmap/level.cpp b/src/worldmap/level.cpp index b6aa12c35..3f324964b 100644 --- a/src/worldmap/level.cpp +++ b/src/worldmap/level.cpp @@ -32,7 +32,7 @@ namespace WorldMapNS { LevelTile::LevelTile(const std::string& basedir, const lisp::Lisp* lisp) - : solved(false), auto_path(true), basedir(basedir), picture_cached(false), picture(0) + : GameObject(*lisp), solved(false), auto_path(true), basedir(basedir), picture_cached(false), picture(0) { lisp->get("x", pos.x); lisp->get("y", pos.y); @@ -42,7 +42,6 @@ LevelTile::LevelTile(const std::string& basedir, const lisp::Lisp* lisp) sprite.reset(sprite_manager->create(spritefile)); lisp->get("extro-script", extro_script); - lisp->get("name", name); if (!PHYSFS_exists((basedir + name).c_str())) { diff --git a/src/worldmap/level.hpp b/src/worldmap/level.hpp index be081c940..dabd4d740 100644 --- a/src/worldmap/level.hpp +++ b/src/worldmap/level.hpp @@ -42,7 +42,6 @@ public: virtual void update(float elapsed_time); Vector pos; - std::string name; std::string title; bool solved; diff --git a/src/worldmap/worldmap.cpp b/src/worldmap/worldmap.cpp index 0d21134b0..7ee9c5ee1 100644 --- a/src/worldmap/worldmap.cpp +++ b/src/worldmap/worldmap.cpp @@ -308,7 +308,7 @@ WorldMap::get_level_title(LevelTile& level) try { lisp::Parser parser; - std::auto_ptr root (parser.parse(levels_path + level.name)); + std::auto_ptr root (parser.parse(levels_path + level.get_name())); const lisp::Lisp* level_lisp = root->get_lisp("supertux-level"); if(!level_lisp) @@ -552,7 +552,7 @@ WorldMap::update(float delta) try { Vector shrinkpos = Vector(level->pos.x*32 + 16 - camera_offset.x, level->pos.y*32 + 16 - camera_offset.y); - std::string levelfile = levels_path + level->name; + std::string levelfile = levels_path + level->get_name(); main_loop->push_screen(new GameSession(levelfile, &level->statistics), new ShrinkFade(shrinkpos, 0.5)); } catch(std::exception& e) { @@ -903,7 +903,7 @@ WorldMap::save_state() LevelTile* level = *i; if (level->solved) { - sq_pushstring(vm, level->name.c_str(), -1); + sq_pushstring(vm, level->get_name().c_str(), -1); sq_newtable(vm); store_bool(vm, "solved", true); @@ -971,7 +971,7 @@ WorldMap::load_state() for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) { LevelTile* level = *i; - sq_pushstring(vm, level->name.c_str(), -1); + sq_pushstring(vm, level->get_name().c_str(), -1); if(SQ_SUCCEEDED(sq_get(vm, -2))) { level->solved = read_bool(vm, "solved"); level->sprite->set_action(level->solved ? "solved" : "default"); -- 2.11.0