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

Merge branch 'main' into game-logic

This commit is contained in:
JohnBreaux 2021-11-12 02:10:34 -06:00 committed by GitHub
commit c558578e2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 178 additions and 224 deletions

View File

@ -8,6 +8,7 @@ extents = Vector2( 16, 32 )
[node name="2Ship" type="RigidBody2D"] [node name="2Ship" type="RigidBody2D"]
input_pickable = true input_pickable = true
gravity_scale = 0.0
custom_integrator = true custom_integrator = true
script = ExtResource( 1 ) script = ExtResource( 1 )
@ -16,5 +17,11 @@ position = Vector2( 0, 16 )
texture = ExtResource( 2 ) texture = ExtResource( 2 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2( 0, 16.25 ) position = Vector2( 0.125, 16 )
shape = SubResource( 1 )
[node name="Area2D" type="Area2D" parent="."]
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
position = Vector2( 0.125, 16 )
shape = SubResource( 1 ) shape = SubResource( 1 )

View File

@ -8,6 +8,7 @@ extents = Vector2( 16, 48 )
[node name="3ShipB" type="RigidBody2D"] [node name="3ShipB" type="RigidBody2D"]
input_pickable = true input_pickable = true
gravity_scale = 0.0
custom_integrator = true custom_integrator = true
script = ExtResource( 1 ) script = ExtResource( 1 )
@ -16,3 +17,8 @@ texture = ExtResource( 2 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource( 1 ) shape = SubResource( 1 )
[node name="Area2D" type="Area2D" parent="."]
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
shape = SubResource( 1 )

View File

@ -8,6 +8,7 @@ extents = Vector2( 16, 48 )
[node name="3ShipA" type="RigidBody2D"] [node name="3ShipA" type="RigidBody2D"]
input_pickable = true input_pickable = true
gravity_scale = 0.0
custom_integrator = true custom_integrator = true
script = ExtResource( 1 ) script = ExtResource( 1 )
@ -16,3 +17,8 @@ texture = ExtResource( 2 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource( 1 ) shape = SubResource( 1 )
[node name="Area2D" type="Area2D" parent="."]
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
shape = SubResource( 1 )

View File

@ -8,6 +8,7 @@ extents = Vector2( 16, 64 )
[node name="4Ship" type="RigidBody2D"] [node name="4Ship" type="RigidBody2D"]
input_pickable = true input_pickable = true
gravity_scale = 0.0
custom_integrator = true custom_integrator = true
script = ExtResource( 1 ) script = ExtResource( 1 )
@ -18,3 +19,9 @@ texture = ExtResource( 2 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2( 1, 15 ) position = Vector2( 1, 15 )
shape = SubResource( 1 ) shape = SubResource( 1 )
[node name="Area2D" type="Area2D" parent="."]
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
position = Vector2( 1, 15 )
shape = SubResource( 1 )

View File

@ -15,4 +15,10 @@ script = ExtResource( 1 )
texture = ExtResource( 2 ) texture = ExtResource( 2 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
visible = false
shape = SubResource( 1 )
[node name="Area2D" type="Area2D" parent="."]
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
shape = SubResource( 1 ) shape = SubResource( 1 )

View File

@ -1,21 +1,13 @@
extends Control extends Control
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
# 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():
if find_next_valid_focus(): find_next_valid_focus().grab_focus() if find_next_valid_focus(): find_next_valid_focus().grab_focus()
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
func _on_Forfeit_pressed(): func _on_Forfeit_pressed():
AudioBus.emit_signal("button_clicked") AudioBus.emit_signal("button_clicked")
queue_free(); queue_free();
MessageBus.emit_signal("change_scene", "Title") MessageBus.emit_signal("change_scene", "Title")

View File

@ -1,117 +1,60 @@
extends RigidBody2D extends RigidBody2D
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
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 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 TwoShip_StartingPos var mousePos
var ThreeShipA_StartingPos
var ThreeShipB_StartingPos # Ships are all named starting with their length,
var FourShip_StartingPos # So we cast from string to int, on the ship name, and get the length
var FiveShip_StartingPos onready var ship_length = int(name)
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.
func _ready(): func _ready():
startingPos = position # Sets the position of ships when game is started to the startingPos variable mode = MODE_KINEMATIC
TwoShip_StartingPos = get_parent().get_node("2Ship").position # Snap the ships to the grid, so the engine won't get mad when they're moved away from the starting position every frame
ThreeShipA_StartingPos = get_parent().get_node("3ShipA").position position = (position - offset).snapped(Vector2(32, 32)) + offset
ThreeShipB_StartingPos = get_parent().get_node("3ShipB").position startingPos = position
FourShip_StartingPos = get_parent().get_node("4Ship").position var _trash
FiveShip_StartingPos = get_parent().get_node("5Ship").position # Connect to my own signals, and not the signals of my fellowships
# PLEASE don't parameterize; there's no way to tell these signals apart with the args the engine provides.
_trash = connect("body_entered", self, "ship_stacked")
_trash = connect("body_exited", self, "ship_unstacked")
# Radius of the "knob" on the center of each ship
var click_radius = 16 var click_radius = 16
var orient = 0;
func _input(event): func _input(event):
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT: if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
if (event.position - position).length() < click_radius: if (event.position - position).length() < click_radius:
if not held and event.pressed: if not held and event.pressed:
AudioBus.emit_signal("button_clicked") AudioBus.emit_signal("button_clicked")
held = true; pickup()
if held and not event.pressed: if held and not event.pressed:
held = false;
# If ship is placed on board, snap to board drop()
if (position.x > 17.4 and position.x < 335.5) and (position.y > 20.2 and position.y < 335.5): # Convert the center of this piece to board-space
position = position.snapped(Vector2(32, 32)) + Vector2(4, 4) # Position snapping on board var bs_position = world_to_board_space(position)
else: # If not placed on board, ships are placed back at the starting position # 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 (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:
position = originalPos collision = true
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
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
if event is InputEventMouseMotion and held: if event is InputEventMouseMotion and held:
if snapOriginalPos == false: if snapOriginalPos == false:
originalPos = position originalPos = position
snapOriginalPos = true snapOriginalPos = true
position = event.position; # Save the moise position, so _physics_process can use it
mousePos = event.position;
if event.is_action_pressed("ui_rotate"): if event.is_action_pressed("ui_rotate"):
if held: if held:
@ -124,141 +67,128 @@ func _input(event):
if position == originalPos: if position == originalPos:
return return
elif(event.position - position).length() < click_radius: elif(event.position - position).length() < click_radius:
if vertical == true: # Rotation has been moved to _physics_process,
rotate(-PI/2) # as per recommendation of godot_engine.org
vertical = false #rotation = (-PI/2)
else: vertical = not vertical
rotate(PI/2)
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):
# 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
# 3-Ship A # Offset from the corner of the screen to the corner of the board
if (get_parent().get_node("3ShipA").rotation_degrees == 0): const offset = Vector2(36, 36)
if (get_parent().get_node("3ShipA").position.y > 308): # The previous verticality of the object
get_parent().get_node("3ShipA").position.y -= 32 var prev_vertical = true
if (get_parent().get_node("3ShipA").position.y < 52): # The previous position of the object
get_parent().get_node("3ShipA").position.y += 32 var prev_position = Vector2(0,0)
if (get_parent().get_node("3ShipA").rotation_degrees == -90): # The number of frames after an object is released to check for physics updates
if (get_parent().get_node("3ShipA").position.x > 308): var released = 0
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 # _physics_process: called in place of the physics processor
if (get_parent().get_node("4Ship").rotation_degrees == 0): # Checks collision and updates the position and rotation of the object
if (get_parent().get_node("4Ship").position.y > 308.8): func _physics_process(_delta):
get_parent().get_node("4Ship").position.y -= 64; # calculate whether the piece has been rotated or moved
elif (get_parent().get_node("4Ship").position.y > 276.8): var rotated = prev_vertical != vertical
get_parent().get_node("4Ship").position.y -= 32; var moved = prev_position != position
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):
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):
get_parent().get_node("4Ship").position.x -= 32
if (get_parent().get_node("4Ship").position.x < 52):
get_parent().get_node("4Ship").position.x += 32
# 5-Ship # If the piece is held, move it to the mouse:
if (get_parent().get_node("5Ship").rotation_degrees == 0): if held and mousePos and mousePos != position:
if (get_parent().get_node("5Ship").position.y > 308.8): position = mousePos
get_parent().get_node("5Ship").position.y -= 64 mousePos = null
elif (get_parent().get_node("5Ship").position.y > 276.8):
get_parent().get_node("5Ship").position.y -= 32
if (get_parent().get_node("5Ship").position.y < 52): # Snap it to the grid if not held (and previously moved)
get_parent().get_node("5Ship").position.y += 64 if not held and moved:
elif (get_parent().get_node("5Ship").position.y < 84.8): position = (position - offset).snapped(Vector2(32, 32)) + offset
get_parent().get_node("5Ship").position.y += 32 prev_position = position
if (get_parent().get_node("5Ship").rotation_degrees == -90): # Check collisions after released, reset if colliding
if (get_parent().get_node("5Ship").position.x > 308.8): if collision and released:
get_parent().get_node("5Ship").position.x -= 64 position = startingPos
elif (get_parent().get_node("5Ship").position.x > 276.8):
get_parent().get_node("5Ship").position.x -= 32
if (get_parent().get_node("5Ship").position.x < 52): # If it's been moved or rotated, snap it to the board
get_parent().get_node("5Ship").position.x += 64 if released or rotated:
elif (get_parent().get_node("5Ship").position.x < 84.8): # check whether the ends of the piece are within the board
get_parent().get_node("5Ship").position.x += 32 var linear_move = check_extents(position, vertical, ship_length)
# if not, move them back inside
if linear_move:
if vertical:
position += 32 * Vector2(0, linear_move)
else:
position += 32 * Vector2(linear_move, 0)
pass
# Rotate if the piece needs to be rotated
if rotated:
prev_vertical = vertical
rotation = -PI/2 * int(not vertical) # int(true) == 1, int(false) == 0
# Count down the number of physics timesteps left until the piece can stop processing
if released > 0:
released = released - 1
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
func pickup(): func pickup():
if held: if not held:
return raise() # Render this ship on top of other ships
mode = RigidBody2D.MODE_STATIC held = true # mark it as held
held = true collision = false # Assume we're not colliding by default
func drop(impulse=Vector2.ZERO): func drop():
if held: if held:
mode = RigidBody2D.MODE_RIGID released = 1 # mark the node as released
apply_central_impulse(impulse) held = false # mark the node as not held
held = false
snapOriginalPos = false snapOriginalPos = false
func checkOriginalPos(): # Checks whether the position of the ship is the stating position of the ship func checkOriginalPos():
if position == startingPos: return position == startingPos
return true
else:
return false
func _on_Clear_pressed():
get_parent().get_node("2Ship").position = TwoShip_StartingPos
get_parent().get_node("2Ship").rotation = 0
get_parent().get_node("3ShipA").position = ThreeShipA_StartingPos
get_parent().get_node("3ShipA").rotation = 0
get_parent().get_node("3ShipB").position = ThreeShipB_StartingPos
get_parent().get_node("3ShipB").rotation = 0
get_parent().get_node("4Ship").position = FourShip_StartingPos
get_parent().get_node("4Ship").rotation = 0
get_parent().get_node("5Ship").position = FiveShip_StartingPos
get_parent().get_node("5Ship").rotation = 0
vertical = true
func check_all_ships_are_on_board(): # Called when *this* ship collides with another ship
if !((get_parent().get_node("2Ship").position.x > 17.4 and get_parent().get_node("2Ship").position.x < 335.5) and (get_parent().get_node("2Ship").position.y > 20.2 and get_parent().get_node("2Ship").position.y < 335.5)): func ship_stacked(_body):
return false collision = true
elif !((get_parent().get_node("3ShipA").position.x > 17.4 and get_parent().get_node("3ShipA").position.x < 335.5) and (get_parent().get_node("3ShipA").position.y > 20.2 and get_parent().get_node("3ShipA").position.y < 335.5)): # Called when *this* ship stops colliding with another ship
return false func ship_unstacked(_body):
elif !((get_parent().get_node("3ShipB").position.x > 17.4 and get_parent().get_node("3ShipB").position.x < 335.5) and (get_parent().get_node("3ShipB").position.y > 20.2 and get_parent().get_node("3ShipB").position.y < 335.5)): collision = false
return false
elif !((get_parent().get_node("4Ship").position.x > 17.4 and get_parent().get_node("4Ship").position.x < 335.5) and (get_parent().get_node("4Ship").position.y > 20.2 and get_parent().get_node("4Ship").position.y < 335.5)):
return false
elif !((get_parent().get_node("5Ship").position.x > 17.4 and get_parent().get_node("5Ship").position.x < 335.5) and (get_parent().get_node("5Ship").position.y > 20.2 and get_parent().get_node("5Ship").position.y < 335.5)):
return false
else:
return true
func _on_Confirm_Placement_pressed(): # Calculate the extents (front to back) of the ship and check whether they're on the board
if check_all_ships_are_on_board(): # Returns how many squares to move the ship along its orientation axis (positive or negative)
print("all ships are on board") # Remove this line func check_extents(center, orientation, length):
# Convert ships to board pieces center = world_to_board_space(center) # Convert to board-space (0-10)
print("Center: ", center)
# Calculate the position of the front of the ship
# Orientation is true when the ship is vertical
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 bow < 0:
print("return: ", -bow)
return -bow
# Calculate the position of the rear of the ship
var stern = vectorget(center, orientation) + floor(length / 2)
print("Stern: ", stern)
# If out of bounds, return how much to move the ship by
if stern >= 10:
print("return: ", -(stern - 9))
return -(stern - 9)
print("return: ", 0)
return 0
# Convert the world-space coordinates to positions on the board
func world_to_board_space(vector):
# Do math
var res = (vector - offset) / 32 # Subtract the distance between the screen corner and square (0,0)
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
# index a Vector2 like an array
# Why is this needed?
# So we can discard the unimportant axis! (a ship is always 1 unit wide!)
func vectorget(vector, axis):
if axis:
return vector.y
else: else:
print("You can't confirm placement until all ships are placed") return vector.x
get_parent().get_node("AcceptDialog").popup()
#OS.alert("You can't confirm placement until all ships are placed", "Confirm Placement")