1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2
3
4 #include "RotOverheadController.h"
5
6 #include "System/Config/ConfigHandler.h"
7 #include "Game/Camera.h"
8 #include "System/Log/ILog.h"
9 #include "Map/Ground.h"
10 #include "System/myMath.h"
11
12 CONFIG(float, RotOverheadMouseScale).defaultValue(0.01f);
13 CONFIG(int, RotOverheadScrollSpeed).defaultValue(10);
14 CONFIG(bool, RotOverheadEnabled).defaultValue(true);
15 CONFIG(float, RotOverheadFOV).defaultValue(45.0f);
16
17
CRotOverheadController()18 CRotOverheadController::CRotOverheadController()
19 : oldHeight(500)
20 {
21 mouseScale = configHandler->GetFloat("RotOverheadMouseScale");
22 scrollSpeed = configHandler->GetInt("RotOverheadScrollSpeed") * 0.1f;
23 enabled = configHandler->GetBool("RotOverheadEnabled");
24 fov = configHandler->GetFloat("RotOverheadFOV");
25 UpdateVectors();
26 }
27
28
KeyMove(float3 move)29 void CRotOverheadController::KeyMove(float3 move)
30 {
31 move *= math::sqrt(move.z) * 400;
32
33 float3 flatForward = camera->forward;
34 if(camera->forward.y < -0.9f)
35 flatForward += camera->up;
36 flatForward.y = 0;
37 flatForward.ANormalize();
38
39 pos += (flatForward * move.y + camera->right * move.x) * scrollSpeed;
40 UpdateVectors();
41 }
42
43
MouseMove(float3 move)44 void CRotOverheadController::MouseMove(float3 move)
45 {
46 camera->rot.y -= mouseScale * move.x;
47 camera->rot.x -= mouseScale * move.y * move.z;
48 camera->rot.x = Clamp(camera->rot.x, -PI*0.4999f, PI*0.4999f);
49 UpdateVectors();
50 }
51
52
ScreenEdgeMove(float3 move)53 void CRotOverheadController::ScreenEdgeMove(float3 move)
54 {
55 KeyMove(move);
56 }
57
58
MouseWheelMove(float move)59 void CRotOverheadController::MouseWheelMove(float move)
60 {
61 const float gheight = CGround::GetHeightAboveWater(pos.x, pos.z, false);
62 float height = pos.y - gheight;
63
64 height *= (1.0f + (move * mouseScale));
65 pos.y = height + gheight;
66
67 UpdateVectors();
68 }
69
UpdateVectors()70 void CRotOverheadController::UpdateVectors()
71 {
72 dir.x=(float)(math::sin(camera->rot.y) * math::cos(camera->rot.x));
73 dir.y=(float)(math::sin(camera->rot.x));
74 dir.z=(float)(math::cos(camera->rot.y) * math::cos(camera->rot.x));
75 dir.ANormalize();
76
77 pos.x = Clamp(pos.x, 0.01f, gs->mapx * SQUARE_SIZE - 0.01f);
78 pos.z = Clamp(pos.z, 0.01f, gs->mapy * SQUARE_SIZE - 0.01f);
79
80 float h = CGround::GetHeightAboveWater(pos.x, pos.z, false);
81 pos.y = Clamp(pos.y, h + 5, 9000.0f);
82 oldHeight = pos.y - h;
83 }
84
SetPos(const float3 & newPos)85 void CRotOverheadController::SetPos(const float3& newPos)
86 {
87 CCameraController::SetPos(newPos);
88 pos.y = CGround::GetHeightAboveWater(pos.x, pos.z, false) + oldHeight;
89 UpdateVectors();
90 }
91
92
SwitchFrom() const93 float3 CRotOverheadController::SwitchFrom() const
94 {
95 return pos;
96 }
97
98
SwitchTo(bool showText)99 void CRotOverheadController::SwitchTo(bool showText)
100 {
101 if (showText) {
102 LOG("Switching to Rotatable overhead camera");
103 }
104 }
105
106
GetState(StateMap & sm) const107 void CRotOverheadController::GetState(StateMap& sm) const
108 {
109 CCameraController::GetState(sm);
110
111 sm["oldHeight"] = oldHeight;
112 }
113
114
SetState(const StateMap & sm)115 bool CRotOverheadController::SetState(const StateMap& sm)
116 {
117 CCameraController::SetState(sm);
118
119 SetStateFloat(sm, "oldHeight", oldHeight);
120
121 return true;
122 }
123
124