Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

ModelFactory.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
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  * Add model at scene.
00020  * @param kind kind of item (e.g. "fish_big", "item_light", ...)
00021  * @param loc placement location
00022  * @param shape see Shape for format
00023  * @return model
00024  *
00025  * @throws LogicException for unknown kind
00026  * @throws LayoutException when shape or location is bad
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  * Determine object params.
00050  * @throws LogicException when kind is unkown
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  * Create unit for driveable fish.
00097  * @param kind kind of item (e.g. "fish_big", "item_light", ...)
00098  * @return new unit or NULL
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  * Create special model, which will be used for outher space.
00135  * NOTE: hack border around field
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  * Create one way output out of room.
00147  * @throws LogicException when output_DIR is not known
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  * Define controls symbols for extra fish.
00179  * Format: "fish_extra-UDLR"
00180  * @throws LogicException when symbols are not specified
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 

Generated on Wed Jun 1 09:54:31 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2