JNR
laStateObject.cpp
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
30 //
31 // FILE: laStateObject.cpp
32 //
33 // Copyright (C) 2007-2013 Atanas Laskov, <latanas@gmail.com>
34 //
35 #include "stdafx.h"
36 #include "Core-Level-JR.h"
37 
38 laStateObject::laStateObject(void)
39 {
40  //_bExplicidDirection = M_FALSE;
41  _nState = 0;
42 }
43 
44 laStateObject::~laStateObject(void) {
45 }
46 
47 void laStateObject::load(class laFileParser *fp)
48 {
49  ERRORLEVEL_BEGIN;
50  laObject::load(fp);
51 
52  laStaticModel* pm = getObject()->getModel(_nState)->framePtr();
53  _animBlend.blend(pm, pm);
54 
55  ERRORLEVEL_END;
56 }
57 
58 void laStateObject::create()
59 {
60  ERRORLEVEL_BEGIN;
61 
62  laStaticModel* pm = getObject()->getModel(_nState)->framePtr();
63  _animBlend.blend(pm, pm);
64 
65  ERRORLEVEL_END;
66 }
67 
68 // Respawn
69 //
70 void laStateObject::respawn() {
71  laObject::respawn();
72 
73  _nState = 0;
74  //_nOldState = 0;
75 }
76 
77 // Switch to a different state
78 //
79 void laStateObject::state(unsigned nState, double dBlendTime) {
80  ASSERT( nState < getObject()->getModelCnt(), "Requested state %d not implemented in TS", nState);
81 
82  // Get a snapshot of the current frame and make a new blending animation
83  //
84  getStateModel()->snapshot(&_snapshot);
85  _animBlend.blend( &_snapshot, getObject()->getModel(nState)->framePtr(), dBlendTime);
86 
87  _nState = nState;
88  _dBlendTime = dBlendTime;
89 }
90 
91 laAnimatedModel* laStateObject::getStateModel() {
92  return ( _animBlend.frameIndex() < 1 ) ? &_animBlend : getObject()->getModel(_nState);
93  //return getObject()->getModel(_nState);
94 }
95 
96 // Draw geometry
97 //
98 // NOTE: This method is different form laObject::_draw_positioned_rotated() in that
99 // it draws only the 3D model corresponding to the active state, rather than all of them
100 // Do not call laObject::_draw_positioned_rotated() here, it will draw all states.
101 //
102 void laStateObject::_draw_positioned_rotated(laRenderer *r) {
103  getStateModel()->draw( r );
104 }
105 
106 // Draw FX
107 // TODO: Switch fx to state fx only?
108 //
109 void laStateObject::drawFx(laRenderer *r, laPoint3 ptBasePos) {
110  laObject::drawFx(r, ptBasePos);
111 }
112 
113 // This function determines state switches;
114 // Can be overloaded by laStateObject derivatives
115 //
116 unsigned laStateObject::_next_state(unsigned nCurrentState ) {
117  return nCurrentState;
118 }
119 
120 // Animate
121 //
122 void laStateObject::animate(laTimer &t)
123 {
124  ERRORLEVEL_BEGIN;
125 
126  unsigned nNewState = this->_next_state(_nState); //< derived objects can overload this
127 
128  // Detect changes of state
129  //
130  if( nNewState != _nState )
131  {
132  // Get a snapshot of the current frame and make a new blending animation
133  //
134  getStateModel()->snapshot(&_snapshot);
135  _animBlend.blend( &_snapshot, getObject()->getModel(nNewState)->framePtr(), _dBlendTime);
136 
137  _nState = nNewState;
138  //fxMessages.add(1, laColor(), "%d -> %d", _nOldState, _nState);
139  }
140 
141  // Blend between the two states by useing the liner interpulation implemented in
142  // laAnimated model
143  //
144  _animBlend.animate(t);
145 
146  // Default
147  //
148  laObject::animate(t);
149  ERRORLEVEL_END;
150 }
Animated 3D Model.
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98
Static 3D Model.
Definition: laStaticModel.h:44
File Parser.
Definition: laFileParser.h:41