2021-11-13 10:34:39 +00:00
|
|
|
extends Control
|
|
|
|
|
2021-11-24 18:17:07 +00:00
|
|
|
# Signal to pass the fire location back to parent
|
|
|
|
signal fire_at
|
|
|
|
|
2021-11-24 06:10:58 +00:00
|
|
|
var atlas = preload("res://assets/game/HitMissAtlas.png")
|
|
|
|
var sprites = []
|
2021-11-24 18:17:07 +00:00
|
|
|
var hits
|
2021-11-24 06:10:58 +00:00
|
|
|
|
2021-11-13 10:34:39 +00:00
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
func _ready():
|
2021-11-24 18:17:07 +00:00
|
|
|
for x in 10:
|
|
|
|
for y in 10:
|
|
|
|
texture(Vector2(x,y))
|
2021-11-13 10:34:39 +00:00
|
|
|
pass # Replace with function body.
|
|
|
|
|
|
|
|
func _on_Fire_pressed():
|
|
|
|
var crosshair = get_node("Crosshair")
|
2021-11-18 15:00:15 +00:00
|
|
|
# Check if the crosshair is in a valid position
|
2021-11-24 06:10:58 +00:00
|
|
|
if crosshair.validate_position(crosshair.position):
|
2021-11-13 10:34:39 +00:00
|
|
|
var crosshair_pos = crosshair.world_to_board_space(crosshair.position)
|
2021-11-24 06:10:58 +00:00
|
|
|
if(hits[crosshair_pos.x][crosshair_pos.y] == 0):
|
|
|
|
# fires at position
|
|
|
|
emit_signal("fire_at", crosshair_pos)
|
|
|
|
return
|
|
|
|
#if invalid position popup appears
|
|
|
|
var dialog = get_node("FireDialog")
|
|
|
|
dialog.popup_centered()
|
2021-11-13 10:34:39 +00:00
|
|
|
|
|
|
|
func _on_FireDialog_confirmed():
|
|
|
|
get_node("Crosshair").visible = true
|
2021-11-24 18:17:07 +00:00
|
|
|
|
2021-11-24 06:10:58 +00:00
|
|
|
const OFFSET = Vector2(18, 18)
|
|
|
|
|
|
|
|
func texture(index):
|
|
|
|
if(hits[index.x][index.y] != 0):
|
|
|
|
var textureSize = 32
|
|
|
|
# It's okay to create a new texture every time, as resources are refcounted
|
|
|
|
var t = AtlasTexture.new()
|
|
|
|
t.set_atlas(atlas)
|
|
|
|
t.margin = Rect2(0, 0, 0, 0)
|
|
|
|
t.region = Rect2(
|
|
|
|
0 if(hits[index.x][index.y] < 0) else textureSize,
|
|
|
|
0,
|
|
|
|
textureSize,
|
|
|
|
textureSize
|
|
|
|
)
|
|
|
|
# Create a new Sprite to house the texture, or use the existing sprite
|
|
|
|
var sprite = Sprite.new()
|
|
|
|
sprite.texture = t
|
|
|
|
sprite.position = Vector2(index.x, index.y) * textureSize + OFFSET
|
|
|
|
$board_blue.add_child(sprite)
|