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") 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_radius = 64
var grunt_arrive_speed = 1
var grunt_rot_speed = deg2rad(22.5) # degrees per second var grunt_rot_speed = deg2rad(22.5) # degrees per second
var cur_kinematic var cur_kinematic
var steering
var s_seek var s_seek
var s_arrive var s_arrive
@ -16,8 +18,10 @@ func _ready():
set_process(true) set_process(true)
cur_kinematic = km.Kinematic.new(self, grunt_max_speed) cur_kinematic = km.Kinematic.new(self, grunt_max_speed)
steering = km.Steering.new()
s_seek = km.Seek.new(self) 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") var player = get_tree().get_root().get_node("Game/Player")
s_seek.set_target(player) s_seek.set_target(player)
@ -27,10 +31,9 @@ func type():
return "EnemyGrunt" return "EnemyGrunt"
func _fixed_process(delta): func _fixed_process(delta):
s_seek.update() s_seek.get_steering(steering)
s_arrive.update() s_arrive.get_steering(steering)
cur_kinematic.update(s_seek, delta) cur_kinematic.update(steering, delta)
cur_kinematic.update(s_arrive, delta)
func _process(delta): func _process(delta):
pass pass

View File

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