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 texture_load(&tile->sprite,
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 texture_load(&sprite, datadir + "/images/worldmap/tux.png", USE_ALPHA);
111 input_direction = NONE;
117 float x = tile_pos.x * 32;
118 float y = tile_pos.y * 32;
138 texture_draw(&sprite, (int)x, (int)y);
150 Tux::update(float delta)
154 if (input_direction != NONE)
155 { // We got a new direction, so lets start walking when possible
157 if (worldmap->path_ok(input_direction, tile_pos, &next_tile))
159 tile_pos = next_tile;
161 direction = input_direction;
167 // Let tux walk a few pixels (20 pixel/sec)
168 offset += 20.0f * delta;
171 { // We reached the next tile, so we check what to do now
174 if (worldmap->at(tile_pos)->stop)
181 if (worldmap->path_ok(direction, tile_pos, &next_tile))
183 tile_pos = next_tile;
187 puts("Tilemap data is buggy");
203 texture_load(&level_sprite, datadir + "/images/worldmap/levelmarker.png", USE_ALPHA);
205 input_direction = NONE;
209 music = "SALCON.MOD";
215 WorldMap::~WorldMap()
223 std::string filename = datadir + "levels/default/worldmap.stwm";
225 lisp_object_t* root_obj = lisp_read_from_file(filename);
227 st_abort("Couldn't load file", filename);
229 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
231 lisp_object_t* cur = lisp_cdr(root_obj);
233 while(!lisp_nil_p(cur))
235 lisp_object_t* element = lisp_car(cur);
237 if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0)
239 LispReader reader(lisp_cdr(element));
240 reader.read_int("width", &width);
241 reader.read_int("height", &height);
242 reader.read_int_vector("data", &tilemap);
244 else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
246 LispReader reader(lisp_cdr(element));
247 reader.read_string("name", &name);
248 reader.read_string("music", &music);
250 else if (strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
252 lisp_object_t* cur = lisp_cdr(element);
254 while(!lisp_nil_p(cur))
256 lisp_object_t* element = lisp_car(cur);
258 if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
261 LispReader reader(lisp_cdr(element));
262 reader.read_string("name", &level.name);
263 reader.read_int("x", &level.x);
264 reader.read_int("y", &level.y);
265 levels.push_back(level);
282 WorldMap::get_input()
287 input_direction = NONE;
289 while (SDL_PollEvent(&event))
298 switch(event.key.keysym.sym)
312 case SDL_JOYAXISMOTION:
313 switch(event.jaxis.axis)
316 if (event.jaxis.value < -JOYSTICK_DEAD_ZONE)
317 input_direction = WEST;
318 else if (event.jaxis.value > JOYSTICK_DEAD_ZONE)
319 input_direction = EAST;
322 if (event.jaxis.value > JOYSTICK_DEAD_ZONE)
323 input_direction = SOUTH;
324 else if (event.jaxis.value < -JOYSTICK_DEAD_ZONE)
325 input_direction = NORTH;
330 case SDL_JOYBUTTONDOWN:
331 if (event.jbutton.button == JOY_B)
340 Uint8 *keystate = SDL_GetKeyState(NULL);
342 if (keystate[SDLK_LEFT])
343 input_direction = WEST;
344 else if (keystate[SDLK_RIGHT])
345 input_direction = EAST;
346 else if (keystate[SDLK_UP])
347 input_direction = NORTH;
348 else if (keystate[SDLK_DOWN])
349 input_direction = SOUTH;
353 WorldMap::get_next_tile(Point pos, Direction direction)
376 WorldMap::path_ok(Direction direction, Point old_pos, Point* new_pos)
378 *new_pos = get_next_tile(old_pos, direction);
380 if (!(new_pos->x >= 0 && new_pos->x < width
381 && new_pos->y >= 0 && new_pos->y < height))
382 { // New position is outsite the tilemap
386 { // Check if we the tile allows us to go to new_pos
390 return (at(old_pos)->west && at(*new_pos)->east);
393 return (at(old_pos)->east && at(*new_pos)->west);
396 return (at(old_pos)->north && at(*new_pos)->south);
399 return (at(old_pos)->south && at(*new_pos)->north);
402 assert(!"path_ok() can't work if direction is NONE");
411 if (enter_level && !tux->is_moving())
413 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
415 if (i->x == tux->get_tile_pos().x &&
416 i->y == tux->get_tile_pos().y)
418 std::cout << "Enter the current level: " << i->name << std::endl;;
420 GameSession session(const_cast<char*>((datadir + "levels/default/" + i->name).c_str()),
421 1, ST_GL_LOAD_LEVEL_FILE);
427 std::cout << "Nothing to enter at: "
428 << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
432 tux->set_direction(input_direction);
438 WorldMap::at(Point p)
444 return TileManager::instance()->get(tilemap[width * p.y + p.x]);
450 for(int y = 0; y < height; ++y)
451 for(int x = 0; x < width; ++x)
453 Tile* tile = at(Point(x, y));
454 texture_draw(&tile->sprite, x*32, y*32);
457 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
459 texture_draw(&level_sprite, i->x*32, i->y*32);
471 song = load_song(datadir + "/music/" + music);
484 } // namespace WorldMapNS
488 WorldMapNS::WorldMap worldmap;