3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2004 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"
31 namespace WorldMapNS {
33 TileManager* TileManager::instance_ = 0;
35 TileManager::TileManager()
37 std::string stwt_filename = datadir + "images/worldmap/antarctica.stwt";
38 lisp_object_t* root_obj = lisp_read_from_file(stwt_filename);
41 st_abort("Couldn't load file", stwt_filename);
43 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0)
45 lisp_object_t* cur = lisp_cdr(root_obj);
47 while(!lisp_nil_p(cur))
49 lisp_object_t* element = lisp_car(cur);
51 if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
54 std::string filename = "<invalid>";
56 Tile* tile = new Tile;
63 LispReader reader(lisp_cdr(element));
64 reader.read_int("id", &id);
65 reader.read_bool("north", &tile->north);
66 reader.read_bool("south", &tile->south);
67 reader.read_bool("west", &tile->west);
68 reader.read_bool("east", &tile->east);
69 reader.read_bool("stop", &tile->stop);
70 reader.read_string("image", &filename);
72 tile->sprite = new Surface(
73 datadir + "/images/worldmap/" + filename,
76 if (id >= int(tiles.size()))
83 puts("Unhandled symbol");
96 TileManager::get(int i)
98 assert(i >=0 && i < int(tiles.size()));
102 Tux::Tux(WorldMap* worldmap_)
103 : worldmap(worldmap_)
105 sprite = new Surface(datadir + "/images/worldmap/tux.png", USE_ALPHA);
111 input_direction = NONE;
115 Tux::draw(const Point& offset)
117 Point pos = get_pos();
118 sprite->draw(pos.x + offset.x,
126 float x = tile_pos.x * 32;
127 float y = tile_pos.y * 32;
147 return Point((int)x, (int)y);
159 Tux::update(float delta)
163 if (input_direction != NONE)
164 { // We got a new direction, so lets start walking when possible
166 if (worldmap->path_ok(input_direction, tile_pos, &next_tile))
168 tile_pos = next_tile;
170 direction = input_direction;
176 // Let tux walk a few pixels (20 pixel/sec)
177 offset += 20.0f * delta;
180 { // We reached the next tile, so we check what to do now
183 if (worldmap->at(tile_pos)->stop)
190 if (worldmap->path_ok(direction, tile_pos, &next_tile))
192 tile_pos = next_tile;
196 puts("Tilemap data is buggy");
212 level_sprite = new Surface(datadir + "/images/worldmap/levelmarker.png", USE_ALPHA);
213 leveldot_green = new Surface(datadir + "/images/worldmap/leveldot_green.png", USE_ALPHA);
214 leveldot_red = new Surface(datadir + "/images/worldmap/leveldot_red.png", USE_ALPHA);
216 input_direction = NONE;
220 music = "SALCON.MOD";
226 WorldMap::~WorldMap()
234 std::string filename = datadir + "levels/default/worldmap.stwm";
236 lisp_object_t* root_obj = lisp_read_from_file(filename);
238 st_abort("Couldn't load file", filename);
240 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
242 lisp_object_t* cur = lisp_cdr(root_obj);
244 while(!lisp_nil_p(cur))
246 lisp_object_t* element = lisp_car(cur);
248 if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0)
250 LispReader reader(lisp_cdr(element));
251 reader.read_int("width", &width);
252 reader.read_int("height", &height);
253 reader.read_int_vector("data", &tilemap);
255 else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
257 LispReader reader(lisp_cdr(element));
258 reader.read_string("name", &name);
259 reader.read_string("music", &music);
261 else if (strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
263 lisp_object_t* cur = lisp_cdr(element);
265 while(!lisp_nil_p(cur))
267 lisp_object_t* element = lisp_car(cur);
269 if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
272 LispReader reader(lisp_cdr(element));
273 reader.read_string("name", &level.name);
274 reader.read_int("x", &level.x);
275 reader.read_int("y", &level.y);
276 levels.push_back(level);
293 WorldMap::get_input()
296 input_direction = NONE;
299 while (SDL_PollEvent(&event))
303 current_menu->event(event);
314 switch(event.key.keysym.sym)
317 Menu::set_current(worldmap_menu);
318 show_menu = !show_menu;
329 case SDL_JOYAXISMOTION:
330 switch(event.jaxis.axis)
333 if (event.jaxis.value < -JOYSTICK_DEAD_ZONE)
334 input_direction = WEST;
335 else if (event.jaxis.value > JOYSTICK_DEAD_ZONE)
336 input_direction = EAST;
339 if (event.jaxis.value > JOYSTICK_DEAD_ZONE)
340 input_direction = SOUTH;
341 else if (event.jaxis.value < -JOYSTICK_DEAD_ZONE)
342 input_direction = NORTH;
347 case SDL_JOYBUTTONDOWN:
348 if (event.jbutton.button == JOY_B)
360 Uint8 *keystate = SDL_GetKeyState(NULL);
362 if (keystate[SDLK_LEFT])
363 input_direction = WEST;
364 else if (keystate[SDLK_RIGHT])
365 input_direction = EAST;
366 else if (keystate[SDLK_UP])
367 input_direction = NORTH;
368 else if (keystate[SDLK_DOWN])
369 input_direction = SOUTH;
374 WorldMap::get_next_tile(Point pos, Direction direction)
397 WorldMap::path_ok(Direction direction, Point old_pos, Point* new_pos)
399 *new_pos = get_next_tile(old_pos, direction);
401 if (!(new_pos->x >= 0 && new_pos->x < width
402 && new_pos->y >= 0 && new_pos->y < height))
403 { // New position is outsite the tilemap
407 { // Check if we the tile allows us to go to new_pos
411 return (at(old_pos)->west && at(*new_pos)->east);
414 return (at(old_pos)->east && at(*new_pos)->west);
417 return (at(old_pos)->north && at(*new_pos)->south);
420 return (at(old_pos)->south && at(*new_pos)->north);
423 assert(!"path_ok() can't work if direction is NONE");
432 if (enter_level && !tux->is_moving())
434 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
436 if (i->x == tux->get_tile_pos().x &&
437 i->y == tux->get_tile_pos().y)
439 std::cout << "Enter the current level: " << i->name << std::endl;;
441 GameSession session(datadir + "levels/default/" + i->name,
442 1, ST_GL_LOAD_LEVEL_FILE);
448 std::cout << "Nothing to enter at: "
449 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
453 tux->set_direction(input_direction);
459 if(current_menu == worldmap_menu)
461 switch (worldmap_menu->check())
463 case 2: // Return to game
466 case 5: // Quit Worldmap
475 WorldMap::at(Point p)
481 return TileManager::instance()->get(tilemap[width * p.y + p.x]);
485 WorldMap::draw(const Point& offset)
487 for(int y = 0; y < height; ++y)
488 for(int x = 0; x < width; ++x)
490 Tile* tile = at(Point(x, y));
491 tile->sprite->draw(x*32 + offset.x,
495 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
497 leveldot_green->draw(i->x*32 + offset.x,
509 song = load_song(datadir + "/music/" + music);
513 Point tux_pos = tux->get_pos();
516 offset.x = -tux_pos.x + screen->w/2;
517 offset.y = -tux_pos.y + screen->h/2;
519 if (offset.x > 0) offset.x = 0;
520 if (offset.y > 0) offset.y = 0;
522 if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
523 if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
530 menu_process_current();
539 } // namespace WorldMapNS
543 WorldMapNS::WorldMap worldmap;