Skip to content

Commit 1cd3b6a

Browse files
committed
Add a tile-based troll that moves between cells
Games with hexagonal maps usually want characters to stay within the grid, so add another movement option. This troll has no physics and uses tweens to move. Instead of collision, it uses tile values to determine where they're allowed to go.
1 parent 3a3e186 commit 1cd3b6a

15 files changed

+296
-6
lines changed

2d/hexagonal_map/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Hexagonal Game
22

3-
Very simple demo showing a hexagonal TileMap and TileSet.
3+
Very simple demo showing a hexagonal TileMap and TileSet. Has two movement modes:
4+
* Moving from one hex tile to another.
5+
* Freely moving without snapping to tiles.
46

57
Language: GDScript
68

2d/hexagonal_map/arrow.png

1.04 KB
Loading

2d/hexagonal_map/arrow.png.import

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="CompressedTexture2D"
5+
uid="uid://c72pk3msag483"
6+
path="res://.godot/imported/arrow.png-9a52328c9c8f79a188b7fabb13b1f3fc.ctex"
7+
metadata={
8+
"vram_texture": false
9+
}
10+
11+
[deps]
12+
13+
source_file="res://arrow.png"
14+
dest_files=["res://.godot/imported/arrow.png-9a52328c9c8f79a188b7fabb13b1f3fc.ctex"]
15+
16+
[params]
17+
18+
compress/mode=0
19+
compress/high_quality=false
20+
compress/lossy_quality=0.7
21+
compress/uastc_level=0
22+
compress/rdo_quality_loss=0.0
23+
compress/hdr_compression=1
24+
compress/normal_map=0
25+
compress/channel_pack=0
26+
mipmaps/generate=false
27+
mipmaps/limit=-1
28+
roughness/mode=0
29+
roughness/src_normal=""
30+
process/channel_remap/red=0
31+
process/channel_remap/green=1
32+
process/channel_remap/blue=2
33+
process/channel_remap/alpha=3
34+
process/fix_alpha_border=true
35+
process/premult_alpha=false
36+
process/normal_map_invert_y=false
37+
process/hdr_as_srgb=false
38+
process/hdr_clamp_exposure=false
39+
process/size_limit=0
40+
detect_3d/compress_to=1

2d/hexagonal_map/grid_layer.gd

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
extends TileMapLayer
2+
3+
4+
# Returns a global position that's the bottom corner of a tile.
5+
# https://www.reddit.com/r/godot/comments/fuejci/having_trouble_with_snapping_to_isometric_grid/fmcl07f/
6+
func snap_global_to_cell(world_pos: Vector2) -> Vector2:
7+
# map_to_local says it's the center, but it ignores texture_origin.
8+
return to_global(map_to_local(local_to_map(to_local(world_pos))))
9+
10+
11+
func global_to_cell_pos(world_pos: Vector2) -> Vector2i:
12+
return local_to_map(to_local(world_pos))
13+
14+
15+
func cell_to_global_pos(cell_pos: Vector2i) -> Vector2:
16+
return to_global(map_to_local(cell_pos))
17+
18+
19+
# Change the visual appearance of tile at position world_pos. Select a TileMapLayer and
20+
# view TileSet > Tile Sources. Use the ID field in Setup to select that tile.
21+
func set_world_tile(world_pos: Vector2, id: int) -> void:
22+
var cell_space: Vector2i = global_to_cell_pos(world_pos)
23+
set_cell(cell_space, id)
24+
25+
26+
func get_world_tile(world_pos: Vector2) -> int:
27+
var cell_space: Vector2i = global_to_cell_pos(world_pos)
28+
var id: int = get_cell_source_id(cell_space)
29+
return id

2d/hexagonal_map/grid_layer.gd.uid

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://dfr4hpa3ar62a

2d/hexagonal_map/map.tscn

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
[gd_scene load_steps=3 format=3 uid="uid://4ug0kqe00j8g"]
1+
[gd_scene load_steps=6 format=4 uid="uid://4ug0kqe00j8g"]
22

33
[ext_resource type="TileSet" uid="uid://bgao55br0duys" path="res://tileset.tres" id="1"]
4-
[ext_resource type="PackedScene" uid="uid://bhgyilxufjioq" path="res://troll.tscn" id="2"]
4+
[ext_resource type="PackedScene" uid="uid://bhgyilxufjioq" path="res://troll_free.tscn" id="2"]
5+
[ext_resource type="Script" path="res://grid_layer.gd" id="2_p5pve"]
6+
[ext_resource type="PackedScene" uid="uid://guyl5tp23ex5" path="res://troll_tile.tscn" id="3_e0an3"]
7+
[ext_resource type="Script" path="res://troll_toggler.gd" id="3_urblq"]
58

69
[node name="Map" type="Node2D"]
710

