From b00e4b77859d13bdf29bd50e91315a46a15bd01d Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Wed, 16 Nov 2016 16:49:54 +0100 Subject: [PATCH] less restrictive teleport command changed context requirements from player to entity --- src/shared/CLIContext.hpp | 35 ++++++++++++++++++++++++++++++----- src/shared/cli.cpp | 21 ++++++++++++++++----- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/shared/CLIContext.hpp b/src/shared/CLIContext.hpp index 5308bb2..e07571f 100644 --- a/src/shared/CLIContext.hpp +++ b/src/shared/CLIContext.hpp @@ -7,18 +7,40 @@ namespace blank { class Player; +class Entity; class CLIContext { public: - explicit CLIContext(Player *p = nullptr) - : player(p) { } + /// Create context with optional player and entity + /// entity defaults to the player's if given + /// Associated player or entity can be changed during + /// the context's lifetime and will assume the original + /// values when reset. + explicit CLIContext(Player *p = nullptr, Entity *e = nullptr); /// check if this context associates a player - bool HasPlayer() { return player; } + bool HasPlayer() { return effective_player; } /// get the player responsible for all this /// only valid if HasPlayer() returns true - Player &GetPlayer() { return *player; } + Player &GetPlayer() { return *effective_player; } + /// change the effective player of this context + /// note that this will *not* change the effective entity + void SetPlayer(Player &p) { effective_player = &p; } + + /// check if this context associates an entity + bool HasEntity() { return effective_entity; } + /// get the entity on which operations should be performed + /// only valid if HasPlayer() returns true + Entity &GetEntity() { return *effective_entity; } + /// change the effective player of this context + void SetEntity(Entity &e) { effective_entity = &e; } + + /// reset effective player and entity to their original values + void Reset() { + effective_player = original_player; + effective_player = original_player; + } /// an error has happened and the player should be notified virtual void Error(const std::string &) = 0; @@ -32,7 +54,10 @@ public: virtual void Broadcast(const std::string &) = 0; private: - Player *player; + Player *original_player; + Player *effective_player; + Entity *original_entity; + Entity *effective_entity; }; diff --git a/src/shared/cli.cpp b/src/shared/cli.cpp index cb46cfc..bb4aa6d 100644 --- a/src/shared/cli.cpp +++ b/src/shared/cli.cpp @@ -63,19 +63,30 @@ CLI::Command::~Command() { } +CLIContext::CLIContext(Player *p, Entity *e) +: original_player(p) +, effective_player(p) +, original_entity(e) +, effective_entity(e) { + if (!e && p) { + original_entity = effective_entity = &p->GetEntity(); + } +} + + void TeleportCommand::Execute(CLI &, CLIContext &ctx, TokenStreamReader &args) { - if (!ctx.HasPlayer()) { - ctx.Error("teleport needs player to operate on"); + if (!ctx.HasEntity()) { + ctx.Error("teleport needs entity to operate on"); return; } glm::vec3 pos(args.GetFloat(), args.GetFloat(), args.GetFloat()); - EntityState state = ctx.GetPlayer().GetEntity().GetState(); + EntityState state = ctx.GetEntity().GetState(); state.pos = ExactLocation(pos).Sanitize(); - ctx.GetPlayer().GetEntity().SetState(state); + ctx.GetEntity().SetState(state); stringstream msg; - msg << ctx.GetPlayer().Name() << " teleported to " << pos; + msg << ctx.GetEntity().Name() << " teleported to " << pos; ctx.Broadcast(msg.str()); } -- 2.39.2