Skip to content

Instantly share code, notes, and snippets.

@smks
Last active June 7, 2020 17:45
Show Gist options
  • Save smks/d42eb6ca14844b29a19345df36bb0d92 to your computer and use it in GitHub Desktop.
Save smks/d42eb6ca14844b29a19345df36bb0d92 to your computer and use it in GitHub Desktop.

Revisions

  1. smks revised this gist Jun 7, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Rabbit.gd
    Original file line number Diff line number Diff line change
    @@ -67,7 +67,7 @@ func change_animation_blend_mode(velocity):
    animation_blend_mode.x = 1
    elif velocity.x < 0:
    animation_blend_mode.x = -1
    elif velocity.y > 0:
    if velocity.y > 0:
    animation_blend_mode.y = 1
    elif velocity.y < 0:
    animation_blend_mode.y = -1
  2. smks revised this gist Jun 7, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Rabbit.gd
    Original file line number Diff line number Diff line change
    @@ -59,7 +59,7 @@ func find_nearest_hole():
    return nearest_hole


    // Solution? :/
    # Solution? :/

    func change_animation_blend_mode(velocity):
    var animation_blend_mode = Vector2.ZERO
  3. smks revised this gist Jun 7, 2020. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions Rabbit.gd
    Original file line number Diff line number Diff line change
    @@ -57,3 +57,18 @@ func find_nearest_hole():
    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
    elif 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)
  4. smks created this gist Jun 7, 2020.
    59 changes: 59 additions & 0 deletions Rabbit.gd
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    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