feat: ant randomization

This commit is contained in:
Djairo Hougee 2025-11-12 20:17:29 +01:00
parent 9156f8b979
commit b3d488e031
4 changed files with 42 additions and 20 deletions

View File

@ -3,8 +3,9 @@ extends Sprite2D
var pos: Vector2i = Vector2i(0,0) var pos: Vector2i = Vector2i(0,0)
var dir: TileSet.CellNeighbor = TileSet.CELL_NEIGHBOR_RIGHT_SIDE var dir: TileSet.CellNeighbor = TileSet.CELL_NEIGHBOR_RIGHT_SIDE
@onready var tiles: TileMapLayer = get_node("../TileMapLayer") @onready var tiles: TileMapLayer = get_node("../TileMapLayer")
@export var randomRules: bool = true
var cur_dir: Dictionary = { var curDir: Dictionary = {
0: TileSet.CELL_NEIGHBOR_RIGHT_SIDE, 0: TileSet.CELL_NEIGHBOR_RIGHT_SIDE,
1: TileSet.CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE, 1: TileSet.CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE,
2: TileSet.CELL_NEIGHBOR_BOTTOM_LEFT_SIDE, 2: TileSet.CELL_NEIGHBOR_BOTTOM_LEFT_SIDE,
@ -13,7 +14,7 @@ var cur_dir: Dictionary = {
5: TileSet.CELL_NEIGHBOR_TOP_RIGHT_SIDE, 5: TileSet.CELL_NEIGHBOR_TOP_RIGHT_SIDE,
} }
var cur_int: Dictionary = { var curInt: Dictionary = {
TileSet.CELL_NEIGHBOR_RIGHT_SIDE: 0, TileSet.CELL_NEIGHBOR_RIGHT_SIDE: 0,
TileSet.CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE: 1, TileSet.CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE: 1,
TileSet.CELL_NEIGHBOR_BOTTOM_LEFT_SIDE: 2, TileSet.CELL_NEIGHBOR_BOTTOM_LEFT_SIDE: 2,
@ -22,21 +23,34 @@ var cur_int: Dictionary = {
TileSet.CELL_NEIGHBOR_TOP_RIGHT_SIDE: 5, TileSet.CELL_NEIGHBOR_TOP_RIGHT_SIDE: 5,
} }
var antProgram: Dictionary = { var antProgram: Array[Vector2i] = [
0: Vector2i(-2, 1), Vector2i(2, 1),
1: Vector2i(0, 2), Vector2i(0, 2),
2: Vector2i(0, 3), Vector2i(3, 3),
3: Vector2i(-1, 4), Vector2i(1, 4),
4: Vector2i(-2, 5), Vector2i(1, 5),
5: Vector2i(-1, 0), Vector2i(2, 0),
} ]
func randomTileset() -> void:
var rng = RandomNumberGenerator.new()
antProgram = []
var s: int = rng.randi_range(10, 99)
for i in range(0, s):
antProgram.append(Vector2i(rng.randi_range(1, 4), (i+1)%s))
func getNextStep(tile: int) -> Vector2i: func getNextStep(tile: int) -> Vector2i:
return antProgram.get(tile, Vector2i(0,0)) if tile > antProgram.size():
return Vector2i(0,0)
else:
return antProgram.get(tile)
func _ready() -> void: func _ready() -> void:
self.set_z_index(1) self.set_z_index(1)
self.set_frame(cur_int[dir]) self.set_frame(curInt[dir])
if randomRules:
randomTileset()
tiles.set_tile(pos, tiles.get_tile_colour(pos)) tiles.set_tile(pos, tiles.get_tile_colour(pos))
move(dir) move(dir)
@ -50,8 +64,9 @@ func update() -> void:
var tile: int = tiles.get_tile_colour(pos) var tile: int = tiles.get_tile_colour(pos)
var res = getNextStep(tile) var res = getNextStep(tile)
dir = cur_dir[(cur_int[dir] + tiles.numTiles + res.x) % tiles.numTiles] dir = curDir[(curInt[dir] + tiles.numTiles + res.x) % tiles.numTiles]
self.set_frame(dir) self.set_frame(curInt[dir])
tiles.set_tile(pos, res.y % tiles.numTiles) tiles.set_tile(pos, res.y % tiles.numTiles)
func move(to: TileSet.CellNeighbor) -> void: func move(to: TileSet.CellNeighbor) -> void:

View File

@ -1,6 +1,6 @@
extends Camera2D extends Camera2D
@export var minZoom := 0.1 @export var minZoom := 0.01
@export var maxZoom := 2.0 @export var maxZoom := 2.0
@export var zoomFactor := 0.1 @export var zoomFactor := 0.1
@export var zoomDuration := 0.2 @export var zoomDuration := 0.2

View File

@ -5,6 +5,7 @@ var paused: bool = false
@export var stepSize: float = 20 @export var stepSize: float = 20
@export var ant: Sprite2D @export var ant: Sprite2D
@export var tiles: TileMapLayer @export var tiles: TileMapLayer
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready() -> void: func _ready() -> void:
pass pass

View File

@ -4,13 +4,19 @@ var tiles: Dictionary = {}
@onready var numTiles: int = get_tile_set().get_source(0).get_tiles_count() @onready var numTiles: int = get_tile_set().get_source(0).get_tiles_count()
#@onready var texSize: Vector2 = get_tile_set().get_tile_texture(get_tile_set().get_tile_id(0)) #@onready var texSize: Vector2 = get_tile_set().get_tile_texture(get_tile_set().get_tile_id(0))
@onready var texSize: Vector2 = Vector2(32, 32) # TODO: get from tileSet @onready var texSize: Vector2 = Vector2(32, 32) # TODO: get from tileSet
@export var randomColors: bool = true
var stateColMap: Dictionary = {
}
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
func get_tile_colour(pos: Vector2i) -> int: func get_tile_colour(pos: Vector2i) -> int:
return self.tiles.get_or_add(pos, 0) return tiles.get_or_add(pos, 0)
# TODO: state should be an enum probably # TODO: state should be an enum probably
func set_tile(pos: Vector2i, state: int) -> void: func set_tile(pos: Vector2i, state: int) -> void:
set_cell(pos, 0, Vector2i(0, state)) self.set_cell(pos, 0, Vector2i(0, state%numTiles))
self.tiles.erase(pos) if randomColors:
self.tiles.get_or_add(pos, state % self.numTiles) var col: Color = stateColMap.get_or_add(state, Color(rng.randf(), rng.randf(), rng.randf(), 1))
self.get_cell_tile_data(pos).set_modulate(col)
tiles.erase(pos)
tiles.get_or_add(pos, state)