summaryrefslogtreecommitdiff
path: root/stage/stage.gd
blob: b2d4736b7b0a59767cb87af5b97d51dd3f6842d2 (plain)
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
class_name Stage
extends Node2D


var grid := AStarGrid2D.new()

@onready var grid_selector: GridSelector = %GridSelector


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	grid.region = $Ground.get_used_rect()
	grid.cell_size = $Floor.tile_set.tile_size
	grid.cell_shape = AStarGrid2D.CELL_SHAPE_ISOMETRIC_DOWN
	grid.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER
	grid.update()
	
	# set whole grid non-walkable initially
	grid.fill_solid_region(grid.region, true)
	
	# pre-set floor tiles as walkable
	for tile in $Floor.get_used_cells():
		grid.set_point_solid(tile, false)


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
	pass


func _input(event: InputEvent) -> void:
	if event.is_action_pressed("menu"):
		$HUDMain.visible = true
		grid_selector.process_mode = Node.PROCESS_MODE_DISABLED
		$HUDMain/PanelContainer/VBoxContainer/Button.grab_focus()
	
	if event.is_action_pressed("test_4"):
		var teams := ["1", "2"]
		var tiles := [Vector2i(1,6), Vector2i(3,6)]
		grid_selector.placement_tile_atlas_coordinates = tiles[(tiles.find(grid_selector.placement_tile_atlas_coordinates) + 1) % tiles.size()]
		grid_selector.current_team = teams[(teams.find(grid_selector.current_team) + 1) % teams.size()]


func _on_grid_selector_placed_tiles(grid_positions: Array) -> void:
	for p in grid_positions:
		grid.set_point_solid(p, false)
	
	var unit = preload("res://unit/unit.tscn").instantiate()
	unit.global_position = %GridSelector.global_position + $Floor.position
	unit.current_team = grid_selector.current_team
	add_child(unit)
	
	# block unit tile for movement
	grid.set_point_solid($Ground.local_to_map(%GridSelector.global_position), true)
	
	grid_selector.current_state = grid_selector.state_select


func _on_grid_selector_move_mode_confirmed(path: Array) -> void:
	grid_selector.process_mode = Node.PROCESS_MODE_DISABLED
	
	var unit: Node2D = grid_selector.current_entity
	var tween = get_tree().create_tween()
	for p in path.slice(1): # remove starting position
		tween.tween_property(unit, "global_position", p + $Floor.position, 0.1)
	
	await tween.finished
	grid_selector.process_mode = Node.PROCESS_MODE_INHERIT
	grid_selector.current_state = grid_selector.get_node("StateSelect")
	grid_selector.current_state.draw($Ground.local_to_map(path[path.size() - 1]))
	
	# clear previous tile and set new tile solid
	grid.set_point_solid($Ground.local_to_map(path[0]), false)
	grid.set_point_solid($Ground.local_to_map(path[path.size() - 1]), true)


func _on_grid_selector_range_select_confirmed(grid_position: Vector2i, entity: Node2D) -> void:
	entity.queue_free()
	grid.set_point_solid(grid_position, false)