00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "ModelFactory.h"
00010
00011 #include "V2.h"
00012 #include "Unit.h"
00013 #include "Shape.h"
00014 #include "LogicException.h"
00015 #include "StringTool.h"
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 Cube *
00029 ModelFactory::createModel(const std::string &kind, const V2 &loc,
00030 const std::string &shape)
00031 {
00032 if (StringTool::startsWith(kind, "output_")) {
00033 return createOutputItem(kind, loc, shape);
00034 }
00035
00036 Cube::eWeight weight;
00037 Cube::eWeight power;
00038 bool alive;
00039 createParams(kind, &weight, &power, &alive);
00040
00041 Shape *newShape = new Shape(shape);
00042 Cube *model = new Cube(loc,
00043 weight, power, alive, newShape);
00044
00045 return model;
00046 }
00047
00048
00049
00050
00051
00052 void
00053 ModelFactory::createParams(const std::string &kind,
00054 Cube::eWeight *out_weight, Cube::eWeight *out_power, bool *out_alive)
00055 {
00056 if ("fish_small" == kind) {
00057 *out_weight = Cube::LIGHT;
00058 *out_power = Cube::LIGHT;
00059 *out_alive = true;
00060 }
00061 else if ("fish_big" == kind) {
00062 *out_weight = Cube::LIGHT;
00063 *out_power = Cube::HEAVY;
00064 *out_alive = true;
00065 }
00066 else if (StringTool::startsWith(kind, "fish_extra")) {
00067 *out_weight = Cube::LIGHT;
00068 *out_power = Cube::LIGHT;
00069 *out_alive = true;
00070 }
00071 else if (StringTool::startsWith(kind, "fish_EXTRA")) {
00072 *out_weight = Cube::LIGHT;
00073 *out_power = Cube::HEAVY;
00074 *out_alive = true;
00075 }
00076 else {
00077 *out_power = Cube::NONE;
00078 *out_alive = false;
00079 if ("item_light" == kind) {
00080 *out_weight = Cube::LIGHT;
00081 }
00082 else if ("item_heavy" == kind) {
00083 *out_weight = Cube::HEAVY;
00084 }
00085 else if ("item_fixed" == kind) {
00086 *out_weight = Cube::FIXED;
00087 }
00088 else {
00089 throw LogicException(ExInfo("unknown model kind")
00090 .addInfo("kind", kind));
00091 }
00092 }
00093 }
00094
00095
00096
00097
00098
00099
00100 Unit *
00101 ModelFactory::createUnit(const std::string &kind)
00102 {
00103 Unit *result = NULL;
00104 if ("fish_small" == kind) {
00105 KeyControl smallfish;
00106 smallfish.setUp(SDLK_i);
00107 smallfish.setDown(SDLK_k);
00108 smallfish.setLeft(SDLK_j);
00109 smallfish.setRight(SDLK_l);
00110 result = new Unit(smallfish, ControlSym('u', 'd', 'l', 'r'), true);
00111 }
00112 else if ("fish_big" == kind) {
00113 KeyControl bigfish;
00114 bigfish.setUp(SDLK_w);
00115 bigfish.setDown(SDLK_s);
00116 bigfish.setLeft(SDLK_a);
00117 bigfish.setRight(SDLK_d);
00118 result = new Unit(bigfish, ControlSym('U', 'D', 'L', 'R'));
00119 }
00120 else if (StringTool::startsWith(kind, "fish_extra") ||
00121 StringTool::startsWith(kind, "fish_EXTRA"))
00122 {
00123 KeyControl extrafish;
00124 extrafish.setUp(SDLK_LAST);
00125 extrafish.setDown(SDLK_LAST);
00126 extrafish.setLeft(SDLK_LAST);
00127 extrafish.setRight(SDLK_LAST);
00128 result = new Unit(extrafish, parseExtraControlSym(kind));
00129 }
00130 return result;
00131 }
00132
00133
00134
00135
00136
00137 Cube *
00138 ModelFactory::createBorder()
00139 {
00140 Cube *border = new Cube(V2(-1,-1), Cube::FIXED, Cube::NONE, false,
00141 new Shape("X\n"));
00142 return border;
00143 }
00144
00145
00146
00147
00148
00149 Cube *
00150 ModelFactory::createOutputItem(const std::string &kind, const V2 &loc,
00151 const std::string &shape)
00152 {
00153 Dir::eDir outDir = Dir::DIR_NO;
00154 if ("output_left" == kind) {
00155 outDir = Dir::DIR_LEFT;
00156 }
00157 else if ("output_right" == kind) {
00158 outDir = Dir::DIR_RIGHT;
00159 }
00160 else if ("output_up" == kind) {
00161 outDir = Dir::DIR_UP;
00162 }
00163 else if ("output_down" == kind) {
00164 outDir = Dir::DIR_DOWN;
00165 }
00166 else {
00167 throw LogicException(ExInfo("unknown border dir")
00168 .addInfo("kind", kind));
00169 }
00170
00171 Cube *model = new Cube(loc,
00172 Cube::FIXED, Cube::NONE, false, new Shape(shape));
00173 model->setOutDir(outDir);
00174 return model;
00175 }
00176
00177
00178
00179
00180
00181
00182 ControlSym
00183 ModelFactory::parseExtraControlSym(const std::string &kind)
00184 {
00185 static const std::string PREFIX = "fish_extra-";
00186 if (kind.size() != PREFIX.size() + 4) {
00187 throw LogicException(ExInfo("you must specify control symbols")
00188 .addInfo("kind", kind));
00189 }
00190
00191 char up = kind[PREFIX.size()];
00192 char down = kind[PREFIX.size() + 1];
00193 char left = kind[PREFIX.size() + 2];
00194 char right = kind[PREFIX.size() + 3];
00195 return ControlSym(up, down, left, right);
00196 }
00197