Moved the kart control events from the kart control class instead

of the kart rewinder.
This commit is contained in:
hiker
2016-08-17 08:08:42 +10:00
parent 7c899c943c
commit e56d324738
6 changed files with 176 additions and 61 deletions

View File

@@ -1,5 +1,5 @@
# Modify this file to change the last-modified date when you add/remove a file.
# This will then trigger a new cmake run automatically.
# This will then trigger a new cmake run automatically.
file(GLOB_RECURSE STK_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.hpp")
file(GLOB_RECURSE STK_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.cpp")
file(GLOB_RECURSE STK_SHADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "data/shaders/*")

View File

@@ -0,0 +1,160 @@
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2008-2016 Joerg Henrichs
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "karts/controller/kart_control.hpp"
#include "network/rewind_manager.hpp"
// ------------------------------------------------------------------------
/** Called when going back in time during a rewind. Nothing to do here
* in this case.
*/
void KartControl::undo(BareNetworkString *buffer)
{
} // undo
// ------------------------------------------------------------------------
/** Replay an event for a KartControl object from the buffer.
* \param buffer BareNetworkString with saved event into.
*/
void KartControl::rewind(BareNetworkString *buffer)
{
if(buffer->getTotalSize()>1)
{
// Full state including accel and steering was saved
setFromBuffer(buffer);
}
else // only a button event was stored
{
setButtonsCompressed(buffer->getUInt8());
}
} // rewind
// ------------------------------------------------------------------------
/** Sets the current steering value. */
void KartControl::setSteer(float f)
{
RewindManager *re = RewindManager::get();
if (re->isEnabled() && !re->isRewinding() && m_steer != f)
{
// Save full status
BareNetworkString *buffer = new BareNetworkString(getLength());
copyToBuffer(buffer);
RewindManager::get()->addEvent(this, buffer);
}
m_steer = f;
} // setSteer
// ----------------------------------------------------------------------------
/** Sets the acceleration. */
void KartControl::setAccel(float f)
{
RewindManager *re = RewindManager::get();
if (re->isEnabled() && !re->isRewinding() && m_accel != f)
{
BareNetworkString *buffer = new BareNetworkString(getLength());
copyToBuffer(buffer);
RewindManager::get()->addEvent(this, buffer);
}
m_accel = f;
} // setAccel
// ----------------------------------------------------------------------------
/** Sets if the kart is braking. */
void KartControl::setBrake(bool b)
{
RewindManager *re = RewindManager::get();
if (re->isEnabled() && !re->isRewinding() && m_brake != b)
{
// Only store the buttons in this case
BareNetworkString *buffer = new BareNetworkString(1);
buffer->addUInt8(getButtonsCompressed());
RewindManager::get()->addEvent(this, buffer);
}
m_brake = b;
} // setBrake
// ----------------------------------------------------------------------------
/** Sets if the kart activates nitro. */
void KartControl::setNitro(bool b)
{
RewindManager *re = RewindManager::get();
if (re->isEnabled() && !re->isRewinding() && m_nitro != b)
{
BareNetworkString *buffer = new BareNetworkString(1);
buffer->addUInt8(getButtonsCompressed());
RewindManager::get()->addEvent(this, buffer);
}
m_nitro = b;
} // setNitro
// ----------------------------------------------------------------------------
/** Sets the skid control for this kart. */
void KartControl::setSkidControl(SkidControl sc)
{
RewindManager *re = RewindManager::get();
if (re->isEnabled() && !re->isRewinding() && m_skid != sc)
{
BareNetworkString *buffer = new BareNetworkString(1);
buffer->addUInt8(getButtonsCompressed());
RewindManager::get()->addEvent(this, buffer);
}
m_skid = sc;
} // seSkidControl
// ----------------------------------------------------------------------------
/** Returns if this kart wants to get rescued. */
void KartControl::setRescue(bool b)
{
RewindManager *re = RewindManager::get();
if (re->isEnabled() && !re->isRewinding() && m_rescue != b)
{
BareNetworkString *buffer = new BareNetworkString(1);
buffer->addUInt8(getButtonsCompressed());
RewindManager::get()->addEvent(this, buffer);
}
m_rescue = b;
} // setRescue
// ----------------------------------------------------------------------------
/** Sets if the kart wants to fire. */
void KartControl::setFire(bool b)
{
RewindManager *re = RewindManager::get();
if (re->isEnabled() && !re->isRewinding() && m_fire != b)
{
BareNetworkString *buffer = new BareNetworkString(1);
buffer->addUInt8(getButtonsCompressed());
RewindManager::get()->addEvent(this, buffer);
}
m_fire = b;
} // setFire
// ----------------------------------------------------------------------------
/** Sets if the kart wants to look (and therefore also fires) backwards. */
void KartControl::setLookBack(bool b)
{
RewindManager *re = RewindManager::get();
if (re->isEnabled() && !re->isRewinding() && m_look_back != b)
{
BareNetworkString *buffer = new BareNetworkString(1);
buffer->addUInt8(getButtonsCompressed());
RewindManager::get()->addEvent(this, buffer);
}
m_look_back = b;
} // setLookBack

View File

@@ -20,13 +20,12 @@
#define HEADER_KART_CONTROL_HPP
#include "network/network_string.hpp"
#include <string.h>
#include "network/rewind_info.hpp"
/**
* \ingroup controller
*/
class KartControl
class KartControl : public EventRewinder
{
public:
/** The skidding control state: SC_NONE: not pressed;
@@ -52,7 +51,18 @@ private:
/** True if the kart looks (and shoots) backwards. */
bool m_look_back;
public:
virtual void undo(BareNetworkString *buffer);
virtual void rewind(BareNetworkString *buffer);
void setSteer(float f);
void setAccel(float f);
void setBrake(bool b);
void setNitro(bool b);
void setSkidControl(SkidControl sc);
void setRescue(bool b);
void setFire(bool b);
void setLookBack(bool b);
// ------------------------------------------------------------------------
KartControl()
{
reset();
@@ -134,54 +144,29 @@ public:
/** Returns the current steering value. */
float getSteer() const { return m_steer; }
// ------------------------------------------------------------------------
/** Sets the current steering value. */
void setSteer(float f) { m_steer = f; }
// ------------------------------------------------------------------------
/** Returns current acceleration. */
float getAccel() const { return m_accel; }
// ------------------------------------------------------------------------
/** Sets the acceleration. */
void setAccel(float f) { m_accel = f; }
// ------------------------------------------------------------------------
/** Returns if the kart is braking. */
bool getBrake() const { return m_brake; }
// ------------------------------------------------------------------------
/** Sets if the kart is braking. */
void setBrake(bool b) { m_brake = b; }
// ------------------------------------------------------------------------
/** Returns if the kart activates nitro. */
bool getNitro() const { return m_nitro; }
// ------------------------------------------------------------------------
/** Sets if the kart activates nitro. */
void setNitro(bool b) { m_nitro = b; }
// ------------------------------------------------------------------------
/** Returns the skidding control state: SC_NONE: not pressed;
* SC_NO_DIRECTION: pressed, but no steering;
* SC_LEFT/RIGHT: pressed in the specified direction. */
SkidControl getSkidControl() const { return m_skid; }
// ------------------------------------------------------------------------
/** Sets the skid control for this kart. */
void setSkidControl(SkidControl sc) { m_skid = sc; }
// ------------------------------------------------------------------------
/** Returns true if the kart triggered rescue. */
bool getRescue() const { return m_rescue; }
// ------------------------------------------------------------------------
/** Returns if this kart wants to get rescued. */
void setRescue(bool b) { m_rescue = b; }
// ------------------------------------------------------------------------
/** Returns if fire is selected. */
bool getFire() const { return m_fire; }
// ------------------------------------------------------------------------
/** Sets if the kart wants to fire. */
void setFire(bool b) { m_fire = b; }
// ------------------------------------------------------------------------
/** Returns if the kart wants to look back (which also implies that it
* will fire backwards. */
bool getLookBack() const { return m_look_back; }
// ------------------------------------------------------------------------
/** Sets if the kart wants to look (and therefore also fires) backwards. */
void setLookBack(bool b) { m_look_back = b; }
};
#endif

View File

@@ -37,7 +37,6 @@ KartRewinder::KartRewinder(AbstractKart *kart) : Rewinder(/*can_be_destroyed*/ f
*/
void KartRewinder::reset()
{
m_previous_control = m_kart->getControls();
} // reset
// ----------------------------------------------------------------------------
@@ -114,35 +113,11 @@ void KartRewinder::rewindToState(BareNetworkString *buffer)
*/
void KartRewinder::update()
{
// Don't store events from a rewind
if(RewindManager::get()->isRewinding()) return;
// Check if an event has happened that needs to be recorded
bool control_event = !(m_kart->getControls() == m_previous_control);
uint8_t type = (control_event ? EVENT_CONTROL : 0);
if(type == 0)
return; // no event
// Likely it is only a single event, so limit initial memory allocation
BareNetworkString *buffer =
new BareNetworkString(m_previous_control.getLength());
buffer->addUInt8(type);
if(control_event)
{
m_previous_control = m_kart->getControls();
m_previous_control.copyToBuffer(buffer);
}
// The rewind manager will free the memory once it's not needed anymore
//XXXXXXXXXXXXXXX RewindManager::get()->addEvent(this, buffer);
} // update
// ----------------------------------------------------------------------------
void KartRewinder::rewindToEvent(BareNetworkString *buffer)
{
buffer->reset();
uint8_t type = buffer->getUInt8();
if(type & EVENT_CONTROL)
m_kart->getControls().setFromBuffer(buffer);
}; // rewindToEvent

View File

@@ -19,8 +19,6 @@
#ifndef HEADER_KART_REWINDER_HPP
#define HEADER_KART_REWINDER_HPP
#include "karts/controller/kart_control.hpp"
#include "network/rewinder.hpp"
#include "utils/cpp2011.hpp"
@@ -33,10 +31,6 @@ private:
/** Pointer to the original kart object. */
AbstractKart *m_kart;
/** The previous kart controls, used to detect if a new event
* needs to be created. */
KartControl m_previous_control;
// Flags to indicate the different event types
enum { EVENT_CONTROL = 0x01,
EVENT_ATTACH = 0x02 };

View File

@@ -195,6 +195,7 @@ public:
* It calls undoEvent in the rewinder. */
virtual void undo()
{
m_buffer->reset();
m_event_rewinder->undo(m_buffer);
} // undo
// ------------------------------------------------------------------------
@@ -205,7 +206,7 @@ public:
{
// Make sure to reset the buffer so we read from the beginning
m_buffer->reset();
m_event_rewinder->rewind(getBuffer());
m_event_rewinder->rewind(m_buffer);
} // rewind
// ------------------------------------------------------------------------
/** Returns the buffer with the event information in it. */