feat: reorganized logic + camera

This commit is contained in:
Djairo Hougee 2025-11-12 15:39:40 +01:00
parent bee8cdeeef
commit fa6e5d9582
12 changed files with 124 additions and 100 deletions

5
langtons-ant/TODO Normal file
View File

@ -0,0 +1,5 @@
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.

View File

@ -17,31 +17,35 @@ var cur_int: 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,
TileSet.CELL_NEIGHBOR_TOP_LEFT_SIDE: 3, TileSet.CELL_NEIGHBOR_LEFT_SIDE: 3,
TileSet.CELL_NEIGHBOR_LEFT_SIDE: 4, TileSet.CELL_NEIGHBOR_TOP_LEFT_SIDE: 4,
TileSet.CELL_NEIGHBOR_TOP_RIGHT_SIDE: 5, TileSet.CELL_NEIGHBOR_TOP_RIGHT_SIDE: 5,
} }
func _ready() -> void: func _ready() -> void:
set_frame(1) self.set_z_index(1)
set_z_index(1) self.set_frame(cur_int[dir])
tiles.set_tile(pos, (tiles.get_tile_colour(pos) + 1) % 2) # TODO: should probably use another dictionary tiles.set_tile(pos, (tiles.get_tile_colour(pos))) # TODO: should probably use another dictionary
move() move()
func update() -> void: func update() -> void:
var tile: int = tiles.get_tile_colour(pos) var tile: int = tiles.get_tile_colour(pos)
print("tile %s" % tile)
var newFrame: int = cur_int[dir] var newFrame: int = cur_int[dir]
# TODO: turn into dict{tileCol, (nextDir, paintCol)} customizable
match tile: match tile:
0: 0:
newFrame = (newFrame + 1) % 6 newFrame = (newFrame + 5) % tiles.numTiles
1: 1:
newFrame = (newFrame + 1) % 6 newFrame = (newFrame + 4) % tiles.numTiles
2: 2:
newFrame = (newFrame + 1) % 6 newFrame = (newFrame + 0) % tiles.numTiles
3: 3:
newFrame = (newFrame + 5) % 6 newFrame = (newFrame + 0) % tiles.numTiles
4:
newFrame = (newFrame + 5) % tiles.numTiles
5:
newFrame = (newFrame + 4) % tiles.numTiles
_: _:
pass pass
dir = next_dir[newFrame] dir = next_dir[newFrame]
@ -49,24 +53,24 @@ func update() -> void:
move() move()
pos = tiles.get_neighbor_cell(pos, dir) pos = tiles.get_neighbor_cell(pos, dir)
tile = tiles.get_tile_colour(pos) tile = tiles.get_tile_colour(pos)
tiles.set_tile(pos, (tile + 1) % 4) # TODO: should probably use another dictionary tiles.set_tile(pos, (tile + 1) % tiles.numTiles) # TODO: should probably use another dictionary
set_frame(newFrame) self.set_frame(newFrame)
func move() -> void: func move() -> void:
var delta: Vector2 var delta: Vector2
match dir: match dir:
TileSet.CELL_NEIGHBOR_RIGHT_SIDE: TileSet.CELL_NEIGHBOR_RIGHT_SIDE:
delta = Vector2(32, 0) delta = Vector2(tiles.texSize.x, 0)
TileSet.CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE: TileSet.CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
delta = Vector2(16, 24) delta = Vector2(tiles.texSize.x/2, tiles.texSize.y/4*3)
TileSet.CELL_NEIGHBOR_BOTTOM_LEFT_SIDE: TileSet.CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
delta = Vector2(-16, 24) delta = Vector2(-tiles.texSize.x/2, tiles.texSize.y/4*3)
TileSet.CELL_NEIGHBOR_LEFT_SIDE: TileSet.CELL_NEIGHBOR_LEFT_SIDE:
delta = Vector2(-32, 0) delta = Vector2(-tiles.texSize.x, 0)
TileSet.CELL_NEIGHBOR_TOP_LEFT_SIDE: TileSet.CELL_NEIGHBOR_TOP_LEFT_SIDE:
delta = Vector2(-16, -24) delta = Vector2(-tiles.texSize.x/2, -tiles.texSize.y/4*3)
TileSet.CELL_NEIGHBOR_TOP_RIGHT_SIDE: TileSet.CELL_NEIGHBOR_TOP_RIGHT_SIDE:
delta = Vector2(16, -24) delta = Vector2(tiles.texSize.x/2, -tiles.texSize.y/4*3)
_: _:
delta = Vector2(0,0) delta = Vector2(0,0)
translate(delta) self.translate(delta)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.7 KiB

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 305 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 305 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 869 B

After

Width:  |  Height:  |  Size: 1.4 KiB

