Split the item update into update and updateGraphics() parts.

This commit is contained in:
hiker
2018-05-04 16:41:03 +10:00
parent c7c061c7c1
commit eb647124e8
5 changed files with 44 additions and 29 deletions

View File

@@ -303,6 +303,7 @@ Item::~Item()
*/
void Item::reset()
{
m_was_available_previously = true;
ItemState::reset();
if (m_node != NULL)
{
@@ -323,51 +324,45 @@ void Item::setParent(AbstractKart* parent)
ItemState::setDeactivatedTicks(stk_config->time2Ticks(1.5f));
} // setParent
//-----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
/** Updated the item - rotates it, takes care of items coming back into
* the game after it has been collected.
* \param ticks Number of physics time steps - should be 1.
*/
void Item::update(int ticks)
void Item::updateGraphics(float dt)
{
bool was_collected = !isAvailable();
ItemState::update(ticks);
if(!m_node) return;
if (was_collected && isAvailable() && m_node)
if (!m_was_available_previously && isAvailable() )
{
// This item is now available again - make sure it is not
// scaled anymore.
m_node->setScale(core::vector3df(1, 1, 1));
}
if (!isAvailable() && m_node &&
getTicksTillReturn() <= stk_config->time2Ticks(1.0f) )
float time_till_return = stk_config->ticks2Time(getTicksTillReturn());
if (!isAvailable() && time_till_return <= 1.0f)
{
// Make it visible by scaling it from 0 to 1:
m_node->setVisible(true);
float t = stk_config->ticks2Time(getTicksTillReturn());
m_node->setScale(core::vector3df(1, 1, 1)*(1 - t));
m_node->setScale(core::vector3df(1, 1, 1)*(1 - time_till_return));
}
if(isAvailable())
{
if(!m_rotate || m_node == NULL) return;
if (isAvailable() && m_rotate)
{
// have it rotate
if (!RewindManager::get()->isRewinding())
{
float dt = stk_config->ticks2Time(ticks);
m_rotation_angle += dt * M_PI;
}
m_rotation_angle += dt * M_PI;
if (m_rotation_angle > M_PI * 2) m_rotation_angle -= M_PI * 2;
btMatrix3x3 m;
m.setRotation(m_original_rotation);
btQuaternion r = btQuaternion(m.getColumn(1), m_rotation_angle) *
m_original_rotation;
m_original_rotation;
Vec3 hpr;
hpr.setHPR(r);
m_node->setRotation(hpr.toIrrHPR());
return;
} // if item is available
m_was_available_previously = isAvailable();
} // update
//-----------------------------------------------------------------------------

View File

@@ -254,10 +254,10 @@ private:
* (bubble gums don't rotate, but it will be replaced with
* a nitro which rotates, and so overwrites the original
* rotation). */
btQuaternion m_original_rotation;
btQuaternion m_original_rotation;
/** Used when rotating the item */
float m_rotation_angle;
float m_rotation_angle;
/** Scene node of this item. */
LODNode *m_node;
@@ -268,31 +268,34 @@ private:
/** The original position - saves calls to m_node->getPosition()
* and then converting this value to a Vec3. */
Vec3 m_xyz;
Vec3 m_xyz;
/** Set to false if item should not rotate. */
bool m_rotate;
bool m_rotate;
/** Stores if the item was available in the previously rendered frame. */
bool m_was_available_previously;
/** Optionally set this if this item was laid by a particular kart. in
* this case the 'm_deactive_ticks' will also be set - see below. */
const AbstractKart *m_event_handler;
const AbstractKart *m_event_handler;
/** Kart that emitted this item if any */
const AbstractKart *m_emitter;
const AbstractKart *m_emitter;
/** callback used if type == ITEM_TRIGGER */
TriggerItemListener* m_listener;
/** square distance at which item is collected */
float m_distance_2;
float m_distance_2;
/** The graph node this item is on. */
int m_graph_node;
int m_graph_node;
/** Distance from the center of the quad this item is in. This value is
* >0 if it is to the right of the center, and undefined if this quad
* is not on any quad. */
float m_distance_from_center;
float m_distance_from_center;
/** The closest point to the left and right of this item at which it
* would not be collected. Used by the AI to avoid items. */
@@ -308,7 +311,7 @@ public:
Item(const Vec3& xyz, float distance,
TriggerItemListener* trigger);
virtual ~Item ();
void update(int ticks);
void updateGraphics(float dt);
virtual void collected(const AbstractKart *kart, int ticks);
void setParent(AbstractKart* parent);
void reset();

View File

@@ -431,6 +431,20 @@ void ItemManager::update(int ticks)
} // for m_all_items
} // update
//-----------------------------------------------------------------------------
/** Updates the graphics, called once per rendered frame.
* \param dt Time based on frame rate.
*/
void ItemManager::updateGraphics(float dt)
{
for (AllItemTypes::iterator i = m_all_items.begin();
i != m_all_items.end(); i++)
{
if (*i) (*i)->updateGraphics(dt);
} // for m_all_items
} // updateGraphics
//-----------------------------------------------------------------------------
/** Removes an items from the items-in-quad list, from the list of all
* items, and then frees the item itself.

View File

@@ -107,6 +107,7 @@ public:
Item* newItem (const Vec3& xyz, float distance,
TriggerItemListener* listener);
void update (int ticks);
void updateGraphics (float dt);
void checkItemHit (AbstractKart* kart);
void reset ();
void collectedItem (Item *item, AbstractKart *kart,

View File

@@ -1533,6 +1533,8 @@ void Track::updateGraphics(float dt)
{
m_animated_textures[i]->update(dt);
}
ItemManager::get()->updateGraphics(dt);
} // updateGraphics
// ----------------------------------------------------------------------------