This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathWorldScript.gd
93 lines (73 loc) · 2.79 KB
/
WorldScript.gd
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
extends Spatial
tool
var pw
onready var player = $Player
onready var block_outline = $BlockOutline
var chunk_x = 1
var chunk_z = 1
var Chunk = load("res://Chunk.gd")
var ProcWorld = load("res://ProcWorld.gd")
var chunk_pos = Vector2()
func _ready():
print("CREATING WORLD")
pw = ProcWorld.new()
add_child(pw)
self.connect("tree_exiting", self, "_on_WorldScript_tree_exiting")
player.connect("place_block", self, "_on_Player_place_block")
player.connect("destroy_block", self, "_on_Player_destroy_block")
player.connect("highlight_block", self, "_on_Player_highlight_block")
player.connect("unhighlight_block", self, "_on_Player_unhighlight_block")
#self.connect("tree_exited", self, "_on_WorldScript_tree_exited")
func _process(delta):
# Check the players chunk position and see if it has changed
if player != null and pw != null and pw.mutex != null:
var player_pos = player.translation
var chunk_x = floor(player.translation.x / Chunk.DIMENSION.x)
var chunk_z = floor(player.translation.z / Chunk.DIMENSION.z)
var new_chunk_pos = Vector2(chunk_x, chunk_z)
# If its a new chunk update for the ProcWorld thread
if new_chunk_pos != chunk_pos:
chunk_pos = new_chunk_pos
#pw.update_player_pos(chunk_pos)
pw.call_deferred("update_player_pos", chunk_pos)
func _on_WorldScript_tree_exiting():
print("Kill map loading thread")
if pw != null:
pw.call_deferred("kill_thread")
#pw.kill_thread()
#pw.thread.wait_to_finish()
print("Finished")
func _on_Player_destroy_block(pos, norm):
# Take a half step into the block
pos -= norm * 0.5
# Get chunk from pos
var cx = int(floor(pos.x / Chunk.DIMENSION.x))
var cz = int(floor(pos.z / Chunk.DIMENSION.z))
# Get block from pos
var bx = fposmod(floor(pos.x), Chunk.DIMENSION.x) + 0.5
var by = fposmod(floor(pos.y), Chunk.DIMENSION.y) + 0.5
var bz = fposmod(floor(pos.z), Chunk.DIMENSION.z) + 0.5
#pw.change_block(cx, cz, bx, by, bz, "Air")
pw.call_deferred("change_block", cx, cz, bx, by, bz, "Air")
func _on_Player_place_block(pos, norm, t):
# Take a half step out of the block
pos += norm * 0.5
# Get chunk from pos
var cx = int(floor(pos.x / Chunk.DIMENSION.x))
var cz = int(floor(pos.z / Chunk.DIMENSION.z))
# Get block from pos
var bx = fposmod(floor(pos.x), Chunk.DIMENSION.x) + 0.5
var by = fposmod(floor(pos.y), Chunk.DIMENSION.y) + 0.5
var bz = fposmod(floor(pos.z), Chunk.DIMENSION.z) + 0.5
#pw.change_block(cx, cz, bx, by, bz, t)
pw.call_deferred("change_block", cx, cz, bx, by, bz, t)
func _on_Player_highlight_block(pos, norm):
block_outline.visible = true
# Take a half step into the block
pos -= norm * 0.5
var bx = floor(pos.x) + 0.5
var by = floor(pos.y) + 0.5
var bz = floor(pos.z) + 0.5
block_outline.translation = Vector3(bx, by, bz)
func _on_Player_unhighlight_block():
block_outline.visible = false