#include #include #include #include "parse.h" #include "person.h" #include "weapon.h" using namespace JFK; using std::string; person::person(const string& iname) : name(iname), health(100), curweapon(WATERGUN), blocked(false), score(0), deaths(0) { r = 17.0; for (size_t i = 0; i < MAX_WEAPON; i++) { hasweapon[i] = i == WATERGUN ? true : false; ammo[i] = i == WATERGUN ? 500 : 0; } } string person::tostring() const { std::ostringstream oss; unsigned weapons = 0; for (size_t i = 0; i < MAX_WEAPON; i++) if (hasweapon[i]) weapons |= 1U << i; oss << "x=" << x << " y=" << y << " dx=" << dx << " dy=" << dy << " v=" << v << " w=" << w << " name=" << name << " curweapon=" << curweapon << " weapons=" << weapons << " blocked=" << blocked << " health=" << health << " ammo=" << ammo[curweapon] << " score=" << score << " deaths=" << deaths; return oss.str(); } void person::fromstring(const string& s) { int index = 0; int weapons = -1; string arg = argument(0, s); while (arg.length() > 0) { string id = identifier(arg); if (id == "x") x = strtodbl(value(arg)); else if (id == "y") y = strtodbl(value(arg)); else if (id == "dx") dx = strtodbl(value(arg)); else if (id == "dy") dy = strtodbl(value(arg)); else if (id == "v") v = strtodbl(value(arg)); else if (id == "w") w = strtodbl(value(arg)); else if (id == "name") name = value(arg); else if (id == "curweapon") curweapon = strtoint(value(arg)); else if (id == "weapons") weapons = strtoint(value(arg)); else if (id == "blocked") blocked = strtoint(value(arg)); else if (id == "health") health = strtoint(value(arg)); else if (id == "ammo") ammo[0] = strtoint(value(arg)); else if (id == "score") score = strtoint(value(arg)); else if (id == "deaths") deaths = strtoint(value(arg)); //else throw exception("wrong string"); better ignore... index++; arg = argument(index, s); } if (weapons != -1) for (size_t i = 0; i < MAX_WEAPON; i++) { if (weapons & (1 << i)) hasweapon[i] = true; else hasweapon[i] = false; } } void person::move(double t) { if (!blocked) { x += v * dx * t; y += v * dy * t; } double tmp = dx; dx = std::cos(w * t) * dx - std::sin(w * t) * dy; dy = std::cos(w * t) * dy + std::sin(w * t) * tmp; } string person::classname() const { return "person"; }