feat: hexagonal ant

This commit is contained in:
Djairo Hougee 2025-11-12 12:11:00 +01:00
parent 25ec8d2585
commit a49f55b694
6 changed files with 73 additions and 45 deletions

View File

@ -1,2 +0,0 @@
Ant navigation is wonky -> check if next_dir behaves as expected.
ant sprite does not budge from centre of screen no matter what i try -> fix that

View File

@ -1,60 +1,72 @@
extends Sprite2D
var pos: Vector2i = Vector2i(0,0)
var dir: TileSet.CellNeighbor = TileSet.CELL_NEIGHBOR_TOP_RIGHT_SIDE
@onready var tiles: TileMapLayer = get_node("TileMapLayer")
var dir: TileSet.CellNeighbor = TileSet.CELL_NEIGHBOR_RIGHT_SIDE
@onready var tiles: TileMapLayer = get_node("../TileMapLayer")
var next_dir: Dictionary = {
0: TileSet.CELL_NEIGHBOR_RIGHT_SIDE,
1: TileSet.CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE,
2: TileSet.CELL_NEIGHBOR_BOTTOM_LEFT_SIDE,
3: TileSet.CELL_NEIGHBOR_TOP_LEFT_SIDE,
4: TileSet.CELL_NEIGHBOR_TOP_RIGHT_SIDE,
3: TileSet.CELL_NEIGHBOR_LEFT_SIDE,
4: TileSet.CELL_NEIGHBOR_TOP_LEFT_SIDE,
5: TileSet.CELL_NEIGHBOR_TOP_RIGHT_SIDE,
}
var next_int: Dictionary = {
TileSet.CELL_NEIGHBOR_RIGHT_SIDE: 1,
TileSet.CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE: 2,
TileSet.CELL_NEIGHBOR_BOTTOM_LEFT_SIDE: 3,
TileSet.CELL_NEIGHBOR_TOP_LEFT_SIDE: 4,
TileSet.CELL_NEIGHBOR_TOP_RIGHT_SIDE: 0,
var cur_int: Dictionary = {
TileSet.CELL_NEIGHBOR_RIGHT_SIDE: 0,
TileSet.CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE: 1,
TileSet.CELL_NEIGHBOR_BOTTOM_LEFT_SIDE: 2,
TileSet.CELL_NEIGHBOR_TOP_LEFT_SIDE: 3,
TileSet.CELL_NEIGHBOR_LEFT_SIDE: 4,
TileSet.CELL_NEIGHBOR_TOP_RIGHT_SIDE: 5,
}
func _ready() -> void:
set_frame(1)
set_z_index(1)
tiles.set_tile(pos, (tiles.get_tile_colour(pos) + 1) % 2) # TODO: should probably use another dictionary
move()
func update() -> void:
var tile: int = tiles.get_tile_colour(pos)
print("tile %s" % tile)
var newFrame: int = next_int[dir]
var newFrame: int = cur_int[dir]
if tile == 0:
newFrame = (newFrame + 1) % 5
dir = next_dir[newFrame]
else:
newFrame = (newFrame) % 5
dir = next_dir[newFrame]
match tile:
0:
newFrame = (newFrame + 1) % 6
1:
newFrame = (newFrame + 1) % 6
2:
newFrame = (newFrame + 1) % 6
3:
newFrame = (newFrame + 5) % 6
_:
pass
dir = next_dir[newFrame]
#var tmpPos: Vector2i
#for i in range(0, 16):
#tmpPos = tiles.get_neighbor_cell(pos, i)
#print("%s: (%d %d)" % [i, tmpPos.x, tmpPos.y])
print("(%d %d)" % [pos.x, pos.y])
move()
pos = tiles.get_neighbor_cell(pos, dir)
tile = tiles.get_tile_colour(pos)
print("(%d %d)" % [pos.x, pos.y])
tiles.set_tile(pos, (tile + 1) % 2) # TODO: should probably use another dictionary
tiles.set_tile(pos, (tile + 1) % 4) # TODO: should probably use another dictionary
set_frame(newFrame)
func _physics_process(delta: float) -> void:
var vel: Vector2 = Vector2(0,0)
func move() -> void:
var delta: Vector2
match dir:
TileSet.CELL_NEIGHBOR_TOP_RIGHT_SIDE:
vel = Vector2(1,1)
TileSet.CELL_NEIGHBOR_RIGHT_SIDE:
vel = Vector2(1,0)
delta = Vector2(32, 0)
TileSet.CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
delta = Vector2(16, 24)
TileSet.CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
delta = Vector2(-16, 24)
TileSet.CELL_NEIGHBOR_LEFT_SIDE:
delta = Vector2(-32, 0)
TileSet.CELL_NEIGHBOR_TOP_LEFT_SIDE:
delta = Vector2(-16, -24)
TileSet.CELL_NEIGHBOR_TOP_RIGHT_SIDE:
delta = Vector2(16, -24)
_:
vel = Vector2(0,-1)
vel = vel.normalized() * 16
global_position += vel * delta
delta = Vector2(0,0)
translate(delta)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 404 B

After

Width:  |  Height:  |  Size: 869 B

View File

@ -16,3 +16,4 @@ func _process(delta: float) -> void:
if (t > 60-speed):
t = 0
ant.update()
#tiles.render_next()

View File

@ -27,17 +27,20 @@ sources/0 = SubResource("TileSetAtlasSource_g2qvd")
script = ExtResource("1_bghhw")
speed = 60.0
ant = NodePath("ant")
tiles = NodePath("ant/TileMapLayer")
tiles = NodePath("TileMapLayer")
[node name="ant" type="Sprite2D" parent="."]
texture = ExtResource("3_sle3t")
vframes = 6
script = ExtResource("1_ebq2e")
[node name="Camera2D" type="Camera2D" parent="ant"]
[node name="TileMapLayer" type="TileMapLayer" parent="ant"]
position = Vector2(-74, -8)
[node name="TileMapLayer" type="TileMapLayer" parent="."]
tile_set = SubResource("TileSet_05i0m")
collision_enabled = false
script = ExtResource("5_fqc2p")
[node name="Camera2D" type="Camera2D" parent="TileMapLayer"]
position = Vector2(0, -8)
scale = Vector2(0.8, 0.8)
[node name="ant" type="Sprite2D" parent="."]
position = Vector2(-15.999998, 14.999998)
scale = Vector2(0.8, 0.8)
texture = ExtResource("3_sle3t")
vframes = 6
script = ExtResource("1_ebq2e")

View File

@ -1,6 +1,20 @@
extends TileMapLayer
var tiles: Dictionary = {}
var idx: int = 0
var dirs: Array[int] = [
TileSet.CELL_NEIGHBOR_TOP_RIGHT_SIDE,
TileSet.CELL_NEIGHBOR_RIGHT_SIDE,
TileSet.CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE,
TileSet.CELL_NEIGHBOR_BOTTOM_LEFT_SIDE,
TileSet.CELL_NEIGHBOR_LEFT_SIDE,
TileSet.CELL_NEIGHBOR_TOP_LEFT_SIDE,
]
func render_next() -> void:
print("rendering {i}")
set_cell(get_neighbor_cell(Vector2i(0, 0), dirs[idx]), 0, Vector2i(0, 1))
idx = (idx + 1) % 6
func get_tile_colour(pos: Vector2i) -> int:
return tiles.get_or_add(pos, 0)