2024-02-22 06:22:22 +11:00
|
|
|
class_name EightDirectionSprite3D extends Sprite3D
|
2023-11-08 14:28:55 +11:00
|
|
|
|
|
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
2024-02-22 06:22:22 +11:00
|
|
|
var cam: Camera3D = get_viewport().get_camera_3d()
|
2023-11-08 14:28:55 +11:00
|
|
|
if !cam:
|
|
|
|
return
|
2024-02-22 06:22:22 +11:00
|
|
|
var tile_size: int = texture.region.size.x
|
2023-11-08 14:28:55 +11:00
|
|
|
|
|
|
|
#stupid algorithm for dummy game developers
|
2024-02-22 06:22:22 +11:00
|
|
|
var camera_look_dir_3D: Vector3 = cam.global_position.direction_to(global_position).normalized()
|
|
|
|
var a: Vector2 = Vector2(global_transform.basis.z.x, global_transform.basis.z.z).normalized()
|
|
|
|
var b: Vector2 = Vector2(camera_look_dir_3D.x, camera_look_dir_3D.z).normalized()
|
|
|
|
var dot: float = a.x * b.x + a.y * b.y
|
|
|
|
var det: float = a.x * b.y - a.y * b.x
|
|
|
|
var final: float = rad_to_deg(atan2(det, dot)) + 180
|
2023-11-08 14:28:55 +11:00
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
var t: Rect2 = texture.region
|
2023-11-08 14:28:55 +11:00
|
|
|
if final > 337.5 or final < 22.5:
|
|
|
|
t = Rect2(tile_size * 4, t.position.y, tile_size, tile_size)
|
|
|
|
elif final > 22.5 and final < 67.5:
|
|
|
|
t = Rect2(tile_size * 5, t.position.y, tile_size, tile_size)
|
|
|
|
elif final > 67.5 and final < 112.5:
|
|
|
|
t = Rect2(tile_size * 6, t.position.y, tile_size, tile_size)
|
|
|
|
elif final > 112.5 and final < 157.5:
|
|
|
|
t = Rect2(tile_size * 7, t.position.y, tile_size, tile_size)
|
|
|
|
elif final > 157.5 and final < 202.5:
|
|
|
|
t = Rect2(0, t.position.y, tile_size, tile_size)
|
|
|
|
elif final > 202.5 and final < 247.5:
|
|
|
|
t = Rect2(tile_size * 1, t.position.y, tile_size, tile_size)
|
|
|
|
elif final > 247.5 and final < 292.5:
|
|
|
|
t = Rect2(tile_size * 2, t.position.y, tile_size, tile_size)
|
|
|
|
elif final > 292.5 and final < 337.5:
|
|
|
|
t = Rect2(tile_size * 3, t.position.y, tile_size, tile_size)
|
|
|
|
texture.region = t
|