ggj2020

My game project for Global Game Jam 2020!
git clone git://git.hellocld.com/ggj2020
Log | Files | Refs

GrabbyHand.gd (1320B)


      1 extends ARVRController
      2 
      3 var _mesh_basis: Basis
      4 onready var _velocity_queue = []
      5 export var _queue_size = 60
      6 export var _throw_force = 30
      7 onready var _last_pos = global_transform.origin
      8 onready var _last_player_pos = get_parent().transform.origin
      9 
     10 signal grabbing
     11 signal releasing(velocity)
     12 
     13 
     14 func _ready():
     15 	connect("button_pressed", self, "_grab")
     16 	connect("button_release", self, "_release")
     17 
     18 
     19 func _physics_process(delta):
     20 	_velocity_queue.push_back((global_transform.origin - _last_pos) - 
     21 			(get_parent().transform.origin - _last_player_pos))
     22 	_last_pos = global_transform.origin
     23 	_last_player_pos = get_parent().transform.origin #use this to neutralize player movement while holding something
     24 	if _velocity_queue.size() > _queue_size:
     25 		_velocity_queue.pop_front()
     26 
     27 
     28 func _grab(var btn):
     29 	if btn == JOY_VR_GRIP:
     30 		print("Grabbing")
     31 		_velocity_queue.clear()
     32 		emit_signal("grabbing")
     33 
     34 
     35 func _release(var btn):
     36 	if btn == JOY_VR_GRIP:
     37 		print("Releasing")
     38 		print(_get_velocity())
     39 		emit_signal("releasing", _get_velocity())
     40 
     41 
     42 func is_grabbing() -> bool:
     43 	return true if is_button_pressed(JOY_VR_GRIP) else false
     44 
     45 
     46 func _get_velocity() -> Vector3:
     47 	var vel = Vector3.ZERO
     48 	for i in range(_velocity_queue.size()):
     49 		vel += (_velocity_queue[i] * pow(float(i) / _velocity_queue.size(), 3))
     50 	return vel * _throw_force