Cube & Scoring Logic Part 1
This commit is contained in:
65
game/Objects/Cube.gd
Normal file
65
game/Objects/Cube.gd
Normal file
@@ -0,0 +1,65 @@
|
||||
extends Area2D
|
||||
|
||||
@export var cubeHeight = 364
|
||||
@export var cursor_path: NodePath
|
||||
@onready var cursor = get_node(cursor_path)
|
||||
@onready var mesh_instance: MeshInstance2D = $MeshInstance2D
|
||||
@onready var line = $Vector/Line2D
|
||||
@onready var shape: CollisionShape2D = $HitBox
|
||||
|
||||
@onready var red_line = $Vector/CursorVector
|
||||
@onready var green_line = $Vector/Top
|
||||
@onready var pink_line = $Vector/Mid
|
||||
|
||||
var cube_top_center: Vector2
|
||||
var CorrectHit = false
|
||||
var TopScore = 0
|
||||
var MidScore = 0
|
||||
var BotScore = 0
|
||||
|
||||
|
||||
@onready var area_direction
|
||||
@onready var body_direction
|
||||
|
||||
|
||||
var sliced = false
|
||||
|
||||
func _ready():
|
||||
|
||||
|
||||
var shape_size = Vector2.ZERO
|
||||
if shape.shape is RectangleShape2D:
|
||||
shape_size = shape.shape.extents * 2 # extents are half-size
|
||||
elif shape.shape is CapsuleShape2D:
|
||||
shape_size = Vector2(shape.shape.radius * 2, shape.shape.height)
|
||||
# Add other shape types as needed
|
||||
|
||||
# Now use actual height
|
||||
var cube_height = shape_size.y
|
||||
|
||||
|
||||
func _on_area_entered(area: Area2D) -> void:
|
||||
if area.name != "Cursor":
|
||||
return
|
||||
|
||||
area_direction = -area.global_transform.y.normalized()
|
||||
body_direction = -self.global_transform.y.normalized()
|
||||
|
||||
|
||||
var local_pos = to_local(area.global_position)
|
||||
var side_offset = local_pos.y # this is the offset *perpendicular* to Area2D's facing
|
||||
var alignment = area_direction.dot(body_direction)
|
||||
|
||||
if abs(side_offset) < 5:
|
||||
print("Hit near center Top", " ", alignment)
|
||||
elif side_offset < 0:
|
||||
print("Hit to the LEFT Side, offset:", side_offset, " ", alignment)
|
||||
else:
|
||||
print("Hit to the RIGHT Side, offset:", side_offset, " ", alignment)
|
||||
|
||||
|
||||
func get_closest_point_on_line(line_origin: Vector2, line_dir: Vector2, point: Vector2) -> Vector2:
|
||||
# Projects point onto the infinite line defined by (line_origin, line_dir)
|
||||
var v = point - line_origin
|
||||
var d = v.dot(line_dir)
|
||||
return line_origin + line_dir * d
|
||||
Reference in New Issue
Block a user