diff --git a/langtons-ant/TODO b/langtons-ant/TODO new file mode 100644 index 0000000..3133743 --- /dev/null +++ b/langtons-ant/TODO @@ -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. diff --git a/langtons-ant/ant.gd b/langtons-ant/ant.gd index 0a42e4c..d4fcb0c 100644 --- a/langtons-ant/ant.gd +++ b/langtons-ant/ant.gd @@ -17,31 +17,35 @@ 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_LEFT_SIDE: 3, + TileSet.CELL_NEIGHBOR_TOP_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 + 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() func update() -> void: var tile: int = tiles.get_tile_colour(pos) - print("tile %s" % tile) var newFrame: int = cur_int[dir] + # TODO: turn into dict{tileCol, (nextDir, paintCol)} customizable match tile: 0: - newFrame = (newFrame + 1) % 6 + newFrame = (newFrame + 5) % tiles.numTiles 1: - newFrame = (newFrame + 1) % 6 + newFrame = (newFrame + 4) % tiles.numTiles 2: - newFrame = (newFrame + 1) % 6 + newFrame = (newFrame + 0) % tiles.numTiles 3: - newFrame = (newFrame + 5) % 6 + newFrame = (newFrame + 0) % tiles.numTiles + 4: + newFrame = (newFrame + 5) % tiles.numTiles + 5: + newFrame = (newFrame + 4) % tiles.numTiles _: pass dir = next_dir[newFrame] @@ -49,24 +53,24 @@ func update() -> void: move() pos = tiles.get_neighbor_cell(pos, dir) tile = tiles.get_tile_colour(pos) - tiles.set_tile(pos, (tile + 1) % 4) # TODO: should probably use another dictionary - set_frame(newFrame) + tiles.set_tile(pos, (tile + 1) % tiles.numTiles) # TODO: should probably use another dictionary + self.set_frame(newFrame) func move() -> void: var delta: Vector2 match dir: TileSet.CELL_NEIGHBOR_RIGHT_SIDE: - delta = Vector2(32, 0) + delta = Vector2(tiles.texSize.x, 0) 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: - delta = Vector2(-16, 24) + delta = Vector2(-tiles.texSize.x/2, tiles.texSize.y/4*3) TileSet.CELL_NEIGHBOR_LEFT_SIDE: - delta = Vector2(-32, 0) + delta = Vector2(-tiles.texSize.x, 0) 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: - delta = Vector2(16, -24) + delta = Vector2(tiles.texSize.x/2, -tiles.texSize.y/4*3) _: delta = Vector2(0,0) - translate(delta) + self.translate(delta) diff --git a/langtons-ant/assets/ant.png b/langtons-ant/assets/ant.png index e4d3132..748baf0 100644 Binary files a/langtons-ant/assets/ant.png and b/langtons-ant/assets/ant.png differ diff --git a/langtons-ant/assets/tile0.png b/langtons-ant/assets/tile0.png deleted file mode 100644 index 27959af..0000000 Binary files a/langtons-ant/assets/tile0.png and /dev/null differ diff --git a/langtons-ant/assets/tile1.png b/langtons-ant/assets/tile1.png deleted file mode 100644 index a17a638..0000000 Binary files a/langtons-ant/assets/tile1.png and /dev/null differ diff --git a/langtons-ant/assets/tiles.png b/langtons-ant/assets/tiles.png index 2d398f1..fa0a47e 100644 Binary files a/langtons-ant/assets/tiles.png and b/langtons-ant/assets/tiles.png differ diff --git a/langtons-ant/camera.gd b/langtons-ant/camera.gd new file mode 100644 index 0000000..34495a5 --- /dev/null +++ b/langtons-ant/camera.gd @@ -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) diff --git a/langtons-ant/camera.gd.uid b/langtons-ant/camera.gd.uid new file mode 100644 index 0000000..eccb470 --- /dev/null +++ b/langtons-ant/camera.gd.uid @@ -0,0 +1 @@ +uid://bqjbnbfd7jlob diff --git a/langtons-ant/grid.gd b/langtons-ant/grid.gd index f154bbd..9d445ae 100644 --- a/langtons-ant/grid.gd +++ b/langtons-ant/grid.gd @@ -1,19 +1,24 @@ extends Node2D -var t: float = 0 -@export var speed: float = 58 +@export var stepSize: float = 2 @export var ant: Sprite2D @export var tiles: TileMapLayer +var t: float = -1 +var paused: bool = false # Called when the node enters the scene tree for the first time. func _ready() -> void: pass - +func _input(event): + if event.is_action_pressed("togglePause"): + paused = !paused + # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: + if paused: return + t += delta - if (t > 60-speed): + if (t > stepSize/100): t = 0 ant.update() - #tiles.render_next() diff --git a/langtons-ant/grid.tscn b/langtons-ant/grid.tscn index 9a4d25b..a6ebae1 100644 --- a/langtons-ant/grid.tscn +++ b/langtons-ant/grid.tscn @@ -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://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://d0v1qlkltasln" path="res://assets/tiles.png" id="4_fqc2p"] +[ext_resource type="Texture2D" uid="uid://ykvioy4g357o" path="res://assets/ant.png" id="3_sle3t"] +[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://bqjbnbfd7jlob" path="res://camera.gd" id="6_g2qvd"] [sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_g2qvd"] texture = ExtResource("4_fqc2p") @@ -17,6 +18,8 @@ texture_region_size = Vector2i(32, 32) 1:2/0 = 0 0:3/0 = 0 1:3/0 = 0 +0:4/0 = 0 +0:5/0 = 0 [sub_resource type="TileSet" id="TileSet_05i0m"] tile_shape = 3 @@ -25,8 +28,8 @@ sources/0 = SubResource("TileSetAtlasSource_g2qvd") [node name="Grid" type="Node2D" node_paths=PackedStringArray("ant", "tiles")] script = ExtResource("1_bghhw") -speed = 60.0 -ant = NodePath("ant") +stepSize = 1.0 +ant = NodePath("Ant") tiles = NodePath("TileMapLayer") [node name="TileMapLayer" type="TileMapLayer" parent="."] @@ -34,13 +37,14 @@ 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="."] +[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") + +[node name="Camera" type="Camera2D" parent="."] +position = Vector2(-3.8146973e-06, -8) +scale = Vector2(0.79999983, 0.79999983) +script = ExtResource("6_g2qvd") diff --git a/langtons-ant/project.godot b/langtons-ant/project.godot index 4261f85..354c558 100644 --- a/langtons-ant/project.godot +++ b/langtons-ant/project.godot @@ -18,3 +18,26 @@ config/icon="res://icon.svg" [editor_plugins] 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) +] +} diff --git a/langtons-ant/tile_map_layer.gd b/langtons-ant/tile_map_layer.gd index 72a6313..17393d4 100644 --- a/langtons-ant/tile_map_layer.gd +++ b/langtons-ant/tile_map_layer.gd @@ -1,75 +1,16 @@ 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, -] +@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 = Vector2(32, 32) # TODO: get from tileSet -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) + 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: - print("setting cell (%s) at %d %d\n" % [state, pos.x, pos.y]) set_cell(pos, 0, Vector2i(0, state)) - tiles.erase(pos) - tiles.get_or_add(pos, state) - -#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 + self.tiles.erase(pos) + self.tiles.get_or_add(pos, state % self.numTiles)