diff options
| author | Matheus <matheus.guedes.mg.m@gmail.com> | 2025-08-16 13:38:40 -0300 |
|---|---|---|
| committer | Matheus <matheus.guedes.mg.m@gmail.com> | 2025-08-16 13:38:40 -0300 |
| commit | 588a80a339a64f15b2e3bada550beb14e16abe3e (patch) | |
| tree | d5ae45279170e594ff1f3c589c070006b1bc7e90 | |
| parent | 579a2fe0f837236eec5d9f9347baa8f6a49f6458 (diff) | |
Parar de verificar input a cada frame.
| -rw-r--r-- | scenes/character.tscn | 4 | ||||
| -rw-r--r-- | scenes/test_map.tscn | 12 | ||||
| -rw-r--r-- | scripts/Character.cs | 29 |
3 files changed, 24 insertions, 21 deletions
diff --git a/scenes/character.tscn b/scenes/character.tscn index c99e194..825f5bf 100644 --- a/scenes/character.tscn +++ b/scenes/character.tscn @@ -1,7 +1,9 @@ -[gd_scene load_steps=2 format=3 uid="uid://bl01llfbscv6r"] +[gd_scene load_steps=3 format=3 uid="uid://bl01llfbscv6r"] [ext_resource type="Texture2D" uid="uid://dwky8qc2y602k" path="res://assets/sprites/character.png" id="1_agfs1"] +[ext_resource type="Script" uid="uid://c840l08453pu2" path="res://scripts/Character.cs" id="2_rkhd4"] [node name="Character" type="Sprite2D"] texture_filter = 1 texture = ExtResource("1_agfs1") +script = ExtResource("2_rkhd4") diff --git a/scenes/test_map.tscn b/scenes/test_map.tscn index 427fd21..14a3aa4 100644 --- a/scenes/test_map.tscn +++ b/scenes/test_map.tscn @@ -1,8 +1,7 @@ -[gd_scene load_steps=4 format=4 uid="uid://u5h6iqyi8wd0"] +[gd_scene load_steps=3 format=4 uid="uid://u5h6iqyi8wd0"] [ext_resource type="TileSet" uid="uid://biilh0ufacguh" path="res://assets/test_tileset.tres" id="1_57l66"] [ext_resource type="PackedScene" uid="uid://bl01llfbscv6r" path="res://scenes/character.tscn" id="2_fn2sa"] -[ext_resource type="Script" uid="uid://c840l08453pu2" path="res://scripts/Character.cs" id="3_57l66"] [node name="Node2D" type="Node2D"] @@ -11,11 +10,10 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AAAAWAAoAAAABAAAAAAAXAAoAAAABAAAAAAAXAAkAAAABAAAAAAAXAAgAAAABAAAAAAAXAAcAAAABAAAAAAAXAAYAAAABAAAAAAAXAAUAAAABAAAAAAAXAAQAAAABAAAAAAAXAAMAAAABAAAAAAAWAAMAAAABAAAAAAAVAAMAAAABAAAAAAAUAAMAAAABAAAAAAATAAMAAAABAAAAAAASAAMAAAABAAAAAAARAAMAAAABAAAAAAAQAAMAAAABAAAAAAAPAAMAAAABAAAAAAAPAAQAAAABAAAAAAAPAAUAAAABAAAAAAAQAAkAAAAAAAAAAAARAAkAAAAAAAAAAAARAAgAAAAAAAAAAAASAAgAAAAAAAAAAAATAAgAAAAAAAAAAAAUAAkAAAAAAAAAAAATAAkAAAAAAAAAAAARAAcAAAAAAAAAAAAQAAcAAAAAAAAAAAAQAAYAAAAAAAAAAAAQAAUAAAAAAAAAAAARAAUAAAAAAAAAAAARAAQAAAAAAAAAAAASAAQAAAAAAAAAAAATAAQAAAAAAAAAAAAUAAQAAAAAAAAAAAAUAAUAAAAAAAAAAAAVAAUAAAAAAAAAAAAVAAYAAAAAAAAAAAAVAAcAAAAAAAAAAAAVAAgAAAAAAAAAAAAVAAkAAAAAAAAAAAASAAcAAAAAAAAAAAASAAYAAAAAAAAAAAASAAUAAAAAAAAAAAATAAUAAAAAAAAAAAAUAAYAAAAAAAAAAAAUAAcAAAAAAAAAAAAUAAgAAAAAAAAAAAATAAcAAAAAAAAAAAATAAYAAAAAAAAAAAAQAAQAAAAAAAAAAAAQAAgAAAAAAAAAAAARAAYAAAAAAAAAAAASAAkAAAAAAAAAAAAWAAkAAAAAAAAAAAAWAAgAAAAAAAAAAAAWAAcAAAAAAAAAAAAWAAYAAAAAAAAAAAAWAAUAAAAAAAAAAAAWAAQAAAAAAAAAAAAVAAQAAAAAAAAAAAA=") tile_set = ExtResource("1_57l66") -[node name="Character" parent="." node_paths=PackedStringArray("map") instance=ExtResource("2_fn2sa")] -position = Vector2(152, 136) -script = ExtResource("3_57l66") -map = NodePath("../Map") - [node name="Camera2D" type="Camera2D" parent="."] anchor_mode = 0 zoom = Vector2(2, 2) + +[node name="Character" parent="." node_paths=PackedStringArray("map") instance=ExtResource("2_fn2sa")] +position = Vector2(135, 135) +map = NodePath("../Map") diff --git a/scripts/Character.cs b/scripts/Character.cs index d1865f1..1e11fcc 100644 --- a/scripts/Character.cs +++ b/scripts/Character.cs @@ -4,21 +4,25 @@ using System; public partial class Character : Sprite2D { [Export] public TileMapLayer map; - public override void _Process(double delta) { - base._Process(delta); + + public override void _Input(InputEvent @event) + { + base._Input(@event); + + if (!@event.IsPressed()) return; Vector2I offset = Vector2I.Zero; - if (Input.IsActionJustPressed("walk-up")) { + if (@event.IsActionPressed("walk-up")) { offset += Vector2I.Up; } - if (Input.IsActionJustPressed("walk-down")) { + if (@event.IsActionPressed("walk-down")) { offset += Vector2I.Down; } - if (Input.IsActionJustPressed("walk-left")) { + if (@event.IsActionPressed("walk-left")) { offset += Vector2I.Left; } - if (Input.IsActionJustPressed("walk-right")) { + if (@event.IsActionPressed("walk-right")) { offset += Vector2I.Right; } @@ -28,15 +32,14 @@ public partial class Character : Sprite2D { } private void Walk(Vector2I offset) { - Vector2I gridCoords = map.LocalToMap(Position); - gridCoords += offset; - - TileData tile = map.GetCellTileData(gridCoords); - + Vector2I toMovePos = map.LocalToMap(Position); + toMovePos += offset; + TileData tile = map.GetCellTileData(toMovePos); + if (tile.HasCustomData("isWalkable") && (bool) tile.GetCustomData("isWalkable")) { - GD.Print(gridCoords); - Position = map.MapToLocal(gridCoords); + GD.Print(toMovePos); + Position = map.MapToLocal(toMovePos); } } } |
