diff --git a/assets/textures/debug/dummy-player-atlas.png b/assets/textures/debug/dummy-player-atlas.png new file mode 100644 index 0000000..86fac47 Binary files /dev/null and b/assets/textures/debug/dummy-player-atlas.png differ diff --git a/assets/textures/debug/dummy-player-atlas.png.import b/assets/textures/debug/dummy-player-atlas.png.import new file mode 100644 index 0000000..0e46850 --- /dev/null +++ b/assets/textures/debug/dummy-player-atlas.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://olceowuycu8c" +path="res://.godot/imported/dummy-player-atlas.png-68f05802eecc35f4774851352daddd72.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://assets/textures/debug/dummy-player-atlas.png" +dest_files=["res://.godot/imported/dummy-player-atlas.png-68f05802eecc35f4774851352daddd72.ctex"] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/bptc_ldr=0 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/assets/textures/debug/dummy-player-normal.png b/assets/textures/debug/dummy-player-normal.png index a76bda0..27b1b4b 100644 Binary files a/assets/textures/debug/dummy-player-normal.png and b/assets/textures/debug/dummy-player-normal.png differ diff --git a/scenes/player.tscn b/scenes/player.tscn index c9c7e2d..e9959c4 100644 --- a/scenes/player.tscn +++ b/scenes/player.tscn @@ -1,19 +1,61 @@ -[gd_scene load_steps=5 format=3 uid="uid://ujfhvssvbgpg"] +[gd_scene load_steps=13 format=3 uid="uid://ujfhvssvbgpg"] [ext_resource type="Script" path="res://src/scene-scripts/player.cs" id="1_qehox"] -[ext_resource type="Texture2D" uid="uid://377657cxmdpa" path="res://assets/textures/debug/dummy-player-normal.png" id="2_48656"] +[ext_resource type="Texture2D" uid="uid://olceowuycu8c" path="res://assets/textures/debug/dummy-player-atlas.png" id="2_yu1q5"] + +[sub_resource type="AtlasTexture" id="AtlasTexture_tirvd"] +atlas = ExtResource("2_yu1q5") +region = Rect2(0, 0, 84, 128) + +[sub_resource type="AtlasTexture" id="AtlasTexture_0qv2m"] +atlas = ExtResource("2_yu1q5") +region = Rect2(84, 0, 84, 128) + +[sub_resource type="AtlasTexture" id="AtlasTexture_qomfj"] +atlas = ExtResource("2_yu1q5") +region = Rect2(168, 0, 84, 128) + +[sub_resource type="AtlasTexture" id="AtlasTexture_ulejc"] +atlas = ExtResource("2_yu1q5") +region = Rect2(252, 0, 84, 128) + +[sub_resource type="AtlasTexture" id="AtlasTexture_1x805"] +atlas = ExtResource("2_yu1q5") +region = Rect2(252, 128, 84, 128) + +[sub_resource type="AtlasTexture" id="AtlasTexture_x338k"] +atlas = ExtResource("2_yu1q5") +region = Rect2(0, 128, 84, 128) + +[sub_resource type="AtlasTexture" id="AtlasTexture_6ynw8"] +atlas = ExtResource("2_yu1q5") +region = Rect2(84, 128, 84, 128) + +[sub_resource type="AtlasTexture" id="AtlasTexture_2yirn"] +atlas = ExtResource("2_yu1q5") +region = Rect2(168, 128, 84, 128) [sub_resource type="SpriteFrames" id="SpriteFrames_2x1cy"] animations = [{ -"frames": [ExtResource("2_48656")], +"frames": [SubResource("AtlasTexture_tirvd"), SubResource("AtlasTexture_0qv2m"), SubResource("AtlasTexture_tirvd"), SubResource("AtlasTexture_qomfj")], "loop": true, -"name": &"default", -"speed": 5.0 +"name": &"move_down", +"speed": 8.0 +}, { +"frames": [SubResource("AtlasTexture_ulejc"), SubResource("AtlasTexture_1x805")], +"loop": true, +"name": &"move_side", +"speed": 8.0 +}, { +"frames": [SubResource("AtlasTexture_x338k"), SubResource("AtlasTexture_6ynw8"), SubResource("AtlasTexture_x338k"), SubResource("AtlasTexture_2yirn")], +"loop": true, +"name": &"move_up", +"speed": 8.0 }] [sub_resource type="CapsuleShape2D" id="CapsuleShape2D_38v5o"] radius = 18.057 -height = 56.0051 +height = 43.9998 [node name="player" type="CharacterBody2D"] process_mode = 1 @@ -23,6 +65,7 @@ script = ExtResource("1_qehox") position = Vector2(-0.0625, -17.0625) scale = Vector2(0.733398, 0.733398) frames = SubResource("SpriteFrames_2x1cy") +animation = &"move_down" [node name="CollisionShape2D" type="CollisionShape2D" parent="."] position = Vector2(0, 12) diff --git a/src/scene-scripts/player.cs b/src/scene-scripts/player.cs index b882ec8..d321318 100644 --- a/src/scene-scripts/player.cs +++ b/src/scene-scripts/player.cs @@ -7,18 +7,12 @@ public partial class player : CharacterBody2D public string playerName; [Export] public int speed = 400; + public Vector2 velocity; public override void _PhysicsProcess(double delta) { - MoveAndCollide(new Vector2 - ( - Input.GetActionStrength("move_right") - - Input.GetActionStrength("move_left"), - Input.GetActionStrength("move_down") - - Input.GetActionStrength("move_up") - ).LimitLength(1) - * speed * (float)delta - ); + velocity = Input.GetVector("move_left", "move_right", "move_up", "move_down").LimitLength(1); + MoveAndCollide(velocity * speed * (float)delta); } public void ChangeProcess(bool process){ if(process) ProcessMode = ProcessModeEnum.Inherit; else ProcessMode = ProcessModeEnum.Disabled; } public override void _Process(double delta) @@ -29,9 +23,30 @@ public partial class player : CharacterBody2D if (Input.IsActionJustPressed("move_left")) GetNode("ray_cast_2d").TargetPosition = new Vector2(-raylength, 0); if (Input.IsActionJustPressed("move_down")) GetNode("ray_cast_2d").TargetPosition = new Vector2(0, raylength); if (Input.IsActionJustPressed("move_up")) GetNode("ray_cast_2d").TargetPosition = new Vector2(0, -raylength); - //call event in raycasted object if (Input.IsActionJustPressed("ui_accept") && GetNode("ray_cast_2d").IsColliding()) GetNode("ray_cast_2d").GetCollider().Call("OnInteraction", playerName); + //animation system (with controller support wcih cant get normalized vector) + var animatedSprite = GetNode("AnimatedSprite2D"); + if (velocity.Length() != 0) + animatedSprite.Play(); + else + { + animatedSprite.Frame = 0; + animatedSprite.Stop(); + } + if (Input.IsActionPressed("move_right") || Input.IsActionPressed("move_left")) + { + animatedSprite.Animation = "move_side"; + animatedSprite.FlipH = velocity.x < 0.5; + animatedSprite.SpeedScale = Math.Abs(velocity.x); + } + else if (Input.IsActionPressed("move_up") || Input.IsActionPressed("move_down")) + { + if (velocity.y > 0.05) animatedSprite.Animation = "move_down"; + if (velocity.y < 0.05) animatedSprite.Animation = "move_up"; + animatedSprite.FlipH = false; + animatedSprite.SpeedScale = Math.Abs(velocity.y); + } } }