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
|
||||
@@ -1,11 +1,14 @@
|
||||
[gd_scene load_steps=4 format=3 uid="uid://ctcv7rr8w3fnf"]
|
||||
[gd_scene load_steps=5 format=3 uid="uid://ctcv7rr8w3fnf"]
|
||||
|
||||
[ext_resource type="Texture2D" uid="uid://2nxh3mui3ia7" path="res://Textures/svg/Cube.svg" id="1_k3834"]
|
||||
[ext_resource type="Script" uid="uid://cu1ss1gvex68v" path="res://Objects/SelfSlicer.gd" id="1_m4rrt"]
|
||||
[ext_resource type="Script" uid="uid://cu1ss1gvex68v" path="res://Objects/Cube.gd" id="1_m4rrt"]
|
||||
|
||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_ae35o"]
|
||||
size = Vector2(68, 68)
|
||||
|
||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_m4rrt"]
|
||||
size = Vector2(26.5, 68)
|
||||
|
||||
[node name="Node2D" type="Area2D"]
|
||||
script = ExtResource("1_m4rrt")
|
||||
|
||||
@@ -13,5 +16,25 @@ script = ExtResource("1_m4rrt")
|
||||
scale = Vector2(0.5, 0.5)
|
||||
texture = ExtResource("1_k3834")
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
[node name="HitBox" type="CollisionShape2D" parent="."]
|
||||
shape = SubResource("RectangleShape2D_ae35o")
|
||||
|
||||
[node name="Area2D" type="Area2D" parent="."]
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
|
||||
position = Vector2(-21.25, 0)
|
||||
shape = SubResource("RectangleShape2D_m4rrt")
|
||||
debug_color = Color(0.988715, 0, 0.298839, 0.42)
|
||||
|
||||
[node name="Vector" type="Node2D" parent="."]
|
||||
|
||||
[node name="CursorVector" type="Line2D" parent="Vector"]
|
||||
|
||||
[node name="Mid" type="Line2D" parent="Vector"]
|
||||
default_color = Color(0, 0.952941, 0, 1)
|
||||
|
||||
[node name="Top" type="Line2D" parent="Vector"]
|
||||
position = Vector2(-32, 0)
|
||||
default_color = Color(1, 0, 0, 1)
|
||||
|
||||
[connection signal="area_entered" from="." to="." method="_on_area_entered"]
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
[ext_resource type="Texture2D" uid="uid://d0hckoql2xnbq" path="res://Textures/svg/Cursor.svg" id="2_r0du0"]
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_v75j3"]
|
||||
radius = 19.0263
|
||||
radius = 28.0
|
||||
|
||||
[node name="Cursor" type="Area2D"]
|
||||
script = ExtResource("1_uu6xs")
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
extends Area2D
|
||||
class_name SelfSlicer
|
||||
|
||||
@export var cursor_path: NodePath
|
||||
@onready var cursor = get_node(cursor_path)
|
||||
@onready var mesh_instance: MeshInstance2D = $MeshInstance2D
|
||||
|
||||
var sliced = false
|
||||
|
||||
func _ready():
|
||||
|
||||
pass
|
||||
@@ -1,11 +1,13 @@
|
||||
extends Area2D
|
||||
@onready var Cursor = $Cursor
|
||||
@onready var MouseOn = true
|
||||
@export var ShowMouse = false
|
||||
var old_mouse_pos = Vector2.ZERO
|
||||
var mouse_velocity = Vector2.ZERO
|
||||
@export var vector_length = 100
|
||||
@export var vector_length = 360
|
||||
@export var vector_width = 5
|
||||
|
||||
|
||||
var line_2d : Line2D
|
||||
var is_touchingCube = false
|
||||
var collision_normal = Vector2.ZERO
|
||||
@@ -39,30 +41,32 @@ func _ready() -> void:
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
if is_touchingCube:
|
||||
draw_vector()
|
||||
# draw_vector()
|
||||
pass
|
||||
else:
|
||||
line_2d.clear_points()
|
||||
|
||||
|
||||
var current_mouse_pos = get_viewport().get_mouse_position()
|
||||
|
||||
mouse_velocity = current_mouse_pos - old_mouse_pos
|
||||
|
||||
if mouse_velocity.length_squared()>0.1:
|
||||
rotation = lerp_angle(rotation, mouse_velocity.angle(), 80 * delta)
|
||||
|
||||
if MouseOn:
|
||||
var current_mouse_pos = get_viewport().get_mouse_position()
|
||||
|
||||
var direction_to_mouse = (current_mouse_pos - global_position).normalized()
|
||||
global_position = current_mouse_pos
|
||||
|
||||
old_mouse_pos = current_mouse_pos
|
||||
mouse_velocity = current_mouse_pos - old_mouse_pos
|
||||
|
||||
if mouse_velocity.length_squared()>0.1:
|
||||
rotation = lerp_angle(rotation, mouse_velocity.angle(), 80 * delta)
|
||||
|
||||
|
||||
var direction_to_mouse = (current_mouse_pos - global_position).normalized()
|
||||
global_position = current_mouse_pos
|
||||
|
||||
old_mouse_pos = current_mouse_pos
|
||||
pass
|
||||
|
||||
|
||||
func _on_area_entered(area: Area2D) -> void:
|
||||
print("AAAA " + str(area.name))
|
||||
# print("AAAA " + str(area.name))
|
||||
if area.name == "Node2D":
|
||||
|
||||
#freeze cursor on touch block
|
||||
MouseOn = true
|
||||
is_touchingCube = true
|
||||
draw_vector()
|
||||
|
||||
@@ -92,5 +96,5 @@ func draw_vector():
|
||||
line_2d.add_point(start_point,1)
|
||||
line_2d.add_point(end_point,2)
|
||||
|
||||
print("Draws the vector\nStart %s\nEnd: %s\nDirection %s" % [start_point, end_point, vector_direction])
|
||||
#print("Draws the vector\nStart %s\nEnd: %s\nDirection %s" % [start_point, end_point, vector_direction])
|
||||
pass
|
||||
|
||||
@@ -11,6 +11,7 @@ UseNativeDisplaySize = true
|
||||
|
||||
[node name="Node2D" parent="." instance=ExtResource("3_66313")]
|
||||
position = Vector2(409, 323)
|
||||
scale = Vector2(3.385, 3.385)
|
||||
cursor_path = NodePath("../Cursor")
|
||||
|
||||
[node name="Node2D2" parent="." instance=ExtResource("3_66313")]
|
||||
@@ -18,7 +19,7 @@ position = Vector2(625, 528)
|
||||
rotation = 0.60511
|
||||
|
||||
[node name="Cursor" parent="." instance=ExtResource("1_rkps0")]
|
||||
position = Vector2(263, 291)
|
||||
position = Vector2(162, 227)
|
||||
|
||||
[node name="CanvasLayer" type="CanvasLayer" parent="."]
|
||||
script = ExtResource("1_85h15")
|
||||
@@ -36,6 +37,10 @@ size_flags_vertical = 8
|
||||
layout_mode = 2
|
||||
text = "Info?"
|
||||
|
||||
[node name="Resolution" type="Label" parent="CanvasLayer/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Info?"
|
||||
|
||||
[node name="Fps" type="Label" parent="CanvasLayer/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "FPS : "
|
||||
|
||||
@@ -8,8 +8,10 @@ func _ready() -> void:
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
$VBoxContainer/Resolution.text = "Resolution: %s" % get_viewport().size
|
||||
$VBoxContainer/Fps.text = "FPS: %s" % Performance.get_monitor(Performance.TIME_FPS)
|
||||
$VBoxContainer/Frametime.text = "FrameTime: %s" % snapped(Engine.get_physics_interpolation_fraction(), 0.000001)
|
||||
$VBoxContainer/PT.text = "Process time: %s ms" % snapped(Performance.get_monitor(Performance.TIME_PROCESS)*1000, 0.001)
|
||||
$VBoxContainer/VideoMemory.text ="Video memory Used: %s MB" % snapped(Performance.get_monitor(Performance.RENDER_VIDEO_MEM_USED)/1024**2, 0.001)
|
||||
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user