1 /*****************************************************************************
2 * Copyright (c) 2007 Russ Adams, Sean Eubanks, Asgard Contributors
3 * This file is part of Asgard.
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"
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"
36 int MapObjectFactory::processRow(void *mapObjectType
, int columnCount
, char **columnValue
, char **columnName
)
38 MapObjectType
*type
= static_cast<MapObjectType
*>(mapObjectType
);
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; }
54 void MapObjectFactory::createTile(char **columnValue
)
59 // MapObject Req'd Members
65 TileType tileType
= (TileType
)atoi(columnValue
[TILE_COLUMN_TILE_TYPE
]);
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; }
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();
102 // MapObject Req'd Members
109 Container
* container
= new Container();
112 rs
= db
->loadHardpoints(atoi(columnValue
[CONTAINER_COLUMN_MAP_OBJECT_ID
]));
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();
150 // MapObject Req'd Members
156 NonPlayerCharacter
*npc
= new NonPlayerCharacter();
161 rs
= db
->loadHardpoints(atoi(columnValue
[NON_PLAYER_CHARACTER_COLUMN_MAP_OBJECT_ID
]));
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
]));
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();
201 // MapObject Req'd Members
207 StaticMapObject
*staticMapObject
= new StaticMapObject();
209 if (staticMapObject
!= NULL
)
212 rs
= db
->loadHardpoints(atoi(columnValue
[STATIC_MAP_OBJECT_COLUMN_MAP_OBJECT_ID
]));
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
)
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
);
269 return new RectHardpoint();
274 Coordinate
* MapObjectFactory::createNonPlayerCharacterPathPoint(RowSet
* rs
, int row
)
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
);