84 lines
2.6 KiB
GDScript
84 lines
2.6 KiB
GDScript
extends Node
|
|
|
|
var projectile: = preload("res://scenes/weapons/projectile.tscn")
|
|
|
|
func _ready():
|
|
pass # Replace with function body.
|
|
|
|
var handles = {}
|
|
|
|
func body_enter(obj, pj):
|
|
# if !obj is StaticBody:
|
|
# print("body enter: ", obj.name)
|
|
if obj.is_in_group("characters"):
|
|
rpg.projectile_attack(pj, obj)
|
|
rpg.add_agression(obj, 1.3)
|
|
# else:
|
|
# if obj is StaticBody:
|
|
# pj.queue_free()
|
|
|
|
func register(obj):
|
|
assert(obj.is_in_group("characters"))
|
|
var obj_path = get_path_to(obj)
|
|
var skel = obj.get_skeleton()
|
|
var att: = BoneAttachment.new()
|
|
att.bone_name = "grabber_R"
|
|
skel.add_child(att)
|
|
var att_path = get_path_to(att)
|
|
handles[obj_path] = att_path
|
|
var anim:AnimationPlayer = obj.get_anim_player()
|
|
var throw_anim: Animation = anim.get_animation("throw").duplicate()
|
|
var track = throw_anim.add_track(Animation.TYPE_METHOD)
|
|
throw_anim.track_set_path(track, "/root/combat")
|
|
# throw_anim.track_insert_key(track, 0.05, {"method": "shoot", "args": [obj]})
|
|
throw_anim.track_insert_key(track, throw_anim.length - 0.02, {"method": "shoot", "args": [obj]})
|
|
anim.remove_animation("throw")
|
|
var ret = anim.add_animation("throw", throw_anim)
|
|
assert(ret == OK)
|
|
|
|
func shoot(from):
|
|
assert(from.is_in_group("characters"))
|
|
# print(from.name, " shoots projectile")
|
|
var obj_path = get_path_to(from)
|
|
var handle_path = handles[obj_path]
|
|
var handle = get_node(handle_path)
|
|
var from_pos = handle.global_transform.origin
|
|
var from_dir = -from.global_transform.basis[2]
|
|
# top impulse 6000
|
|
# ok = 100
|
|
# so-so = 50
|
|
var stamina = from.get_meta("stamina")
|
|
if stamina > 5:
|
|
var impulse = from_dir * (10.0 + clamp(from.get_meta("strength") / 4.0, 0, 10) + randi() % int(clamp((1 + from.get_meta("strength") / 4.0), 0, 5)))
|
|
var pj = projectile.instance()
|
|
pj.connect("body_entered", self, "body_enter", [pj])
|
|
get_node("/root").add_child(pj)
|
|
pj.set_meta("owner", from)
|
|
pj.global_transform.origin = from_pos
|
|
pj.add_collision_exception_with(from)
|
|
pj.apply_impulse(Vector3(), impulse)
|
|
stamina -= 5 + impulse.length() / (1 + from.get_meta("strength") * 0.1)
|
|
if stamina < 0:
|
|
stamina = 0
|
|
from.set_meta("stamina", stamina)
|
|
# print("set stamina to: ", from.name)
|
|
|
|
func player_nearby(obj, dst):
|
|
assert(obj.is_in_group("characters"))
|
|
var ppos = global.player.global_transform.origin
|
|
var opos = obj.global_transform.origin
|
|
if opos.distance_to(ppos) < dst:
|
|
return true
|
|
return false
|
|
func enemy_nearby(obj, dst):
|
|
if !obj.is_in_group("npc"):
|
|
return false
|
|
var bb = obj.get_node("blackboard")
|
|
var enemy = bb.get("last_enemy")
|
|
if enemy:
|
|
var ppos = enemy.global_transform.origin
|
|
var opos = obj.global_transform.origin
|
|
if opos.distance_to(ppos) < dst:
|
|
return true
|
|
return false
|