put both Ryan's and my flapping code into player.cpp with mine currently activated.
[supertux.git] / src / leveleditor.cpp
index 2cf77d5..8d27512 100644 (file)
@@ -37,6 +37,7 @@
 #include "badguy.h"
 #include "gameobjs.h"
 #include "door.h"
+#include "camera.h"
 
 LevelEditor::LevelEditor()
 {
@@ -47,8 +48,7 @@ global_frame_counter = 0;
 frame_timer.init(true);
 level_name_timer.init(true);
 selection_end = selection_ini = Vector(0,0);
-left_button = false;
-middle_button = false;
+left_button = middle_button = mouse_moved =  false;
 
 cur_layer = LAYER_TILES;
 level_changed = false;
@@ -215,7 +215,10 @@ Menu::set_current(0);
 DrawingContext context;
 
 if(!filename.empty())
+  {
+  level_nb = -1;
   load_level(filename);
+  }
 else
   Menu::set_current(main_menu);
 
@@ -236,6 +239,8 @@ if(level_changed)
 
 void LevelEditor::events()
 {
+mouse_moved = false;
+
 while(SDL_PollEvent(&event))
   {
   Menu* menu = Menu::current();
@@ -373,6 +378,7 @@ std::cerr << "previous sector.\n";
     switch(event.type)
       {
       case SDL_MOUSEMOTION:
+        mouse_moved = true;
         if(SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(SDL_BUTTON_RIGHT))
           {  // movement like in strategy games
           scroll.x += -1 * event.motion.xrel;
@@ -381,6 +387,7 @@ std::cerr << "previous sector.\n";
         break;
 
       case SDL_MOUSEBUTTONDOWN:
+        mouse_moved = true;
         if(event.button.button == SDL_BUTTON_LEFT)
           left_button = true;
         else if(event.button.button == SDL_BUTTON_MIDDLE)
@@ -391,6 +398,7 @@ std::cerr << "previous sector.\n";
         break;
 
       case SDL_MOUSEBUTTONUP:
+        mouse_moved = true;
         if(event.button.button == SDL_BUTTON_LEFT)
           left_button = false;
         else if(event.button.button == SDL_BUTTON_MIDDLE)
@@ -525,11 +533,14 @@ if(sector)
   if(scroll.y > height - screen->h/2)
     scroll.y = height - screen->h/2;
 
-  if(left_button)
+  // set camera translation, since BadGuys like it
+  sector->camera->set_scrolling((int)scroll.x, (int)scroll.y);
+
+  if(left_button && mouse_moved)
     for(unsigned int x = 0; x < selection.size(); x++)
       for(unsigned int y = 0; y < selection[x].size(); y++)
-        change((int)((scroll.x + event.button.x)/(32*zoom)) + x,
-             (int)((scroll.y + event.button.y)/(32*zoom)) + y, selection[x][y], 
+        change((int)(scroll.x + event.button.x) + (x*32),
+             (int)(scroll.y + event.button.y) + (y*32), selection[x][y], 
              cur_layer);
   }
 }
@@ -545,9 +556,12 @@ context.draw_filled_rect(Vector(0,0), Vector(screen->w,screen->h), Color(60,60,6
 if(level_name_timer.check())
   {
   context.draw_text(gold_text, level.name, Vector(screen->w/2, 30), CENTER_ALLIGN, LAYER_GUI);
-  char str[128];
-  sprintf(str, "%i/%i", level_nb+1, level_subset.get_num_levels());
-  context.draw_text(gold_text, str, Vector(screen->w/2, 50), CENTER_ALLIGN, LAYER_GUI);
+  if(level_nb != -1)
+    {
+    char str[128];
+    sprintf(str, "%i/%i", level_nb+1, level_subset.get_num_levels());
+    context.draw_text(gold_text, str, Vector(screen->w/2, 50), CENTER_ALLIGN, LAYER_GUI);
+    }
   }
 if(sector)
   context.draw_text(white_small_text, _("F1 for help"), Vector(5, 510), LEFT_ALLIGN, LAYER_GUI-10);
@@ -804,57 +818,66 @@ sound_manager->halt_music();
 
 void LevelEditor::change(int x, int y, int newtile, int layer)
 {  // find the tilemap of the current layer, and then change the tile
-if(x < 0 || (unsigned int)x > sector->solids->get_width() ||
-   y < 0 || (unsigned int)y > sector->solids->get_height())
+if(x < 0 || (unsigned int)x > sector->solids->get_width()*32 ||
+   y < 0 || (unsigned int)y > sector->solids->get_height()*32)
   return;
 
 level_changed = true;
 
+if(zoom != 1)
+  {  // no need to do this for normal view (no zoom)
+  x = (int)(x * (zoom*32) / 32);
+  y = (int)(y * (zoom*32) / 32);
+  }
+
 if(newtile < 0)  // add object
   {
   // remove an active tile or object that might be there
   change(x, y, 0, LAYER_TILES);
 
   if(newtile == OBJ_TRAMPOLINE)
-    sector->add_object(new Trampoline(x*32, y*32));
+    sector->add_object(new Trampoline(x, y));
   else if(newtile == OBJ_FLYING_PLATFORM)
-    sector->add_object(new FlyingPlatform(x*32, y*32));
+    sector->add_object(new FlyingPlatform(x, y));
   else if(newtile == OBJ_DOOR)
-    sector->add_object(new Door(x*32, y*32));
+    sector->add_object(new Door(x, y));
   else
-    sector->add_object(new BadGuy(BadGuyKind((-newtile)-1), x*32, y*32));
+    sector->add_object(new BadGuy(BadGuyKind((-newtile)-1), x, y));
 
   sector->update_game_objects();
   }
 else if(cur_layer == LAYER_FOREGROUNDTILES)
-  foregrounds->change(x, y, newtile);
+  foregrounds->change(x/32, y/32, newtile);
 else if(cur_layer == LAYER_TILES)
   {
   // remove a bad guy if it's there
+  // we /32 in order to round numbers
   for(Sector::GameObjects::iterator i = sector->gameobjects.begin(); i < sector->gameobjects.end(); i++)
     {
     BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
     if(badguy)
-      if(badguy->base.x == x*32 && badguy->base.y == y*32)
+      if((int)badguy->base.x/32 == x/32 && (int)badguy->base.y/32 == y/32)
         sector->gameobjects.erase(i);
     Trampoline* trampoline = dynamic_cast<Trampoline*> (*i);
     if(trampoline)
-      if(trampoline->base.x == x*32 && trampoline->base.y == y*32)
+    {
+      if((int)trampoline->base.x/32 == x/32 && (int)trampoline->base.y/32 == y/32)
         sector->gameobjects.erase(i);
+        }
     FlyingPlatform* flying_platform = dynamic_cast<FlyingPlatform*> (*i);
     if(flying_platform)
-      if(flying_platform->base.x == x*32 && flying_platform->base.y == y*32)
+      if((int)flying_platform->base.x/32 == x/32 && (int)flying_platform->base.y/32 == y/32)
         sector->gameobjects.erase(i);
     Door* door = dynamic_cast<Door*> (*i);
     if(door)
-      if(door->get_area().x == x*32 && door->get_area().y == y*32)
+      if((int)door->get_area().x/32 == x/32 && (int)door->get_area().y/32 == y/32)
         sector->gameobjects.erase(i);
     }
   sector->update_game_objects();
-  solids->change(x, y, newtile);
+  solids->change(x/32, y/32, newtile);
   }
 else if(cur_layer == LAYER_BACKGROUNDTILES)
-  backgrounds->change(x, y, newtile);
+  backgrounds->change(x/32, y/32, newtile);
 }
 
 void LevelEditor::show_help()