2 #include "commands.hpp"
4 #include "../io/TokenStreamReader.hpp"
5 #include "../world/Entity.hpp"
6 #include "../world/Player.hpp"
16 CLI::CLI(World &world)
19 AddCommand("tp", new TeleportCommand);
23 for (auto &entry : commands) {
28 void CLI::AddCommand(const string &name, Command *cmd) {
32 void CLI::Execute(Player &player, const string &line) {
34 TokenStreamReader args(s);
35 if (!args.HasMore()) {
36 // ignore empty command line
39 if (args.Peek().type != Token::IDENTIFIER) {
40 Error("I don't understand");
44 args.ReadIdentifier(name);
45 auto entry = commands.find(name);
46 if (entry == commands.end()) {
47 Error(name + ": command not found");
51 entry->second->Execute(*this, player, args);
52 } catch (exception &e) {
53 Error(name + ": " + e.what());
55 Error(name + ": unknown execution error");
59 void CLI::Message(const string &msg) {
60 // TODO: display message to player
64 void CLI::Error(const string &msg) {
65 Message("CLI error: " + msg);
68 CLI::Command::~Command() {
73 void TeleportCommand::Execute(CLI &cli, Player &player, TokenStreamReader &args) {
74 glm::vec3 pos(args.GetFloat(), args.GetFloat(), args.GetFloat());
75 EntityState state = player.GetEntity().GetState();
76 state.pos = ExactLocation(pos).Sanitize();
77 player.GetEntity().SetState(state);