mirror of https://github.com/profan/ld-39-jam.git
				
				
				
			kinematic stuff also steering behaviours testing stuff
This commit is contained in:
		
							parent
							
								
									52f1bb3bdd
								
							
						
					
					
						commit
						2ff9042c96
					
				| 
						 | 
				
			
			@ -1,17 +1,37 @@
 | 
			
		|||
extends KinematicBody2D
 | 
			
		||||
 | 
			
		||||
const km = preload("Kinematic.gd")
 | 
			
		||||
 | 
			
		||||
var grunt_max_speed = 64 # pixels per second
 | 
			
		||||
var grunt_arrive_radius = 64
 | 
			
		||||
var grunt_rot_speed = deg2rad(22.5) # degrees per second
 | 
			
		||||
var current_target = null
 | 
			
		||||
 | 
			
		||||
var cur_kinematic
 | 
			
		||||
var s_seek
 | 
			
		||||
var s_arrive
 | 
			
		||||
 | 
			
		||||
func _ready():
 | 
			
		||||
	var t = get_tree()
 | 
			
		||||
	var r = t.get_root()
 | 
			
		||||
	var n = r.get_node("Game/MinimapControl")
 | 
			
		||||
	n.register_entity(self)
 | 
			
		||||
	var t = get_tree().get_root().get_node("Game/MinimapControl").register_entity(self)
 | 
			
		||||
	set_fixed_process(true)
 | 
			
		||||
	set_process(true)
 | 
			
		||||
	
 | 
			
		||||
	cur_kinematic = km.Kinematic.new(self, grunt_max_speed)
 | 
			
		||||
	s_seek = km.Seek.new(self)
 | 
			
		||||
	s_arrive = km.Arrive.new(self, grunt_arrive_radius)
 | 
			
		||||
	
 | 
			
		||||
	var player = get_tree().get_root().get_node("Game/Player")
 | 
			
		||||
	s_seek.set_target(player)
 | 
			
		||||
	s_arrive.set_target(player)
 | 
			
		||||
 | 
			
		||||
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)
 | 
			
		||||
	
 | 
			
		||||
func _process(delta):
 | 
			
		||||
	rotate(grunt_rot_speed * delta)
 | 
			
		||||
	pass
 | 
			
		||||
	# rotate(grunt_rot_speed * delta)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,98 @@
 | 
			
		|||
extends Script
 | 
			
		||||
 | 
			
		||||
class Kinematic:
 | 
			
		||||
	
 | 
			
		||||
	var owner
 | 
			
		||||
	var velocity = Vector2(0, 0) # velocity to apply to position
 | 
			
		||||
	var orientation = Vector2(0, 0) # current orientation
 | 
			
		||||
	var rotation = 0.0 # radians to rotate
 | 
			
		||||
	
 | 
			
		||||
	# per instance custom
 | 
			
		||||
	var max_speed
 | 
			
		||||
	
 | 
			
		||||
	func _init(o, m):
 | 
			
		||||
		owner = o
 | 
			
		||||
		max_speed = m
 | 
			
		||||
	
 | 
			
		||||
	func get_position():
 | 
			
		||||
		return owner.get_global_pos()
 | 
			
		||||
	
 | 
			
		||||
	func get_orientation():
 | 
			
		||||
		return velocity.normalized()
 | 
			
		||||
	
 | 
			
		||||
	func update(steering, delta):
 | 
			
		||||
		
 | 
			
		||||
		owner.move(velocity * delta)
 | 
			
		||||
		owner.set_rot(velocity.normalized().angle())
 | 
			
		||||
		# owner.rotate(rotation * delta)
 | 
			
		||||
		velocity += steering.velocity * delta
 | 
			
		||||
		rotation += steering.rotation * delta
 | 
			
		||||
		
 | 
			
		||||
		# clip at kinematic
 | 
			
		||||
		if velocity.length() > max_speed:
 | 
			
		||||
			velocity = velocity.normalized() * max_speed
 | 
			
		||||
 | 
			
		||||
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():
 | 
			
		||||
		var tr = target.get_ref()
 | 
			
		||||
		if tr:
 | 
			
		||||
			# update velocity
 | 
			
		||||
			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)
 | 
			
		||||
		else:
 | 
			
		||||
			target = null
 | 
			
		||||
	
 | 
			
		||||
	func set_target(t):
 | 
			
		||||
		target = weakref(t)
 | 
			
		||||
		
 | 
			
		||||
class Arrive:
 | 
			
		||||
	
 | 
			
		||||
	var owner
 | 
			
		||||
	var target
 | 
			
		||||
	var velocity = Vector2(0, 0)
 | 
			
		||||
	var rotation = 0
 | 
			
		||||
	
 | 
			
		||||
	# custom
 | 
			
		||||
	var radius = 0
 | 
			
		||||
	var time_to_target = 1
 | 
			
		||||
	
 | 
			
		||||
	func _init(o, r):
 | 
			
		||||
		owner = o
 | 
			
		||||
		radius = r
 | 
			
		||||
	
 | 
			
		||||
	func update():
 | 
			
		||||
		var tr = target.get_ref()
 | 
			
		||||
		if tr:
 | 
			
		||||
			if velocity.length() < radius:
 | 
			
		||||
				velocity.x = 0
 | 
			
		||||
				velocity.y = 0
 | 
			
		||||
			velocity /= time_to_target
 | 
			
		||||
		else:
 | 
			
		||||
			target = null
 | 
			
		||||
	
 | 
			
		||||
	func set_target(t):
 | 
			
		||||
		target = weakref(t)
 | 
			
		||||
 | 
			
		||||
class Flee:
 | 
			
		||||
	func init():
 | 
			
		||||
		pass
 | 
			
		||||
		
 | 
			
		||||
	func get_steering():
 | 
			
		||||
		pass
 | 
			
		||||
 | 
			
		||||
func _ready():
 | 
			
		||||
	pass
 | 
			
		||||
		Loading…
	
		Reference in New Issue