~ubuntu-branches/ubuntu/natty/plee-the-bear/natty

« back to all changes in this revision

Viewing changes to plee-the-bear/src/ptb/item/forest/code/frog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Julien Jorge, Julien Jorge
  • Date: 2010-11-17 20:13:34 UTC
  • mfrom: (6.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20101117201334-o4dp7uq437to7oxb
Tags: 0.5.1-1
[ Julien Jorge ]
* New upstream release (Closes: #565062, #546514).
* Add armhf architecture (Closes: #604689).
* Remove patches integrated upstream: rpath-editors.diff, rpath-game.diff,
  editors-menu-section.diff.
* Bump the Standard-Version, no changes.
* Update my email address.
* Set build dependency of libclaw to 1.6.0.
* Add the missing ${misc:Depends} in debian/control.
* List gettext translations in bear-factory.install and plee-the-bear.install.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Plee the Bear
 
3
 
 
4
  Copyright (C) 2005-2010 Julien Jorge, Sebastien Angibaud
 
5
 
 
6
  This program is free software; you can redistribute it and/or modify it
 
7
  under the terms of the GNU General Public License as published by the
 
8
  Free Software Foundation; either version 2 of the License, or (at your
 
9
  option) any later version.
 
10
 
 
11
  This program is distributed in the hope that it will be useful, but WITHOUT
 
12
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
13
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 
14
  more details.
 
15
 
 
16
  You should have received a copy of the GNU General Public License along
 
17
  with this program; if not, write to the Free Software Foundation, Inc.,
 
18
  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 
 
20
  contact: plee-the-bear@gamned.org
 
21
 
 
22
  Please add the tag [PTB] in the subject of your mails.
 
23
*/
 
24
/**
 
25
 * \file frog.cpp
 
26
 * \brief Implementation of the ptb::frog class.
 
27
 * \author S�bastien Angibaud
 
28
 */
 
29
#include "ptb/item/forest/frog.hpp"
 
30
 
 
31
#include "ptb/player.hpp"
 
32
#include "ptb/item/floating_score.hpp"
 
33
#include "engine/layer/layer.hpp"
 
34
#include "engine/world.hpp"
 
35
#include "engine/export.hpp"
 
36
 
 
37
#include <claw/assert.hpp>
 
38
 
 
39
BASE_ITEM_EXPORT( frog, ptb )
 
40
 
 
41
 
 
42
/*----------------------------------------------------------------------------*/
 
43
/**
 
44
 * \brief Constructor.
 
45
 */
 
46
ptb::frog::frog()
 
47
  : m_progress(NULL), m_max_distance(100), m_last_player_index(1)
 
48
{
 
49
  set_z_fixed(false);
 
50
  set_mass(1);
 
51
  set_density(2);
 
52
  set_friction(0.9);
 
53
 
 
54
  set_can_move_items(true);
 
55
  set_system_angle_as_visual_angle(true);
 
56
 
 
57
  get_rendering_attributes().mirror(false);
 
58
} // frog::frog()
 
59
 
 
60
/*----------------------------------------------------------------------------*/
 
61
/**
 
62
 * \brief Destructor.
 
63
 */
 
64
ptb::frog::~frog()
 
65
{
 
66
} // frog::~frog()
 
67
 
 
68
/*----------------------------------------------------------------------------*/
 
69
/**
 
70
 * \brief Load the media required by this class.
 
71
 */
 
72
void ptb::frog::pre_cache()
 
73
{
 
74
  get_level_globals().load_model("model/forest/frog.cm");
 
75
} // frog::pre_cache()
 
76
 
 
77
/*----------------------------------------------------------------------------*/
 
78
/**
 
79
 * \brief Initialise the item.
 
80
 */
 
81
void ptb::frog::build()
 
82
{
 
83
  super::build();
 
84
 
 
85
  set_model_actor( get_level_globals().get_model("model/forest/frog.cm") );
 
86
  m_initial_position = get_center_of_mass();
 
87
 
 
88
  choose_idle_action();
 
89
  m_progress = &frog::progress_idle;
 
90
} // frog::build()
 
91
 
 
92
/*---------------------------------------------------------------------------*/
 
93
/**
 
94
 * \brief Do one iteration in the progression of the item.
 
95
 * \param elapsed_time Elapsed time since the last call.
 
96
 */
 
97
void ptb::frog::progress( bear::universe::time_type elapsed_time )
 
98
{
 
99
  super::progress( elapsed_time );
 
100
 
 
101
  test_explose();
 
102
 
 
103
  if ( m_progress != NULL )
 
104
    (this->*m_progress)(elapsed_time);
 
105
} // frog::progress()
 
106
 
 
107
/*----------------------------------------------------------------------------*/
 
108
/**
 
109
 * \brief Set a field of type <real>.
 
110
 * \param name The name of the field.
 
111
 * \param value The new value of the field.
 
112
 * \return false if the field "name" is unknow, true otherwise.
 
113
 */
 
114
bool ptb::frog::set_real_field
 
115
( const std::string& name, double value )
 
116
{
 
117
  bool ok = true;
 
118
 
 
119
  if (name == "frog.max_distance")
 
120
    m_max_distance = value;
 
121
  else
 
122
    ok = super::set_real_field(name,value);
 
123
 
 
124
  return ok;
 
125
} // frog::set_real_field()
 
126
 
 
127
/*----------------------------------------------------------------------------*/
 
128
/**
 
129
 * \brief Call a function.
 
130
 * \param name The function to call.
 
131
 */
 
132
void ptb::frog::execute_function( const std::string& name )
 
133
{
 
134
  if ( name == "start_idle" )
 
135
    start_idle();
 
136
  else if ( name == "start_jump" )
 
137
    start_jump();
 
138
  else if ( name == "start_fall" )
 
139
    start_fall();
 
140
  else if ( name == "start_explose" )
 
141
    start_explose();
 
142
  else if ( name == "try_to_jump" )
 
143
    try_to_jump();
 
144
  else
 
145
    super::execute_function(name);
 
146
} // frog::execute_function()
 
147
 
 
148
/*----------------------------------------------------------------------------*/
 
149
/**
 
150
 * \brief Call collision_as_train().
 
151
 * \param that The other item of the collision.
 
152
 * \param info Some informations about the collision.
 
153
 */
 
154
void ptb::frog::collision
 
155
( bear::engine::base_item& that, bear::universe::collision_info& info )
 
156
{
 
157
  if ( info.get_collision_side() == bear::universe::zone::top_zone )
 
158
    {
 
159
      if ( default_collision(info) )
 
160
        {
 
161
          player* p = dynamic_cast<player*>(&that);
 
162
 
 
163
          if ( p != NULL )
 
164
            m_last_player_index = p->get_index();
 
165
        }
 
166
    }
 
167
  else
 
168
    {
 
169
      if ( ( get_current_action_name() == "fall" ) ||
 
170
           ( get_current_action_name() == "jump" ) )
 
171
        {
 
172
          player* p = dynamic_cast<player*>(&that);
 
173
 
 
174
          if ( p != NULL )
 
175
            that.set_contact_friction( that.get_contact_friction()*0.8);
 
176
        }
 
177
    }
 
178
}// frog::collision_as_train()
 
179
 
 
180
/*----------------------------------------------------------------------------*/
 
181
/**
 
182
 * \brief Progress in the state idle.
 
183
 */
 
184
void ptb::frog::progress_idle( bear::universe::time_type elapsed_time )
 
185
{
 
186
  if ( !test_in_sky() )
 
187
    ; // do nothing
 
188
} // frog::progress_idle()
 
189
 
 
190
/*----------------------------------------------------------------------------*/
 
191
/**
 
192
 * \brief Progress in the state jump.
 
193
 */
 
194
void ptb::frog::progress_jump( bear::universe::time_type elapsed_time )
 
195
{
 
196
  if ( !test_bottom_contact() )
 
197
    if( get_speed().y <= 0 )
 
198
      start_model_action("fall");
 
199
} // frog::progress_jump()
 
200
 
 
201
 
 
202
/*----------------------------------------------------------------------------*/
 
203
/**
 
204
 * \brief Progress in the state fall.
 
205
 */
 
206
void ptb::frog::progress_fall( bear::universe::time_type elapsed_time )
 
207
{
 
208
  if ( !test_bottom_contact() )
 
209
    if( get_speed().y >= 0 )
 
210
      start_model_action("jump");
 
211
} // frog::progress_fall()
 
212
 
 
213
/*----------------------------------------------------------------------------*/
 
214
/**
 
215
 * \brief Progress in the state explose.
 
216
 */
 
217
void ptb::frog::progress_explose( bear::universe::time_type elapsed_time )
 
218
{
 
219
  // do nothing
 
220
} // frog::progress_explose()
 
221
 
 
222
/*----------------------------------------------------------------------------*/
 
223
/**
 
224
 * \brief Start idle state.
 
225
 */
 
226
void ptb::frog::start_idle()
 
227
{
 
228
  m_progress = &frog::progress_idle;
 
229
} // frog::start_idle()
 
230
 
 
231
/*----------------------------------------------------------------------------*/
 
232
/**
 
233
 * \brief Start to jump.
 
234
 */
 
235
void ptb::frog::start_jump()
 
236
{
 
237
  m_progress = &frog::progress_jump;
 
238
} // frog::start_jump()
 
239
 
 
240
/*----------------------------------------------------------------------------*/
 
241
/**
 
242
 * \brief Start to fall.
 
243
 */
 
244
void ptb::frog::start_fall()
 
245
{
 
246
  m_progress = &frog::progress_fall;
 
247
} // frog::start_fall()
 
248
 
 
249
/*----------------------------------------------------------------------------*/
 
250
/**
 
251
 * \brief Start to explose.
 
252
 */
 
253
void ptb::frog::start_explose()
 
254
{
 
255
  set_can_move_items(false);
 
256
  m_progress = &frog::progress_explose;
 
257
  create_floating_score();
 
258
} // frog::start_explose()
 
259
 
 
260
 
 
261
/*----------------------------------------------------------------------------*/
 
262
/**
 
263
 * \brief Apply a jump.
 
264
 */
 
265
void ptb::frog::apply_jump()
 
266
{
 
267
  if ( get_rendering_attributes().is_mirrored() )
 
268
    add_internal_force( bear::universe::force_type(-40000, 50000) );
 
269
  else
 
270
    add_internal_force( bear::universe::force_type(40000, 50000) );
 
271
 
 
272
  start_model_action("jump");
 
273
} // frog::apply_jump()
 
274
 
 
275
/*----------------------------------------------------------------------------*/
 
276
/**
 
277
 * \brief Choose an idle action.
 
278
 */
 
279
void ptb::frog::choose_idle_action()
 
280
{
 
281
  std::ostringstream s;
 
282
  s << "idle_" << (rand() % 2 + 1);
 
283
  start_model_action(s.str());
 
284
} // frog::choose_idle_action()
 
285
 
 
286
/*----------------------------------------------------------------------------*/
 
287
/**
 
288
 * \brief Scan if there is a player in a given direction.
 
289
 * \param origin The position from which we start watching.
 
290
 * \param dir The direction of the scan.
 
291
 */
 
292
bool ptb::frog::scan_no_wall_in_direction
 
293
( const bear::universe::position_type& origin,
 
294
  const bear::universe::vector_type& dir ) const
 
295
{
 
296
  if ( get_layer().has_world() )
 
297
    {
 
298
      bear::universe::item_picking_filter filter;
 
299
      filter.set_can_move_items_value(true);
 
300
      filter.set_forbidden_position(origin);
 
301
 
 
302
      return get_layer().get_world().pick_item_in_direction
 
303
        (origin, dir, filter) == NULL;
 
304
    }
 
305
  else
 
306
    return false;
 
307
} // frog::scan_no_wall_in_direction()
 
308
 
 
309
/*---------------------------------------------------------------------------*/
 
310
/**
 
311
 * \brief Testeif the frog is in the sky and change state thereof.
 
312
 *         Return true if the frog is in the sky.
 
313
 */
 
314
bool ptb::frog::test_in_sky()
 
315
{
 
316
  bool result = false;
 
317
 
 
318
  if ( !has_bottom_contact() )
 
319
    {
 
320
      result = true;
 
321
 
 
322
      if( get_speed().y <= 0 )
 
323
        start_model_action("fall");
 
324
      else
 
325
        start_model_action("jump");
 
326
    }
 
327
 
 
328
  return result;
 
329
} // frog::test_in_sky()
 
330
 
 
331
/*---------------------------------------------------------------------------*/
 
332
/**
 
333
 * \brief Test if the frog has bottom contact and change state thereof.
 
334
 *         Return true if the frog is now in idle state.
 
335
 */
 
336
bool ptb::frog::test_bottom_contact()
 
337
{
 
338
  bool result = false;
 
339
 
 
340
  if ( has_bottom_contact() )
 
341
    {
 
342
      choose_idle_action();
 
343
      result = true;
 
344
    }
 
345
 
 
346
  return result;
 
347
} // frog::test_bottom_contact()
 
348
 
 
349
/*---------------------------------------------------------------------------*/
 
350
/**
 
351
 * \brief Test if the frog must explose.
 
352
 */
 
353
void ptb::frog::test_explose()
 
354
{
 
355
  if ( has_bottom_contact() && has_top_contact() )
 
356
    start_model_action("explose");
 
357
} // frog::test_explose()
 
358
 
 
359
/*----------------------------------------------------------------------------*/
 
360
/**
 
361
 * \brief The frog try to jump.
 
362
 *
 
363
 */
 
364
void ptb::frog::try_to_jump()
 
365
{
 
366
  if ( has_bottom_contact() )
 
367
    {
 
368
 
 
369
      if ( (rand() % 2) == 1)
 
370
        get_rendering_attributes().mirror
 
371
          (!get_rendering_attributes().is_mirrored());
 
372
 
 
373
      if ( can_jump() )
 
374
        apply_jump();
 
375
      else
 
376
        {
 
377
          get_rendering_attributes().mirror
 
378
            (!get_rendering_attributes().is_mirrored());
 
379
 
 
380
          if ( can_jump() )
 
381
            apply_jump();
 
382
          else
 
383
            get_rendering_attributes().mirror
 
384
              (!get_rendering_attributes().is_mirrored());
 
385
        }
 
386
    }
 
387
} // frog::try_to_jump()
 
388
 
 
389
/*----------------------------------------------------------------------------*/
 
390
/**
 
391
 * \brief Test if the frog can jump.
 
392
 *
 
393
 */
 
394
bool ptb::frog::can_jump() const
 
395
{
 
396
  bool result = true;
 
397
 
 
398
  bear::universe::coordinate_type dist;
 
399
 
 
400
  dist = m_initial_position.distance(get_center_of_mass());
 
401
  if ( dist > m_max_distance )
 
402
    {
 
403
      if ( get_rendering_attributes().is_mirrored() &&
 
404
           (m_initial_position.x > get_center_of_mass().x) )
 
405
        result = false;
 
406
 
 
407
      if ( !get_rendering_attributes().is_mirrored() &&
 
408
           (m_initial_position.x < get_center_of_mass().x) )
 
409
        result = false;
 
410
    }
 
411
 
 
412
  if ( result )
 
413
    {
 
414
      bear::universe::vector_type dir(150, 0);
 
415
 
 
416
      if ( get_rendering_attributes().is_mirrored() )
 
417
        dir.x *= -1;
 
418
 
 
419
      result = scan_no_wall_in_direction(get_center_of_mass(), dir);
 
420
    }
 
421
 
 
422
  return result;
 
423
} // frog::can_jump()
 
424
 
 
425
/*----------------------------------------------------------------------------*/
 
426
/**
 
427
 * \brief Create a floating score.
 
428
 */
 
429
void ptb::frog::create_floating_score() const
 
430
{
 
431
  floating_score* s = new floating_score;
 
432
 
 
433
  s->set_z_position( super::get_z_position() + 10 );
 
434
  s->set_center_of_mass( super::get_center_of_mass() );
 
435
  new_item(*s);
 
436
 
 
437
  s->add_points( m_last_player_index, 1 );
 
438
} // rabbit::create_floating_score()