summaryrefslogtreecommitdiff
path: root/scripts/Character.cs
blob: 1e11fcc58a47c7841dc057832d66d5ff11f255dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using Godot;
using System;

public partial class Character : Sprite2D {
	[Export]
	public TileMapLayer map;

	public override void _Input(InputEvent @event)
	{
		base._Input(@event);

		if (!@event.IsPressed()) return;

		Vector2I offset = Vector2I.Zero;

		if (@event.IsActionPressed("walk-up")) {
			offset += Vector2I.Up;
		}
		if (@event.IsActionPressed("walk-down")) {
			offset += Vector2I.Down;
		}
		if (@event.IsActionPressed("walk-left")) {
			offset += Vector2I.Left;
		}
		if (@event.IsActionPressed("walk-right")) {
			offset += Vector2I.Right;
		}

		if (offset != Vector2I.Zero) {
			Walk(offset);
		}
	}

	private void Walk(Vector2I offset) {
		Vector2I toMovePos = map.LocalToMap(Position);
		toMovePos += offset;
		
		TileData tile = map.GetCellTileData(toMovePos);

		if (tile.HasCustomData("isWalkable") && (bool) tile.GetCustomData("isWalkable")) {
			GD.Print(toMovePos);
			Position = map.MapToLocal(toMovePos);
		}
	}
}