1
0
mirror of https://github.com/JohnBreaux/Boat-Battle.git synced 2024-11-15 05:25:57 +00:00

Crosshair now snaps into place

This commit is contained in:
hpham474 2021-11-12 18:07:37 -06:00
parent c87a1d031c
commit 421c2af83c
3 changed files with 66 additions and 17 deletions

View File

@ -124,7 +124,18 @@ __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
[node name="AcceptDialog" type="AcceptDialog" parent="."] [node name="Fire" type="Button" parent="."]
visible = false
margin_left = 443.545
margin_top = 264.473
margin_right = 528.545
margin_bottom = 318.473
text = "FIRE"
__meta__ = {
"_edit_use_anchors_": false
}
[node name="PlaceShipDialog" type="AcceptDialog" parent="."]
anchor_left = 0.5 anchor_left = 0.5
anchor_top = 0.5 anchor_top = 0.5
anchor_right = 0.5 anchor_right = 0.5
@ -138,19 +149,15 @@ __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
[node name="Fire" type="Button" parent="."] [node name="FireDialog" type="AcceptDialog" parent="."]
visible = false pause_mode = 2
margin_left = 443.545 margin_right = 83.0
margin_top = 264.473 margin_bottom = 58.0
margin_right = 528.545 dialog_text = "You can't fire outside the board"
margin_bottom = 318.473
text = "FIRE"
__meta__ = {
"_edit_use_anchors_": false
}
[connection signal="pressed" from="VBoxContainer/Forfeit" to="." method="_on_Forfeit_pressed"] [connection signal="pressed" from="VBoxContainer/Forfeit" to="." method="_on_Forfeit_pressed"]
[connection signal="about_to_show" from="ConfirmationDialog" to="." method="_on_ConfirmationDialog_about_to_show"] [connection signal="about_to_show" from="ConfirmationDialog" to="." method="_on_ConfirmationDialog_about_to_show"]
[connection signal="pressed" from="Confirm Placement" to="." method="_on_Confirm_Placement_pressed"] [connection signal="pressed" from="Confirm Placement" to="." method="_on_Confirm_Placement_pressed"]
[connection signal="pressed" from="Clear" to="." method="_on_Clear_pressed"] [connection signal="pressed" from="Clear" to="." method="_on_Clear_pressed"]
[connection signal="pressed" from="Fire" to="." method="_on_Fire_pressed"] [connection signal="pressed" from="Fire" to="." method="_on_Fire_pressed"]
[connection signal="confirmed" from="FireDialog" to="." method="_on_FireDialog_confirmed"]

View File

@ -29,7 +29,7 @@ func _on_Confirm_Placement_pressed():
valid = false valid = false
print ("Placement: ", valid) print ("Placement: ", valid)
if valid == false: if valid == false:
get_node("AcceptDialog").popup() get_node("PlaceShipDialog").popup()
else: else:
#Saves the location of ships and length of ship into an array #Saves the location of ships and length of ship into an array
var shipLocation = [] var shipLocation = []
@ -75,5 +75,17 @@ func _on_Clear_pressed():
func _on_Fire_pressed(): func _on_Fire_pressed():
var crosshair = get_node("Crosshair") var crosshair = get_node("Crosshair")
print("Fire at position: ", crosshair.position) # hides crosshair
crosshair.visible = false
if crosshair.validate_position(crosshair.position) == true:
# fires at position
print("Fire at position: ", crosshair.position)
else:
#if invalid position popup appears
var dialog = get_node("FireDialog")
dialog.popup_centered()
pass # Replace with function body.
func _on_FireDialog_confirmed():
get_node("Crosshair").visible = true
pass # Replace with function body. pass # Replace with function body.

View File

@ -16,14 +16,44 @@ func _physics_process(delta):
position += (get_global_mouse_position() - position)/10 position += (get_global_mouse_position() - position)/10
func _input(event): func _input(event):
#Check if left click is being clicked and the sprite is visible (i.e only checks for inputs after ship positions are confirmed) # Check if left click is being clicked and the sprite is visible (i.e only checks for inputs after ship positions are confirmed)
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and visible == true: if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and visible == true:
#Locks the position of the crosshair with left click # Locks the position of the crosshair with left click
snapped = true if validate_position(position) == true:
# rounds the board position to the nearest integer
snapped = true
position.x = int(round(world_to_board_space(position).x))
position.y = int(round(world_to_board_space(position).y))
position = board_to_world_space(position)
# Check if left click is being clicked and the sprite is visible (i.e only checks for inputs after ship positions are confirmed)
if event is InputEventMouseButton and event.button_index == BUTTON_RIGHT and visible == true: if event is InputEventMouseButton and event.button_index == BUTTON_RIGHT and visible == true:
#Unlocks the position of the crosshair with right click # Unlocks the position of the crosshair with right click
snapped = false snapped = false
func validate_position(vector):
# rounds the board position to the nearest integer
var boardx = int(round(world_to_board_space(vector).x))
var boardy = int(round(world_to_board_space(vector).y))
# Checks if the board position is within bounds of the board
if boardx < 11 and boardx > 0 and boardy < 11 and boardy > 0:
# changes the position of the crosshair
return true
else:
# unlocks the crosshair if not within bounds
return false
# Convert the world-space coordinates to positions on the board
func world_to_board_space(vector):
# Do math
var res = (vector - offset) / 32 # Basically Fahrenheit/Celcius conversion, but in 2D
return res
# Inverse of the above function.
func board_to_world_space(vector):
# Do math
var res = (vector * 32) + offset #Invert the above function
return res #Truncate decimals
# Called every frame. 'delta' is the elapsed time since the previous frame. # Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta): #func _process(delta):
# pass # pass