-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.gd
More file actions
191 lines (130 loc) · 5.14 KB
/
Main.gd
File metadata and controls
191 lines (130 loc) · 5.14 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
extends Node2D
var last_world_state = 0
var Unit_NUMS = 0
var deleted_Unit = []
var Player1ID
var Player2ID
var interpolation_offset:int = 100
onready var catapult_projectile = preload("res://players/Catapult/Catapult_Projectile.tscn")
onready var tent = preload("res://players/Players/PlayerTemplate.tscn")
var UnitlistUser = [
preload("res://players/knight/Knight.tscn"),
preload("res://players/Catapult/CatapultPlayer.tscn"),
preload("res://players/Catapult/CatapultPlayer.tscn")
]
var UnitlistEnemy = [
preload("res://players/knight/KnightTemplete.tscn"),
preload("res://players/Catapult/CatapultTemplate.tscn"),
]
var world_state_buffer = []
func _ready():
Server.RequestSpawnTent()
Project.main = self
func shoot_Catapult(catapult_pos):
var new_instance = catapult_projectile.instance()
get_node("Projectile").add_child(new_instance)
new_instance.global_position = Vector2(catapult_pos.x+65,
catapult_pos.y-200)
new_instance.launch()
print("Spawned new projectile")
func SpawnTent(player_id, Spawn_Position):
if get_node("Tents").has_node("Tent"):
return
var new_instance = tent.instance()
get_node("Tents").add_child(new_instance)
new_instance.name == str(player_id)
func PlayerSpawnUnit(UnitID, IDEN):
var new_instance = UnitlistUser[UnitID].instance()
get_node("Navigation2D/MyUnits").add_child(new_instance)
new_instance.position = Vector2(1167, 287)
new_instance.set_name(str(IDEN))
new_instance.IDEN = IDEN
func SpawnUnit(Player_State, l):
var spawn_pos
print("ello")
for Unit in Player_State[1].keys():
if str(Unit) == "T":
continue
elif deleted_Unit.has(str(Player_State[1][Unit])):
continue
elif Player_State[1][Unit]["HP"] <= 0:
continue
else:
var UnitCreatorID = Player_State[1][Unit]["CI"]
if UnitCreatorID == get_tree().get_network_unique_id():
print(Player_State[1][Unit]["U"])
var new_instance = UnitlistUser[
Player_State[1][Unit]["U"]
].instance()
spawn_pos = Vector2(1167, 287)
get_node("Navigation2D/MyUnits").add_child(new_instance)
new_instance.set_name(str(Player_State[1][Unit]["IDEN"]))
new_instance.global_position = spawn_pos
else:
print(Player_State[1][Unit]["U"])
var new_instance = UnitlistEnemy[Player_State[1][Unit]["U"]].instance()
spawn_pos = Vector2(-275, 287)
get_node("Navigation2D/Units").add_child(new_instance)
new_instance.set_name(str(Player_State[1][Unit]["IDEN"]))
new_instance.global_position = spawn_pos
func _physics_process(delta: float) -> void:
var render_time = OS.get_system_time_msecs() - interpolation_offset
if world_state_buffer.size() > 1:
while world_state_buffer.size() > 2 and render_time > world_state_buffer[2].T:
world_state_buffer.remove(0)
if world_state_buffer.size() > 2:
var interpolation_factor := float(
render_time - world_state_buffer[1]["T"]) / float(world_state_buffer[2]["T"] - world_state_buffer[1]["T"])
var UnitNode = get_node("Navigation2D/Units")
for player in world_state_buffer[2].keys():
if str(player) == "T":
continue
if not world_state_buffer[1].has(player):
continue
if deleted_Unit.has(world_state_buffer[1][player]["IDEN"]):
continue
if UnitNode.has_node(str(world_state_buffer[1][player]["IDEN"])):
if world_state_buffer[1][player]["CI"] != get_tree().get_network_unique_id():
var IDEN = world_state_buffer[1][player]["IDEN"]
var new_position = lerp(world_state_buffer[1][player]["P"],
world_state_buffer[2][player]["P"], interpolation_factor)
get_node("Navigation2D/Units/" + str(IDEN)
).MovePlayer(new_position, world_state_buffer[1][player])
elif get_node("Navigation2D/MyUnits").has_node(
str(world_state_buffer[1][player]["IDEN"])):
continue
else:
if get_node("Navigation2D/MyUnits").has_node(
str(world_state_buffer[1][player]["IDEN"])):
continue
elif UnitNode.has_node(str(world_state_buffer[1][player]["IDEN"])):
continue
print("spawning a new unit")
SpawnUnit(world_state_buffer, "HI")
func UpdateWorldState(world_state, unit_num, player_state):
if world_state["T"] > last_world_state:
last_world_state = world_state["T"]
world_state_buffer.append(world_state)
func Inflict_damage(Unit_IDE:int, damage_taken:int):
var UnitNode = get_node("Navigation2D/MyUnits")
if UnitNode.has_node(str(Unit_IDE)):
var unit = get_node("Navigation2D/MyUnits/" + str(Unit_IDE))
unit.health -= damage_taken
unit.update_health()
if get_node("Navigation2D/Units").has_node(str(Unit_IDE)):
pass
func Erase(unit_iden):
if get_node("Navigation2D/MyUnits").has_node(str(unit_iden)):
get_node("Navigation2D/MyUnits/" + str(unit_iden)).queue_free()
elif get_node("Navigation2D/Units").has_node(str(unit_iden)):
get_node("Navigation2D/Units/" + str(unit_iden)).queue_free()
deleted_Unit = [unit_iden]
func TentUpdate(State:Dictionary, ID:int):
if ID == get_tree().get_network_unique_id():
get_node("Tents/Tent").Update_Health(State["HP"])
return
var Tent = get_node("Tents/PlayerMain")
if State["HP"] <= 0:
Tent.queue_free()
return
Tent.HEALTH = State["HP"]