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<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(command);
if ((i == commands.end()) || (i->second.size() == 0)) {
}
bool
+Console::consoleCommand(std::string command, std::vector<std::string> arguments)
+{
+ if (command == "ccrs") {
+ if (arguments.size() != 1) {
+ msg_info << "Usage: ccrs <command>" << std::endl;
+ return true;
+ }
+ std::map<std::string, std::list<ConsoleCommandReceiver*> >::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<ConsoleCommandReceiver*> &ccrs = i->second;
+ std::list<ConsoleCommandReceiver*>::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;
i->second.erase(j);
}
+void
+Console::unregisterCommands(ConsoleCommandReceiver* ccr)
+{
+ for (std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.begin(); i != commands.end(); i++) {
+ std::list<ConsoleCommandReceiver*> &ccrs = i->second;
+ std::list<ConsoleCommandReceiver*>::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<std::string> Console::lines;
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<std::string> 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 */
class ConsoleCommandReceiver
{
public:
- ConsoleCommandReceiver()
- {
- //Console::registerCommandReceiver(this);
- }
+ ConsoleCommandReceiver() {}
virtual bool consoleCommand(std::string command, std::vector<std::string> arguments) = 0; /**< callback from Console; return false if command was unknown, true otherwise */
virtual ~ConsoleCommandReceiver()
{
- //Console::unregisterCommandReceiver(this);
+ Console::unregisterCommands(this);
}
};
"foo",
"whereami",
"camera",
- "grow",
- "fire",
- "ice",
- "coins",
- "numberofthebeast",
"grease",
"invincible",
"mortal",
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;
}
bool
-GameSession::consoleCommand(std::string command, std::vector<std::string> arguments)
+GameSession::consoleCommand(std::string command, std::vector<std::string>)
{
if (command == "foo") {
msg_info << "bar" << std::endl;
// 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<int>(arguments[0]))) {
- msg_info << "Usage: coins <number>" << std::endl;
- } else {
- player_status->coins = Console::string_to<int>(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;
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();
}
{
physic.set_velocity_x(speed);
}
+
+bool
+Player::consoleCommand(std::string command, std::vector<std::string> 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;
+}
+
#include "scripting/player.hpp"
#include "player_status.hpp"
#include "display_effect.hpp"
+#include "console.hpp"
class BadGuy;
class Portable;
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 };
bool get_visible();
bool on_ground();
+
+ bool consoleCommand(std::string command, std::vector<std::string> arguments); /**< callback from Console; return false if command was unknown, true otherwise */
private:
void handle_input();
key_gold->set_action("outline");
tux_life.reset(sprite_manager->create("images/creatures/tux_small/tux-life.sprite"));
+
+ Console::registerCommand("coins", this);
}
PlayerStatus::~PlayerStatus()
keys = other.keys;
}
+bool
+PlayerStatus::consoleCommand(std::string command, std::vector<std::string> arguments)
+{
+ if (command == "coins") {
+ if ((arguments.size() < 1) || (!Console::string_is<int>(arguments[0]))) {
+ msg_info << "Usage: coins <number>" << std::endl;
+ } else {
+ coins = Console::string_to<int>(arguments[0]);
+ }
+ return true;
+ }
+ return false;
+}
+
#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;
* 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();
void draw(DrawingContext& context);
void draw_keys(DrawingContext& context);
+ bool consoleCommand(std::string command, std::vector<std::string> arguments); /**< callback from Console; return false if command was unknown, true otherwise */
+
int coins;
BonusType bonus;