vectors and shit

This commit is contained in:
Robin Hübner 2017-08-10 21:17:39 +02:00
parent 2ff9042c96
commit c78b49fef6
2 changed files with 27 additions and 24 deletions

View File

@ -2,11 +2,13 @@ extends KinematicBody2D
const km = preload("Kinematic.gd")
var grunt_max_speed = 64 # pixels per second
var grunt_max_speed = 128 # pixels per second
var grunt_arrive_radius = 64
var grunt_arrive_speed = 1
var grunt_rot_speed = deg2rad(22.5) # degrees per second
var cur_kinematic
var steering
var s_seek
var s_arrive
@ -16,8 +18,10 @@ func _ready():
set_process(true)
cur_kinematic = km.Kinematic.new(self, grunt_max_speed)
steering = km.Steering.new()
s_seek = km.Seek.new(self)
s_arrive = km.Arrive.new(self, grunt_arrive_radius)
s_arrive = km.Arrive.new(self, grunt_arrive_radius, grunt_arrive_speed)
var player = get_tree().get_root().get_node("Game/Player")
s_seek.set_target(player)
@ -27,10 +31,9 @@ func type():
return "EnemyGrunt"
func _fixed_process(delta):
s_seek.update()
s_arrive.update()
cur_kinematic.update(s_seek, delta)
cur_kinematic.update(s_arrive, delta)
s_seek.get_steering(steering)
s_arrive.get_steering(steering)
cur_kinematic.update(steering, delta)
func _process(delta):
pass

View File

@ -23,33 +23,34 @@ class Kinematic:
func update(steering, delta):
owner.move(velocity * delta)
owner.set_rot(velocity.normalized().angle())
# owner.rotate(rotation * delta)
if velocity.length() > 0:
owner.set_rot(velocity.normalized().angle())
velocity += steering.velocity * delta
rotation += steering.rotation * delta
# clip at kinematic
# clip at max speed
if velocity.length() > max_speed:
velocity = velocity.normalized() * max_speed
class Steering:
var velocity = Vector2(0, 0)
var rotation = 0
class Seek:
var owner
var target
var max_rot_speed = deg2rad(64) # degrees per second
# output here
var velocity = Vector2(0, 0)
var rotation = 0
func _init(o):
owner = o
func update():
func get_steering(s):
var tr = target.get_ref()
if tr:
# update velocity
velocity = tr.get_global_pos() - owner.get_global_pos()
s.velocity = tr.get_global_pos() - owner.get_global_pos()
# update orientation
#if velocity.length() > 0:
# rotation = clamp(velocity.normalized().angle(), -max_rot_speed, max_rot_speed)
@ -63,24 +64,23 @@ class Arrive:
var owner
var target
var velocity = Vector2(0, 0)
var rotation = 0
# custom
var radius = 0
var time_to_target = 1
var time_to_target = 0
func _init(o, r):
func _init(o, r, a):
owner = o
radius = r
time_to_target = a
func update():
func get_steering(s):
var tr = target.get_ref()
if tr:
if velocity.length() < radius:
velocity.x = 0
velocity.y = 0
velocity /= time_to_target
if s.velocity.length() < radius:
s.velocity.x = 0
s.velocity.y = 0
s.velocity /= time_to_target
else:
target = null