ddnet/src/game/client/components/camera.cpp

107 lines
3.3 KiB
C++
Raw Normal View History

2010-11-20 10:37:14 +00:00
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
2011-08-31 11:56:04 +00:00
#include <base/tl/string.h>
2010-05-29 07:25:38 +00:00
#include <engine/shared/config.h>
2010-05-29 07:25:38 +00:00
#include <base/math.h>
#include <game/collision.h>
#include <game/client/gameclient.h>
#include <game/client/component.h>
2010-05-29 07:25:38 +00:00
#include "camera.h"
#include "controls.h"
#include <engine/serverbrowser.h>
2010-05-29 07:25:38 +00:00
CCamera::CCamera()
{
2010-10-17 08:43:27 +00:00
m_Zoom = 1.0f;
m_CamType = CAMTYPE_UNDEFINED;
}
2010-05-29 07:25:38 +00:00
void CCamera::OnRender()
{
//vec2 center;
//m_Zoom = 1.0f;
// update camera center
if(m_pClient->m_Snap.m_SpecInfo.m_Active && !m_pClient->m_Snap.m_SpecInfo.m_UsePosition)
{
if(m_CamType != CAMTYPE_SPEC)
{
2014-05-23 17:28:26 +00:00
m_LastPos[g_Config.m_ClDummy] = m_pClient->m_pControls->m_MousePos[g_Config.m_ClDummy];
m_pClient->m_pControls->m_MousePos[g_Config.m_ClDummy] = m_PrevCenter;
m_pClient->m_pControls->ClampMousePos();
m_CamType = CAMTYPE_SPEC;
}
m_Center = m_pClient->m_pControls->m_MousePos[g_Config.m_ClDummy];
}
else
{
if(m_CamType != CAMTYPE_PLAYER)
{
2014-05-23 17:28:26 +00:00
m_pClient->m_pControls->m_MousePos[g_Config.m_ClDummy] = m_LastPos[g_Config.m_ClDummy];
m_pClient->m_pControls->ClampMousePos();
m_CamType = CAMTYPE_PLAYER;
}
2011-03-10 16:57:15 +00:00
2010-05-29 07:25:38 +00:00
vec2 CameraOffset(0, 0);
float l = length(m_pClient->m_pControls->m_MousePos[g_Config.m_ClDummy]);
if(l > 0.0001f) // make sure that this isn't 0
2011-03-10 16:57:15 +00:00
{
float DeadZone = g_Config.m_ClMouseDeadzone;
float FollowFactor = g_Config.m_ClMouseFollowfactor/100.0f;
float OffsetAmount = max(l-DeadZone, 0.0f) * FollowFactor;
CameraOffset = normalize(m_pClient->m_pControls->m_MousePos[g_Config.m_ClDummy])*OffsetAmount;
2011-03-10 16:57:15 +00:00
}
if(m_pClient->m_Snap.m_SpecInfo.m_Active)
m_Center = m_pClient->m_Snap.m_SpecInfo.m_Position + CameraOffset;
2011-03-10 16:57:15 +00:00
else
m_Center = m_pClient->m_LocalCharacterPos + CameraOffset;
}
m_PrevCenter = m_Center;
}
2010-10-17 08:43:27 +00:00
void CCamera::OnConsoleInit()
{
Console()->Register("zoom+", "", CFGFLAG_CLIENT, ConZoomPlus, this, "Zoom increase");
Console()->Register("zoom-", "", CFGFLAG_CLIENT, ConZoomMinus, this, "Zoom decrease");
Console()->Register("zoom", "", CFGFLAG_CLIENT, ConZoomReset, this, "Zoom reset");
2010-10-17 08:43:27 +00:00
}
2011-01-06 03:46:10 +00:00
2011-12-20 20:45:07 +00:00
void CCamera::OnReset()
{
m_Zoom = 1.0f;
}
2013-11-13 12:24:56 +00:00
const float ZoomStep = 0.866025f;
void CCamera::ConZoomPlus(IConsole::IResult *pResult, void *pUserData)
{
CCamera *pSelf = (CCamera *)pUserData;
CServerInfo Info;
pSelf->Client()->GetServerInfo(&Info);
2014-01-22 15:59:43 +00:00
if(pSelf->m_pClient->m_Snap.m_SpecInfo.m_Active || (str_find_nocase(Info.m_aGameType, "race") || str_find_nocase(Info.m_aGameType, "fastcap")) || pSelf->Client()->State() == IClient::STATE_DEMOPLAYBACK)
2011-01-06 03:46:10 +00:00
((CCamera *)pUserData)->m_Zoom *= ZoomStep;
}
void CCamera::ConZoomMinus(IConsole::IResult *pResult, void *pUserData)
{
CCamera *pSelf = (CCamera *)pUserData;
CServerInfo Info;
pSelf->Client()->GetServerInfo(&Info);
2014-01-22 15:59:43 +00:00
if(pSelf->m_pClient->m_Snap.m_SpecInfo.m_Active || (str_find_nocase(Info.m_aGameType, "race") || str_find_nocase(Info.m_aGameType, "fastcap")) || pSelf->Client()->State() == IClient::STATE_DEMOPLAYBACK)
((CCamera *)pUserData)->m_Zoom *= 1/ZoomStep;
}
void CCamera::ConZoomReset(IConsole::IResult *pResult, void *pUserData)
{
CCamera *pSelf = (CCamera *)pUserData;
CServerInfo Info;
pSelf->Client()->GetServerInfo(&Info);
2011-12-20 20:45:07 +00:00
((CCamera *)pUserData)->m_Zoom = 1.0f;
2011-01-06 03:46:10 +00:00
}