Godot version of Conway's game of life
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

46 lines
1.3 KiB

## Conway's Game of Life
## Rules:
## 1. Any cell with >2 neighbors survives
## 2. Any dead cell with 3 live neighbors becomes alive
## 3. All other cells die (all dead cells remaing dead)
extends Node2D
@export var world_seed: int
@export var cell_size: Vector2 = Vector2(16, 16)
@export var cell_texture: Texture2D
@export var world_size: Vector2 = Vector2(128, 128)
var cell_instance
var generation: int = 1
var world: Array = []
func _ready() -> void:
if world_seed: seed(world_seed)
generate_world()
# Create the cell using the rendering server
func create_cell(location: Vector2) -> RID:
var rs = RenderingServer
cell_instance = rs.canvas_item_create()
rs.canvas_item_set_parent(cell_instance, get_canvas_item())
var rect: Rect2 = Rect2(-cell_size.x/2, -cell_size.y/2, cell_size.x, cell_size.y) # Centered with cell size
rs.canvas_item_add_texture_rect(cell_instance, rect, cell_texture)
var location_fixed = Vector2(location.x * cell_size.x, location.y * cell_size.y)
var trans = Transform2D(0, location_fixed)
rs.canvas_item_set_transform(cell_instance, trans)
return cell_instance
## Generate the world with the initial cells
func generate_world() -> void:
for x in range(world_size.x):
world.append([])
for y in range(world_size.y):
if randi_range(0, 1):
world[x].append(create_cell(Vector2(x, y)))
else:
world[x].append(0)