cursor state machine
This commit is contained in:
parent
ba06aacff2
commit
f60105d56a
4 changed files with 290 additions and 10 deletions
121
core/Cur6C3A.tmp
Normal file
121
core/Cur6C3A.tmp
Normal file
|
@ -0,0 +1,121 @@
|
|||
[gd_scene load_steps=8 format=3 uid="uid://524sv8spw6go"]
|
||||
|
||||
[ext_resource type="Texture2D" uid="uid://q1rdbr8uh78r" path="res://textures/cursor_default.tres" id="1_50ts1"]
|
||||
[ext_resource type="Texture2D" uid="uid://bdstohvc7pvot" path="res://textures/cursor_click.tres" id="1_h0do2"]
|
||||
[ext_resource type="Texture2D" uid="uid://buxws7r3kn0d7" path="res://textures/cursor_grab_01.tres" id="3_fj3w4"]
|
||||
[ext_resource type="Texture2D" uid="uid://pbahcjllgjjq" path="res://textures/cursor_grab_02.tres" id="4_153q8"]
|
||||
[ext_resource type="Texture2D" uid="uid://6fajq480n7se" path="res://textures/cursor_grab_03.tres" id="5_tsejy"]
|
||||
|
||||
[sub_resource type="GDScript" id="GDScript_h2l04"]
|
||||
script/source = "extends Node2D
|
||||
|
||||
const Speed:float = 900
|
||||
|
||||
var spaceState
|
||||
var query
|
||||
var CastResult
|
||||
var old_result
|
||||
var selectedSticker
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
if not OS.is_debug_build():
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_CONFINED_HIDDEN)
|
||||
query = PhysicsPointQueryParameters2D.new()
|
||||
spaceState = get_world_2d()
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta):
|
||||
|
||||
#region Mouse Position
|
||||
position = get_global_mouse_position()
|
||||
#endregion
|
||||
|
||||
pointcast()
|
||||
cursor_look()
|
||||
|
||||
|
||||
func pointcast():
|
||||
query.collide_with_areas = true
|
||||
query.position = get_global_mouse_position()
|
||||
CastResult = spaceState.direct_space_state.intersect_point(query)
|
||||
if ( (CastResult.size() > 0) and (old_result != CastResult) ):
|
||||
old_result = CastResult
|
||||
for _result in CastResult:
|
||||
if (_result.collider.input_pickable == true):
|
||||
selectedSticker = _result.collider.get_parent()
|
||||
print(\"selected sticker %\",selectedSticker)
|
||||
|
||||
|
||||
if (CastResult.size() == 0):
|
||||
CastResult.clear()
|
||||
old_result = null
|
||||
selectedSticker = null
|
||||
|
||||
func cursor_look():
|
||||
$AnimatedSprite2D.play(\"default\")
|
||||
if (Input.is_action_pressed(\"select\")):
|
||||
if (selectedSticker):
|
||||
$AnimatedSprite2D.play(\"grab\")
|
||||
else:
|
||||
$AnimatedSprite2D.play(\"click\")
|
||||
else:
|
||||
if (selectedSticker):
|
||||
$AnimatedSprite2D.play(\"grab_intro\")
|
||||
|
||||
|
||||
"
|
||||
|
||||
[sub_resource type="SpriteFrames" id="SpriteFrames_u3xkr"]
|
||||
resource_local_to_scene = true
|
||||
animations = [{
|
||||
"frames": [{
|
||||
"duration": 1.0,
|
||||
"texture": ExtResource("1_h0do2")
|
||||
}],
|
||||
"loop": true,
|
||||
"name": &"click",
|
||||
"speed": 5.0
|
||||
}, {
|
||||
"frames": [{
|
||||
"duration": 1.0,
|
||||
"texture": ExtResource("1_50ts1")
|
||||
}],
|
||||
"loop": true,
|
||||
"name": &"default",
|
||||
"speed": 5.0
|
||||
}, {
|
||||
"frames": [{
|
||||
"duration": 1.0,
|
||||
"texture": ExtResource("3_fj3w4")
|
||||
}, {
|
||||
"duration": 1.0,
|
||||
"texture": ExtResource("4_153q8")
|
||||
}, {
|
||||
"duration": 1.0,
|
||||
"texture": ExtResource("5_tsejy")
|
||||
}],
|
||||
"loop": true,
|
||||
"name": &"grab",
|
||||
"speed": 7.0
|
||||
}, {
|
||||
"frames": [{
|
||||
"duration": 1.0,
|
||||
"texture": ExtResource("3_fj3w4")
|
||||
}],
|
||||
"loop": true,
|
||||
"name": &"grab_intro",
|
||||
"speed": 5.0
|
||||
}]
|
||||
|
||||
[node name="Cursor" type="Node2D"]
|
||||
top_level = true
|
||||
script = SubResource("GDScript_h2l04")
|
||||
|
||||
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."]
|
||||
scale = Vector2(1.5, 1.5)
|
||||
sprite_frames = SubResource("SpriteFrames_u3xkr")
|
||||
animation = &"grab"
|
||||
centered = false
|
||||
offset = Vector2(-80, -190)
|
121
core/Cur9BFD.tmp
Normal file
121
core/Cur9BFD.tmp
Normal file
|
@ -0,0 +1,121 @@
|
|||
[gd_scene load_steps=8 format=3 uid="uid://524sv8spw6go"]
|
||||
|
||||
[ext_resource type="Texture2D" uid="uid://q1rdbr8uh78r" path="res://textures/cursor_default.tres" id="1_50ts1"]
|
||||
[ext_resource type="Texture2D" uid="uid://bdstohvc7pvot" path="res://textures/cursor_click.tres" id="1_h0do2"]
|
||||
[ext_resource type="Texture2D" uid="uid://buxws7r3kn0d7" path="res://textures/cursor_grab_01.tres" id="3_fj3w4"]
|
||||
[ext_resource type="Texture2D" uid="uid://pbahcjllgjjq" path="res://textures/cursor_grab_02.tres" id="4_153q8"]
|
||||
[ext_resource type="Texture2D" uid="uid://6fajq480n7se" path="res://textures/cursor_grab_03.tres" id="5_tsejy"]
|
||||
|
||||
[sub_resource type="GDScript" id="GDScript_h2l04"]
|
||||
script/source = "extends Node2D
|
||||
|
||||
const Speed:float = 900
|
||||
|
||||
var spaceState
|
||||
var query
|
||||
var CastResult
|
||||
var old_result
|
||||
var selectedSticker
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
if not OS.is_debug_build():
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_CONFINED_HIDDEN)
|
||||
query = PhysicsPointQueryParameters2D.new()
|
||||
spaceState = get_world_2d()
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta):
|
||||
|
||||
#region Mouse Position
|
||||
position = get_global_mouse_position()
|
||||
#endregion
|
||||
|
||||
pointcast()
|
||||
cursor_look()
|
||||
|
||||
|
||||
func pointcast():
|
||||
query.collide_with_areas = true
|
||||
query.position = get_global_mouse_position()
|
||||
CastResult = spaceState.direct_space_state.intersect_point(query)
|
||||
if ( (CastResult.size() > 0) and (old_result != CastResult) ):
|
||||
old_result = CastResult
|
||||
for _result in CastResult:
|
||||
if (_result.collider.input_pickable == true):
|
||||
selectedSticker = _result.collider.get_parent()
|
||||
print(\"selected sticker %\",selectedSticker)
|
||||
|
||||
|
||||
if (CastResult.size() == 0):
|
||||
CastResult.clear()
|
||||
old_result = null
|
||||
selectedSticker = null
|
||||
|
||||
func cursor_look():
|
||||
$AnimatedSprite2D.play(\"default\")
|
||||
if (Input.is_action_pressed(\"select\")):
|
||||
if (selectedSticker):
|
||||
$AnimatedSprite2D.play(\"grab\")
|
||||
else:
|
||||
$AnimatedSprite2D.play(\"click\")
|
||||
if (selectedSticker):
|
||||
$AnimatedSprite2D.play(\"grab_intro\")
|
||||
|
||||
|
||||
"
|
||||
|
||||
[sub_resource type="SpriteFrames" id="SpriteFrames_u3xkr"]
|
||||
resource_local_to_scene = true
|
||||
animations = [{
|
||||
"frames": [{
|
||||
"duration": 1.0,
|
||||
"texture": ExtResource("1_h0do2")
|
||||
}],
|
||||
"loop": true,
|
||||
"name": &"click",
|
||||
"speed": 5.0
|
||||
}, {
|
||||
"frames": [{
|
||||
"duration": 1.0,
|
||||
"texture": ExtResource("1_50ts1")
|
||||
}],
|
||||
"loop": true,
|
||||
"name": &"default",
|
||||
"speed": 5.0
|
||||
}, {
|
||||
"frames": [{
|
||||
"duration": 1.0,
|
||||
"texture": ExtResource("3_fj3w4")
|
||||
}, {
|
||||
"duration": 1.0,
|
||||
"texture": ExtResource("4_153q8")
|
||||
}, {
|
||||
"duration": 1.0,
|
||||
"texture": ExtResource("5_tsejy")
|
||||
}],
|
||||
"loop": true,
|
||||
"name": &"grab",
|
||||
"speed": 7.0
|
||||
}, {
|
||||
"frames": [{
|
||||
"duration": 1.0,
|
||||
"texture": ExtResource("3_fj3w4")
|
||||
}],
|
||||
"loop": true,
|
||||
"name": &"grab_intro",
|
||||
"speed": 5.0
|
||||
}]
|
||||
|
||||
[node name="Cursor" type="Node2D"]
|
||||
top_level = true
|
||||
script = SubResource("GDScript_h2l04")
|
||||
|
||||
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."]
|
||||
scale = Vector2(1.5, 1.5)
|
||||
sprite_frames = SubResource("SpriteFrames_u3xkr")
|
||||
animation = &"grab"
|
||||
autoplay = "grab"
|
||||
centered = false
|
||||
offset = Vector2(-80, -190)
|
|
@ -1,6 +1,7 @@
|
|||
[gd_scene load_steps=7 format=3 uid="uid://524sv8spw6go"]
|
||||
[gd_scene load_steps=8 format=3 uid="uid://524sv8spw6go"]
|
||||
|
||||
[ext_resource type="Texture2D" uid="uid://q1rdbr8uh78r" path="res://textures/cursor_default.tres" id="1_50ts1"]
|
||||
[ext_resource type="Texture2D" uid="uid://bdstohvc7pvot" path="res://textures/cursor_click.tres" id="1_h0do2"]
|
||||
[ext_resource type="Texture2D" uid="uid://buxws7r3kn0d7" path="res://textures/cursor_grab_01.tres" id="3_fj3w4"]
|
||||
[ext_resource type="Texture2D" uid="uid://pbahcjllgjjq" path="res://textures/cursor_grab_02.tres" id="4_153q8"]
|
||||
[ext_resource type="Texture2D" uid="uid://6fajq480n7se" path="res://textures/cursor_grab_03.tres" id="5_tsejy"]
|
||||
|
@ -15,7 +16,9 @@ var query
|
|||
var CastResult
|
||||
var old_result
|
||||
var selectedSticker
|
||||
|
||||
enum CURSOR_STATE {DEFAULT, CLICK, GRAB, GRABBED}
|
||||
var currentCursorState:CURSOR_STATE
|
||||
var oldCursorState:CURSOR_STATE
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
|
@ -28,10 +31,7 @@ func _ready():
|
|||
func _process(delta):
|
||||
|
||||
#region Mouse Position
|
||||
var old_pos
|
||||
#if (get_global_mouse_position() != old_pos):
|
||||
position = get_global_mouse_position()
|
||||
old_pos = position
|
||||
#endregion
|
||||
|
||||
pointcast()
|
||||
|
@ -56,11 +56,29 @@ func pointcast():
|
|||
selectedSticker = null
|
||||
|
||||
func cursor_look():
|
||||
if (selectedSticker):
|
||||
$AnimatedSprite2D.play(\"grab_intro\")
|
||||
|
||||
|
||||
if (Input.is_action_pressed(\"select\")):
|
||||
if (selectedSticker):
|
||||
currentCursorState = CURSOR_STATE.GRABBED
|
||||
else:
|
||||
currentCursorState = CURSOR_STATE.CLICK
|
||||
else:
|
||||
$AnimatedSprite2D.play(\"default\")
|
||||
if (selectedSticker):
|
||||
currentCursorState = CURSOR_STATE.GRAB
|
||||
else:
|
||||
currentCursorState = CURSOR_STATE.DEFAULT
|
||||
|
||||
if (currentCursorState != oldCursorState):
|
||||
oldCursorState = currentCursorState
|
||||
match oldCursorState:
|
||||
CURSOR_STATE.DEFAULT:
|
||||
$AnimatedSprite2D.play(\"default\")
|
||||
CURSOR_STATE.CLICK:
|
||||
$AnimatedSprite2D.play(\"click\")
|
||||
CURSOR_STATE.GRAB:
|
||||
$AnimatedSprite2D.play(\"grab_intro\")
|
||||
CURSOR_STATE.GRABBED:
|
||||
$AnimatedSprite2D.play(\"grab\")
|
||||
"
|
||||
|
||||
[sub_resource type="SpriteFrames" id="SpriteFrames_u3xkr"]
|
||||
|
@ -68,6 +86,14 @@ resource_local_to_scene = true
|
|||
animations = [{
|
||||
"frames": [{
|
||||
"duration": 1.0,
|
||||
"texture": ExtResource("1_h0do2")
|
||||
}],
|
||||
"loop": true,
|
||||
"name": &"click",
|
||||
"speed": 5.0
|
||||
}, {
|
||||
"frames": [{
|
||||
"duration": 1.0,
|
||||
"texture": ExtResource("1_50ts1")
|
||||
}],
|
||||
"loop": true,
|
||||
|
@ -104,6 +130,9 @@ script = SubResource("GDScript_h2l04")
|
|||
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."]
|
||||
scale = Vector2(1.5, 1.5)
|
||||
sprite_frames = SubResource("SpriteFrames_u3xkr")
|
||||
autoplay = "default"
|
||||
animation = &"grab"
|
||||
autoplay = "grab"
|
||||
frame = 2
|
||||
frame_progress = 1.0
|
||||
centered = false
|
||||
offset = Vector2(-80, -190)
|
||||
|
|
9
textures/cursor_click.tres
Normal file
9
textures/cursor_click.tres
Normal file
|
@ -0,0 +1,9 @@
|
|||
[gd_resource type="AtlasTexture" load_steps=2 format=3 uid="uid://bdstohvc7pvot"]
|
||||
|
||||
[ext_resource type="Texture2D" uid="uid://docoki1q4vvfh" path="res://textures/atlas/SPRT_Hand.png" id="1_2ch11"]
|
||||
|
||||
[resource]
|
||||
atlas = ExtResource("1_2ch11")
|
||||
region = Rect2(0, 0, 256, 256)
|
||||
margin = Rect2(-5, 5, 5, 5)
|
||||
filter_clip = true
|
Loading…
Reference in a new issue