@@ -11,7 +14,23 @@
1114
use_parent_material = true
1215
tile_map_data = PackedByteArray("AAAFAPn/AAAAAAAAAAAGAPn/AAAAAAAAAAAEAPr/AAAAAAAAAAAFAPr/AAAAAAAAAAAGAPr/AAAAAAAAAAAHAPr/AAAAAAAAAAAIAPr/AAAAAAAAAAAJAPr/AAAAAAAAAAACAPv/AAAAAAAAAAADAPv/AAAAAAAAAAAEAPv/AAAAAAAAAAAFAPv/BgAAAAAAAAAGAPv/BgAAAAAAAAAHAPv/BgAAAAAAAAAIAPv/BgAAAAAAAAAJAPv/AAAAAAAAAAAKAPv/AAAAAAAAAAALAPv/AAAAAAAAAAACAPz/AAAAAAAAAAADAPz/AAAAAAAAAAAEAPz/BgAAAAAAAAAFAPz/BgAAAAAAAAAGAPz/BgAAAAAAAAAHAPz/BgAAAAAAAAAIAPz/BgAAAAAAAAAJAPz/AAAAAAAAAAAKAPz/AAAAAAAAAAALAPz/AAAAAAAAAAAMAPz/AAAAAAAAAAANAPz/AAAAAAAAAAACAP3/AAAAAAAAAAADAP3/AAAAAAAAAAAEAP3/BgAAAAAAAAAFAP3/BgAAAAAAAAAGAP3/BgAAAAAAAAAHAP3/BgAAAAAAAAAIAP3/AQAAAAAAAAAJAP3/AAAAAAAAAAAKAP3/AQAAAAAAAAALAP3/AQAAAAAAAAAMAP3/AAAAAAAAAAANAP3/AAAAAAAAAAAOAP3/AAAAAAAAAAABAP7/CQAAAAAAAAACAP7/AAAAAAAAAAADAP7/AAAAAAAAAAAEAP7/AgAAAAAAAAAFAP7/AgAAAAAAAAAGAP7/AAAAAAAAAAAHAP7/FQAAAAAAAAAIAP7/EwAAAAAAAAAJAP7/AAAAAAAAAAAKAP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AAAPAAIAAAAAAAAAAAAQAAIAAAAAAAAAAAARAAIAAAAAAAAAAAACAAMAAAAAAAAAAAADAAMAAAAAAAAAAAAEAAMAAAAAAAAAAAAFAAMAFwAAAAAAAAAGAAMAAAAAAAAAAAAHAAMAAAAAAAAAAAAIAAMAAAAAAAAAAAAJAAMABQAAAAAAAAAKAAMABQAAAAAAAAALAAMAAAAAAAAAAAAMAAMAAAAAAAAAAAANAAMAAAAAAAAAAAAOAAMAAAAAAAAAAAAPAAMAFwAAAAAAAAAQAAMAAAAAAAAAAAAEAAQAAAAAAAAAAAAFAAQAAAAAAAAAAAAGAAQAAAAAAAAAAAAHAAQAAAAAAAAAAAAIAAQACAAAAAAAAAAJAAQABQAAAAAAAAAKAAQABQAAAAAAAAALAAQAAAAAAAAAAAAMAAQAAAAAAAAAAAANAAQAFQAAAAAAAAAOAAQAAAAAAAAAAAAPAAQAAAAAAAAAAAAQAAQAAAAAAAAAAAAGAAUAAAAAAAAAAAAHAAUAAAAAAAAAAAAIAAUAAAAAAAAAAAAJAAUAAAAAAAAAAAAKAAUAAAAAAAAAAAALAAUAAAAAAAAAAAAMAAUAAAAAAAAAAAANAAUAAAAAAAAAAAAOAAUAAAAAAAAAAAA=")
1316
tile_set = ExtResource("1")
17+
script = ExtResource("2_p5pve")
1418

15-
[node name="Troll" parent="." instance=ExtResource("2")]
19+
[node name="TrollSpawner" type="Node2D" parent="."]
20+
position = Vector2(599, 98)
21+
script = ExtResource("3_urblq")
22+
23+
[node name="TileMovementTroll" parent="TrollSpawner" node_paths=PackedStringArray("grid") instance=ExtResource("3_e0an3")]
24+
position = Vector2(-174, 28)
25+
grid = NodePath("../../Tiles/Layer0")
26+
27+
[node name="FreeMovementTroll" parent="TrollSpawner" instance=ExtResource("2")]
1628
modulate = Color(1.5, 1.5, 1.5, 1)
17-
position = Vector2(602.819, -39.2876)
29+
position = Vector2(3.81897, -137.288)
30+
31+
[node name="CanvasLayer" type="CanvasLayer" parent="."]
32+
33+
[node name="Label" type="Label" parent="CanvasLayer"]
34+
offset_right = 40.0
35+
offset_bottom = 23.0
36+
text = "Press Enter/Start to toggle between Free and Tile-Based Movement."

2d/hexagonal_map/project.godot

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ move_up={
6060
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194320,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
6161
]
6262
}
63+
toggle_movement_method={
64+
"deadzone": 0.2,
65+
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194309,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
66+
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":6,"pressure":0.0,"pressed":true,"script":null)
67+
]
68+
}
6369

6470
[physics]
6571

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ const MOTION_SPEED = 30
44
const FRICTION_FACTOR = 0.89
55
const TAN30DEG = tan(deg_to_rad(30))
66

7+
8+
func become_active_troll() -> void:
9+
pass
10+
11+
712
func _physics_process(_delta: float) -> void:
813
var motion := Vector2()
914
motion.x = Input.get_axis(&"move_left", &"move_right")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[gd_scene load_steps=4 format=3 uid="uid://bhgyilxufjioq"]
22

3-
[ext_resource type="Script" uid="uid://qcncmx710eij" path="res://troll.gd" id="1"]
3+
[ext_resource type="Script" uid="uid://qcncmx710eij" path="res://troll_free.gd" id="1"]
44
[ext_resource type="Texture2D" uid="uid://bhkphpfidntnn" path="res://troll.png" id="2"]
55

66
[sub_resource type="CircleShape2D" id="1"]

0 commit comments

Comments
 (0)