summaryrefslogtreecommitdiffstats
path: root/src/object
diff options
context:
space:
mode:
Diffstat (limited to 'src/object')
-rw-r--r--src/object/brain.cpp6
-rw-r--r--src/object/robotmain.cpp108
-rw-r--r--src/object/robotmain.h42
3 files changed, 146 insertions, 10 deletions
diff --git a/src/object/brain.cpp b/src/object/brain.cpp
index c395c6a..953f878 100644
--- a/src/object/brain.cpp
+++ b/src/object/brain.cpp
@@ -361,9 +361,9 @@ bool CBrain::EventProcess(const Event &event)
m_buttonAxe = EVENT_NULL;
}
- axeX = event.axeX;
- axeY = event.axeY;
- axeZ = event.axeZ;
+ axeX = event.motionInput.x;
+ axeY = event.motionInput.y;
+ axeZ = event.motionInput.z;
if ( !m_main->GetTrainerPilot() &&
m_object->GetTrainer() ) // drive vehicle?
diff --git a/src/object/robotmain.cpp b/src/object/robotmain.cpp
index 9dca371..8048b5b 100644
--- a/src/object/robotmain.cpp
+++ b/src/object/robotmain.cpp
@@ -678,6 +678,9 @@ CRobotMain::CRobotMain(CInstanceManager* iMan, CApplication* app)
m_endingLostRank = 0;
m_winTerminate = false;
+ m_joystickDeadzone = 0.2f;
+ SetDefaultInputBindings();
+
FlushDisplayInfo();
m_fontSize = 9.0f;
@@ -813,8 +816,6 @@ CRobotMain::CRobotMain(CInstanceManager* iMan, CApplication* app)
InitClassFILE();
CScript::InitFonctions();
-
- SetDefaultInputBindings();
}
//! Destructor of robot application
@@ -869,6 +870,12 @@ void CRobotMain::SetDefaultInputBindings()
m_inputBindings[i].key = m_inputBindings[i].joy = KEY_INVALID;
}
+ for (int i = 0; i < JOY_AXIS_SLOT_MAX; i++)
+ {
+ m_joyAxisBindings[i].axis = AXIS_INVALID;
+ m_joyAxisBindings[i].invert = false;
+ }
+
m_inputBindings[INPUT_SLOT_LEFT ].key = KEY(LEFT);
m_inputBindings[INPUT_SLOT_RIGHT ].key = KEY(RIGHT);
m_inputBindings[INPUT_SLOT_UP ].key = KEY(UP);
@@ -897,6 +904,10 @@ void CRobotMain::SetDefaultInputBindings()
m_inputBindings[INPUT_SLOT_SPEED10].key = KEY(F4);
m_inputBindings[INPUT_SLOT_SPEED15].key = KEY(F5);
m_inputBindings[INPUT_SLOT_SPEED20].key = KEY(F6);
+
+ m_joyAxisBindings[JOY_AXIS_SLOT_X].axis = 0;
+ m_joyAxisBindings[JOY_AXIS_SLOT_Y].axis = 1;
+ m_joyAxisBindings[JOY_AXIS_SLOT_Z].axis = 2;
}
void CRobotMain::SetInputBinding(InputSlot slot, InputBinding binding)
@@ -913,6 +924,36 @@ const InputBinding& CRobotMain::GetInputBinding(InputSlot slot)
return m_inputBindings[index];
}
+void CRobotMain::SetJoyAxisBinding(JoyAxisSlot slot, JoyAxisBinding binding)
+{
+ unsigned int index = static_cast<unsigned int>(slot);
+ assert(index >= 0 && index < JOY_AXIS_SLOT_MAX);
+ m_joyAxisBindings[index] = binding;
+}
+
+const JoyAxisBinding& CRobotMain::GetJoyAxisBinding(JoyAxisSlot slot)
+{
+ unsigned int index = static_cast<unsigned int>(slot);
+ assert(index >= 0 && index < JOY_AXIS_SLOT_MAX);
+ return m_joyAxisBindings[index];
+}
+
+void CRobotMain::SetJoystickDeadzone(float zone)
+{
+ m_joystickDeadzone = zone;
+}
+
+float CRobotMain::GetJoystickDeadzone()
+{
+ return m_joystickDeadzone;
+}
+
+void CRobotMain::ResetKeyStates()
+{
+ m_keyMotion = Math::Vector(0.0f, 0.0f, 0.0f);
+ m_joyMotion = Math::Vector(0.0f, 0.0f, 0.0f);
+}
+
//! Changes phase
void CRobotMain::ChangePhase(Phase phase)
{
@@ -1159,10 +1200,69 @@ void CRobotMain::ChangePhase(Phase phase)
m_engine->LoadAllTextures();
}
-
//! Processes an event
-bool CRobotMain::EventProcess(const Event &event)
+bool CRobotMain::EventProcess(Event &event)
{
+ /* Motion vector management */
+
+ if (event.type == EVENT_KEY_DOWN)
+ {
+ if (event.key.key == GetInputBinding(INPUT_SLOT_UP ).key) m_keyMotion.y = 1.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_UP ).joy) m_keyMotion.y = 1.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_DOWN ).key) m_keyMotion.y = -1.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_DOWN ).joy) m_keyMotion.y = -1.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_LEFT ).key) m_keyMotion.x = -1.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_LEFT ).joy) m_keyMotion.x = -1.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_RIGHT).key) m_keyMotion.x = 1.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_RIGHT).joy) m_keyMotion.x = 1.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_GUP ).key) m_keyMotion.z = 1.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_GUP ).joy) m_keyMotion.z = 1.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_GDOWN).key) m_keyMotion.z = -1.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_GDOWN).joy) m_keyMotion.z = -1.0f;
+ }
+ else if (event.type == EVENT_KEY_UP)
+ {
+ if (event.key.key == GetInputBinding(INPUT_SLOT_UP ).key) m_keyMotion.y = 0.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_UP ).joy) m_keyMotion.y = 0.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_DOWN ).key) m_keyMotion.y = 0.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_DOWN ).joy) m_keyMotion.y = 0.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_LEFT ).key) m_keyMotion.x = 0.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_LEFT ).joy) m_keyMotion.x = 0.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_RIGHT).key) m_keyMotion.x = 0.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_RIGHT).joy) m_keyMotion.x = 0.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_GUP ).key) m_keyMotion.z = 0.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_GUP ).joy) m_keyMotion.z = 0.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_GDOWN).key) m_keyMotion.z = 0.0f;
+ if (event.key.key == GetInputBinding(INPUT_SLOT_GDOWN).joy) m_keyMotion.z = 0.0f;
+ }
+ else if (event.type == EVENT_JOY_AXIS)
+ {
+ if (event.joyAxis.axis == GetJoyAxisBinding(JOY_AXIS_SLOT_X).axis)
+ {
+ m_joyMotion.x = Math::Neutral(event.joyAxis.value / 32768.0f, m_joystickDeadzone);
+ if (GetJoyAxisBinding(JOY_AXIS_SLOT_X).invert)
+ m_joyMotion.x *= -1.0f;
+ }
+
+ if (event.joyAxis.axis == GetJoyAxisBinding(JOY_AXIS_SLOT_Y).axis)
+ {
+ m_joyMotion.y = Math::Neutral(event.joyAxis.value / 32768.0f, m_joystickDeadzone);
+ if (GetJoyAxisBinding(JOY_AXIS_SLOT_Y).invert)
+ m_joyMotion.y *= -1.0f;
+ }
+
+ if (event.joyAxis.axis == GetJoyAxisBinding(JOY_AXIS_SLOT_Z).axis)
+ {
+ m_joyMotion.z = Math::Neutral(event.joyAxis.value / 32768.0f, m_joystickDeadzone);
+ if (GetJoyAxisBinding(JOY_AXIS_SLOT_Z).invert)
+ m_joyMotion.z *= -1.0f;
+ }
+ }
+
+ event.motionInput = Math::Clamp(m_joyMotion + m_keyMotion, Math::Vector(-1.0f, -1.0f, -1.0f), Math::Vector(1.0f, 1.0f, 1.0f));
+
+
+
if (event.type == EVENT_FRAME)
{
if (!m_movie->EventProcess(event)) // end of the movie?
diff --git a/src/object/robotmain.h b/src/object/robotmain.h
index bce8e17..0a5a5a2 100644
--- a/src/object/robotmain.h
+++ b/src/object/robotmain.h
@@ -143,7 +143,7 @@ const int SATCOM_MAX = 6;
/**
- * \enum InputBinding
+ * \struct InputBinding
* \brief Binding for input slot
*/
struct InputBinding
@@ -156,6 +156,20 @@ struct InputBinding
InputBinding() : key(KEY_INVALID), joy(KEY_INVALID) {}
};
+/**
+ * \struct JoyAxisBinding
+ * \brief Binding for joystick axis
+ */
+struct JoyAxisBinding
+{
+ //! Axis index or AXIS_INVALID
+ int axis;
+ //! True to invert axis value
+ bool invert;
+};
+
+//! Invalid value for axis binding (no axis assigned)
+const int AXIS_INVALID = -1;
class CRobotMain : public CSingleton<CRobotMain>
{
@@ -165,7 +179,7 @@ public:
void CreateIni();
- //! Sets the default input bindings
+ //! Sets the default input bindings (key and axes)
void SetDefaultInputBindings();
//! Management of input bindings
@@ -174,8 +188,23 @@ public:
const InputBinding& GetInputBinding(InputSlot slot);
//@}
+ //! Management of joystick axis bindings
+ //@{
+ void SetJoyAxisBinding(JoyAxisSlot slot, JoyAxisBinding binding);
+ const JoyAxisBinding& GetJoyAxisBinding(JoyAxisSlot slot);
+ //@}
+
+ //! Management of joystick deadzone
+ //@{
+ void SetJoystickDeadzone(float zone);
+ float GetJoystickDeadzone();
+ //@}
+
+ //! Resets tracked key states (motion vectors)
+ void ResetKeyStates();
+
void ChangePhase(Phase phase);
- bool EventProcess(const Event &event);
+ bool EventProcess(Event &event);
bool CreateShortcuts();
void ScenePerso();
@@ -379,6 +408,13 @@ protected:
//! Bindings for user inputs
InputBinding m_inputBindings[INPUT_SLOT_MAX];
+ JoyAxisBinding m_joyAxisBindings[JOY_AXIS_SLOT_MAX];
+ float m_joystickDeadzone;
+ //! Motion vector set by keyboard or joystick buttons
+ Math::Vector m_keyMotion;
+ //! Motion vector set by joystick axes
+ Math::Vector m_joyMotion;
+
float m_time;
float m_gameTime;