41
langtons-ant/camera.gd Normal file
View File

@ -0,0 +1,41 @@
extends Camera2D
@export var minZoom := 0.1
@export var maxZoom := 2.0
@export var zoomFactor := 0.1
@export var zoomDuration := 0.2
var zoomLevel: float = 1
var dragging: bool = false
func _ready():
pass
func _input(event):
if event.is_action_pressed("zoomIn"):
setZoomLevel(zoomLevel + zoomFactor)
elif event.is_action_pressed("zoomOut"):
setZoomLevel(zoomLevel - zoomFactor)
elif event.is_action_pressed("resetCamera"):
reset()
func _unhandled_input(event):
if event is InputEventMouseButton:
if event.get_button_index() == 1:
dragging = event.is_pressed()
elif event is InputEventMouseMotion:
if dragging:
self.global_position -= event.get_relative() * 1/zoomLevel
func setZoomLevel(level: float, mouse_world_position = self.get_global_mouse_position()):
var old_zoomLevel = zoomLevel
zoomLevel = clampf(level, minZoom, maxZoom)
var direction = (mouse_world_position - global_position)
var new_position = self.global_position + direction - ((direction) / (zoomLevel/old_zoomLevel))
zoom = Vector2(zoomLevel, zoomLevel)
self.global_position = new_position
func reset():
self.global_position = Vector2(0, 0)

View File

@ -0,0 +1 @@
uid://bqjbnbfd7jlob

View File

@ -1,19 +1,24 @@
extends Node2D extends Node2D
var t: float = 0 @export var stepSize: float = 2
@export var speed: float = 58
@export var ant: Sprite2D @export var ant: Sprite2D
@export var tiles: TileMapLayer @export var tiles: TileMapLayer
var t: float = -1
var paused: bool = false
# 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
func _input(event):
if event.is_action_pressed("togglePause"):
paused = !paused
# Called every frame. 'delta' is the elapsed time since the previous frame. # Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void: func _process(delta: float) -> void:
if paused: return
t += delta t += delta
if (t > 60-speed): if (t > stepSize/100):
t = 0 t = 0
ant.update() ant.update()
#tiles.render_next()

View File

@ -1,10 +1,11 @@
[gd_scene load_steps=8 format=3 uid="uid://b6xi32r3co6md"] [gd_scene load_steps=9 format=3 uid="uid://b6xi32r3co6md"]
[ext_resource type="Script" uid="uid://by3d3t5ymxl3m" path="res://grid.gd" id="1_bghhw"] [ext_resource type="Script" uid="uid://by3d3t5ymxl3m" path="res://grid.gd" id="1_bghhw"]
[ext_resource type="Script" uid="uid://b6ll30b7xtwal" path="res://ant.gd" id="1_ebq2e"] [ext_resource type="Script" uid="uid://b6ll30b7xtwal" path="res://ant.gd" id="1_ebq2e"]
[ext_resource type="Texture2D" uid="uid://bnfy5vx72ux33" path="res://assets/ant.png" id="3_sle3t"] [ext_resource type="Texture2D" uid="uid://ykvioy4g357o" path="res://assets/ant.png" id="3_sle3t"]
[ext_resource type="Texture2D" uid="uid://d0v1qlkltasln" path="res://assets/tiles.png" id="4_fqc2p"] [ext_resource type="Texture2D" uid="uid://b8dilj6w2vq2l" path="res://assets/tiles.png" id="4_fqc2p"]
[ext_resource type="Script" uid="uid://cqxfmo3hixad1" path="res://tile_map_layer.gd" id="5_fqc2p"] [ext_resource type="Script" uid="uid://cqxfmo3hixad1" path="res://tile_map_layer.gd" id="5_fqc2p"]
[ext_resource type="Script" uid="uid://bqjbnbfd7jlob" path="res://camera.gd" id="6_g2qvd"]
[sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_g2qvd"] [sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_g2qvd"]
texture = ExtResource("4_fqc2p") texture = ExtResource("4_fqc2p")
@ -17,6 +18,8 @@ texture_region_size = Vector2i(32, 32)
1:2/0 = 0 1:2/0 = 0
0:3/0 = 0 0:3/0 = 0
1:3/0 = 0 1:3/0 = 0
0:4/0 = 0
0:5/0 = 0
[sub_resource type="TileSet" id="TileSet_05i0m"] [sub_resource type="TileSet" id="TileSet_05i0m"]
tile_shape = 3 tile_shape = 3
@ -25,8 +28,8 @@ sources/0 = SubResource("TileSetAtlasSource_g2qvd")
[node name="Grid" type="Node2D" node_paths=PackedStringArray("ant", "tiles")] [node name="Grid" type="Node2D" node_paths=PackedStringArray("ant", "tiles")]
script = ExtResource("1_bghhw") script = ExtResource("1_bghhw")
speed = 60.0 stepSize = 1.0
ant = NodePath("ant") ant = NodePath("Ant")
tiles = NodePath("TileMapLayer") tiles = NodePath("TileMapLayer")
[node name="TileMapLayer" type="TileMapLayer" parent="."] [node name="TileMapLayer" type="TileMapLayer" parent="."]
@ -34,13 +37,14 @@ tile_set = SubResource("TileSet_05i0m")
collision_enabled = false collision_enabled = false
script = ExtResource("5_fqc2p") script = ExtResource("5_fqc2p")
[node name="Camera2D" type="Camera2D" parent="TileMapLayer"] [node name="Ant" type="Sprite2D" parent="."]
position = Vector2(0, -8)
scale = Vector2(0.8, 0.8)
[node name="ant" type="Sprite2D" parent="."]
position = Vector2(-15.999998, 14.999998) position = Vector2(-15.999998, 14.999998)
scale = Vector2(0.8, 0.8) scale = Vector2(0.8, 0.8)
texture = ExtResource("3_sle3t") texture = ExtResource("3_sle3t")
vframes = 6 vframes = 6
script = ExtResource("1_ebq2e") script = ExtResource("1_ebq2e")
[node name="Camera" type="Camera2D" parent="."]
position = Vector2(-3.8146973e-06, -8)
scale = Vector2(0.79999983, 0.79999983)
script = ExtResource("6_g2qvd")

