-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.cs
160 lines (133 loc) · 3.89 KB
/
Game.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
using Godot;
using System;
public class Game : Spatial
{
private Godot.Object bluetooth;
private uint chunk_size = 32;
private OpenSimplexNoise hydro;
private OpenSimplexNoise heat;
public OpenSimplexNoise height;
private System.Collections.Generic.Dictionary<(int, int), Chunk> chunks = new System.Collections.Generic.Dictionary<(int, int), Chunk>() { };
private System.Collections.Generic.Dictionary<(int, int), Chunk> chunks_cache = new System.Collections.Generic.Dictionary<(int, int), Chunk>() { };
private Player player;
public float rpm = 0.0f;
public override void _Ready()
{
if (Engine.HasSingleton("GodotBluetooth"))
{
MobileVRInterface vr = (MobileVRInterface)ARVRServer.FindInterface("Native mobile");
if (vr != null && vr.Initialize())
{
GetViewport().Arvr = true;
// My VR headset values, consider modifyong this with yours ;)
/*
vendor: "T.T. International"
model: "3D VR Viewer Deluxe"
screen_to_lens_distance: 0.063
inter_lens_distance: 0.04
left_eye_field_of_view_angles: 50
left_eye_field_of_view_angles: 50
left_eye_field_of_view_angles: 50
left_eye_field_of_view_angles: 50
tray_to_lens_distance: 0.035
distortion_coefficients: 0.01
distortion_coefficients: 0.01
has_magnet: true
vertical_alignment: CENTER
primary_button: MAGNET
*/
vr.DisplayToLens = 6.3f;
vr.Iod = 4;
vr.K1 = 0.1f;
vr.K2 = 0.1f;
}
bluetooth = Engine.GetSingleton("GodotBluetooth");
bluetooth.Call("init", GetInstanceId(), true);
GetTree().Paused = true;
connect();
}
else
{
GetNode<Camera>("Player/Camera").Current = true;
}
var gen = new Random();
hydro = new OpenSimplexNoise();
hydro.Seed = gen.Next();
hydro.Period = 30;
hydro.Octaves = 1;
heat = new OpenSimplexNoise();
heat.Seed = gen.Next();
heat.Period = 300;
heat.Octaves = 1;
height = new OpenSimplexNoise();
height.Seed = gen.Next();
height.Period = 200;
height.Octaves = 2;
height.Persistence = 0.5f;
Chunk.Test(hydro, heat);
//heat.GetImage(120 * (int)chunk_size, 120 * (int)chunk_size).SavePng("heat.png");
//hydro.GetImage(120 * (int)chunk_size, 120 * (int)chunk_size).SavePng("hydro.png");
var init_pos = new Vector3(-0.5f, height.GetNoise1d(16) * 20, 0);
player = GetNode<Player>("Player");
player.Translate(init_pos);
}
public void connect()
{
bluetooth.Call("listPairedDevices");
}
public void _on_single_device_found(String deviceName, String deviceAddress, String deviceID)
{
if (deviceName.Contains("HC-06")) // HC-06
{
if (Engine.HasSingleton("GodotToast"))
Engine.GetSingleton("GodotToast").Call("sendToast", "device scanning " + deviceID);
bluetooth.Call("connect", deviceID.ToInt());
}
}
public void _on_connected(String deviceName, String deviceAddress)
{
GetTree().Paused = false;
}
public void _on_connected_error()
{
connect();
}
public void _on_disconnected()
{
connect();
}
public void _on_data_received(byte[] dataReceived)
{
rpm = dataReceived[0];
}
public override void _Process(float delta)
{
var player_pos = player.Translation;
for (int i = (int)((player_pos.x / chunk_size) - 8); i < (int)((player_pos.x / chunk_size) + 8); ++i)
{
for (int j = (int)((player_pos.z / chunk_size) + 12); j > (int)((player_pos.z / chunk_size) - 12); --j)
{
if (!chunks_cache.ContainsKey((i, j)))
{
chunks.Add((i, j), new Chunk());
var p = new Vector2(i, j) * chunk_size;
chunks[(i, j)].Init(chunk_size, p, height, hydro, heat);
chunks[(i, j)].Translate(new Vector3(p.x, 0, p.y));
AddChild(chunks[(i, j)]);
}
else
{
chunks.Add((i, j), chunks_cache[(i, j)]);
chunks_cache.Remove((i, j));
}
}
}
foreach (var pair in chunks_cache)
{
pair.Value.QueueFree();
}
chunks_cache.Clear();
chunks_cache = chunks;
chunks = new System.Collections.Generic.Dictionary<(int, int), Chunk>();
}
}