From 4e6c65d8ea159586b5a35713ebd3d65f64d0d492 Mon Sep 17 00:00:00 2001 From: Snoweuph Date: Mon, 10 Apr 2023 23:47:14 +0200 Subject: [PATCH] Fixing Bunny Animations by using dot Products --- Assets/UI/Theme.tres | 4 +-- Prefabs/bunny.tscn | 58 +++++++++++++++++------------------ Scripts/EntitySystem/Bunny.gd | 26 ++++++++++------ 3 files changed, 47 insertions(+), 41 deletions(-) diff --git a/Assets/UI/Theme.tres b/Assets/UI/Theme.tres index 8fa7dc9..fe4f3c6 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_hhwo6"] +[sub_resource type="Image" id="Image_kwuuq"] 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_hhwo6") +image = SubResource("Image_kwuuq") [sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_sj7h5"] diff --git a/Prefabs/bunny.tscn b/Prefabs/bunny.tscn index 7cd45e4..4f4e0a5 100644 --- a/Prefabs/bunny.tscn +++ b/Prefabs/bunny.tscn @@ -8,10 +8,8 @@ [sub_resource type="CircleShape2D" id="CircleShape2D_itvjj"] radius = 14.0 -[sub_resource type="Animation" id="Animation_l8j6b"] -resource_name = "MoveLeft" -length = 0.5 -loop_mode = 1 +[sub_resource type="Animation" id="Animation_6br4g"] +resource_name = "Idle" tracks/0/type = "value" tracks/0/imported = false tracks/0/enabled = true @@ -19,27 +17,10 @@ tracks/0/path = NodePath("Sprite2D:frame") tracks/0/interp = 1 tracks/0/loop_wrap = true tracks/0/keys = { -"times": PackedFloat32Array(0, 0.1, 0.2, 0.3, 0.4), -"transitions": PackedFloat32Array(1, 1, 1, 1, 1), +"times": PackedFloat32Array(0), +"transitions": PackedFloat32Array(1), "update": 1, -"values": [5, 6, 7, 8, 9] -} - -[sub_resource type="Animation" id="Animation_45lfy"] -resource_name = "MoveUp" -length = 0.5 -loop_mode = 1 -tracks/0/type = "value" -tracks/0/imported = false -tracks/0/enabled = true -tracks/0/path = NodePath("Sprite2D:frame") -tracks/0/interp = 1 -tracks/0/loop_wrap = true -tracks/0/keys = { -"times": PackedFloat32Array(0, 0.1, 0.2, 0.3, 0.4), -"transitions": PackedFloat32Array(1, 1, 1, 1, 1), -"update": 1, -"values": [15, 16, 17, 18, 19] +"values": [0] } [sub_resource type="Animation" id="Animation_olc18"] @@ -59,6 +40,23 @@ tracks/0/keys = { "values": [0, 1, 2, 3, 4] } +[sub_resource type="Animation" id="Animation_l8j6b"] +resource_name = "MoveLeft" +length = 0.5 +loop_mode = 1 +tracks/0/type = "value" +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/path = NodePath("Sprite2D:frame") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/keys = { +"times": PackedFloat32Array(0, 0.1, 0.2, 0.3, 0.4), +"transitions": PackedFloat32Array(1, 1, 1, 1, 1), +"update": 1, +"values": [5, 6, 7, 8, 9] +} + [sub_resource type="Animation" id="Animation_agj62"] resource_name = "MoveRight" length = 0.5 @@ -76,8 +74,10 @@ tracks/0/keys = { "values": [10, 11, 12, 13, 14] } -[sub_resource type="Animation" id="Animation_6br4g"] -resource_name = "Idle" +[sub_resource type="Animation" id="Animation_45lfy"] +resource_name = "MoveUp" +length = 0.5 +loop_mode = 1 tracks/0/type = "value" tracks/0/imported = false tracks/0/enabled = true @@ -85,10 +85,10 @@ tracks/0/path = NodePath("Sprite2D:frame") tracks/0/interp = 1 tracks/0/loop_wrap = true tracks/0/keys = { -"times": PackedFloat32Array(0), -"transitions": PackedFloat32Array(1), +"times": PackedFloat32Array(0, 0.1, 0.2, 0.3, 0.4), +"transitions": PackedFloat32Array(1, 1, 1, 1, 1), "update": 1, -"values": [0] +"values": [15, 16, 17, 18, 19] } [sub_resource type="Animation" id="Animation_mp212"] diff --git a/Scripts/EntitySystem/Bunny.gd b/Scripts/EntitySystem/Bunny.gd index 5889c6a..afa660f 100644 --- a/Scripts/EntitySystem/Bunny.gd +++ b/Scripts/EntitySystem/Bunny.gd @@ -38,6 +38,8 @@ func _physics_process(delta): if delta > 0.5: print(delta) move_and_slide() + else: + animation_player.stop() pass func _on_collision(body): @@ -67,18 +69,22 @@ func sub_on_death(callback : Callable): pass func update_animation(): - var move_vector = Vector2(1 if velocity.x > 0 else (-1 if velocity.x < 0 else 0), 1 if velocity.y > 0 else (-1 if velocity.y < 0 else 0)) - match move_vector: + match self.velocity: 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) + _: handle_diagonal_animations(self.velocity) 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(dir) + var right_dot = Vector2.RIGHT.dot(dir) + var up_dot = Vector2.UP.dot(dir) + var down_dot = Vector2.DOWN.dot(dir) + + var max_dot = minf(left_dot, minf(right_dot, minf(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