1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2
3 #include <boost/cstdint.hpp>
4 #include <SDL_keycode.h>
5
6 #include "TWController.h"
7 #include "Game/Camera.h"
8 #include "Map/Ground.h"
9 #include "Game/UI/MouseHandler.h"
10 #include "Rendering/GlobalRendering.h"
11 #include "System/Config/ConfigHandler.h"
12 #include "System/Log/ILog.h"
13 #include "System/myMath.h"
14 #include "System/Input/KeyInput.h"
15
16 CONFIG(int, TWScrollSpeed).defaultValue(10);
17 CONFIG(bool, TWEnabled).defaultValue(true);
18 CONFIG(float, TWFOV).defaultValue(45.0f);
19
20
CTWController()21 CTWController::CTWController()
22 {
23 scrollSpeed = configHandler->GetInt("TWScrollSpeed") * 0.1f;
24 enabled = configHandler->GetBool("TWEnabled");
25 fov = configHandler->GetFloat("TWFOV");
26 UpdateVectors();
27 }
28
29
KeyMove(float3 move)30 void CTWController::KeyMove(float3 move)
31 {
32 float3 flatForward=camera->forward;
33 flatForward.y=0;
34 flatForward.ANormalize();
35
36 move *= math::sqrt(move.z) * 200;
37 pos += (camera->right * move.x + flatForward * move.y) * scrollSpeed;
38 UpdateVectors();
39 }
40
41
MouseMove(float3 move)42 void CTWController::MouseMove(float3 move)
43 {
44 move *= (1 + KeyInput::GetKeyModState(KMOD_SHIFT) * 3) * pixelSize;
45
46 float3 flatForward = camera->forward;
47 flatForward.y = 0;
48 flatForward.ANormalize();
49
50 pos += (camera->right * move.x - flatForward * move.y) * scrollSpeed;
51 UpdateVectors();
52 }
53
54
ScreenEdgeMove(float3 move)55 void CTWController::ScreenEdgeMove(float3 move)
56 {
57 if (mouse->lasty < globalRendering->viewSizeY / 3) {
58 camera->rot.y -= (move.x * globalRendering->lastFrameTime * 0.5f * 0.2f);
59 move.x = 0.0f;
60 }
61 KeyMove(move);
62 }
63
64
MouseWheelMove(float move)65 void CTWController::MouseWheelMove(float move)
66 {
67 camera->rot.x -= (move * 0.001f);
68 UpdateVectors();
69 }
70
71
UpdateVectors()72 void CTWController::UpdateVectors()
73 {
74 pos.x = Clamp(pos.x, 0.01f, gs->mapx * SQUARE_SIZE - 0.01f);
75 pos.z = Clamp(pos.z, 0.01f, gs->mapy * SQUARE_SIZE - 0.01f);
76 pos.y = CGround::GetHeightAboveWater(pos.x, pos.z, false);
77
78 camera->rot.x = Clamp(camera->rot.x, -PI * 0.4f, -0.1f);
79
80 dir.x = math::sin(camera->rot.y) * math::cos(camera->rot.x);
81 dir.y = math::sin(camera->rot.x);
82 dir.z = math::cos(camera->rot.y) * math::cos(camera->rot.x);
83 dir.ANormalize();
84 }
85
86
Update()87 void CTWController::Update()
88 {
89 pixelSize = (camera->GetTanHalfFov() * 2.0f) / globalRendering->viewSizeY * (-camera->rot.x * 1500) * 2.0f;
90 }
91
92
GetPos() const93 float3 CTWController::GetPos() const
94 {
95 float dist = -camera->rot.x * 1500;
96
97 float3 cpos = pos - dir * dist;
98 if (cpos.y < CGround::GetHeightAboveWater(cpos.x, cpos.z, false) + 5)
99 cpos.y = CGround::GetHeightAboveWater(cpos.x, cpos.z, false) + 5;
100
101 return cpos;
102 }
103
104
SetPos(const float3 & newPos)105 void CTWController::SetPos(const float3& newPos)
106 {
107 pos = newPos;
108 UpdateVectors();
109 }
110
111
SwitchFrom() const112 float3 CTWController::SwitchFrom() const
113 {
114 return pos;
115 }
116
117
SwitchTo(bool showText)118 void CTWController::SwitchTo(bool showText)
119 {
120 if (showText) {
121 LOG("Switching to Total War style camera");
122 }
123 }
124
125
GetState(StateMap & sm) const126 void CTWController::GetState(StateMap& sm) const
127 {
128 CCameraController::GetState(sm);
129 }
130
131
SetState(const StateMap & sm)132 bool CTWController::SetState(const StateMap& sm)
133 {
134 CCameraController::SetState(sm);
135 return true;
136 }
137