Separated EndSequence logic from GameSession. This will allow for nice hierarchy...
[supertux.git] / src / object / endsequence.cpp
1 //  $Id$
2 //
3 //  SuperTux - End Sequence
4 //  Copyright (C) 2007 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
22 #include "endsequence.hpp"
23
24 #include <stdexcept>
25 #include <iostream>
26 #include <sstream>
27 #include "main.hpp"
28 #include "resources.hpp"
29 #include "sector.hpp"
30 #include "gettext.hpp"
31 #include "object_factory.hpp"
32 #include "object/player.hpp"
33 #include "video/drawing_context.hpp"
34 #include "lisp/list_iterator.hpp"
35 #include "log.hpp"
36 #include "scripting/level_time.hpp"
37 #include "scripting/squirrel_util.hpp"
38
39 EndSequence::EndSequence()
40 : isrunning(false), isdone(false), tux_may_walk(true)
41 {
42   end_sequence_controller = 0;
43 }
44
45 EndSequence::~EndSequence()
46 {
47   delete end_sequence_controller;
48 }
49
50 void
51 EndSequence::update(float elapsed_time)
52 {
53   if (!isrunning) return;
54   running(elapsed_time);    
55 }
56
57 void
58 EndSequence::draw(DrawingContext& /*context*/)
59 {
60 }
61
62 void
63 EndSequence::start()
64 {
65   if (isrunning) return;
66   isrunning = true;
67   isdone = false;
68   
69   Player& tux = *Sector::current()->player;
70   end_sequence_controller = new CodeController();
71   tux.set_controller(end_sequence_controller);
72
73   starting();
74 }
75
76 void
77 EndSequence::stop_tux()
78 {
79   tux_may_walk = false;
80 }
81
82 void
83 EndSequence::stop()
84 {
85   if (!isrunning) return;
86   isrunning = false;
87   isdone = true;
88   stopping();
89 }
90
91 bool
92 EndSequence::is_tux_stopped()
93 {
94   return !tux_may_walk;
95 }
96
97  bool
98 EndSequence::is_done()
99 {
100   return isdone;
101 }
102     
103 void
104 EndSequence::starting()
105 {
106   last_x_pos = -1;
107   endsequence_timer.start(7.3);
108 }
109
110 void
111 EndSequence::running(float /*elapsed_time*/)
112 {
113   Player& tux = *Sector::current()->player;
114
115   if (tux_may_walk) {
116     end_sequence_controller->press(Controller::RIGHT);
117     if (int(last_x_pos) == int(tux.get_pos().x)) {
118       end_sequence_controller->press(Controller::JUMP);
119     }
120   }
121
122   last_x_pos = tux.get_pos().x;
123
124   if (endsequence_timer.check()) isdone = true;
125 }
126
127 void
128 EndSequence::stopping()
129 {
130 }
131