/[enigma]/enigma/src/stones_simple.cc
ViewVC logotype

Diff of /enigma/src/stones_simple.cc

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.37 by reallysoft, Sun Jul 13 19:29:15 2003 UTC revision 1.38 by reallysoft, Fri Jul 18 17:18:38 2003 UTC
# Line 347  namespace Line 347  namespace
347    
348      private:      private:
349    
350            // GridObject interface
351            void init_model();
352            void on_removal();
353    
354            // Stone interface
355            void on_impulse(const Impulse& impulse);
356            bool is_removable() { return state == IDLE; }
357            void actor_hit(const StoneContact &sc);
358    
359            // TimeHandler interface
360            void alarm();
361    
362          // Variables :          // Variables :
363          enum State { IDLE, COME, GO } state;          enum State { IDLE, COME, GO } state;
364          Stone     *in_exchange_with;          YieldedGridStone *in_exchange_with;
365          Direction  move_dir;          Direction         move_dir;
366    
367        };
368    
369          SwapStone(Stone *exchanged_with, Direction dir)      void SwapStone::on_removal() {
370              : Stone(exchanged_with->get_kind()) // fake kind          if (state == COME) {
371              , state(GO)              g_timer.remove_alarm(this);
             , in_exchange_with(exchanged_with)  
             , move_dir(dir)  
         {}  
   
         void set_alarm() { g_timer.set_alarm(this, 0.1, false); }  
   
         void on_creation() {  
             if (state != IDLE) set_alarm();  
             Stone::on_creation();  
372          }          }
373        }
374    
375          void on_removal() {      void SwapStone::alarm() {
376              if (state != IDLE) {          assert(state == COME);
377                  g_timer.remove_alarm(this);  
378              }          state = IDLE;
379          }          init_model();
380            play_sound("st-move");
381    
382            GridPos oldPos = move(get_pos(), reverse(move_dir));
383            KillStone(oldPos);
384    
385            assert(in_exchange_with);
386            in_exchange_with->set_stone(oldPos);
387            delete in_exchange_with;
388            in_exchange_with = 0;
389        }
390    
391        void SwapStone::on_impulse(const Impulse& impulse) {
392            if (state == IDLE) {
393                GridPos oldp = get_pos();
394                GridPos newp = move(oldp, impulse.dir);
395    
396                if (!IsLevelBorder(newp)) {
397                    Stone *other = GetStone(newp);
398                    if (other && other->is_removable()) {
399                        SwapStone *newStone = clone();
400    
401                        newStone->state            = COME;
402                        newStone->in_exchange_with = new YieldedGridStone(other); // yields 'other'
403                        newStone->move_dir         = impulse.dir;
404    
405          void alarm() {                      g_timer.set_alarm(newStone, 0.1, false);
406              switch (state) {  
407                  case COME:                      SetStone(newp, newStone);
408                      state       = IDLE;  
409                        state    = GO;
410                        move_dir = impulse.dir;
411                      init_model();                      init_model();
                     play_sound("st-move");  
                     break;  
                 case GO: {  
                     assert(in_exchange_with);  
                     Stone   *st = in_exchange_with;  
                     GridPos  p  = get_pos();  
                     state       = IDLE;  
                     KillStone(p);  
                     SetStone(p, st);  
                     st->on_move();  
                     break;  
                 }  
                 case IDLE:  
                     assert(0);  
                     break;  
             }  
         }  
412    
413          void on_impulse(const Impulse& impulse) {                      play_sound ("st-move");
414              if (state == IDLE) {                      player::IncMoveCounter(1);
                 GridPos oldp = get_pos();  
                 GridPos newp = move(oldp, impulse.dir);  
   
                 if (!IsLevelBorder(newp)) {  
                     if (Stone *st = YieldStone(newp)) {  
                         Stone *self = YieldStone(oldp);  
                         SetStone(newp, self);  
                         state       = COME;  
                         move_dir    = impulse.dir;  
                         set_alarm();  
                         init_model();  
   
                         SetStone(oldp, new SwapStone(st, impulse.dir)); // sets alarm  
                         play_sound ("st-move");  
                         player::IncMoveCounter(2);  
                     }  
415                  }                  }
416              }              }
417          }          }
418        }
419    
420          void actor_hit(const StoneContact &sc) {      void SwapStone::actor_hit(const StoneContact &sc) {
421              Direction dir = get_push_direction (sc);          Direction dir = get_push_direction (sc);
422              if (dir != NODIR) {          if (dir != NODIR) {
423                  send_impulse(get_pos(), dir);              send_impulse(get_pos(), dir);
424            }
425        }
426    
427        void SwapStone::init_model() {
428            const char *model = 0;
429            switch (state) {
430                case IDLE: model = "st-swap"; break;
431                case COME:  {
432                    const char *models[] = { "st-swap-w", "st-swap-s", "st-swap-e", "st-swap-n" };
433                    model = models[move_dir];
434                    break;
435              }              }
436          }              case GO:  {
437                    const char *models[] = { "st-swap-e", "st-swap-n", "st-swap-w", "st-swap-s" };
438          void init_model() {                  model = models[move_dir];
439              const char *model = 0;                  break;
             switch (state) {  
                 case IDLE: model = "st-swap"; break;  
                 case COME:  {  
                     const char *models[] = { "st-swap-w", "st-swap-s", "st-swap-e", "st-swap-n" };  
                     model = models[move_dir];  
                     break;  
                 }  
                 case GO:  {  
                     const char *models[] = { "st-swap-e", "st-swap-n", "st-swap-w", "st-swap-s" };  
                     model = models[move_dir];  
                     break;  
                 }  
440              }              }
   
             set_model(model);  
441          }          }
     };  
442    
443            set_model(model);
444        }
445  }  }
446    
447  //----------------------------------------  //----------------------------------------

Legend:
Removed from v.1.37  
changed lines
  Added in v.1.38

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26