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

Commented my code

Deals with ship placement on board and ship rotation on board
This commit is contained in:
tommy-l-ngo 2021-11-11 15:08:14 -06:00
parent eaa1617b9a
commit f9592f425e

View File

@ -4,14 +4,14 @@ extends RigidBody2D
# var a = 2 # var a = 2
# var b = "text" # var b = "text"
var held = false var held = false
var originalPos var originalPos # Position before moving the ship
var snapOriginalPos = false var snapOriginalPos = false # Gets the original position
var vertical = true var vertical = true # Gets ship which is either vertical or horizonal
var startingPos var startingPos # Starting position of ships before being placed
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
startingPos = position startingPos = position # Sets the position of ships when game is started to the startingPos variable
pass pass
var click_radius = 16 var click_radius = 16
@ -26,14 +26,17 @@ func _input(event):
if held and not event.pressed: if held and not event.pressed:
held = false; held = false;
# If ship is placed on board, snap to board
if (position.x > 17.4 and position.x < 335.5) and (position.y > 20.2 and position.y < 335.5): if (position.x > 17.4 and position.x < 335.5) and (position.y > 20.2 and position.y < 335.5):
position = position.snapped(Vector2(32, 32)) + Vector2(4, 4) position = position.snapped(Vector2(32, 32)) + Vector2(4, 4) # Position snapping on board
else: else: # If not placed on board, ships are placed back at the starting position
if originalPos != null: if originalPos != null:
position = originalPos position = originalPos
rotation = 0 rotation = 0
vertical = true vertical = true
# Lines 40-98 make sure that the ships placed on the board are not able to hang off the board
# 2-Ship # 2-Ship
if (get_parent().get_node("2Ship").rotation_degrees == 0): if (get_parent().get_node("2Ship").rotation_degrees == 0):
if (get_parent().get_node("2Ship").position.y > 308): if (get_parent().get_node("2Ship").position.y > 308):
@ -119,6 +122,7 @@ func _input(event):
rotate(PI/2) rotate(PI/2)
vertical = true vertical = true
# Lines 126-196 move the ship back accordingly after being rotated to make sure that the ships do not hang off the board
if(position.x > 17.4 and position.x < 335.5) and (position.y > 20.2 and position.y < 335.5): if(position.x > 17.4 and position.x < 335.5) and (position.y > 20.2 and position.y < 335.5):
# 2-Ship # 2-Ship
if (get_parent().get_node("2Ship").rotation_degrees == 0): if (get_parent().get_node("2Ship").rotation_degrees == 0):
@ -208,7 +212,7 @@ func drop(impulse=Vector2.ZERO):
held = false held = false
snapOriginalPos = false snapOriginalPos = false
func checkOriginalPos(): func checkOriginalPos(): # Checks whether the position of the ship is the stating position of the ship
if position == startingPos: if position == startingPos:
return true return true
else: else: