From: Christoph Sommer Date: Mon, 10 Apr 2006 13:08:45 +0000 (+0000) Subject: Moved some console commands' implementations nearer to target classes X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=eb6819ec3002b546e67401f7600cc93e0ec32daf;p=supertux.git Moved some console commands' implementations nearer to target classes SVN-Revision: 3283 --- diff --git a/src/console.cpp b/src/console.cpp index 41d2d7c62..69f854753 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -144,6 +144,9 @@ Console::parse(std::string s) std::string command = args.front(); args.erase(args.begin()); + // ignore if it's an internal command + if (consoleCommand(command,args)) return; + // look up registered ccr std::map >::iterator i = commands.find(command); if ((i == commands.end()) || (i->second.size() == 0)) { @@ -157,6 +160,35 @@ Console::parse(std::string s) } bool +Console::consoleCommand(std::string command, std::vector arguments) +{ + if (command == "ccrs") { + if (arguments.size() != 1) { + msg_info << "Usage: ccrs " << std::endl; + return true; + } + std::map >::iterator i = commands.find(arguments[0]); + if ((i == commands.end()) || (i->second.size() == 0)) { + msg_info << "unknown command: \"" << arguments[0] << "\"" << std::endl; + return true; + } + + std::ostringstream ccr_list; + std::list &ccrs = i->second; + std::list::iterator j; + for (j = ccrs.begin(); j != ccrs.end(); j++) { + if (j != ccrs.begin()) ccr_list << ", "; + ccr_list << "[" << *j << "]"; + } + + msg_info << "registered ccrs for \"" << arguments[0] << "\": " << ccr_list.str() << std::endl; + return true; + } + + return false; +} + +bool Console::hasFocus() { return focused; @@ -249,6 +281,18 @@ Console::unregisterCommand(std::string command, ConsoleCommandReceiver* ccr) i->second.erase(j); } +void +Console::unregisterCommands(ConsoleCommandReceiver* ccr) +{ + for (std::map >::iterator i = commands.begin(); i != commands.end(); i++) { + std::list &ccrs = i->second; + std::list::iterator j; + while ((j = find(ccrs.begin(), ccrs.end(), ccr)) != ccrs.end()) { + ccrs.erase(j); + } + } +} + int Console::height = 0; bool Console::focused = false; std::list Console::lines; diff --git a/src/console.hpp b/src/console.hpp index 445c91245..7834d76f6 100644 --- a/src/console.hpp +++ b/src/console.hpp @@ -91,6 +91,7 @@ class Console static void addLine(std::string s); /**< display a line in the console */ static void parse(std::string s); /**< react to a given command */ + static bool consoleCommand(std::string command, std::vector arguments); /**< process internal command; return false if command was unknown, true otherwise */ friend class ConsoleStreamBuffer; static void flush(ConsoleStreamBuffer* buffer); /**< act upon changes in a ConsoleStreamBuffer */ @@ -109,14 +110,11 @@ class ConsoleStreamBuffer : public std::stringbuf class ConsoleCommandReceiver { public: - ConsoleCommandReceiver() - { - //Console::registerCommandReceiver(this); - } + ConsoleCommandReceiver() {} virtual bool consoleCommand(std::string command, std::vector arguments) = 0; /**< callback from Console; return false if command was unknown, true otherwise */ virtual ~ConsoleCommandReceiver() { - //Console::unregisterCommandReceiver(this); + Console::unregisterCommands(this); } }; diff --git a/src/game_session.cpp b/src/game_session.cpp index 53152b35a..bad7bfd63 100644 --- a/src/game_session.cpp +++ b/src/game_session.cpp @@ -80,11 +80,6 @@ namespace { "foo", "whereami", "camera", - "grow", - "fire", - "ice", - "coins", - "numberofthebeast", "grease", "invincible", "mortal", @@ -193,9 +188,6 @@ GameSession::~GameSession() delete end_sequence_controller; delete level; - for (uint16_t i=0; i < sizeof(::consoleCommands)/sizeof(typeof(consoleCommands[0])); i++) { - Console::unregisterCommand(consoleCommands[i], this); - } delete statistics_backdrop; @@ -351,7 +343,7 @@ GameSession::process_events() } bool -GameSession::consoleCommand(std::string command, std::vector arguments) +GameSession::consoleCommand(std::string command, std::vector) { if (command == "foo") { msg_info << "bar" << std::endl; @@ -363,30 +355,6 @@ GameSession::consoleCommand(std::string command, std::vector argume // Cheating words (the goal of this is really for debugging, // but could be used for some cheating, nothing wrong with that) - if (command == "grow") { - tux.set_bonus(GROWUP_BONUS, false); - return true; - } - if (command == "fire") { - tux.set_bonus(FIRE_BONUS, false); - return true; - } - if (command == "ice") { - tux.set_bonus(ICE_BONUS, false); - return true; - } - if (command == "coins") { - if ((arguments.size() < 1) || (!Console::string_is(arguments[0]))) { - msg_info << "Usage: coins " << std::endl; - } else { - player_status->coins = Console::string_to(arguments[0]); - } - return true; - } - if (command == "numberofthebeast") { - player_status->coins += 55; - return true; - } if (command == "grease") { tux.physic.set_velocity_x(tux.physic.get_velocity_x()*3); return true; diff --git a/src/object/player.cpp b/src/object/player.cpp index 97a93d36b..96e10b31f 100644 --- a/src/object/player.cpp +++ b/src/object/player.cpp @@ -106,6 +106,9 @@ Player::Player(PlayerStatus* _player_status) smalltux_gameover = sprite_manager->create("images/creatures/tux_small/smalltux-gameover.sprite"); smalltux_star = sprite_manager->create("images/creatures/tux_small/smalltux-star.sprite"); bigtux_star = sprite_manager->create("images/creatures/tux_big/bigtux-star.sprite"); + + Console::registerCommand("set_bonus", this); + init(); } @@ -949,3 +952,28 @@ void Player::walk(float speed) { physic.set_velocity_x(speed); } + +bool +Player::consoleCommand(std::string command, std::vector arguments) +{ + if (command == "set_bonus") { + if (arguments.size() == 1) { + if (arguments[0] == "egg") { + set_bonus(GROWUP_BONUS, false); + return true; + } + if (arguments[0] == "fire") { + set_bonus(FIRE_BONUS, false); + return true; + } + if (arguments[0] == "ice") { + set_bonus(ICE_BONUS, false); + return true; + } + } + msg_info << "Usage: give {\"egg\" | \"fire\" | \"ice\"}" << std::endl; + return true; + } + return false; +} + diff --git a/src/object/player.hpp b/src/object/player.hpp index ffa727c99..43cddf999 100644 --- a/src/object/player.hpp +++ b/src/object/player.hpp @@ -32,6 +32,7 @@ #include "scripting/player.hpp" #include "player_status.hpp" #include "display_effect.hpp" +#include "console.hpp" class BadGuy; class Portable; @@ -77,7 +78,7 @@ extern TuxBodyParts* big_tux; extern TuxBodyParts* fire_tux; extern TuxBodyParts* ice_tux; -class Player : public MovingObject, public Scripting::Player +class Player : public MovingObject, public Scripting::Player, public ConsoleCommandReceiver { public: enum HurtMode { KILL, SHRINK }; @@ -163,6 +164,8 @@ public: bool get_visible(); bool on_ground(); + + bool consoleCommand(std::string command, std::vector arguments); /**< callback from Console; return false if command was unknown, true otherwise */ private: void handle_input(); diff --git a/src/player_status.cpp b/src/player_status.cpp index 32285d416..02a44e9d9 100644 --- a/src/player_status.cpp +++ b/src/player_status.cpp @@ -54,6 +54,8 @@ PlayerStatus::PlayerStatus() key_gold->set_action("outline"); tux_life.reset(sprite_manager->create("images/creatures/tux_small/tux-life.sprite")); + + Console::registerCommand("coins", this); } PlayerStatus::~PlayerStatus() @@ -207,3 +209,17 @@ PlayerStatus::operator= (const PlayerStatus& other) keys = other.keys; } +bool +PlayerStatus::consoleCommand(std::string command, std::vector arguments) +{ + if (command == "coins") { + if ((arguments.size() < 1) || (!Console::string_is(arguments[0]))) { + msg_info << "Usage: coins " << std::endl; + } else { + coins = Console::string_to(arguments[0]); + } + return true; + } + return false; +} + diff --git a/src/player_status.hpp b/src/player_status.hpp index 7b352cf77..99af85b1f 100644 --- a/src/player_status.hpp +++ b/src/player_status.hpp @@ -25,6 +25,7 @@ #include "timer.hpp" #include "serializable.hpp" #include "sprite/sprite.hpp" +#include "console.hpp" static const float BORDER_X = 10; static const float BORDER_Y = 10; @@ -38,7 +39,7 @@ class DrawingContext; * This class memorizes player status between different game sessions (for * example when switching maps in the worldmap) */ -class PlayerStatus : public Serializable +class PlayerStatus : public Serializable, public ConsoleCommandReceiver { public: PlayerStatus(); @@ -54,6 +55,8 @@ public: void draw(DrawingContext& context); void draw_keys(DrawingContext& context); + bool consoleCommand(std::string command, std::vector arguments); /**< callback from Console; return false if command was unknown, true otherwise */ + int coins; BonusType bonus;