View File

@ -18,3 +18,26 @@ config/icon="res://icon.svg"
[editor_plugins] [editor_plugins]
enabled=PackedStringArray("res://addons/godot-vim/plugin.cfg") enabled=PackedStringArray("res://addons/godot-vim/plugin.cfg")
[input]
zoomIn={
"deadzone": 0.2,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":4,"canceled":false,"pressed":false,"double_click":false,"script":null)
]
}
zoomOut={
"deadzone": 0.2,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":5,"canceled":false,"pressed":false,"double_click":false,"script":null)
]
}
togglePause={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":32,"key_label":0,"unicode":32,"location":0,"echo":false,"script":null)
]
}
resetCamera={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":82,"physical_keycode":0,"key_label":0,"unicode":114,"location":0,"echo":false,"script":null)
]
}

View File

@ -1,75 +1,16 @@
extends TileMapLayer extends TileMapLayer
var tiles: Dictionary = {} var tiles: Dictionary = {}
var idx: int = 0 @onready var numTiles: int = get_tile_set().get_source(0).get_tiles_count()
var dirs: Array[int] = [ #@onready var texSize: Vector2 = get_tile_set().get_tile_texture(get_tile_set().get_tile_id(0))
TileSet.CELL_NEIGHBOR_TOP_RIGHT_SIDE, @onready var texSize: Vector2 = Vector2(32, 32) # TODO: get from tileSet
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: func get_tile_colour(pos: Vector2i) -> int:
return tiles.get_or_add(pos, 0) return self.tiles.get_or_add(pos, 0)
# TODO: state sh1ould 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:
print("setting cell (%s) at %d %d\n" % [state, pos.x, pos.y])
set_cell(pos, 0, Vector2i(0, state)) set_cell(pos, 0, Vector2i(0, state))
tiles.erase(pos) self.tiles.erase(pos)
tiles.get_or_add(pos, state) self.tiles.get_or_add(pos, state % self.numTiles)
#var direction = 0
#var antPos = Vector2i(0,0)
#var grid = {}
#var tileSize = 16
#
## Called when the node enters the scene tree for the first time.
#func _ready() -> void:
#set_process(true)
#grid[antPos] = 0
#updateTile(antPos, 0)
#
#func updateTile(pos: Vector2i, col: int):
#var tileCol = Color.WHITE if col == 0 else Color.BLACK
#var tileRect = ColorRect.new()
#tileRect.color = tileCol
#tileRect.size = Vector2(tileSize, tileSize)
#tileRect.position = Vector2(pos.x * tileSize, pos.y * tileSize)
#add_child(tileRect)
#
#
## Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta: float) -> void:
#moveAnt()
#
#func moveAnt():
#var curCol = grid.get(antPos, 0)
#
#if curCol == 0:
#direction = (direction + 1) % 4
#else:
#direction = (direction + 3) % 4
#
#grid[antPos] = 1 - curCol
#updateTile(antPos, grid[antPos])
#
#match direction:
#0:
#antPos.y -= 1
#1:
#antPos.x += 1
#2:
#antPos.y += 1
#3:
#antPos.x -= 1
#
#if not grid.has(antPos):
#grid[antPos] = 0