-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSprite.gd
36 lines (29 loc) · 1.1 KB
/
Sprite.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
extends Sprite
var mouse_in = false
var dragging = false
var mouse_to_center_set = false
var mouse_to_center
var sprite_pos
var mouse_pos
func _input(_event):
if (Input.is_action_just_pressed("left_click") && mouse_in): #When clicking
#First we set mouse_to_center as a static vector
#for preventing the sprite to move its center to the mouse position
mouse_pos = get_viewport().get_mouse_position()
mouse_to_center = restaVectores(self.position, mouse_pos)
func _process(_delta):
if (dragging):
mouse_pos = get_viewport().get_mouse_position()
#Set the position of the sprite to
#mouse position + static mouse_to_center vector
set_position(sumaVectores(mouse_pos, mouse_to_center))
func _on_Area2D_mouse_entered():
mouse_in = true
get_parent()._add_sprite(self) #Add the sprite to the sprite list
func _on_Area2D_mouse_exited():
mouse_in = false
get_parent()._remove_sprite(self) #Remove the sprite from the sprite list
func restaVectores(v1, v2): #vector substraction
return Vector2(v1.x - v2.x, v1.y - v2.y)
func sumaVectores(v1, v2): #vector sum
return Vector2(v1.x + v2.x, v1.y + v2.y)