~ubuntu-branches/ubuntu/precise/supertuxkart/precise

« back to all changes in this revision

Viewing changes to src/tracks/track_object.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Egger
  • Date: 2011-02-24 22:36:25 UTC
  • mfrom: (1.1.9 upstream) (6.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20110224223625-ygrjfpg92obovuch
Tags: 0.7+dfsg1-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  $Id: track_object.cpp 4308 2009-12-17 00:22:29Z hikerstk $
 
2
//
 
3
//  SuperTuxKart - a fun racing game with go-kart
 
4
//  Copyright (C) 2009 Joerg Henrichs
 
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 3
 
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 "tracks/track_object.hpp"
 
21
 
 
22
#include "graphics/irr_driver.hpp"
 
23
#include "io/file_manager.hpp"
 
24
#include "io/xml_node.hpp"
 
25
#include "modes/world.hpp"
 
26
#include "tracks/track.hpp"
 
27
 
 
28
/** A track object: any additional object on the track. This object implements
 
29
 *  a graphics-only representation, i.e. there is no physical representation.
 
30
 *  Derived classes can implement a physical representation (see 
 
31
 *  physics/physical_object) or animations.
 
32
 * \param xml_node The xml node from which the initial data is taken. This is
 
33
 *                 for now: initial position, initial rotation, name of the
 
34
 *                 model, enable/disable status, timer information.
 
35
 */
 
36
TrackObject::TrackObject(const XMLNode &xml_node)
 
37
{
 
38
 
 
39
    std::string model_name;
 
40
    xml_node.get("model", &model_name);
 
41
    std::string full_path = World::getWorld()->getTrack()->getTrackFile(model_name);
 
42
    m_animated_mesh = irr_driver->getAnimatedMesh(full_path);
 
43
    if(!m_animated_mesh)
 
44
    {
 
45
        // If the model isn't found in the track directory, look 
 
46
        // in STK's model directory.
 
47
        full_path = file_manager->getModelFile(model_name);
 
48
        m_animated_mesh = irr_driver->getAnimatedMesh(full_path);
 
49
        if(!m_animated_mesh)
 
50
        {
 
51
            fprintf(stderr, "Warning: '%s' in '%s' not found and is ignored.\n",
 
52
                    xml_node.getName().c_str(), model_name.c_str());
 
53
            return;
 
54
        }   // if(!m_animated_mesh)
 
55
    }
 
56
    m_animated_node = irr_driver->addAnimatedMesh(m_animated_mesh);
 
57
#ifdef DEBUG
 
58
    std::string debug_name = model_name+" (track-object)";
 
59
    m_animated_node->setName(debug_name.c_str());
 
60
#endif
 
61
 
 
62
    // Get the information from the xml node.
 
63
    m_enabled = true;
 
64
    xml_node.get("enabled", &m_enabled);
 
65
 
 
66
    m_is_looped = false;
 
67
    xml_node.get("looped", &m_is_looped);
 
68
 
 
69
    m_frame_start = m_animated_node->getStartFrame();
 
70
    xml_node.get("frame-start", &m_frame_start);
 
71
 
 
72
    m_frame_end = m_animated_node->getEndFrame();
 
73
    xml_node.get("frame-end", &m_frame_end);
 
74
 
 
75
    if(!m_enabled)
 
76
        m_animated_node->setVisible(false);
 
77
 
 
78
    m_init_xyz   = core::vector3df(0,0,0);
 
79
    int result   = xml_node.get("xyz", &m_init_xyz);
 
80
    m_init_hpr   = core::vector3df(0,0,0);
 
81
    result       = xml_node.get("hpr", &m_init_hpr);
 
82
    m_init_scale = core::vector3df(1,1,1);
 
83
    result       = xml_node.get("scale", &m_init_scale);
 
84
    if(!XMLNode::hasP(result) ||
 
85
       !XMLNode::hasR(result))   // Needs perhaps pitch and roll
 
86
    {
 
87
    }
 
88
    m_animated_node->setPosition(m_init_xyz);
 
89
    m_animated_node->setRotation(m_init_hpr);
 
90
    m_animated_node->setScale(m_init_scale);
 
91
    m_animated_node->setMaterialFlag(video::EMF_LIGHTING, false);
 
92
    reset();
 
93
}   // TrackObject
 
94
 
 
95
// ----------------------------------------------------------------------------
 
96
TrackObject::~TrackObject()
 
97
{
 
98
    irr_driver->removeNode(m_animated_node);
 
99
    irr_driver->removeMesh(m_animated_mesh);
 
100
}   // ~TrackObject
 
101
 
 
102
// ----------------------------------------------------------------------------
 
103
/** Initialises an object before a race starts.
 
104
 */
 
105
void TrackObject::reset()
 
106
{
 
107
    m_animated_node->setPosition(m_init_xyz);
 
108
    m_animated_node->setRotation(m_init_hpr);
 
109
    m_animated_node->setScale(m_init_scale);
 
110
    m_animated_node->setLoopMode(m_is_looped);
 
111
 
 
112
    if(m_is_looped)
 
113
    {
 
114
        m_animated_node->setFrameLoop(m_frame_start, m_frame_end);
 
115
    }
 
116
}   // reset
 
117
// ----------------------------------------------------------------------------
 
118
/** Enables or disables this object. This affects the visibility, i.e. 
 
119
 *  disabled objects will not be displayed anymore.
 
120
 *  \param mode Enable (true) or disable (false) this object.
 
121
 */
 
122
void TrackObject::setEnable(bool mode)
 
123
{
 
124
    m_enabled = mode;
 
125
    m_animated_node->setVisible(m_enabled);
 
126
}   // setEnable
 
127
// ----------------------------------------------------------------------------
 
128
/** This function is called from irrlicht when a (non-looped) animation ends.
 
129
 */
 
130
void TrackObject::OnAnimationEnd(scene::IAnimatedMeshSceneNode* node)
 
131
{
 
132
}   // OnAnimationEnd
 
133
 
 
134
// ----------------------------------------------------------------------------
 
135
void TrackObject::update(float dt)
 
136
{
 
137
}   // update
 
138
// ----------------------------------------------------------------------------