-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit.gd
286 lines (226 loc) · 6.63 KB
/
unit.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
extends Area2D
class_name Unit
var inputs = {
"right": Vector2.RIGHT,
"left": Vector2.LEFT,
"up": Vector2.UP,
"down": Vector2.DOWN,
}
var move_animation_speed = 10
var fighting = false
var moving = false
var facing = 0 # default/right
var requested_direction = null
var in_city = null
var sleep_turns = 0
var movement_tween : Tween
# path-finding
var _path = PackedVector2Array()
signal finished_fighting
signal finished_movement
@export var moves_per_turn = 2
var moves_remaining
@export var my_team = "NoTeam"
@onready var ray = $RayCast2D
#@onready var navigation_agent: NavigationAgent2D = $NavigationAgent2D
func _on_mouse_entered():
SignalBus.mouse_entered_unit.emit(self)
func _on_mouse_exited():
SignalBus.mouse_exited_unit.emit(self)
# the cursor chooses who gets the events
# thus, we do not use _unhandle_input() here
func handle_cursor_input_event(event):
if moving or fighting: return
if event.is_action_pressed("click"):
if on_my_team():
await select_me()
if is_sleeping():
await awaken()
return
$Sounds.play_denied()
return
for direction in inputs.keys():
if event.is_action_pressed(direction):
await request_move(direction)
return
if event.is_action_pressed('sleep'):
await toggle_sleep()
return
if event.is_action_pressed('next'):
SignalBus.want_next_unit.emit(self.my_team)
return
func _ready():
moves_per_turn = 2
sleep_turns = 0
position = position.snapped(Vector2.ONE * Global.tile_size / 2)
await refill_moves()
await assign_groups()
_path.clear()
func attack(defender):
if(defender.my_team == my_team):
print(
"Warning: refusing to attack unit ",
defender,
" already owned by, ",
my_team)
return
SignalBus.unit_attacked_unit.emit(self, defender)
func start_fighting():
fighting = true
func stop_fighting():
fighting = false
finished_fighting.emit()
func awaken():
sleep_turns = 0
if has_more_moves():
$Inactive.awaken()
else:
$Inactive.done_moving()
func go_to_sleep():
deselect_me()
sleep_turns = -1
$Inactive.sleep_infinity()
SignalBus.unit_completed_moves.emit(my_team)
func toggle_sleep():
if is_sleeping():
await awaken()
else:
await go_to_sleep()
func is_awake() -> bool:
return (sleep_turns == 0)
func is_done() -> bool:
if has_more_moves(): return false
if is_moving(): return false
if is_fighting(): return false
return true
func is_fighting() -> bool:
return fighting
func is_moving() -> bool:
return moving
func is_sleeping() -> bool:
return (sleep_turns != 0)
func request_move(direction):
if is_moving():
return
if not has_more_moves():
await deny_move()
return
requested_direction = direction
ray.target_position = inputs[direction] * Global.tile_size
ray.force_raycast_update()
# nothing's there, free to move
if not(ray.is_colliding()):
await move_to_requested()
return
# get all ray collision targets
# units can be inside other units or in cities, so ray can collide with multiple
# technique from https://forum.godotengine.org/t/27326
var targets = []
while ray.is_colliding():
var target = ray.get_collider()
targets.append(target)
if target is TileMap:
await deny_move()
return
ray.add_exception(target)
ray.force_raycast_update()
var target_city : Area2D = null
var target_unit : Area2D = null
for target in targets:
ray.remove_exception(target)
if target.is_in_group("Cities"): target_city = target
if target.is_in_group("Units"): target_unit = target
if target_unit != null:
await request_move_into_unit(target_unit)
return
if target_city != null:
await request_move_into_city(target_city)
return
# fallback case
await deny_move()
func request_move_into_city(city):
if city.is_open_to_team(my_team):
await move_to_requested()
await enter_city(city)
return
await city.attacked_by(self)
func request_move_into_unit(unit):
if unit.my_team == my_team:
await move_to_requested()
return
attack(unit)
await reduce_moves()
func enter_city(city):
await city.occupy_by(self)
in_city = city
$Sprite2D.scale = Vector2(0.05, 0.05)
# TODO: derive these from sprite/size properties instead of hard coding -10, etc
$Sprite2D.position = Vector2(-10, 10)
#$Sprite2D.centered = false
func leave_city():
in_city = null
# TODO: read this from resource, instead of hard coding here
$Sprite2D.scale = Vector2(0.07, 0.07)
$Sprite2D.position = Vector2(0, 0)
#$Sprite2D.centered = true
func face_toward(direction):
if(direction == "right"):
$Sprite2D.flip_h = false
return
if(direction == "left"):
$Sprite2D.flip_h = true
return
func move_to_requested():
if(requested_direction == null):
print("Error! Avoiding move towards undefined direction")
return
await move(requested_direction)
func move(direction):
$Sounds.play_move()
face_toward(direction)
movement_tween = create_tween()
movement_tween.tween_property(self, "position",
position +
inputs[direction] *
Global.tile_size,
1.0/move_animation_speed
).set_trans(Tween.TRANS_SINE)
moving = true
await movement_tween.finished
finished_movement.emit()
moving = false
if(in_city != null):
if(position != in_city.position):
await leave_city()
await reduce_moves()
func reduce_moves():
moves_remaining -= 1
if not has_more_moves():
SignalBus.unit_completed_moves.emit(my_team)
await deselect_me()
$Inactive.done_moving()
func assign_groups():
modulate = Global.team_colors[my_team]
add_to_group(my_team)
add_to_group("Units")
func on_my_team() -> bool:
# TODO: look for a better way to implement debugging overrides
if Global.debug_select_any or my_team == Global.human_team:
return true
return false
func deny_move():
$Sounds.play_denied()
func select_me():
$Sounds.play_ready()
func deselect_me():
$Sounds.stop_all()
func has_more_moves():
if is_sleeping(): return false
return moves_remaining > 0
func refill_moves():
moves_remaining = moves_per_turn
if is_awake():
$Inactive.awaken()
func disband():
SignalBus.unit_disbanded.emit(self)
queue_free()