2021-10-21 06:24:27 +00:00
|
|
|
extends RigidBody2D
|
2021-10-21 03:33:14 +00:00
|
|
|
|
|
|
|
# Declare member variables here. Examples:
|
|
|
|
# var a = 2
|
|
|
|
# var b = "text"
|
2021-10-21 06:24:27 +00:00
|
|
|
var held = false
|
2021-11-11 21:08:14 +00:00
|
|
|
var originalPos # Position before moving the ship
|
|
|
|
var snapOriginalPos = false # Gets the original position
|
|
|
|
var vertical = true # Gets ship which is either vertical or horizonal
|
|
|
|
var startingPos # Starting position of ships before being placed
|
2021-10-21 03:33:14 +00:00
|
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
func _ready():
|
2021-11-11 21:08:14 +00:00
|
|
|
startingPos = position # Sets the position of ships when game is started to the startingPos variable
|
2021-11-01 03:48:20 +00:00
|
|
|
pass
|
2021-10-21 03:33:14 +00:00
|
|
|
|
2021-10-21 08:18:31 +00:00
|
|
|
var click_radius = 16
|
2021-10-21 03:33:14 +00:00
|
|
|
var orient = 0;
|
|
|
|
|
|
|
|
func _input(event):
|
2021-11-08 02:53:08 +00:00
|
|
|
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
|
2021-10-21 06:24:27 +00:00
|
|
|
if (event.position - position).length() < click_radius:
|
|
|
|
if not held and event.pressed:
|
2021-11-08 02:53:08 +00:00
|
|
|
AudioBus.emit_signal("button_clicked")
|
2021-10-21 06:24:27 +00:00
|
|
|
held = true;
|
2021-10-21 03:33:14 +00:00
|
|
|
|
2021-10-21 06:24:27 +00:00
|
|
|
if held and not event.pressed:
|
|
|
|
held = false;
|
2021-11-11 21:08:14 +00:00
|
|
|
# If ship is placed on board, snap to board
|
2021-11-02 01:44:44 +00:00
|
|
|
if (position.x > 17.4 and position.x < 335.5) and (position.y > 20.2 and position.y < 335.5):
|
2021-11-11 21:08:14 +00:00
|
|
|
position = position.snapped(Vector2(32, 32)) + Vector2(4, 4) # Position snapping on board
|
|
|
|
else: # If not placed on board, ships are placed back at the starting position
|
2021-11-01 04:05:22 +00:00
|
|
|
if originalPos != null:
|
|
|
|
position = originalPos
|
2021-11-01 05:11:54 +00:00
|
|
|
rotation = 0
|
|
|
|
vertical = true
|
|
|
|
|
2021-11-11 21:08:14 +00:00
|
|
|
# Lines 40-98 make sure that the ships placed on the board are not able to hang off the board
|
|
|
|
|
2021-11-01 05:11:54 +00:00
|
|
|
# 2-Ship
|
|
|
|
if (get_parent().get_node("2Ship").rotation_degrees == 0):
|
|
|
|
if (get_parent().get_node("2Ship").position.y > 308):
|
|
|
|
position = originalPos
|
|
|
|
rotation = 0
|
|
|
|
vertical = true
|
|
|
|
if (get_parent().get_node("2Ship").rotation_degrees == -90):
|
|
|
|
if (get_parent().get_node("2Ship").position.x > 308):
|
|
|
|
position = originalPos
|
|
|
|
rotation = 0
|
|
|
|
vertical = true
|
|
|
|
|
|
|
|
# 3-Ship A
|
|
|
|
if (get_parent().get_node("3ShipA").rotation_degrees == 0):
|
|
|
|
if (get_parent().get_node("3ShipA").position.y > 308) or (get_parent().get_node("3ShipA").position.y < 52):
|
|
|
|
position = originalPos
|
|
|
|
rotation = 0
|
|
|
|
vertical = true
|
|
|
|
if (get_parent().get_node("3ShipA").rotation_degrees == -90):
|
|
|
|
if (get_parent().get_node("3ShipA").position.x > 308) or (get_parent().get_node("3ShipA").position.x < 52):
|
|
|
|
position = originalPos
|
|
|
|
rotation = 0
|
|
|
|
vertical = true
|
|
|
|
|
|
|
|
# 3-Ship B
|
|
|
|
if (get_parent().get_node("3ShipB").rotation_degrees == 0):
|
|
|
|
if (get_parent().get_node("3ShipB").position.y > 308) or (get_parent().get_node("3ShipB").position.y < 52):
|
|
|
|
position = originalPos
|
|
|
|
rotation = 0
|
|
|
|
vertical = true
|
|
|
|
if (get_parent().get_node("3ShipB").rotation_degrees == -90):
|
|
|
|
if (get_parent().get_node("3ShipB").position.x > 308) or (get_parent().get_node("3ShipB").position.x < 52):
|
|
|
|
position = originalPos
|
|
|
|
rotation = 0
|
|
|
|
vertical = true
|
|
|
|
|
|
|
|
# 4-Ship
|
|
|
|
if (get_parent().get_node("4Ship").rotation_degrees == 0):
|
|
|
|
if (get_parent().get_node("4Ship").position.y > 276.8) or (get_parent().get_node("4Ship").position.y < 52):
|
|
|
|
position = originalPos
|
|
|
|
rotation = 0
|
|
|
|
vertical = true
|
|
|
|
if (get_parent().get_node("4Ship").rotation_degrees == -90):
|
|
|
|
if (get_parent().get_node("4Ship").position.x > 276.8) or (get_parent().get_node("4Ship").position.x < 52):
|
|
|
|
position = originalPos
|
|
|
|
rotation = 0
|
|
|
|
vertical = true
|
|
|
|
|
|
|
|
# 5-Ship
|
|
|
|
if (get_parent().get_node("5Ship").rotation_degrees == 0):
|
|
|
|
if (get_parent().get_node("5Ship").position.y > 276.8) or (get_parent().get_node("5Ship").position.y < 84.8):
|
|
|
|
position = originalPos
|
|
|
|
rotation = 0
|
|
|
|
vertical = true
|
|
|
|
if (get_parent().get_node("5Ship").rotation_degrees == -90):
|
|
|
|
if (get_parent().get_node("5Ship").position.x > 276.8) or (get_parent().get_node("5Ship").position.x < 84.8):
|
|
|
|
position = originalPos
|
|
|
|
rotation = 0
|
|
|
|
vertical = true
|
|
|
|
|
2021-10-21 03:33:14 +00:00
|
|
|
|
2021-10-21 06:24:27 +00:00
|
|
|
if event is InputEventMouseMotion and held:
|
2021-11-01 03:48:20 +00:00
|
|
|
if snapOriginalPos == false:
|
|
|
|
originalPos = position
|
|
|
|
snapOriginalPos = true
|
2021-10-21 03:33:14 +00:00
|
|
|
position = event.position;
|
|
|
|
|
|
|
|
if event.is_action_pressed("ui_rotate"):
|
2021-11-07 05:06:16 +00:00
|
|
|
if held:
|
|
|
|
return
|
2021-11-02 01:40:31 +00:00
|
|
|
if checkOriginalPos():
|
|
|
|
return
|
|
|
|
else:
|
2021-11-08 02:53:08 +00:00
|
|
|
AudioBus.emit_signal("button_clicked")
|
2021-11-02 01:40:31 +00:00
|
|
|
if originalPos == null:
|
|
|
|
if position == originalPos:
|
|
|
|
return
|
|
|
|
elif(event.position - position).length() < click_radius:
|
|
|
|
if vertical == true:
|
|
|
|
rotate(-PI/2)
|
|
|
|
vertical = false
|
|
|
|
else:
|
|
|
|
rotate(PI/2)
|
|
|
|
vertical = true
|
2021-11-01 05:37:40 +00:00
|
|
|
|
2021-11-11 21:08:14 +00:00
|
|
|
# Lines 126-196 move the ship back accordingly after being rotated to make sure that the ships do not hang off the board
|
2021-11-02 01:44:44 +00:00
|
|
|
if(position.x > 17.4 and position.x < 335.5) and (position.y > 20.2 and position.y < 335.5):
|
2021-11-01 20:22:03 +00:00
|
|
|
# 2-Ship
|
|
|
|
if (get_parent().get_node("2Ship").rotation_degrees == 0):
|
|
|
|
if (get_parent().get_node("2Ship").position.y > 308):
|
|
|
|
get_parent().get_node("2Ship").position.y -= 32
|
|
|
|
if (get_parent().get_node("2Ship").rotation_degrees == -90):
|
|
|
|
if (get_parent().get_node("2Ship").position.x > 308):
|
|
|
|
get_parent().get_node("2Ship").position.x -= 32
|
2021-11-01 05:37:40 +00:00
|
|
|
|
2021-11-01 20:22:03 +00:00
|
|
|
# 3-Ship A
|
|
|
|
if (get_parent().get_node("3ShipA").rotation_degrees == 0):
|
|
|
|
if (get_parent().get_node("3ShipA").position.y > 308):
|
|
|
|
get_parent().get_node("3ShipA").position.y -= 32
|
|
|
|
if (get_parent().get_node("3ShipA").position.y < 52):
|
|
|
|
get_parent().get_node("3ShipA").position.y += 32
|
|
|
|
if (get_parent().get_node("3ShipA").rotation_degrees == -90):
|
|
|
|
if (get_parent().get_node("3ShipA").position.x > 308):
|
|
|
|
get_parent().get_node("3ShipA").position.x -= 32
|
|
|
|
if (get_parent().get_node("3ShipA").position.x < 52):
|
|
|
|
get_parent().get_node("3ShipA").position.x += 32
|
|
|
|
|
|
|
|
# 3-Ship B
|
|
|
|
if (get_parent().get_node("3ShipB").rotation_degrees == 0):
|
|
|
|
if (get_parent().get_node("3ShipB").position.y > 308):
|
|
|
|
get_parent().get_node("3ShipB").position.y -= 32
|
|
|
|
if (get_parent().get_node("3ShipB").position.y < 52):
|
|
|
|
get_parent().get_node("3ShipB").position.y += 32
|
|
|
|
if (get_parent().get_node("3ShipB").rotation_degrees == -90):
|
|
|
|
if (get_parent().get_node("3ShipB").position.x > 308):
|
|
|
|
get_parent().get_node("3ShipB").position.x -= 32
|
|
|
|
if (get_parent().get_node("3ShipB").position.x < 52):
|
|
|
|
get_parent().get_node("3ShipB").position.x += 32
|
|
|
|
|
|
|
|
# 4-Ship
|
|
|
|
if (get_parent().get_node("4Ship").rotation_degrees == 0):
|
2021-11-02 01:40:31 +00:00
|
|
|
if (get_parent().get_node("4Ship").position.y > 308.8):
|
|
|
|
get_parent().get_node("4Ship").position.y -= 64;
|
|
|
|
elif (get_parent().get_node("4Ship").position.y > 276.8):
|
|
|
|
get_parent().get_node("4Ship").position.y -= 32;
|
2021-11-01 20:22:03 +00:00
|
|
|
if (get_parent().get_node("4Ship").position.y < 52):
|
|
|
|
get_parent().get_node("4Ship").position.y += 32
|
|
|
|
if (get_parent().get_node("4Ship").rotation_degrees == -90):
|
2021-11-02 01:40:31 +00:00
|
|
|
if (get_parent().get_node("4Ship").position.x > 308.8):
|
|
|
|
get_parent().get_node("4Ship").position.x -= 64
|
|
|
|
elif (get_parent().get_node("4Ship").position.x > 276.8):
|
2021-11-01 20:10:12 +00:00
|
|
|
get_parent().get_node("4Ship").position.x -= 32
|
2021-11-01 20:22:03 +00:00
|
|
|
if (get_parent().get_node("4Ship").position.x < 52):
|
|
|
|
get_parent().get_node("4Ship").position.x += 32
|
|
|
|
|
|
|
|
# 5-Ship
|
|
|
|
if (get_parent().get_node("5Ship").rotation_degrees == 0):
|
2021-11-02 01:40:31 +00:00
|
|
|
if (get_parent().get_node("5Ship").position.y > 308.8):
|
|
|
|
get_parent().get_node("5Ship").position.y -= 64
|
|
|
|
elif (get_parent().get_node("5Ship").position.y > 276.8):
|
2021-11-01 20:10:12 +00:00
|
|
|
get_parent().get_node("5Ship").position.y -= 32
|
2021-11-02 01:40:31 +00:00
|
|
|
|
|
|
|
if (get_parent().get_node("5Ship").position.y < 52):
|
|
|
|
get_parent().get_node("5Ship").position.y += 64
|
|
|
|
elif (get_parent().get_node("5Ship").position.y < 84.8):
|
2021-11-01 20:10:12 +00:00
|
|
|
get_parent().get_node("5Ship").position.y += 32
|
2021-11-02 01:40:31 +00:00
|
|
|
|
2021-11-01 20:22:03 +00:00
|
|
|
if (get_parent().get_node("5Ship").rotation_degrees == -90):
|
2021-11-02 01:40:31 +00:00
|
|
|
if (get_parent().get_node("5Ship").position.x > 308.8):
|
|
|
|
get_parent().get_node("5Ship").position.x -= 64
|
|
|
|
elif (get_parent().get_node("5Ship").position.x > 276.8):
|
2021-11-01 20:10:12 +00:00
|
|
|
get_parent().get_node("5Ship").position.x -= 32
|
2021-11-02 01:40:31 +00:00
|
|
|
|
|
|
|
if (get_parent().get_node("5Ship").position.x < 52):
|
|
|
|
get_parent().get_node("5Ship").position.x += 64
|
|
|
|
elif (get_parent().get_node("5Ship").position.x < 84.8):
|
2021-11-01 20:10:12 +00:00
|
|
|
get_parent().get_node("5Ship").position.x += 32
|
2021-11-01 05:37:40 +00:00
|
|
|
|
2021-10-21 03:33:14 +00:00
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
|
|
#func _process(delta):
|
|
|
|
# pass
|
2021-10-21 06:24:27 +00:00
|
|
|
|
|
|
|
func pickup():
|
|
|
|
if held:
|
|
|
|
return
|
|
|
|
mode = RigidBody2D.MODE_STATIC
|
|
|
|
held = true
|
|
|
|
|
|
|
|
func drop(impulse=Vector2.ZERO):
|
|
|
|
if held:
|
|
|
|
mode = RigidBody2D.MODE_RIGID
|
|
|
|
apply_central_impulse(impulse)
|
|
|
|
held = false
|
2021-11-01 03:48:20 +00:00
|
|
|
snapOriginalPos = false
|
2021-11-02 01:40:31 +00:00
|
|
|
|
2021-11-11 21:08:14 +00:00
|
|
|
func checkOriginalPos(): # Checks whether the position of the ship is the stating position of the ship
|
2021-11-02 01:40:31 +00:00
|
|
|
if position == startingPos:
|
|
|
|
return true
|
|
|
|
else:
|
|
|
|
return false
|