ball now more consistently collides with arbitrary objects and bounces off them appropriately
This commit is contained in:
+35
-9
@@ -1,17 +1,43 @@
|
||||
extends CharacterBody2D
|
||||
|
||||
@export var speed: float = 5
|
||||
const _speed_mult: float = 200
|
||||
var _direction: Vector2 = Vector2(1.0, 0.5)
|
||||
@export var strength: int = 1
|
||||
|
||||
const _speed_mult: float = 20
|
||||
var _direction: Vector2 = Vector2(-1.0, -0.5)
|
||||
var _velocity: Vector2
|
||||
|
||||
var deg: float
|
||||
|
||||
func _ready():
|
||||
_velocity.x = 200#speed * _speed_mult * _direction.x * delta
|
||||
_velocity.y = 250#speed * _speed_mult * _direction.y * delta
|
||||
|
||||
|
||||
func _physics_process(delta):
|
||||
velocity.x = speed * _speed_mult * _direction.x
|
||||
velocity.y = speed * _speed_mult * _direction.y
|
||||
|
||||
move_and_slide()
|
||||
var collide = move_and_collide(_velocity * delta)
|
||||
if collide:
|
||||
deg = rad_to_deg(collide.get_angle())
|
||||
if (deg >= 45 and deg < 135) or (deg >= 225 and deg < 315):
|
||||
_invert_x()
|
||||
if (deg >= 135 and deg < 225) or deg < 45 or deg >= 315:
|
||||
_invert_y()
|
||||
if collide.get_collider() is Block:
|
||||
print(true)
|
||||
collide.get_collider().take_damage(strength)
|
||||
|
||||
func invert_x():
|
||||
_direction.x *= -1
|
||||
func _invert_x():
|
||||
_velocity.x *= -1
|
||||
|
||||
func invert_y():
|
||||
_direction.y *= -1
|
||||
func _invert_y():
|
||||
_velocity.y *= -1
|
||||
|
||||
func has_collided():
|
||||
pass
|
||||
#if $Raycasts/North.is_colliding() or $Raycasts/South.is_colliding():
|
||||
#_invert_y()
|
||||
#print("y")
|
||||
#if $Raycasts/West.is_colliding() or $Raycasts/East.is_colliding():
|
||||
#_invert_x()
|
||||
#print("x")
|
||||
|
||||
Reference in New Issue
Block a user