-
Notifications
You must be signed in to change notification settings - Fork 1
/
World.cs
272 lines (243 loc) · 6.29 KB
/
World.cs
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
using Godot;
using System;
public class World : Node
{
public static World instance;
public static Viewport root;
public static Vector2 RESOLUTION=new Vector2(512f,288f);
public static void Init(Viewport viewPort)
{
root=viewPort;
}
public static void mergeMaps(Level newLevel, Level nextLevel)
{
int lx=(int)newLevel.GetUsedRect().End.x;
for(int x=0;x<33;x++)
{
for(int y=0;y<18;y++)
{
newLevel.SetCell(lx+x,y,nextLevel.GetCell(x,y),false,false,false,nextLevel.GetCellAutotileCoord(x,y));
}
}
}
public static void changeScene(PackedScene newScene)
{
Node currentScene=root.GetTree().CurrentScene;
root.AddChild(newScene.Instance());
root.RemoveChild(currentScene);
if(currentScene.GetType().Name.Equals("World"))
{
((World)currentScene)._Free();
}
else
{
if(!currentScene.IsQueuedForDeletion())
{
currentScene.QueueFree();
}
}
Worker.gc();
}
public static void onObjectExitedScreen(Node node)
{
if(PlayerCamera.instance.Zoom.x==1f)
{
node.QueueFree();
}
}
public static void quit()
{
Worker.stop();
if(instance!=null)
{
instance._Free();
}
root.GetTree().Quit();
}
public static Level level;
private static Level cachedLevel;
public TileSet tileSet;
private Background background;
public Renderer renderer;
public InputController input;
private int currentLevel,nextLevel;
private static Gamestate lastState;
public static Gamestate state;
private delegate void Goal(float delta);
private Goal goal;
private AudioStreamPlayer2D musicPlayer;
public override void _Ready()
{
musicPlayer=new AudioStreamPlayer2D();
musicPlayer.Bus="Background";
onMusicFinishedPlaying();
musicPlayer.Position=new Vector2(RESOLUTION.x*0.5f,RESOLUTION.y*0.5f);
musicPlayer.Connect("finished",this,nameof(onMusicFinishedPlaying));
AddChild(musicPlayer);
musicPlayer.Play();
ResourceUtils.camera.Instance<PlayerCamera>();
if(GameSettings.current.usage==Viewport.UsageEnum.Usage2d)
{
GetNode("WorldEnvironment").QueueFree();
}
input=ResourceUtils.getInputController(this);
renderer=GetNode<Renderer>("Renderer");
tileSet=ResourceUtils.tilesets[(int)MathUtils.randomRange(0,ResourceUtils.tilesets.Count)];
currentLevel=(int)MathUtils.randomRange(0,ResourceUtils.levels.Count);
level=(Level)ResourceUtils.levels[currentLevel].Instance();
nextLevel=(int)MathUtils.randomRange(0,ResourceUtils.levels.Count);
cachedLevel=(Level)ResourceUtils.levels[nextLevel].Instance();
mergeMaps(level,cachedLevel);
ResourceUtils.player.Instance();
Player.LIVES=3;
background=(Background)ResourceUtils.background.Instance();
Player.instance.AddChild(PlayerCamera.instance);
renderer.AddChild(level);
renderer.AddChild(Player.instance);
renderer.AddChild(background);
setGamestate(Gamestate.RUNNING);
}
public override void _Process(float delta)
{
goal(delta);
}
private void tick(float delta)
{
level.MoveLocalX(level.direction.x*level.Speed*delta);
level.MoveLocalY(level.direction.y*level.Speed*delta);
if((state==Gamestate.RUNNING)&&Mathf.Abs(level.Position.x)>=level.pixelLength-RESOLUTION.x)
{
setGamestate(Gamestate.SCENE_CHANGE);
Worker.setStatus(Worker.State.PREPARELEVEL);
}
}
private void sceneRunning(float delta)
{
if(input.getPause())
{
GetTree().Paused^=true;
if(GetTree().Paused)
{
lastState=state;
setGamestate(Gamestate.PAUSED);
PauseUI pause=(PauseUI)ResourceUtils.pause.Instance();
pause.PauseMode=PauseModeEnum.Process;
Node2D node=new Node2D();
node.ZIndex=VisualServer.CanvasItemZMax;
node.AddChild(pause);
World.instance.renderer.AddChild(node);
}
}
else if(input.getQuit())
{
CallDeferred(nameof(restartLevel),false);
return;
}
tick(delta);
}
private void sceneChanged(float delta)
{
if(level.IsInsideTree())
{
setGamestate(Gamestate.RUNNING);
tick(delta);
}
}
public void resetGamestate()
{
setGamestate(lastState);
}
private void sceneChange(float delta)
{
if(level!=null&&level.IsInsideTree())
{
tick(delta);
}
}
private void sceneIdle(float delta) {}
public void setGamestate(Gamestate s)
{
state=s;
switch(state)
{
case Gamestate.SCENE_CHANGED:
goal=sceneChanged;
break;
case Gamestate.SCENE_CHANGE:
goal=sceneChange;
break;
case Gamestate.RUNNING:
case Gamestate.DIEING:
goal=sceneRunning;
break;
default:
goal=sceneIdle;
break;
}
}
public void restartLevel(bool keepLevel=false)
{
Worker.gc();
setGamestate(Gamestate.RESTART);
renderer.RemoveChild(level);
if(!keepLevel)
{
currentLevel=(int)MathUtils.randomRange(0,ResourceUtils.levels.Count);
}
level=(Level)ResourceUtils.levels[currentLevel].Instance();
mergeMaps(level,cachedLevel);
renderer.AddChild(level);
renderer.RemoveChild(Player.instance);
Player.instance.RemoveChild(PlayerCamera.instance);
Player.instance.QueueFree();
ResourceUtils.player.Instance();
Player.instance.AddChild(PlayerCamera.instance);
renderer.AddChild(Player.instance);
setGamestate(Gamestate.RUNNING);
}
public void prepareAndChangeLevel()
{
currentLevel=nextLevel;
nextLevel=(int)MathUtils.randomRange(0,ResourceUtils.levels.Count);
Level newLevel=cachedLevel!=null?cachedLevel:(Level)ResourceUtils.levels[currentLevel].Instance();
cachedLevel=(Level)ResourceUtils.levels[nextLevel].Instance();
mergeMaps(newLevel,cachedLevel);
renderer.CallDeferred("add_child",newLevel);
while(!newLevel.IsInsideTree())
{
OS.DelayMsec(1);
}
renderer.CallDeferred("remove_child",level);
newLevel.Position=new Vector2(-(Mathf.Abs(level.Position.x)-(level.pixelLength-RESOLUTION.x)),level.Position.y);
level=newLevel;
setGamestate(Gamestate.SCENE_CHANGED);
}
public void _Free()
{
setGamestate(Gamestate.RESTART);
CallDeferred("queue_free");
if(cachedLevel!=null)
{
cachedLevel.freeLevel();
}
input._free();
World.instance=null;
}
public override void _Notification(int what)
{
if(what==MainLoop.NotificationWmQuitRequest)
{
quit();
}
base._Notification(what);
}
public override void _EnterTree()
{
instance=this;
GetTree().CurrentScene=this;
}
private void onMusicFinishedPlaying()
{
musicPlayer.Stream=ResourceUtils.ingameMusic[MathUtils.randomRangeInt(0,ResourceUtils.ingameMusic.Count)];
}
}