From e39c090fa38e4e9faa895d7933f041b8f8a8e5c0 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Wed, 29 Jul 2020 23:43:29 +0300 Subject: Turn player to look at walking dir --- scenes/Scene1.tscn | 56 +++++++++++++++++++++++----------------------- scripts/DwarfNPC.gd | 5 +++-- scripts/PlayerCharacter.gd | 12 ++++++++-- 3 files changed, 41 insertions(+), 32 deletions(-) diff --git a/scenes/Scene1.tscn b/scenes/Scene1.tscn index d6a9d88..4b37eba 100644 --- a/scenes/Scene1.tscn +++ b/scenes/Scene1.tscn @@ -29,46 +29,46 @@ points = PoolVector2Array( 8, 8, 0, 8, 0, 4, 8, 4 ) [sub_resource type="ConvexPolygonShape2D" id=8] points = PoolVector2Array( 8, 8, 4, 8, 4, 4, 8, 4 ) -[sub_resource type="ConvexPolygonShape2D" id=10] +[sub_resource type="ConvexPolygonShape2D" id=9] points = PoolVector2Array( 8, 8, 4, 8, 4, 0, 8, 0 ) -[sub_resource type="ConvexPolygonShape2D" id=11] +[sub_resource type="ConvexPolygonShape2D" id=10] points = PoolVector2Array( 0, 0, 4, 0, 4, 8, 0, 8 ) -[sub_resource type="ConvexPolygonShape2D" id=12] +[sub_resource type="ConvexPolygonShape2D" id=11] points = PoolVector2Array( 4, 8, 0, 8, 0, 0, 4, 0 ) -[sub_resource type="ConvexPolygonShape2D" id=13] +[sub_resource type="ConvexPolygonShape2D" id=12] points = PoolVector2Array( 4, 8, 0, 8, 0, 4, 4, 4 ) -[sub_resource type="ConvexPolygonShape2D" id=14] +[sub_resource type="ConvexPolygonShape2D" id=13] points = PoolVector2Array( 4, 8, 0, 8, 0, 0, 4, 0 ) -[sub_resource type="ConvexPolygonShape2D" id=16] +[sub_resource type="ConvexPolygonShape2D" id=14] points = PoolVector2Array( 8, 8, 0, 8, 0, 4, 8, 4 ) -[sub_resource type="ConvexPolygonShape2D" id=17] +[sub_resource type="ConvexPolygonShape2D" id=15] points = PoolVector2Array( 4, 0, 4, 4, 8, 4, 8, 8, 0, 8, 0, 0 ) -[sub_resource type="ConvexPolygonShape2D" id=18] +[sub_resource type="ConvexPolygonShape2D" id=16] points = PoolVector2Array( 4, 4, 4, 0, 8, 0, 8, 8, 0, 8, 0, 4 ) -[sub_resource type="ConvexPolygonShape2D" id=19] +[sub_resource type="ConvexPolygonShape2D" id=17] points = PoolVector2Array( 8, 8, 0, 8, 0, 4, 8, 4 ) -[sub_resource type="ConvexPolygonShape2D" id=20] +[sub_resource type="ConvexPolygonShape2D" id=18] points = PoolVector2Array( 8, 8, 0, 8, 0, 4, 8, 4 ) -[sub_resource type="ConvexPolygonShape2D" id=21] +[sub_resource type="ConvexPolygonShape2D" id=19] points = PoolVector2Array( 8, 8, 0, 8, 0, 4, 8, 4 ) -[sub_resource type="ConvexPolygonShape2D" id=22] +[sub_resource type="ConvexPolygonShape2D" id=20] points = PoolVector2Array( 8, 8, 0, 8, 0, 4, 8, 4 ) -[sub_resource type="ConvexPolygonShape2D" id=24] +[sub_resource type="ConvexPolygonShape2D" id=21] points = PoolVector2Array( 4, 0, 4, 4, 8, 4, 8, 8, 0, 8, 0, 0 ) -[sub_resource type="ConvexPolygonShape2D" id=25] +[sub_resource type="ConvexPolygonShape2D" id=22] points = PoolVector2Array( 4, 4, 4, 0, 8, 0, 8, 8, 0, 8, 0, 4 ) [sub_resource type="TileSet" id=23] @@ -144,85 +144,85 @@ points = PoolVector2Array( 4, 4, 4, 0, 8, 0, 8, 8, 0, 8, 0, 4 ) "autotile_coord": Vector2( 2, 2 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 10 ), +"shape": SubResource( 9 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 3, 2 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 11 ), +"shape": SubResource( 10 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 1, 2 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 12 ), +"shape": SubResource( 11 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 1, 1 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 13 ), +"shape": SubResource( 12 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 3, 1 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 14 ), +"shape": SubResource( 13 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 5, 0 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 16 ), +"shape": SubResource( 14 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 4, 1 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 17 ), +"shape": SubResource( 15 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 5, 1 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 18 ), +"shape": SubResource( 16 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 6, 1 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 19 ), +"shape": SubResource( 17 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 7, 1 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 20 ), +"shape": SubResource( 18 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 6, 2 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 21 ), +"shape": SubResource( 19 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 7, 2 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 22 ), +"shape": SubResource( 20 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 4, 2 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 24 ), +"shape": SubResource( 21 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) }, { "autotile_coord": Vector2( 5, 2 ), "one_way": false, "one_way_margin": 1.0, -"shape": SubResource( 25 ), +"shape": SubResource( 22 ), "shape_transform": Transform2D( 1, 0, 0, 1, 0, 0 ) } ] 0/z_index = 0 diff --git a/scripts/DwarfNPC.gd b/scripts/DwarfNPC.gd index 3cacc3c..b55fff2 100644 --- a/scripts/DwarfNPC.gd +++ b/scripts/DwarfNPC.gd @@ -15,7 +15,8 @@ func _ready(): # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(_delta): + # Look at target node var target_x: float = target_node.position[0] var dx = target_x - self.position[0] - var scale_val = -1 if dx < .0 else 1 - $Sprite.set_scale(Vector2(scale_val, 1)) + var dir = -1 if dx < .0 else 1 + $Sprite.set_scale(Vector2(dir, 1)) diff --git a/scripts/PlayerCharacter.gd b/scripts/PlayerCharacter.gd index 06602a4..9f52a23 100644 --- a/scripts/PlayerCharacter.gd +++ b/scripts/PlayerCharacter.gd @@ -3,6 +3,8 @@ extends KinematicBody2D export (int) var speed = 8 export (float) var walk_wobble_coeff = 5 var velocity = Vector2() +var look_dir = 1 # right = 1 + # left = -1 func get_input(): velocity = Vector2() @@ -17,6 +19,7 @@ func get_input(): velocity = velocity.normalized() * speed func _process(_delta): + # Update walking wobble var is_moving = velocity.length_squared() > .0 if is_moving: var time = OS.get_system_time_msecs() / 1000.0 @@ -25,8 +28,13 @@ func _process(_delta): else: $Sprite.set_rotation(0) - #$Sprite.z_index = self.position[1] - #print($Sprite.z_index) + # Look at walking direction + if velocity[0] > .0: + look_dir = 1 + elif velocity[0] < .0: + look_dir = -1 + + $Sprite.set_scale(Vector2(look_dir, 1)) func _physics_process(_delta): get_input() -- cgit v1.3