diff --git a/langtons-ant/TODO b/langtons-ant/TODO deleted file mode 100644 index ecfb606..0000000 --- a/langtons-ant/TODO +++ /dev/null @@ -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 diff --git a/langtons-ant/ant.gd b/langtons-ant/ant.gd index 904610c..0a42e4c 100644 --- a/langtons-ant/ant.gd +++ b/langtons-ant/ant.gd @@ -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) diff --git a/langtons-ant/assets/tiles.png b/langtons-ant/assets/tiles.png index 9c378e5..2d398f1 100644 Binary files a/langtons-ant/assets/tiles.png and b/langtons-ant/assets/tiles.png differ diff --git a/langtons-ant/grid.gd b/langtons-ant/grid.gd index 60c7912..f154bbd 100644 --- a/langtons-ant/grid.gd +++ b/langtons-ant/grid.gd @@ -16,3 +16,4 @@ func _process(delta: float) -> void: if (t > 60-speed): t = 0 ant.update() + #tiles.render_next() diff --git a/langtons-ant/grid.tscn b/langtons-ant/grid.tscn index f6069f5..9a4d25b 100644 --- a/langtons-ant/grid.tscn +++ b/langtons-ant/grid.tscn @@ -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") diff --git a/langtons-ant/tile_map_layer.gd b/langtons-ant/tile_map_layer.gd index e3b9dfc..72a6313 100644 --- a/langtons-ant/tile_map_layer.gd +++ b/langtons-ant/tile_map_layer.gd @@ -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)