Last active
June 7, 2020 17:45
-
-
Save smks/d42eb6ca14844b29a19345df36bb0d92 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends KinematicBody2D | |
export var ACCELERATION = 500 | |
export var MAX_SPEED = 600 | |
export var FRICTION = 200 | |
onready var threat_detection_zone = $ThreatDetectionZone | |
onready var animation_tree = $AnimationTree | |
onready var animation_state = animation_tree.get("parameters/playback") | |
enum { | |
IDLE, | |
RUNNING, | |
JUMP | |
} | |
var hole_to_go_to = null | |
var current_state = IDLE | |
var has_detected_threat = false | |
var velocity = Vector2.ZERO | |
var knockback = Vector2.ZERO | |
# Called when the node enters the scene tree for the first time. | |
func _ready(): | |
animation_tree.active = true | |
# Called every frame. 'delta' is the elapsed time since the previous frame. | |
func _physics_process(delta): | |
var threat = threat_detection_zone.threat | |
if threat != null: | |
move_to_hole(delta) | |
func run_state(): | |
animation_state.travel('Run') | |
func move_to_hole(delta): | |
if hole_to_go_to == null: | |
hole_to_go_to = find_nearest_hole() | |
current_state = RUNNING | |
var direction = (hole_to_go_to.global_position - global_position).normalized() | |
velocity = velocity.move_toward(direction * MAX_SPEED, ACCELERATION * delta) | |
velocity = move_and_slide(velocity) | |
run_state() | |
animation_tree.set("parameters/Run/blend_position", direction) | |
func drop_down_hole(): | |
queue_free() | |
func find_nearest_hole(): | |
var holes = get_parent().get_parent().get_node('RabbitHoles').get_children() | |
var nearest_hole = holes[0] | |
for hole in holes: | |
var hole_pos_diff = self.position.distance_to(hole.position) | |
var current_nearest_hole_diff = self.position.distance_to(nearest_hole.position) | |
if hole_pos_diff < current_nearest_hole_diff: | |
nearest_hole = hole | |
return nearest_hole | |
# Solution? :/ | |
func change_animation_blend_mode(velocity): | |
var animation_blend_mode = Vector2.ZERO | |
if velocity.x > 0: | |
animation_blend_mode.x = 1 | |
elif velocity.x < 0: | |
animation_blend_mode.x = -1 | |
if velocity.y > 0: | |
animation_blend_mode.y = 1 | |
elif velocity.y < 0: | |
animation_blend_mode.y = -1 | |
animation_tree.set("parameters/Run/blend_position", animation_blend_mode) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment