From c725c6f662f4af619b54f3e879449160ca48598d Mon Sep 17 00:00:00 2001 From: Arnaud Lefebvre Date: Mon, 2 Apr 2018 00:37:26 +0200 Subject: [PATCH] Set ship yaw accordingly to the keys the user is holding Fixes #20 --- Interface/ViewTactical.cpp | 77 ++++++++++++++++++++++++++++++++++---- Interface/ViewTactical.h | 8 ++++ 2 files changed, 77 insertions(+), 8 deletions(-) diff --git a/Interface/ViewTactical.cpp b/Interface/ViewTactical.cpp index d4047b7a..187baad3 100644 --- a/Interface/ViewTactical.cpp +++ b/Interface/ViewTactical.cpp @@ -101,6 +101,7 @@ ViewTactical::ViewTactical() : m_CameraZoomLevel = 3; m_fYaw = m_fYawV = 0.0f; + YawStatePressed m_yawState = YawStatePressed::None; m_SetHeading = m_SetHeadingV = 0.0f; m_SetVelocity = m_SetVelocityV = 0.0f; m_BeginControl = false; @@ -780,8 +781,13 @@ bool ViewTactical::onMessage( const Message & msg ) case HK_LEFT: if ( m_bYawControl ) { - if ( m_fYaw > -1.0f ) + if ( m_yawState != YawStatePressed::Left && m_yawState != YawStatePressed::Both ) { + if (m_yawState == YawStatePressed::Right) + m_yawState = YawStatePressed::Both; + else + m_yawState = YawStatePressed::Left; + m_BeginControl = true; m_UpdateControl = true; m_fYaw = -1.0f; @@ -796,9 +802,13 @@ bool ViewTactical::onMessage( const Message & msg ) case 'A': if ( m_bYawControl ) { - if ( m_fYaw > -1.0f ) + if ( m_yawState != YawStatePressed::Left && m_yawState != YawStatePressed::Both ) { m_BeginControl = true; + if (m_yawState == YawStatePressed::Right) + m_yawState = YawStatePressed::Both; + else + m_yawState = YawStatePressed::Left; m_fYawV = -SET_YAW_RATE; if ( m_fYaw > 0.0f ) @@ -814,8 +824,13 @@ bool ViewTactical::onMessage( const Message & msg ) case HK_RIGHT: if ( m_bYawControl ) { - if ( m_fYaw < 1.0f ) + if ( m_yawState != YawStatePressed::Right && m_yawState != YawStatePressed::Both ) { + if (m_yawState == YawStatePressed::Left) + m_yawState = YawStatePressed::Both; + else + m_yawState = YawStatePressed::Right; + m_BeginControl = true; m_UpdateControl = true; m_fYaw = 1.0f; @@ -830,9 +845,13 @@ bool ViewTactical::onMessage( const Message & msg ) case 'D': if ( m_bYawControl ) { - if ( m_fYaw < 1.0f ) + if ( m_yawState != YawStatePressed::Right && m_yawState != YawStatePressed::Both ) { m_BeginControl = true; + if (m_yawState == YawStatePressed::Left) + m_yawState = YawStatePressed::Both; + else + m_yawState = YawStatePressed::Right; m_fYawV = SET_YAW_RATE; if ( m_fYaw < 0.0f ) @@ -880,19 +899,61 @@ bool ViewTactical::onMessage( const Message & msg ) } return true; case HK_LEFT: + if (m_yawState == YawStatePressed::Both) { + m_yawState = YawStatePressed::Right; + m_fYaw = 1.0f; + m_UpdateControl = true; + } + else if (m_yawState == YawStatePressed::Left) { + m_yawState = YawStatePressed::None; + m_fYaw = m_fYawV = 0.0f; + m_UpdateControl = true; + } + return true; case HK_RIGHT: - if ( m_fYaw != 0.0f ) - { + if (m_yawState == YawStatePressed::Both) { + m_yawState = YawStatePressed::Left; + m_fYaw = -1.0f; + m_UpdateControl = true; + } + else if (m_yawState == YawStatePressed::Right) { + m_yawState = YawStatePressed::None; m_fYaw = m_fYawV = 0.0f; m_UpdateControl = true; } return true; case 'A': + if (m_bYawControl) { + if (m_yawState == YawStatePressed::Both) { + m_yawState = YawStatePressed::Right; + m_fYawV = 1.0f; + m_UpdateControl = true; + } + else if (m_yawState == YawStatePressed::Left) { + m_yawState = YawStatePressed::None; + m_fYawV = 0.0f; + m_UpdateControl = true; + } + } + else + { + if ( m_SetHeadingV != 0.0f ) + { + m_SetHeadingV = 0.0f; + m_UpdateControl = true; + } + } + return true; case 'D': if ( m_bYawControl ) { - if ( m_fYawV != 0.0f ) - { + if (m_yawState == YawStatePressed::Both) { + m_yawState = YawStatePressed::Left; + m_fYawV = -1.0f; + m_UpdateControl = true; + } + else if (m_yawState == YawStatePressed::Right) { + m_yawState = YawStatePressed::None; m_fYawV = 0.0f; m_UpdateControl = true; } diff --git a/Interface/ViewTactical.h b/Interface/ViewTactical.h index 0900c717..a205d237 100644 --- a/Interface/ViewTactical.h +++ b/Interface/ViewTactical.h @@ -22,6 +22,13 @@ class ViewGame; //---------------------------------------------------------------------------- +enum YawStatePressed { + None, + Left, + Right, + Both +}; + class ViewTactical : public WindowView::View { public: @@ -149,6 +156,7 @@ class ViewTactical : public WindowView::View bool m_UpdateControl; // ship manual controls float m_fYaw; float m_fYawV; + YawStatePressed m_yawState; float m_SetHeading; float m_SetHeadingV; float m_SetVelocity; // desired ship velocity