From 11e59ce0709acccc50ea88225a2d0c0390fcc406 Mon Sep 17 00:00:00 2001 From: Snoweuph Date: Tue, 11 Apr 2023 20:52:35 +0200 Subject: [PATCH] Use Input Dotproduct based Animation Choosing for Player --- Assets/UI/Theme.tres | 4 ++-- Scripts/PlayerController.gd | 28 ++++++++++++++-------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Assets/UI/Theme.tres b/Assets/UI/Theme.tres index 4144fdf..ebcdebb 100644 --- a/Assets/UI/Theme.tres +++ b/Assets/UI/Theme.tres @@ -41,7 +41,7 @@ [ext_resource type="StyleBox" uid="uid://0jfr1uwuog0s" path="res://Assets/UI/Slider/v/background.tres" id="39_dl1e4"] [ext_resource type="FontFile" uid="uid://dqdeftjkwxe64" path="res://Assets/Fonts/Dogica/dogicapixel.ttf" id="40_bmcvq"] -[sub_resource type="Image" id="Image_avfxn"] +[sub_resource type="Image" id="Image_fjlcl"] data = { "data": PackedByteArray(255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 64, 255, 255, 255, 64, 255, 255, 255, 0), "format": "RGBA8", @@ -51,7 +51,7 @@ data = { } [sub_resource type="ImageTexture" id="ImageTexture_g5bup"] -image = SubResource("Image_avfxn") +image = SubResource("Image_fjlcl") [sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_sj7h5"] diff --git a/Scripts/PlayerController.gd b/Scripts/PlayerController.gd index 1f77e73..6dc4630 100644 --- a/Scripts/PlayerController.gd +++ b/Scripts/PlayerController.gd @@ -25,20 +25,20 @@ func update_player_movement(delta : float): # Update Objects Physics calculations self.move_and_slide() pass - + func update_player_animation(): - var move_vector = get_move_input_vector() - match move_vector: + match get_move_input_vector(): Vector2.ZERO: animation_player.play("Idle") - Vector2.LEFT: animation_player.play("MoveLeft") - Vector2.RIGHT: animation_player.play("MoveRight") - Vector2.UP: animation_player.play("MoveUp") - Vector2.DOWN: animation_player.play("MoveDown") - _: handle_diagonal_animations(move_vector) - pass - -func handle_diagonal_animations(dir : Vector2): - if (dir.y > 0 and dir.x < 0) or (dir.y < 0 and dir.x < 0): animation_player.play("MoveLeft") - elif (dir.y > 0 and dir.x > 0) or (dir.y < 0 and dir.x > 0): animation_player.play("MoveRight") - else: animation_player.play("Idle") + _: + var left_dot = Vector2.LEFT.dot(self.velocity) + var right_dot = Vector2.RIGHT.dot(self.velocity) + var up_dot = Vector2.UP.dot(self.velocity) + var down_dot = Vector2.DOWN.dot(self.velocity) + + var max_dot = maxf(left_dot, maxf(right_dot, maxf(up_dot, down_dot))) + match max_dot: + left_dot: animation_player.play("MoveLeft") + right_dot: animation_player.play("MoveRight") + up_dot: animation_player.play("MoveUp") + down_dot: animation_player.play("MoveDown") pass