3 // Pingus - A free Lemmings clone
4 // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include "lispreader.h"
30 namespace WorldMapNS {
32 TileManager* TileManager::instance_ = 0;
34 TileManager::TileManager()
36 lisp_object_t* root_obj = lisp_read_from_file(datadir + "images/worldmap/antarctica.scm");
40 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0)
42 lisp_object_t* cur = lisp_cdr(root_obj);
44 while(!lisp_nil_p(cur))
46 lisp_object_t* element = lisp_car(cur);
48 if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
51 std::string filename = "<invalid>";
53 Tile* tile = new Tile;
60 LispReader reader(lisp_cdr(element));
61 reader.read_int("id", &id);
62 reader.read_bool("north", &tile->north);
63 reader.read_bool("south", &tile->south);
64 reader.read_bool("west", &tile->west);
65 reader.read_bool("east", &tile->east);
66 reader.read_bool("stop", &tile->stop);
67 reader.read_string("image", &filename);
69 texture_load(&tile->sprite,
70 datadir + "/images/worldmap/" + filename,
73 if (id >= int(tiles.size()))
80 puts("Unhandled symbol");
93 TileManager::get(int i)
95 assert(i >=0 && i < int(tiles.size()));
99 Tux::Tux(WorldMap* worldmap_)
100 : worldmap(worldmap_)
102 texture_load(&sprite, datadir + "/images/worldmap/tux.png", USE_ALPHA);
108 input_direction = NONE;
114 float x = tile_pos.x * 32;
115 float y = tile_pos.y * 32;
135 texture_draw(&sprite, (int)x, (int)y, NO_UPDATE);
147 Tux::update(float delta)
151 if (input_direction != NONE)
152 { // We got a new direction, so lets start walking when possible
154 if (worldmap->path_ok(input_direction, tile_pos, &next_tile))
156 tile_pos = next_tile;
158 direction = input_direction;
164 // Let tux walk a few pixels (20 pixel/sec)
165 offset += 20.0f * delta;
168 { // We reached the next tile, so we check what to do now
171 if (worldmap->at(tile_pos)->stop)
178 if (worldmap->path_ok(direction, tile_pos, &next_tile))
180 tile_pos = next_tile;
184 puts("Tilemap data is buggy");
200 texture_load(&level_sprite, datadir + "/images/worldmap/levelmarker.png", USE_ALPHA);
202 input_direction = NONE;
206 music = "SALCON.MOD";
212 WorldMap::~WorldMap()
220 lisp_object_t* root_obj = lisp_read_from_file(datadir + "levels/default/worldmap.scm");
223 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
225 lisp_object_t* cur = lisp_cdr(root_obj);
227 while(!lisp_nil_p(cur))
229 lisp_object_t* element = lisp_car(cur);
231 if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0)
233 LispReader reader(lisp_cdr(element));
234 reader.read_int("width", &width);
235 reader.read_int("height", &height);
236 reader.read_int_vector("data", &tilemap);
238 else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
240 LispReader reader(lisp_cdr(element));
241 reader.read_string("name", &name);
242 reader.read_string("music", &music);
244 else if (strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
246 lisp_object_t* cur = lisp_cdr(element);
248 while(!lisp_nil_p(cur))
250 lisp_object_t* element = lisp_car(cur);
252 if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
255 LispReader reader(lisp_cdr(element));
256 reader.read_string("name", &level.name);
257 reader.read_int("x-pos", &level.x);
258 reader.read_int("y-pos", &level.y);
259 levels.push_back(level);
276 WorldMap::get_input()
282 while (SDL_PollEvent(&event))
291 switch(event.key.keysym.sym)
307 Uint8 *keystate = SDL_GetKeyState(NULL);
309 input_direction = NONE;
311 if (keystate[SDLK_LEFT])
312 input_direction = WEST;
313 else if (keystate[SDLK_RIGHT])
314 input_direction = EAST;
315 else if (keystate[SDLK_UP])
316 input_direction = NORTH;
317 else if (keystate[SDLK_DOWN])
318 input_direction = SOUTH;
322 WorldMap::get_next_tile(Point pos, Direction direction)
345 WorldMap::path_ok(Direction direction, Point old_pos, Point* new_pos)
347 *new_pos = get_next_tile(old_pos, direction);
349 if (!(new_pos->x >= 0 && new_pos->x < width
350 && new_pos->y >= 0 && new_pos->y < height))
351 { // New position is outsite the tilemap
355 { // Check if we the tile allows us to go to new_pos
359 return (at(old_pos)->west && at(*new_pos)->east);
362 return (at(old_pos)->east && at(*new_pos)->west);
365 return (at(old_pos)->north && at(*new_pos)->south);
368 return (at(old_pos)->south && at(*new_pos)->north);
371 assert(!"path_ok() can't work if direction is NONE");
380 if (enter_level && !tux->is_moving())
382 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
384 if (i->x == tux->get_tile_pos().x &&
385 i->y == tux->get_tile_pos().y)
387 std::cout << "Enter the current level: " << i->name << std::endl;;
389 gameloop(const_cast<char*>((datadir + "levels/default/" + i->name).c_str()),
390 1, ST_GL_LOAD_LEVEL_FILE);
398 tux->set_direction(input_direction);
404 WorldMap::at(Point p)
410 return TileManager::instance()->get(tilemap[width * p.y + p.x]);
416 for(int y = 0; y < height; ++y)
417 for(int x = 0; x < width; ++x)
419 Tile* tile = at(Point(x, y));
420 texture_draw(&tile->sprite, x*32, y*32, NO_UPDATE);
423 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
425 texture_draw(&level_sprite, i->x*32, i->y*32, NO_UPDATE);
437 song = load_song(datadir + "/music/" + music);
450 } // namespace WorldMapNS
454 WorldMapNS::WorldMap worldmap;