B91: Tux now guesses direction to walk in during an end sequence.
[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(Direction dir)
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   tux.set_speedlimit(230); //MAX_WALK_XM
73
74   walk_dir = dir;
75
76   starting();
77 }
78
79 void
80 EndSequence::stop_tux()
81 {
82   tux_may_walk = false;
83 }
84
85 void
86 EndSequence::stop()
87 {
88   if (!isrunning) return;
89   isrunning = false;
90   isdone = true;
91   stopping();
92 }
93
94 bool
95 EndSequence::is_tux_stopped()
96 {
97   return !tux_may_walk;
98 }
99
100  bool
101 EndSequence::is_done()
102 {
103   return isdone;
104 }
105
106 void
107 EndSequence::starting()
108 {
109   last_x_pos = -1;
110   endsequence_timer.start(7.3f);
111 }
112
113 void
114 EndSequence::running(float /*elapsed_time*/)
115 {
116   Player& tux = *Sector::current()->player;
117
118   if (tux_may_walk) {
119     end_sequence_controller->press((walk_dir == RIGHT) ? Controller::RIGHT : Controller::LEFT);
120     if (int(last_x_pos) == int(tux.get_pos().x)) {
121       end_sequence_controller->press(Controller::JUMP);
122     }
123   }
124
125   last_x_pos = tux.get_pos().x;
126
127   if (endsequence_timer.check()) isdone = true;
128 }
129
130 void
131 EndSequence::stopping()
132 {
133 }