-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCartographer.cs
More file actions
215 lines (187 loc) · 7.04 KB
/
Cartographer.cs
File metadata and controls
215 lines (187 loc) · 7.04 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using System.Linq;
using FunkEngine;
using Godot;
using Godot.Collections;
using Array = System.Array;
/**
* <summary>Handles drawing a map from a MapGrid and scene transitions from player input. Currently, this also handles overarching win/lose conditions.</summary>
*/
public partial class Cartographer : Node2D
{
public static readonly string LoadPath = "res://Scenes/Maps/Cartographer.tscn";
[Export]
public Sprite2D PlayerSprite;
[Export]
public Theme ButtonTheme;
[Export]
public Camera2D Camera;
private Button[] _validButtons = Array.Empty<Button>();
private Button _focusedButton;
private static readonly Dictionary<Stages, Texture2D> StageIcons = new()
{
{ Stages.Battle, GD.Load<Texture2D>("res://Scenes/Maps/Assets/BattleIcon.png") },
{ Stages.Elite, GD.Load<Texture2D>("res://Scenes/Maps/Assets/EliteIcon.png") },
{ Stages.Boss, GD.Load<Texture2D>("res://Scenes/Maps/Assets/BossIcon.png") },
{ Stages.Chest, GD.Load<Texture2D>("res://Scenes/Maps/Assets/ChestIcon.png") },
{ Stages.Shop, GD.Load<Texture2D>("res://Scenes/Maps/Assets/ShopIcon.png") },
{ Stages.Event, GD.Load<Texture2D>("res://Scenes/Maps/Assets/EventIcon.png") },
{ Stages.Map, GD.Load<Texture2D>("res://Scenes/Maps/Assets/FirstIcon.png") },
};
public override void _Ready()
{
DrawMap();
SaveSystem.SaveGame();
if (
StageProducer.GetCurRoom().Type == Stages.Boss
&& StageProducer.GetCurRoom().Children.Length == 0
)
{
WinArea();
}
}
const float CameraSpeed = 120f;
public override void _Process(double delta)
{
if (Camera.Position.Y > Camera.LimitTop && Input.IsActionPressed("ui_up"))
{
Camera.Position = Camera.Position with
{
Y = (float)(Camera.Position.Y - CameraSpeed * delta),
};
}
else if (
Camera.Position.Y < Camera.LimitBottom - GetViewportRect().Size.Y
&& Input.IsActionPressed("ui_down")
)
{
Camera.Position = Camera.Position with
{
Y = (float)(Camera.Position.Y + CameraSpeed * delta),
};
}
}
public override void _EnterTree()
{
BgAudioPlayer.LiveInstance.PlayLevelMusic();
if (!SaveSystem.GetConfigValue(SaveSystem.ConfigSettings.FirstTime).AsBool())
return;
BattleDirector.VerticalScroll = false;
SaveSystem.UpdateConfig(SaveSystem.ConfigSettings.VerticalScroll, false);
}
private Vector2 GetPosition(int x, int y)
{
return new Vector2((float)(x + 1) * 640 / (StageProducer.Map.Width + 1), y * 48 + 16);
}
private void DrawMap()
{
var rooms = StageProducer.Map.GetRooms();
foreach (MapGrid.Room room in rooms)
{
DrawMapSprite(room);
foreach (int roomIdx in room.Children)
{
Line2D newLine = new Line2D();
newLine.AddPoint(GetPosition(room.X, room.Y));
newLine.AddPoint(GetPosition(rooms[roomIdx].X, rooms[roomIdx].Y));
AddChild(newLine);
}
}
_validButtons = _validButtons.OrderBy(x => x.Position.X).ToArray();
AddFocusNeighbors();
}
private static readonly Vector2 MapIconSize = new(48, 48);
private void DrawMapSprite(MapGrid.Room room)
{
var newButton = new Button();
newButton.Theme = ButtonTheme;
newButton.CustomMinimumSize = MapIconSize;
newButton.IconAlignment = HorizontalAlignment.Center;
AddChild(newButton);
bool isChild = StageProducer.GetCurRoom().Children.Contains(room.Idx);
// checks if the next room is one below current room, player has shortcuts
bool isLaneChangeAllowed =
room.Y == StageProducer.GetCurRoom().Y + 1 && StageProducer.PlayerStats.Shortcuts > 0;
//button is disabled if it is not a child of current room.
//unless player has charges of lane changing
if (!isChild && !isLaneChangeAllowed)
{
newButton.Disabled = true;
newButton.FocusMode = Control.FocusModeEnum.None;
}
else
{
//grab focus on children paths, to really make sure user wants to use a charge of maplanechanges
if (isChild)
{
newButton.GrabFocus();
_focusedButton = newButton;
}
newButton.Pressed += () =>
{
if (!isChild)
StageProducer.PlayerStats.Shortcuts--;
EnterStage(room.Idx, newButton);
};
_validButtons = _validButtons.Append(newButton).ToArray();
}
newButton.Icon = StageIcons[room.Type];
if (room.Y == 0)
newButton.Icon = StageIcons[Stages.Map];
newButton.ZIndex = 1;
newButton.Position = GetPosition(room.X, room.Y) - newButton.Size / 2;
if (room != StageProducer.GetCurRoom())
return;
PlayerSprite.Position = newButton.Position + newButton.Size * .5f;
Camera.Position = new Vector2(0, PlayerSprite.Position.Y - MapIconSize.Y / 2);
}
private void AddFocusNeighbors()
{
for (int i = 0; i < _validButtons.Length; i++)
{
_validButtons[i].FocusNeighborRight = _validButtons[(i + 1) % (_validButtons.Length)]
.GetPath();
_validButtons[(i + 1) % (_validButtons.Length)].FocusNeighborLeft = _validButtons[i]
.GetPath();
}
}
private void EnterStage(int roomIdx, Button button)
{
StageProducer.LiveInstance.PreloadScene(roomIdx);
foreach (Button btn in _validButtons)
{
btn.Disabled = true;
if (btn == button)
continue;
btn.FocusMode = Control.FocusModeEnum.None;
}
var tween = CreateTween()
.TweenProperty(PlayerSprite, "position", button.Position + button.Size * .5f, 1f);
tween.SetTrans(Tween.TransitionType.Back).SetEase(Tween.EaseType.InOut);
tween.Finished += () =>
{
BgAudioPlayer.LiveInstance.StopMusic();
StageProducer.LiveInstance.TransitionFromRoom(roomIdx);
};
}
private void WinArea()
{
if (StageProducer.IsMoreLevels())
{
GetTree().Paused = false;
//What the living fuck? Can't do this during ready?
Callable
.From(() => StageProducer.LiveInstance.TransitionStage(Stages.Continue))
.CallDeferred();
return;
}
//Achievement code for emptyPockets
if (StageProducer.PlayerStats.CurRelics.Length == 0)
{
SteamWhisperer.PopAchievement("emptyPockets");
}
EndScreen es = GD.Load<PackedScene>(EndScreen.LoadPath).Instantiate<EndScreen>();
AddChild(es);
es.TopLabel.Text = Tr("BATTLE_ROOM_WIN");
ProcessMode = ProcessModeEnum.Disabled;
}
}