From 408ed71722903e83350da0b1c61b3d026797dbb3 Mon Sep 17 00:00:00 2001 From: djairoh Date: Wed, 12 Nov 2025 16:45:38 +0100 Subject: [PATCH] feat: made antProgramming dynamic --- langtons-ant/TODO | 1 - langtons-ant/ant.gd | 58 +++++++++++++++++++++----------------------- langtons-ant/grid.gd | 9 +++---- 3 files changed, 32 insertions(+), 36 deletions(-) diff --git a/langtons-ant/TODO b/langtons-ant/TODO index 3133743..3518fb3 100644 --- a/langtons-ant/TODO +++ b/langtons-ant/TODO @@ -1,5 +1,4 @@ UI -> toggleButton so camera follows ant -> reset camera button -> pause button -customizable tile->dir->color logic (im thinking we do a dict: {tileState, (newDir, newTileCol)}) dynamically generate tile colours (using a big palette for indexing maybe?) this probably needs to be a shader. diff --git a/langtons-ant/ant.gd b/langtons-ant/ant.gd index d4fcb0c..8ce52db 100644 --- a/langtons-ant/ant.gd +++ b/langtons-ant/ant.gd @@ -4,7 +4,7 @@ var pos: Vector2i = Vector2i(0,0) var dir: TileSet.CellNeighbor = TileSet.CELL_NEIGHBOR_RIGHT_SIDE @onready var tiles: TileMapLayer = get_node("../TileMapLayer") -var next_dir: Dictionary = { +var cur_dir: Dictionary = { 0: TileSet.CELL_NEIGHBOR_RIGHT_SIDE, 1: TileSet.CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE, 2: TileSet.CELL_NEIGHBOR_BOTTOM_LEFT_SIDE, @@ -22,43 +22,41 @@ var cur_int: Dictionary = { TileSet.CELL_NEIGHBOR_TOP_RIGHT_SIDE: 5, } +var antProgram: Dictionary = { + 0: Vector2i(-2, 1), + 1: Vector2i(0, 2), + 2: Vector2i(0, 3), + 3: Vector2i(-1, 4), + 4: Vector2i(-2, 5), + 5: Vector2i(-1, 0), +} + +func getNextStep(tile: int) -> Vector2i: + return antProgram.get(tile, Vector2i(0,0)) + func _ready() -> void: self.set_z_index(1) self.set_frame(cur_int[dir]) - tiles.set_tile(pos, (tiles.get_tile_colour(pos))) # TODO: should probably use another dictionary - move() + tiles.set_tile(pos, tiles.get_tile_colour(pos)) + move(dir) +# update loop: +# move in dir +# get tile col, change dir based on col +# paint tile col func update() -> void: - var tile: int = tiles.get_tile_colour(pos) - var newFrame: int = cur_int[dir] - - # TODO: turn into dict{tileCol, (nextDir, paintCol)} customizable - match tile: - 0: - newFrame = (newFrame + 5) % tiles.numTiles - 1: - newFrame = (newFrame + 4) % tiles.numTiles - 2: - newFrame = (newFrame + 0) % tiles.numTiles - 3: - newFrame = (newFrame + 0) % tiles.numTiles - 4: - newFrame = (newFrame + 5) % tiles.numTiles - 5: - newFrame = (newFrame + 4) % tiles.numTiles - _: - pass - dir = next_dir[newFrame] - - move() + move(dir) pos = tiles.get_neighbor_cell(pos, dir) - tile = tiles.get_tile_colour(pos) - tiles.set_tile(pos, (tile + 1) % tiles.numTiles) # TODO: should probably use another dictionary - self.set_frame(newFrame) -func move() -> void: + var tile: int = tiles.get_tile_colour(pos) + var res = getNextStep(tile) + dir = cur_dir[(cur_int[dir] + tiles.numTiles + res.x) % tiles.numTiles] + self.set_frame(dir) + tiles.set_tile(pos, res.y % tiles.numTiles) + +func move(to: TileSet.CellNeighbor) -> void: var delta: Vector2 - match dir: + match to: TileSet.CELL_NEIGHBOR_RIGHT_SIDE: delta = Vector2(tiles.texSize.x, 0) TileSet.CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE: diff --git a/langtons-ant/grid.gd b/langtons-ant/grid.gd index 9d445ae..a8d98aa 100644 --- a/langtons-ant/grid.gd +++ b/langtons-ant/grid.gd @@ -1,11 +1,10 @@ extends Node2D -@export var stepSize: float = 2 -@export var ant: Sprite2D -@export var tiles: TileMapLayer var t: float = -1 var paused: bool = false - +@export var stepSize: float = 20 +@export var ant: Sprite2D +@export var tiles: TileMapLayer # Called when the node enters the scene tree for the first time. func _ready() -> void: pass @@ -19,6 +18,6 @@ func _process(delta: float) -> void: if paused: return t += delta - if (t > stepSize/100): + if (t > stepSize/1000): t = 0 ant.update()