-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·136 lines (118 loc) · 4.64 KB
/
main.cpp
File metadata and controls
executable file
·136 lines (118 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "main.h"
#include <numbers>
IMod* BMLEntry(IBML* bml) {
return new StaticCamera(bml);
}
// Vx(*)Vector::Normalize: not supported on Virtools 2.1?
inline bool normalize(float& x, float& y) {
const auto square_sum = x * x + y * y;
if (square_sum == 0) return false;
const auto factor = std::sqrt(1 / square_sum);
x *= factor, y *= factor;
return true;
};
CKCamera* StaticCamera::get_ingame_cam() {
return m_bml->GetTargetCameraByName("InGameCam");
}
CK3dObject* StaticCamera::get_current_ball() {
return static_cast<CK3dObject*>((current_level_array_)->GetElementObject(0, 1));
}
void StaticCamera::load_config() {
enabled_ = prop_enabled_->GetBoolean();
key_sync_ = prop_key_sync_->GetKey();
key_rotate_ = prop_key_rotate_->GetKey();
if (m_bml->IsIngame()) {
if (enabled_) enter_static_cam();
else exit_static_cam();
}
if (prop_alt_key_check_->GetBoolean()) {
is_key_down_ = [this](CKKEYBOARD key) { return m_bml->GetInputManager()->oIsKeyDown(key); };
is_key_pressed_ = [this](CKKEYBOARD key) { return m_bml->GetInputManager()->oIsKeyPressed(key); };
}
else {
is_key_down_ = [this](CKKEYBOARD key) { return m_bml->GetInputManager()->IsKeyDown(key); };
is_key_pressed_ = [this](CKKEYBOARD key) { return m_bml->GetInputManager()->IsKeyPressed(key); };
}
}
void StaticCamera::OnLoad() {
static_cam_ = static_cast<CKCamera*>(m_bml->GetCKContext()->CreateObject(CKCID_CAMERA, "StaticCamera"));
GetConfig()->SetCategoryComment("Main", "Main");
prop_enabled_ = GetConfig()->GetProperty("Main", "Enabled");
prop_enabled_->SetDefaultBoolean(true);
enabled_ = prop_enabled_->GetBoolean();
prop_key_rotate_ = GetConfig()->GetProperty("Main", "CameraRotateKey");
prop_key_rotate_->SetDefaultKey(CKKEY_LSHIFT);
prop_key_rotate_->SetComment("The key for rotating the camera.");
prop_key_left_ = GetConfig()->GetProperty("Main", "LeftRotateKey");
prop_key_left_->SetDefaultKey(CKKEY_LEFT);
prop_key_right_ = GetConfig()->GetProperty("Main", "RightRotateKey");
prop_key_right_->SetDefaultKey(CKKEY_RIGHT);
prop_key_sync_ = GetConfig()->GetProperty("Main", "CameraSyncKey");
prop_key_sync_->SetDefaultKey(CKKEY_RSHIFT);
prop_key_sync_->SetComment("The key to sync your view with the default ingame camera.");
prop_alt_key_check_ = GetConfig()->GetProperty("Main", "AlternativeKeyCheck");
prop_alt_key_check_->SetDefaultBoolean(false);
prop_alt_key_check_->SetComment("Whether to use the alternative keypress checking method.");
load_config();
}
void StaticCamera::OnModifyConfig(iCKSTRING category, iCKSTRING key, IProperty* prop) {
load_config();
}
void StaticCamera::OnPostStartMenu() {
if (init_) return;
current_level_array_ = m_bml->GetArrayByName("CurrentLevel");
init_ = true;
}
void StaticCamera::OnBallOff() {
pending_reset_ = true;
}
void StaticCamera::OnStartLevel() {
pending_reset_ = true;
}
void StaticCamera::OnCamNavActive() {
if (!enabled_ || !pending_reset_) return;
enter_static_cam();
pending_reset_ = false;
}
void StaticCamera::rotate_static_cam(float factor) {
cam_pos_diff_ = {-cam_pos_diff_.z * factor, cam_pos_diff_.y, cam_pos_diff_.x * factor};
static_cam_->Rotate({0, 1, 0}, std::numbers::pi_v<float> / 2 * factor);
}
void StaticCamera::OnProcess() {
if (!enabled_ || !is_playing()) return;
if (is_key_down_(key_sync_)) {
reset_static_cam();
}
VxVector pos;
get_current_ball()->GetPosition(&pos);
pos += cam_pos_diff_;
static_cam_->SetPosition(pos);
if (is_key_down_(key_rotate_)) {
float current_time = m_bml->GetTimeManager()->GetTime();
if (current_time - last_rotation_timestamp_ < 250.0f) return;
if (is_key_pressed_(prop_key_left_->GetKey())) rotate_static_cam(-1);
else if (is_key_pressed_(prop_key_right_->GetKey())) rotate_static_cam(1);
else return;
last_rotation_timestamp_ = current_time;
}
}
void StaticCamera::enter_static_cam() {
CKCamera* cam = get_ingame_cam();
int width, height;
cam->GetAspectRatio(width, height);
static_cam_->SetAspectRatio(width, height);
reset_static_cam();
m_bml->GetRenderContext()->AttachViewpointToCamera(static_cam_);
}
void StaticCamera::exit_static_cam() {
m_bml->GetRenderContext()->AttachViewpointToCamera(get_ingame_cam());
}
void StaticCamera::reset_static_cam() {
CKCamera* cam = get_ingame_cam();
static_cam_->SetWorldMatrix(cam->GetWorldMatrix());
static_cam_->SetFov(cam->GetFov());
VxVector cam_pos, ball_pos;
static_cam_->GetPosition(&cam_pos);
get_current_ball()->GetPosition(&ball_pos);
cam_pos_diff_ = cam_pos - ball_pos;
}