mirror of
https://github.com/JohnBreaux/Boat-Battle.git
synced 2024-11-15 05:25:57 +00:00
I accidentally the buttons + Fix some hitbox mistakes.
This commit is contained in:
parent
c558578e2e
commit
304389138f
@ -133,5 +133,5 @@ __meta__ = {
|
|||||||
|
|
||||||
[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="2Ship" method="_on_Confirm_Placement_pressed"]
|
[connection signal="pressed" from="Confirm Placement" to="." method="_on_Confirm_Placement_pressed"]
|
||||||
[connection signal="pressed" from="Clear" to="2Ship" method="_on_Clear_pressed"]
|
[connection signal="pressed" from="Clear" to="." method="_on_Clear_pressed"]
|
||||||
|
@ -18,10 +18,12 @@ texture = ExtResource( 2 )
|
|||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||||
position = Vector2( 0.125, 16 )
|
position = Vector2( 0.125, 16 )
|
||||||
|
scale = Vector2( 0.9, 0.95 )
|
||||||
shape = SubResource( 1 )
|
shape = SubResource( 1 )
|
||||||
|
|
||||||
[node name="Area2D" type="Area2D" parent="."]
|
[node name="Area2D" type="Area2D" parent="."]
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
|
||||||
position = Vector2( 0.125, 16 )
|
position = Vector2( 0.125, 16 )
|
||||||
|
scale = Vector2( 0.9, 0.95 )
|
||||||
shape = SubResource( 1 )
|
shape = SubResource( 1 )
|
||||||
|
@ -16,9 +16,11 @@ script = ExtResource( 1 )
|
|||||||
texture = ExtResource( 2 )
|
texture = ExtResource( 2 )
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||||
|
scale = Vector2( 0.9, 0.966 )
|
||||||
shape = SubResource( 1 )
|
shape = SubResource( 1 )
|
||||||
|
|
||||||
[node name="Area2D" type="Area2D" parent="."]
|
[node name="Area2D" type="Area2D" parent="."]
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
|
||||||
|
scale = Vector2( 0.9, 0.966 )
|
||||||
shape = SubResource( 1 )
|
shape = SubResource( 1 )
|
||||||
|
@ -16,9 +16,11 @@ script = ExtResource( 1 )
|
|||||||
texture = ExtResource( 2 )
|
texture = ExtResource( 2 )
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||||
|
scale = Vector2( 0.9, 0.966 )
|
||||||
shape = SubResource( 1 )
|
shape = SubResource( 1 )
|
||||||
|
|
||||||
[node name="Area2D" type="Area2D" parent="."]
|
[node name="Area2D" type="Area2D" parent="."]
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
|
||||||
|
scale = Vector2( 0.9, 0.966 )
|
||||||
shape = SubResource( 1 )
|
shape = SubResource( 1 )
|
||||||
|
@ -17,11 +17,13 @@ position = Vector2( 0, 16 )
|
|||||||
texture = ExtResource( 2 )
|
texture = ExtResource( 2 )
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||||
position = Vector2( 1, 15 )
|
position = Vector2( 0, 16 )
|
||||||
|
scale = Vector2( 0.9, 0.975 )
|
||||||
shape = SubResource( 1 )
|
shape = SubResource( 1 )
|
||||||
|
|
||||||
[node name="Area2D" type="Area2D" parent="."]
|
[node name="Area2D" type="Area2D" parent="."]
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
|
||||||
position = Vector2( 1, 15 )
|
position = Vector2( 0, 16 )
|
||||||
|
scale = Vector2( 0.9, 0.975 )
|
||||||
shape = SubResource( 1 )
|
shape = SubResource( 1 )
|
||||||
|
@ -15,10 +15,11 @@ script = ExtResource( 1 )
|
|||||||
texture = ExtResource( 2 )
|
texture = ExtResource( 2 )
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||||
visible = false
|
scale = Vector2( 0.9, 0.98 )
|
||||||
shape = SubResource( 1 )
|
shape = SubResource( 1 )
|
||||||
|
|
||||||
[node name="Area2D" type="Area2D" parent="."]
|
[node name="Area2D" type="Area2D" parent="."]
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
|
||||||
|
scale = Vector2( 0.9, 0.98 )
|
||||||
shape = SubResource( 1 )
|
shape = SubResource( 1 )
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
extends Control
|
extends Control
|
||||||
|
|
||||||
|
onready var Ships = ["2Ship", "3ShipA", "3ShipB", "4Ship", "5Ship"]
|
||||||
|
|
||||||
|
|
||||||
# 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():
|
||||||
@ -11,3 +13,20 @@ func _on_Forfeit_pressed():
|
|||||||
queue_free();
|
queue_free();
|
||||||
MessageBus.emit_signal("change_scene", "Title")
|
MessageBus.emit_signal("change_scene", "Title")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func _on_Confirm_Placement_pressed():
|
||||||
|
var valid = true
|
||||||
|
for ship in Ships:
|
||||||
|
# validate_placement returns the x-axis distance from the board
|
||||||
|
# if this is more than zero, the ship is invalid
|
||||||
|
if get_node(ship).validate_placement():
|
||||||
|
valid = false
|
||||||
|
print ("Placement: ", valid)
|
||||||
|
return valid # Replace with function body.
|
||||||
|
|
||||||
|
|
||||||
|
func _on_Clear_pressed():
|
||||||
|
for ship in Ships:
|
||||||
|
get_node(ship).clear()
|
||||||
|
pass # Replace with function body.
|
||||||
|
@ -4,14 +4,15 @@ extends RigidBody2D
|
|||||||
var held = false
|
var held = false
|
||||||
var originalPos # Position before moving the ship
|
var originalPos # Position before moving the ship
|
||||||
var snapOriginalPos = false # Gets the original position
|
var snapOriginalPos = false # Gets the original position
|
||||||
|
var mousePos
|
||||||
var vertical = true # Gets ship which is either vertical or horizonal
|
var vertical = true # Gets ship which is either vertical or horizonal
|
||||||
var startingPos # Starting position of ships before being placed
|
var startingPos # Starting position of ships before being placed
|
||||||
var mousePos
|
|
||||||
|
|
||||||
# Ships are all named starting with their length,
|
# Ships are all named starting with their length,
|
||||||
# So we cast from string to int, on the ship name, and get the length
|
# So we cast from string to int, on the ship name, and get the length
|
||||||
onready var ship_length = int(name)
|
onready var ship_length = int(name)
|
||||||
|
|
||||||
|
# This is set when we're colliding with something
|
||||||
var collision = false
|
var collision = false
|
||||||
|
|
||||||
# Called when the node enters the scene tree for the first time.
|
# Called when the node enters the scene tree for the first time.
|
||||||
@ -37,16 +38,13 @@ func _input(event):
|
|||||||
pickup()
|
pickup()
|
||||||
|
|
||||||
if held and not event.pressed:
|
if held and not event.pressed:
|
||||||
|
|
||||||
drop()
|
drop()
|
||||||
# Convert the center of this piece to board-space
|
# Convert the center of this piece to board-space
|
||||||
var bs_position = world_to_board_space(position)
|
var bs_position = world_to_board_space(position)
|
||||||
# Check whether the piece is within half a board-space of the grid (-0.5, 9.5)
|
# Check whether the piece is within half a board-space of the grid (-0.5, 9.5)
|
||||||
if not (bs_position.x > -0.5 and bs_position.x < 9.5 and bs_position.y > -0.5 and bs_position.y < 9.5):
|
if not (bs_position.x > -0.5 and bs_position.x < 9.5 and bs_position.y > -0.5 and bs_position.y < 9.5):
|
||||||
# if not (position.x > 17.4 and position.x < 335.5) and (position.y > 20.2 and position.y < 335.5):
|
|
||||||
if originalPos != null:
|
if originalPos != null:
|
||||||
collision = true
|
collision = true
|
||||||
rotation = 0
|
|
||||||
vertical = true
|
vertical = true
|
||||||
|
|
||||||
if event is InputEventMouseMotion and held:
|
if event is InputEventMouseMotion and held:
|
||||||
@ -57,19 +55,12 @@ func _input(event):
|
|||||||
mousePos = event.position;
|
mousePos = event.position;
|
||||||
|
|
||||||
if event.is_action_pressed("ui_rotate"):
|
if event.is_action_pressed("ui_rotate"):
|
||||||
if held:
|
if not held and not checkOriginalPos():
|
||||||
return
|
if(event.position - position).length() < click_radius:
|
||||||
if checkOriginalPos():
|
#Play a sound
|
||||||
return
|
AudioBus.emit_signal("button_clicked")
|
||||||
else:
|
|
||||||
AudioBus.emit_signal("button_clicked")
|
|
||||||
if originalPos == null:
|
|
||||||
if position == originalPos:
|
|
||||||
return
|
|
||||||
elif(event.position - position).length() < click_radius:
|
|
||||||
# Rotation has been moved to _physics_process,
|
# Rotation has been moved to _physics_process,
|
||||||
# as per recommendation of godot_engine.org
|
# as per recommendation of godot_engine.org
|
||||||
#rotation = (-PI/2)
|
|
||||||
vertical = not vertical
|
vertical = not vertical
|
||||||
|
|
||||||
|
|
||||||
@ -82,6 +73,9 @@ var prev_position = Vector2(0,0)
|
|||||||
# The number of frames after an object is released to check for physics updates
|
# The number of frames after an object is released to check for physics updates
|
||||||
var released = 0
|
var released = 0
|
||||||
|
|
||||||
|
func _integrate_forces(state):
|
||||||
|
if state.get_contact_count():
|
||||||
|
collision = true
|
||||||
|
|
||||||
# _physics_process: called in place of the physics processor
|
# _physics_process: called in place of the physics processor
|
||||||
# Checks collision and updates the position and rotation of the object
|
# Checks collision and updates the position and rotation of the object
|
||||||
@ -94,16 +88,12 @@ func _physics_process(_delta):
|
|||||||
if held and mousePos and mousePos != position:
|
if held and mousePos and mousePos != position:
|
||||||
position = mousePos
|
position = mousePos
|
||||||
mousePos = null
|
mousePos = null
|
||||||
|
|
||||||
# Snap it to the grid if not held (and previously moved)
|
# Snap it to the grid if not held (and previously moved)
|
||||||
if not held and moved:
|
if not held and moved:
|
||||||
position = (position - offset).snapped(Vector2(32, 32)) + offset
|
position = (position - offset).snapped(Vector2(32, 32)) + offset
|
||||||
prev_position = position
|
prev_position = position
|
||||||
|
|
||||||
# Check collisions after released, reset if colliding
|
|
||||||
if collision and released:
|
|
||||||
position = startingPos
|
|
||||||
|
|
||||||
# If it's been moved or rotated, snap it to the board
|
# If it's been moved or rotated, snap it to the board
|
||||||
if released or rotated:
|
if released or rotated:
|
||||||
# check whether the ends of the piece are within the board
|
# check whether the ends of the piece are within the board
|
||||||
@ -116,6 +106,11 @@ func _physics_process(_delta):
|
|||||||
position += 32 * Vector2(linear_move, 0)
|
position += 32 * Vector2(linear_move, 0)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# Check collisions after released, reset if colliding
|
||||||
|
if collision and released:
|
||||||
|
position = startingPos
|
||||||
|
rotation = 0
|
||||||
|
vertical = true
|
||||||
# Rotate if the piece needs to be rotated
|
# Rotate if the piece needs to be rotated
|
||||||
if rotated:
|
if rotated:
|
||||||
prev_vertical = vertical
|
prev_vertical = vertical
|
||||||
@ -153,29 +148,36 @@ func ship_unstacked(_body):
|
|||||||
# Returns how many squares to move the ship along its orientation axis (positive or negative)
|
# Returns how many squares to move the ship along its orientation axis (positive or negative)
|
||||||
func check_extents(center, orientation, length):
|
func check_extents(center, orientation, length):
|
||||||
center = world_to_board_space(center) # Convert to board-space (0-10)
|
center = world_to_board_space(center) # Convert to board-space (0-10)
|
||||||
print("Center: ", center)
|
|
||||||
# Calculate the position of the front of the ship
|
# Calculate the position of the front of the ship
|
||||||
# Orientation is true when the ship is vertical
|
# Orientation is true when the ship is vertical
|
||||||
var bow = vectorget(center, orientation) - floor((length - 1) / 2)
|
var bow = vectorget(center, orientation) - floor((length - 1) / 2)
|
||||||
print("Bow: ", bow)
|
|
||||||
# if out of bounds, return how much to move the ship by
|
# if out of bounds, return how much to move the ship by
|
||||||
if bow < 0:
|
if bow < 0:
|
||||||
print("return: ", -bow)
|
|
||||||
return -bow
|
return -bow
|
||||||
# Calculate the position of the rear of the ship
|
# Calculate the position of the rear of the ship
|
||||||
var stern = vectorget(center, orientation) + floor(length / 2)
|
var stern = vectorget(center, orientation) + floor(length / 2)
|
||||||
print("Stern: ", stern)
|
|
||||||
# If out of bounds, return how much to move the ship by
|
# If out of bounds, return how much to move the ship by
|
||||||
if stern >= 10:
|
if stern >= 10:
|
||||||
print("return: ", -(stern - 9))
|
|
||||||
return -(stern - 9)
|
return -(stern - 9)
|
||||||
print("return: ", 0)
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
func validate_placement():
|
||||||
|
# Checks whether the ship's center is on the board.
|
||||||
|
# As long as the ship was moved according to the rules defined in the
|
||||||
|
# _physics_process function, this should be necessary and sufficient
|
||||||
|
# to say the ship is on the board
|
||||||
|
return check_extents(position, false, 1)
|
||||||
|
|
||||||
|
func clear():
|
||||||
|
# ships return home on collision
|
||||||
|
# simulate a collision
|
||||||
|
collision = true
|
||||||
|
released = 1
|
||||||
|
|
||||||
# Convert the world-space coordinates to positions on the board
|
# Convert the world-space coordinates to positions on the board
|
||||||
func world_to_board_space(vector):
|
func world_to_board_space(vector):
|
||||||
# Do math
|
# Do math
|
||||||
var res = (vector - offset) / 32 # Subtract the distance between the screen corner and square (0,0)
|
var res = (vector - offset) / 32 # Basically Fahrenheit/Celcius conversion, but in 2D
|
||||||
return res
|
return res
|
||||||
|
|
||||||
# Inverse of the above function.
|
# Inverse of the above function.
|
||||||
|
Loading…
Reference in New Issue
Block a user