Testing Complete.
[asgard.git] / src / MapObjectFactory.cpp
blob122682f24498d87505d69a3fd87f20c41ae3a3ae
1 /*****************************************************************************
2 * Copyright (c) 2007 Russ Adams, Sean Eubanks, Asgard Contributors
3 * This file is part of Asgard.
4 *
5 * Asgard is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * Asgard is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Asgard; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 ****************************************************************************/
20 #include "MapObjectFactory.h"
21 #include "GameEngine.h"
22 #include "Tile.h"
23 #include "WaterTile.h"
24 #include "GrassTile.h"
25 #include "DesertTile.h"
26 #include "Container.h"
27 #include "StaticMapObject.h"
28 #include "NonPlayerCharacter.h"
29 #include "MapObjectType.h"
30 #include "DatabaseColumnMap.h"
31 #include "Hardpoint.h"
32 #include "CircHardpoint.h"
33 #include "RectHardpoint.h"
34 #include "Database.h"
36 int MapObjectFactory::processRow(void *mapObjectType, int columnCount, char **columnValue, char **columnName)
38 MapObjectType *type = static_cast<MapObjectType*>(mapObjectType);
40 switch(*type)
42 case MAP_OBJECT_TYPE_CONTAINER: { MapObjectFactory::createContainer(columnValue); break; }
43 case MAP_OBJECT_TYPE_NON_PLAYER_CHARACTER: { MapObjectFactory::createNonPlayerCharacter(columnValue); break; }
44 case MAP_OBJECT_TYPE_STATIC_MAP_OBJECT: { MapObjectFactory::createStaticMapObject(columnValue); break; }
45 case MAP_OBJECT_TYPE_TILE: { MapObjectFactory::createTile(columnValue); break; }
46 default: { /* TODO: Error case... add logging */ break; }
50 return 0;
54 void MapObjectFactory::createTile(char **columnValue)
56 // Create new tile
57 Tile *tile;
59 // MapObject Req'd Members
60 int wc_x;
61 int wc_y;
62 int height;
63 int width;
65 TileType tileType = (TileType)atoi(columnValue[TILE_COLUMN_TILE_TYPE]);
67 switch(tileType)
69 case TILE_TYPE_WATER: { tile = new WaterTile(); break; }
70 case TILE_TYPE_DESERT: { tile = new DesertTile(); break; }
71 case TILE_TYPE_GRASS: { tile = new GrassTile(); break; }
72 default: { break; }
75 if(tile != NULL)
77 wc_x = atoi(columnValue[TILE_COLUMN_WC_X]);
78 wc_y = atoi(columnValue[TILE_COLUMN_WC_Y]);
79 width = atoi(columnValue[TILE_COLUMN_WIDTH]);
80 height = atoi(columnValue[TILE_COLUMN_HEIGHT]);
82 // Set tile Map Object variables
83 tile->setLeftCorner(Coordinate(wc_x, wc_y));
84 tile->setWidth(width);
85 tile->setHeight(height);
87 // Get reference to GameEngine
88 GameEngine *gameEngine = GameEngine::getInstance();
90 // Add tile to gameEngine
91 gameEngine->addMapObject((MapObject*)tile);
97 void MapObjectFactory::createContainer(char **columnValue)
99 Database* db = Database::getInstance();
100 RowSet* rs;
102 // MapObject Req'd Members
103 int wc_x;
104 int wc_y;
105 int height;
106 int width;
108 // Objects to Create
109 Container* container = new Container();
111 // Create Hardpoints
112 rs = db->loadHardpoints(atoi(columnValue[CONTAINER_COLUMN_MAP_OBJECT_ID]));
114 if (rs != NULL)
116 for (int row = 0; row < rs->getRowCount(); row++)
117 container->addHardpoint(createHardpoint(rs,row));
121 // TODO: Create Items; Not 0.3.0
124 if (container != NULL)
126 // Set MapObject Members
127 wc_x = atoi(columnValue[CONTAINER_COLUMN_WC_X]);
128 wc_y = atoi(columnValue[CONTAINER_COLUMN_WC_Y]);
129 width = atoi(columnValue[CONTAINER_COLUMN_WIDTH]);
130 height = atoi(columnValue[CONTAINER_COLUMN_HEIGHT]);
132 container->setLeftCorner(Coordinate(wc_x, wc_y));
133 container->setWidth(width);
134 container->setHeight(height);
136 // Get reference to GameEngine
137 GameEngine *gameEngine = GameEngine::getInstance();
139 // Add container to gameEngine
140 gameEngine->addMapObject((MapObject*)container);
145 void MapObjectFactory::createNonPlayerCharacter(char **columnValue)
147 Database* db = Database::getInstance();
148 RowSet* rs;
150 // MapObject Req'd Members
151 int wc_x;
152 int wc_y;
153 int height;
154 int width;
156 NonPlayerCharacter *npc = new NonPlayerCharacter();
158 if (npc != NULL)
160 // Create Hardpoints
161 rs = db->loadHardpoints(atoi(columnValue[NON_PLAYER_CHARACTER_COLUMN_MAP_OBJECT_ID]));
163 if (rs != NULL)
165 for (int row = 0; row < rs->getRowCount(); row++)
166 npc->addHardpoint(createHardpoint(rs,row));
169 // Create NonPlayerCharacterPath
170 rs = db->loadNonPlayerCharacterPath(atoi(columnValue[NON_PLAYER_CHARACTER_COLUMN_MAP_OBJECT_ID]));
172 if (rs != NULL)
174 for (int row = 0; row < rs->getRowCount(); row++)
175 npc->addCoordinateToPath(createNonPlayerCharacterPathPoint(rs,row));
178 wc_x = atoi(columnValue[NON_PLAYER_CHARACTER_COLUMN_WC_X]);
179 wc_y = atoi(columnValue[NON_PLAYER_CHARACTER_COLUMN_WC_Y]);
180 width = atoi(columnValue[NON_PLAYER_CHARACTER_COLUMN_WIDTH]);
181 height = atoi(columnValue[NON_PLAYER_CHARACTER_COLUMN_HEIGHT]);
183 npc->setLeftCorner(Coordinate(wc_x, wc_y));
184 npc->setWidth(width);
185 npc->setHeight(height);
187 // Get reference to GameEngine
188 GameEngine *gameEngine = GameEngine::getInstance();
190 // Add container to gameEngine
191 gameEngine->addMapObject((MapObject*)npc);
196 void MapObjectFactory::createStaticMapObject(char **columnValue)
198 Database* db = Database::getInstance();
199 RowSet* rs;
201 // MapObject Req'd Members
202 int wc_x;
203 int wc_y;
204 int height;
205 int width;
207 StaticMapObject *staticMapObject = new StaticMapObject();
209 if (staticMapObject != NULL)
211 // Create Hardpoints
212 rs = db->loadHardpoints(atoi(columnValue[STATIC_MAP_OBJECT_COLUMN_MAP_OBJECT_ID]));
214 if (rs != NULL)
216 for (int row = 0; row < rs->getRowCount(); row++)
217 staticMapObject->addHardpoint(createHardpoint(rs,row));
220 wc_x = atoi(columnValue[STATIC_MAP_OBJECT_COLUMN_WC_X]);
221 wc_y = atoi(columnValue[STATIC_MAP_OBJECT_COLUMN_WC_Y]);
222 width = atoi(columnValue[STATIC_MAP_OBJECT_COLUMN_WIDTH]);
223 height = atoi(columnValue[STATIC_MAP_OBJECT_COLUMN_HEIGHT]);
225 staticMapObject->setLeftCorner(Coordinate(wc_x, wc_y));
226 staticMapObject->setWidth(width);
227 staticMapObject->setHeight(height);
229 // Get reference to GameEngine
230 GameEngine *gameEngine = GameEngine::getInstance();
232 // Add container to gameEngine
233 gameEngine->addMapObject((MapObject*)staticMapObject);
239 Hardpoint* MapObjectFactory::createHardpoint(RowSet* rs, int row)
241 int x = 0;
242 int y = 0;
243 int height = 0;
244 int width = 0;
245 double r = 0;
246 int type = 0;
248 // Rect and Circ Hardpoints need these.
249 type = atoi(rs->getColumnValue(row,HARDPOINT_COLUMN_HARDPOINT_TYPE));
250 x = atoi(rs->getColumnValue(row,HARDPOINT_COLUMN_RELATIVE_X));
251 y = atoi(rs->getColumnValue(row,HARDPOINT_COLUMN_RELATIVE_Y));
253 // Only RectHardpoints
254 if (type == HARDPOINT_TYPE_RECT)
256 height = atoi(rs->getColumnValue(row,HARDPOINT_COLUMN_WIDTH));
257 width = atoi(rs->getColumnValue(row,HARDPOINT_COLUMN_HEIGHT));
258 return new RectHardpoint(x,y,height,width);
260 // Only CircHardpoints
261 else if (type == HARDPOINT_TYPE_CIRC)
263 r = atof(rs->getColumnValue(row,HARDPOINT_COLUMN_RADIUS));
264 return new CircHardpoint(x,y,type);
266 // Default
267 else
269 return new RectHardpoint();
274 Coordinate* MapObjectFactory::createNonPlayerCharacterPathPoint(RowSet* rs, int row)
276 int wc_x = 0;
277 int wc_y = 0;
279 wc_x = atoi(rs->getColumnValue(row,NON_PLAYER_CHARACTER_PATH_COLUMN_WC_X));
280 wc_y = atoi(rs->getColumnValue(row,NON_PLAYER_CHARACTER_PATH_COLUMN_WC_Y));
282 return new Coordinate(wc_x, wc_y);