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

Improve hitbox leniency, and don't allow ships to rotate into each other.

This commit is contained in:
John 2021-11-12 03:33:31 -06:00
parent 304389138f
commit 9a3061c16e
6 changed files with 23 additions and 19 deletions

View File

@ -18,12 +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 ) scale = Vector2( 0.5, 0.75 )
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 ) scale = Vector2( 0.5, 0.75 )
shape = SubResource( 1 ) shape = SubResource( 1 )

View File

@ -22,5 +22,5 @@ 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 ) scale = Vector2( 0.5, 0.833 )
shape = SubResource( 1 ) shape = SubResource( 1 )

View File

@ -16,11 +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 ) scale = Vector2( 0.5, 0.875 )
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 ) scale = Vector2( 0.5, 0.833 )
shape = SubResource( 1 ) shape = SubResource( 1 )

View File

@ -18,12 +18,12 @@ texture = ExtResource( 2 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2( 0, 16 ) position = Vector2( 0, 16 )
scale = Vector2( 0.9, 0.975 ) scale = Vector2( 0.5, 0.875 )
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, 16 ) position = Vector2( 0, 16 )
scale = Vector2( 0.9, 0.975 ) scale = Vector2( 0.5, 0.875 )
shape = SubResource( 1 ) shape = SubResource( 1 )

View File

@ -15,11 +15,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.98 ) scale = Vector2( 0.5, 0.9 )
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 ) scale = Vector2( 0.5, 0.9 )
shape = SubResource( 1 ) shape = SubResource( 1 )

View File

@ -4,7 +4,7 @@ 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 mousePos # Stores the last known mouse position so the physics engine can use it
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
@ -18,6 +18,8 @@ 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():
mode = MODE_KINEMATIC mode = MODE_KINEMATIC
contacts_reported = 64
set_use_custom_integrator(true)
# Snap the ships to the grid, so the engine won't get mad when they're moved away from the starting position every frame # Snap the ships to the grid, so the engine won't get mad when they're moved away from the starting position every frame
position = (position - offset).snapped(Vector2(32, 32)) + offset position = (position - offset).snapped(Vector2(32, 32)) + offset
startingPos = position startingPos = position
@ -62,7 +64,9 @@ func _input(event):
# 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
vertical = not vertical vertical = not vertical
# It takes 3 physics ticks for the rotation to update the hitboxes
# Determined imperically through testing, may work differently on different machines(?)
released = 3
# Offset from the corner of the screen to the corner of the board # Offset from the corner of the screen to the corner of the board
const offset = Vector2(36, 36) const offset = Vector2(36, 36)
@ -73,9 +77,6 @@ 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
@ -83,17 +84,17 @@ func _physics_process(_delta):
# calculate whether the piece has been rotated or moved # calculate whether the piece has been rotated or moved
var rotated = prev_vertical != vertical var rotated = prev_vertical != vertical
var moved = prev_position != position var moved = prev_position != position
# If the piece is held, move it to the mouse: # If the piece is held, move it to the mouse:
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
# 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
@ -105,17 +106,18 @@ func _physics_process(_delta):
else: else:
position += 32 * Vector2(linear_move, 0) position += 32 * Vector2(linear_move, 0)
pass pass
# Check collisions after released, reset if colliding # Check collisions after released, reset if colliding
if collision and released: if collision and released:
position = startingPos position = startingPos
rotation = 0 rotation = 0
vertical = true 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
rotation = -PI/2 * int(not vertical) # int(true) == 1, int(false) == 0 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 # Count down the number of physics timesteps left until the piece can stop processing
if released > 0: if released > 0:
released = released - 1 released = released - 1
@ -139,9 +141,11 @@ func checkOriginalPos():
# Called when *this* ship collides with another ship # Called when *this* ship collides with another ship
func ship_stacked(_body): func ship_stacked(_body):
print("stacked")
collision = true collision = true
# Called when *this* ship stops colliding with another ship # Called when *this* ship stops colliding with another ship
func ship_unstacked(_body): func ship_unstacked(_body):
print("unstacked")
collision = false collision = false
# Calculate the extents (front to back) of the ship and check whether they're on the board # Calculate the extents (front to back) of the ship and check whether they're on the board