notSpore/langtons-ant/ant.gd

61 lines
1.6 KiB
GDScript

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 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,
}
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,
}
func _ready() -> void:
set_frame(1)
func update() -> void:
var tile: int = tiles.get_tile_colour(pos)
print("tile %s" % tile)
var newFrame: int = next_int[dir]
if tile == 0:
newFrame = (newFrame + 1) % 5
dir = next_dir[newFrame]
else:
newFrame = (newFrame) % 5
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])
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
set_frame(newFrame)
func _physics_process(delta: float) -> void:
var vel: Vector2 = Vector2(0,0)
match dir:
TileSet.CELL_NEIGHBOR_TOP_RIGHT_SIDE:
vel = Vector2(1,1)
TileSet.CELL_NEIGHBOR_RIGHT_SIDE:
vel = Vector2(1,0)
_:
vel = Vector2(0,-1)
vel = vel.normalized() * 16
global_position += vel * delta