Fixing Bunny Animations by using dot Products

This commit is contained in:
Snoweuph 2023-04-10 23:47:14 +02:00
parent a0d1e09d91
commit 4e6c65d8ea
3 changed files with 47 additions and 41 deletions

View file

@ -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="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"] [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 = {
"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), "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", "format": "RGBA8",
@ -51,7 +51,7 @@ data = {
} }
[sub_resource type="ImageTexture" id="ImageTexture_g5bup"] [sub_resource type="ImageTexture" id="ImageTexture_g5bup"]
image = SubResource("Image_hhwo6") image = SubResource("Image_kwuuq")
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_sj7h5"] [sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_sj7h5"]

View file

@ -8,10 +8,8 @@
[sub_resource type="CircleShape2D" id="CircleShape2D_itvjj"] [sub_resource type="CircleShape2D" id="CircleShape2D_itvjj"]
radius = 14.0 radius = 14.0
[sub_resource type="Animation" id="Animation_l8j6b"] [sub_resource type="Animation" id="Animation_6br4g"]
resource_name = "MoveLeft" resource_name = "Idle"
length = 0.5
loop_mode = 1
tracks/0/type = "value" tracks/0/type = "value"
tracks/0/imported = false tracks/0/imported = false
tracks/0/enabled = true tracks/0/enabled = true
@ -19,27 +17,10 @@ tracks/0/path = NodePath("Sprite2D:frame")
tracks/0/interp = 1 tracks/0/interp = 1
tracks/0/loop_wrap = true tracks/0/loop_wrap = true
tracks/0/keys = { tracks/0/keys = {
"times": PackedFloat32Array(0, 0.1, 0.2, 0.3, 0.4), "times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1, 1, 1, 1, 1), "transitions": PackedFloat32Array(1),
"update": 1, "update": 1,
"values": [5, 6, 7, 8, 9] "values": [0]
}
[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]
} }
[sub_resource type="Animation" id="Animation_olc18"] [sub_resource type="Animation" id="Animation_olc18"]
@ -59,6 +40,23 @@ tracks/0/keys = {
"values": [0, 1, 2, 3, 4] "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"] [sub_resource type="Animation" id="Animation_agj62"]
resource_name = "MoveRight" resource_name = "MoveRight"
length = 0.5 length = 0.5
@ -76,8 +74,10 @@ tracks/0/keys = {
"values": [10, 11, 12, 13, 14] "values": [10, 11, 12, 13, 14]
} }
[sub_resource type="Animation" id="Animation_6br4g"] [sub_resource type="Animation" id="Animation_45lfy"]
resource_name = "Idle" resource_name = "MoveUp"
length = 0.5
loop_mode = 1
tracks/0/type = "value" tracks/0/type = "value"
tracks/0/imported = false tracks/0/imported = false
tracks/0/enabled = true tracks/0/enabled = true
@ -85,10 +85,10 @@ tracks/0/path = NodePath("Sprite2D:frame")
tracks/0/interp = 1 tracks/0/interp = 1
tracks/0/loop_wrap = true tracks/0/loop_wrap = true
tracks/0/keys = { tracks/0/keys = {
"times": PackedFloat32Array(0), "times": PackedFloat32Array(0, 0.1, 0.2, 0.3, 0.4),
"transitions": PackedFloat32Array(1), "transitions": PackedFloat32Array(1, 1, 1, 1, 1),
"update": 1, "update": 1,
"values": [0] "values": [15, 16, 17, 18, 19]
} }
[sub_resource type="Animation" id="Animation_mp212"] [sub_resource type="Animation" id="Animation_mp212"]

View file

@ -38,6 +38,8 @@ func _physics_process(delta):
if delta > 0.5: if delta > 0.5:
print(delta) print(delta)
move_and_slide() move_and_slide()
else:
animation_player.stop()
pass pass
func _on_collision(body): func _on_collision(body):
@ -67,18 +69,22 @@ func sub_on_death(callback : Callable):
pass pass
func update_animation(): 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 self.velocity:
match move_vector:
Vector2.ZERO: animation_player.play("Idle") Vector2.ZERO: animation_player.play("Idle")
Vector2.LEFT: animation_player.play("MoveLeft") _: handle_diagonal_animations(self.velocity)
Vector2.RIGHT: animation_player.play("MoveRight")
Vector2.UP: animation_player.play("MoveUp")
Vector2.DOWN: animation_player.play("MoveDown")
_: handle_diagonal_animations(move_vector)
pass pass
func handle_diagonal_animations(dir : Vector2): 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") var left_dot = Vector2.LEFT.dot(dir)
else: animation_player.play("Idle") 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 pass