/[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.31 by reallysoft, Sun Jul 6 12:47:04 2003 UTC revision 1.32 by reallysoft, Wed Jul 9 07:01:05 2003 UTC
# Line 329  set_stone("st-swap", 10,10) Line 329  set_stone("st-swap", 10,10)
329  */  */
330  namespace  namespace
331  {  {
332      class SwapStone : public Stone {      class SwapStone : public Stone, public TimeHandler {
333          CLONEOBJ(SwapStone);          CLONEOBJ(SwapStone);
334    
335      public:      public:
336          SwapStone() : Stone("st-swap") {}          SwapStone()
337                : Stone("st-swap")
338                , state(IDLE)
339                , in_exchange_with(0)
340                , move_dir(NODIR)
341            {}
342    
343      private:      private:
344          void actor_hit(const StoneContact &sc)  
345          {          // Variables :
346            enum State { IDLE, COME, GO } state;
347            Stone     *in_exchange_with;
348            Direction  move_dir;
349    
350    
351            SwapStone(Stone *exchanged_with, Direction dir)
352                : Stone("st-swap")
353                , state(GO)
354                , in_exchange_with(exchanged_with)
355                , move_dir(dir)
356            {}
357    
358            void set_alarm() { g_timer.set_alarm(this, 0.1, false); }
359    
360            void on_creation() {
361                if (state != IDLE) set_alarm();
362                Stone::on_creation();
363            }
364    
365            void alarm() {
366                switch (state) {
367                    case COME:
368                        state       = IDLE;
369                        init_model();
370                        play_sound("st-move");
371                        break;
372                    case GO: {
373                        assert(in_exchange_with);
374                        Stone   *st = in_exchange_with;
375                        GridPos  p  = get_pos();
376                        KillStone(p);
377                        SetStone(p, st);
378                        st->on_move();
379                        break;
380                    }
381                    case IDLE:
382                        assert(0);
383                        break;
384                }
385            }
386    
387            void on_impulse(const Impulse& impulse) {
388                if (state == IDLE) {
389                    GridPos oldp = get_pos();
390                    GridPos newp = move(oldp, impulse.dir);
391    
392                    if (Stone *st = YieldStone(newp)) {
393                        if (!st->is_kind("borderstone")) {
394                            Stone *self = YieldStone(oldp);
395                            SetStone(newp, self);
396                            state       = COME;
397                            move_dir    = impulse.dir;
398                            set_alarm();
399                            init_model();
400    
401                            SetStone(oldp, new SwapStone(st, impulse.dir));
402                            play_sound ("st-move");
403                            player::IncMoveCounter(2);
404                        }
405                    }
406                }
407            }
408    
409            void actor_hit(const StoneContact &sc) {
410              Direction dir = get_push_direction (sc);              Direction dir = get_push_direction (sc);
411              if (dir != NODIR) {              if (dir != NODIR) {
412                  GridPos p = get_pos();                  send_impulse(get_pos(), dir);
                 GridPos newp = move(p, dir);  
                 if (GetStone(newp))  
                     SwapStones(p, newp);  
413              }              }
414          }          }
415    
416            void init_model() {
417                const char *model = 0;
418                switch (state) {
419                    case IDLE: model = "st-swap"; break;
420                    case COME:  {
421                        const char *models[] = { "st-swap-w", "st-swap-s", "st-swap-e", "st-swap-n" };
422                        model = models[move_dir];
423                        break;
424                    }
425                    case GO:  {
426                        const char *models[] = { "st-swap-e", "st-swap-n", "st-swap-w", "st-swap-s" };
427                        model = models[move_dir];
428                        break;
429                    }
430                }
431    
432                set_model(model);
433            }
434      };      };
435    
436  }  }
437    
438  //----------------------------------------  //----------------------------------------

Legend:
Removed from v.1.31  
changed lines
  Added in v.1.32

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